Skip to content

Commit d894cf9

Browse files
committed
feature #186 Rebasing srcset addition (Plamen)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- Rebasing srcset addition | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | Fixes #148 | License | MIT Just a rebase of #176 to get the new build system in there. Cheers! Commits ------- 62da8b1 Rebasing srcset addition
2 parents df19694 + 62da8b1 commit d894cf9

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/LazyImage/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ page has been rendered:
3737
{{ stimulus_controller('symfony/ux-lazy-image/lazy-image') }}
3838
data-hd-src="{{ asset('image/large.png') }}"
3939
40+
srcset="{{ asset('image/small.png') }} 1x, {{ asset('image/small2x.png') }} 2x"
41+
data-hd-srcset="{{ asset('image/large.png') }} 1x, {{ asset('image/large2x.png') }} 2x"
42+
4043
{# Optional but avoids having a page jump when the image is loaded #}
4144
width="200"
4245
height="150"

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ class controller extends Controller {
55
const hd = new Image();
66
hd.addEventListener('load', () => {
77
this.element.src = this.element.getAttribute('data-hd-src');
8+
if (this.element.getAttribute('data-hd-srcset')) {
9+
this.element.srcset = this.element.getAttribute('data-hd-srcset');
10+
}
811
this._dispatchEvent('lazy-image:ready', { hd });
912
});
1013
hd.src = this.element.getAttribute('data-hd-src');
14+
if (this.element.getAttribute('data-hd-srcset')) {
15+
hd.srcset = this.element.getAttribute('data-hd-srcset');
16+
}
1117
this._dispatchEvent('lazy-image:connect', { hd });
1218
}
1319
_dispatchEvent(name, payload = null, canBubble = false, cancelable = false) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ export default class extends Controller {
1717

1818
hd.addEventListener('load', () => {
1919
this.element.src = this.element.getAttribute('data-hd-src');
20+
if (this.element.getAttribute('data-hd-srcset')) {
21+
this.element.srcset = this.element.getAttribute('data-hd-srcset');
22+
}
2023
this._dispatchEvent('lazy-image:ready', { hd });
2124
});
2225

2326
hd.src = this.element.getAttribute('data-hd-src');
27+
if (this.element.getAttribute('data-hd-srcset')) {
28+
hd.srcset = this.element.getAttribute('data-hd-srcset');
29+
}
2430

2531
this._dispatchEvent('lazy-image:connect', { hd });
2632
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ describe('LazyImageController', () => {
3434

3535
beforeEach(() => {
3636
container = mountDOM(`
37-
<img src="https://symfony.com/logos/symfony_black_02.png"
38-
data-testid="img"
39-
data-hd-src="https://symfony.com/logos/symfony_black_03.png"
40-
data-controller="check lazy-image" />
37+
<img
38+
src="https://symfony.com/logos/symfony_black_02.png"
39+
srcset="https://symfony.com/logos/symfony_black_02.png 1x, https://symfony.com/logos/symfony_black_02.png 2x"
40+
data-testid="img"
41+
data-hd-src="https://symfony.com/logos/symfony_black_03.png"
42+
data-hd-srcset="https://symfony.com/logos/symfony_black_03.png 1x, https://symfony.com/logos/symfony_black_03.png 2x"
43+
data-controller="check lazy-image" />
4144
`);
4245
});
4346

0 commit comments

Comments
 (0)