Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 02447b2

Browse files
feat(#118):add srcset and sizes to lazy-img component
1 parent 2e08b03 commit 02447b2

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

webcomponents/lazy-img/src/components.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ export namespace Components {
1414

1515
interface DeckgoLazyImg {
1616
'imgAlt': string;
17+
'imgSizes': string;
1718
'imgSrc': string;
19+
'imgSrcSet': string;
1820
'lazyLoad': () => Promise<void>;
1921
'observerRootMargin': string;
2022
'observerThreshold': number | number[];
2123
}
2224
interface DeckgoLazyImgAttributes extends StencilHTMLAttributes {
2325
'imgAlt'?: string;
26+
'imgSizes'?: string;
2427
'imgSrc'?: string;
28+
'imgSrcSet'?: string;
2529
'observerRootMargin'?: string;
2630
'observerThreshold'?: number | number[];
2731
'onLazyImgDidLoad'?: (event: CustomEvent) => void;

webcomponents/lazy-img/src/components/lazy-img/deckdeckgo-lazy-img.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ export class DeckdeckgoLazyImg {
1414
@Prop({reflectToAttr: true})
1515
imgSrc: string;
1616

17+
@Prop({reflectToAttr: true})
18+
imgSrcSet: string;
19+
1720
@Prop({reflectToAttr: true})
1821
imgAlt: string;
1922

23+
@Prop({reflectToAttr: true})
24+
imgSizes: string;
25+
2026
@Prop()
2127
observerRootMargin: string = '100px 0px';
2228

@@ -80,11 +86,17 @@ export class DeckdeckgoLazyImg {
8086
img.removeAttribute('data-src');
8187
}
8288

89+
if (img && img.hasAttribute('data-srcset')) {
90+
img.setAttribute('srcset', img.getAttribute('data-srcset'));
91+
img.removeAttribute('data-srcset');
92+
}
93+
8394
resolve();
8495
});
8596
}
8697

8798
render() {
88-
return <img data-src={this.imgSrc} alt={this.imgAlt ? this.imgAlt : this.imgSrc}/>;
99+
return <img data-src={this.imgSrc} alt={this.imgAlt ? this.imgAlt : this.imgSrc}
100+
data-srcset={this.imgSrcSet ? this.imgSrcSet : undefined} sizes={this.imgSizes ? this.imgSizes : undefined}/>;
89101
}
90102
}

webcomponents/lazy-img/src/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@
4242
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgo.png"></deckgo-lazy-img>
4343
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgo.png"></deckgo-lazy-img>
4444

45+
<deckgo-lazy-img style="background: red;"
46+
img-sizes="(min-width: 40em) 80vw, 100vw"
47+
img-src-set="https://deckdeckgo.com/assets/img/deckdeckgo.png 1x, https://deckdeckgo.com/assets/img/deckdeckgo.svg 2x"></deckgo-lazy-img>
48+
4549
</body>
4650
</html>

0 commit comments

Comments
 (0)