Skip to content

Rebasing srcset addition #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/LazyImage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ page has been rendered:
{{ stimulus_controller('symfony/ux-lazy-image/lazy-image') }}
data-hd-src="{{ asset('image/large.png') }}"

srcset="{{ asset('image/small.png') }} 1x, {{ asset('image/small2x.png') }} 2x"
data-hd-srcset="{{ asset('image/large.png') }} 1x, {{ asset('image/large2x.png') }} 2x"

{# Optional but avoids having a page jump when the image is loaded #}
width="200"
height="150"
Expand Down
6 changes: 6 additions & 0 deletions src/LazyImage/Resources/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ class controller extends Controller {
const hd = new Image();
hd.addEventListener('load', () => {
this.element.src = this.element.getAttribute('data-hd-src');
if (this.element.getAttribute('data-hd-srcset')) {
this.element.srcset = this.element.getAttribute('data-hd-srcset');
}
this._dispatchEvent('lazy-image:ready', { hd });
});
hd.src = this.element.getAttribute('data-hd-src');
if (this.element.getAttribute('data-hd-srcset')) {
hd.srcset = this.element.getAttribute('data-hd-srcset');
}
this._dispatchEvent('lazy-image:connect', { hd });
}
_dispatchEvent(name, payload = null, canBubble = false, cancelable = false) {
Expand Down
6 changes: 6 additions & 0 deletions src/LazyImage/Resources/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ export default class extends Controller {

hd.addEventListener('load', () => {
this.element.src = this.element.getAttribute('data-hd-src');
if (this.element.getAttribute('data-hd-srcset')) {
this.element.srcset = this.element.getAttribute('data-hd-srcset');
}
this._dispatchEvent('lazy-image:ready', { hd });
});

hd.src = this.element.getAttribute('data-hd-src');
if (this.element.getAttribute('data-hd-srcset')) {
hd.srcset = this.element.getAttribute('data-hd-srcset');
}

this._dispatchEvent('lazy-image:connect', { hd });
}
Expand Down
11 changes: 7 additions & 4 deletions src/LazyImage/Resources/assets/test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ describe('LazyImageController', () => {

beforeEach(() => {
container = mountDOM(`
<img src="https://symfony.com/logos/symfony_black_02.png"
data-testid="img"
data-hd-src="https://symfony.com/logos/symfony_black_03.png"
data-controller="check lazy-image" />
<img
src="https://symfony.com/logos/symfony_black_02.png"
srcset="https://symfony.com/logos/symfony_black_02.png 1x, https://symfony.com/logos/symfony_black_02.png 2x"
data-testid="img"
data-hd-src="https://symfony.com/logos/symfony_black_03.png"
data-hd-srcset="https://symfony.com/logos/symfony_black_03.png 1x, https://symfony.com/logos/symfony_black_03.png 2x"
data-controller="check lazy-image" />
`);
});

Expand Down