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

Commit a2d762a

Browse files
fix: observe image again and fix new url on attributes change
1 parent 37f6557 commit a2d762a

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, Element, Event, EventEmitter, Method, Prop} from '@stencil/core';
1+
import {Component, Element, Event, EventEmitter, Method, Prop, Watch} from '@stencil/core';
22

33
@Component({
44
tag: 'deckgo-lazy-img',
@@ -32,6 +32,17 @@ export class DeckdeckgoLazyImg {
3232
private observer: IntersectionObserver;
3333

3434
componentDidLoad() {
35+
this.init();
36+
37+
this.lazyImgDidLoad.emit();
38+
}
39+
40+
@Watch('imgSrc')
41+
handleAttrImgSrc() {
42+
this.init();
43+
}
44+
45+
private init() {
3546
const img: HTMLImageElement = this.el.shadowRoot.querySelector('img');
3647

3748
if (img) {
@@ -42,8 +53,6 @@ export class DeckdeckgoLazyImg {
4253

4354
this.observer.observe(img);
4455
}
45-
46-
this.lazyImgDidLoad.emit();
4756
}
4857

4958
@Method()
@@ -67,7 +76,6 @@ export class DeckdeckgoLazyImg {
6776
private handleIntersection(entry: IntersectionObserverEntry): Promise<void> {
6877
return new Promise<void>(async (resolve) => {
6978
if (entry.isIntersecting) {
70-
7179
if (this.observer) {
7280
this.observer.disconnect();
7381
}
@@ -81,22 +89,19 @@ export class DeckdeckgoLazyImg {
8189

8290
private load(img: HTMLImageElement | Element): Promise<void> {
8391
return new Promise<void>((resolve) => {
84-
if (img && img.hasAttribute('data-src')) {
85-
img.setAttribute('src', img.getAttribute('data-src'));
86-
img.removeAttribute('data-src');
92+
if (this.imgSrc) {
93+
img.setAttribute('src', this.imgSrc);
8794
}
8895

89-
if (img && img.hasAttribute('data-srcset')) {
90-
img.setAttribute('srcset', img.getAttribute('data-srcset'));
91-
img.removeAttribute('data-srcset');
96+
if (this.imgSrcSet) {
97+
img.setAttribute('srcset', this.imgSrcSet);
9298
}
9399

94100
resolve();
95101
});
96102
}
97103

98104
render() {
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}/>;
105+
return <img alt={this.imgAlt ? this.imgAlt : this.imgSrc} sizes={this.imgSizes ? this.imgSizes : undefined}/>;
101106
}
102107
}

0 commit comments

Comments
 (0)