Skip to content

Commit 10461ed

Browse files
committed
[LazyImage] Renaming values to just src and srcset
1 parent 58e096f commit 10461ed

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

src/LazyImage/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
- Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus`
66
version 3 was added. See the [@symfony/stimulus-bridge CHANGELOG](https://github.com/symfony/stimulus-bridge/blob/main/CHANGELOG.md#300)
77
for more details.
8+
- The `data-hd-src` attribute was changed to use a Stimulus value called `src`. See the
9+
updated README for usage.
810
- Support added for Symfony 6

src/LazyImage/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ page has been rendered:
3535
<img
3636
src="{{ asset('image/small.png') }}"
3737
{{ stimulus_controller('symfony/ux-lazy-image/lazy-image', {
38-
hdSrc: asset('image/large.png')
38+
src: asset('image/large.png')
3939
}) }}
4040
4141
{# Optional but avoids having a page jump when the image is loaded #}
@@ -49,16 +49,16 @@ once the page has loaded and the user's browser has downloaded the larger
4949
image, the `src` attribute will change to `image/large.png`.
5050

5151
There is also support for the `srcset` attribute by passing an
52-
`hdSrcset` value to the controller:
52+
`srcset` value to the controller:
5353

5454
```twig
5555
<img
5656
src="{{ asset('image/small.png') }}"
5757
srcset="{{ asset('image/small.png') }} 1x, {{ asset('image/small2x.png') }} 2x"
5858
5959
{{ stimulus_controller('symfony/ux-lazy-image/lazy-image', {
60-
hdSrc: asset('image/large.png'),
61-
hdSrcset: {
60+
src: asset('image/large.png'),
61+
srcset: {
6262
'1x': asset('image/large.png'),
6363
'2x': asset('image/large2x.png')
6464
}
@@ -76,7 +76,7 @@ the BlurHash algorithm to create a light, blurred, data-uri thumbnail of the ima
7676
<img
7777
src="{{ data_uri_thumbnail('public/image/large.png', 100, 75) }}"
7878
{{ stimulus_controller('symfony/ux-lazy-image/lazy-image', {
79-
hdSrc: asset('image/large.png')
79+
src: asset('image/large.png')
8080
}) }}
8181
8282
{# Using BlurHash, the size is required #}
@@ -134,7 +134,7 @@ Then in your template, add your controller to the HTML attribute:
134134
{{ stimulus_controller({
135135
mylazyimage: {},
136136
'symfony/ux-lazy-image/lazy-image': {
137-
hdSrc: asset('image/large.png')
137+
src: asset('image/large.png')
138138
}
139139
}) }}
140140

src/LazyImage/Resources/assets/dist/controller.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ class default_1 extends Controller {
55
const hd = new Image();
66
const srcsetString = this._calculateSrcsetString();
77
hd.addEventListener('load', () => {
8-
this.element.src = this.hdSrcValue;
8+
this.element.src = this.srcValue;
99
if (srcsetString) {
1010
this.element.srcset = srcsetString;
1111
}
1212
this._dispatchEvent('lazy-image:ready', { hd });
1313
});
14-
hd.src = this.hdSrcValue;
14+
hd.src = this.srcValue;
1515
if (srcsetString) {
1616
hd.srcset = srcsetString;
1717
}
1818
this._dispatchEvent('lazy-image:connect', { hd });
1919
}
2020
_calculateSrcsetString() {
21-
if (!this.hasHdSrcsetValue) {
21+
if (!this.hasSrcsetValue) {
2222
return '';
2323
}
24-
const sets = Object.keys(this.hdSrcsetValue).map((size => {
25-
return `${this.hdSrcsetValue[size]} ${size}`;
26-
}));
24+
const sets = Object.keys(this.srcsetValue).map((size) => {
25+
return `${this.srcsetValue[size]} ${size}`;
26+
});
2727
return sets.join(', ').trimEnd();
2828
}
2929
_dispatchEvent(name, payload = null, canBubble = false, cancelable = false) {
@@ -33,8 +33,8 @@ class default_1 extends Controller {
3333
}
3434
}
3535
default_1.values = {
36-
hdSrc: String,
37-
hdSrcset: Object
36+
src: String,
37+
srcset: Object,
3838
};
3939

4040
export { default_1 as default };

src/LazyImage/Resources/assets/src/controller.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { Controller } from '@hotwired/stimulus';
1313

1414
export default class extends Controller {
1515
static values = {
16-
hdSrc: String,
17-
hdSrcset: Object
16+
src: String,
17+
srcset: Object,
1818
};
1919

2020
connect() {
@@ -23,14 +23,14 @@ export default class extends Controller {
2323
const srcsetString = this._calculateSrcsetString();
2424

2525
hd.addEventListener('load', () => {
26-
this.element.src = this.hdSrcValue;
26+
this.element.src = this.srcValue;
2727
if (srcsetString) {
2828
this.element.srcset = srcsetString;
2929
}
3030
this._dispatchEvent('lazy-image:ready', { hd });
3131
});
3232

33-
hd.src = this.hdSrcValue;
33+
hd.src = this.srcValue;
3434
if (srcsetString) {
3535
hd.srcset = srcsetString;
3636
}
@@ -39,13 +39,13 @@ export default class extends Controller {
3939
}
4040

4141
_calculateSrcsetString(): string {
42-
if (!this.hasHdSrcsetValue) {
42+
if (!this.hasSrcsetValue) {
4343
return '';
4444
}
4545

46-
const sets = Object.keys(this.hdSrcsetValue).map((size => {
47-
return `${this.hdSrcsetValue[size]} ${size}`;
48-
}));
46+
const sets = Object.keys(this.srcsetValue).map((size) => {
47+
return `${this.srcsetValue[size]} ${size}`;
48+
});
4949

5050
return sets.join(', ').trimEnd();
5151
}

src/LazyImage/Resources/assets/test/controller.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ describe('LazyImageController', () => {
4141
src="https://symfony.com/logos/symfony_black_02.png"
4242
srcset="https://symfony.com/logos/symfony_black_02.png 1x, https://symfony.com/logos/symfony_black_02.png 2x"
4343
data-testid="img"
44-
data-lazy-image-hd-src-value="https://symfony.com/logos/symfony_black_03.png"
45-
data-lazy-image-hd-srcset-value="{&quot;1x&quot;: &quot;https://symfony.com/logos/symfony_black_03.png&quot;, &quot;2x&quot;: &quot;https://symfony.com/logos/symfony_black_03_2x.png&quot;}"
44+
data-lazy-image-src-value="https://symfony.com/logos/symfony_black_03.png"
45+
data-lazy-image-srcset-value="{&quot;1x&quot;: &quot;https://symfony.com/logos/symfony_black_03.png&quot;, &quot;2x&quot;: &quot;https://symfony.com/logos/symfony_black_03_2x.png&quot;}"
4646
data-controller="check lazy-image" />
4747
`);
4848
});

0 commit comments

Comments
 (0)