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

Commit b637664

Browse files
feat: don't show alt while loading and img for error fallback
1 parent c663335 commit b637664

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { HTMLStencilElement, JSXBase } from '@stencil/core/internal';
1111
export namespace Components {
1212
interface DeckgoLazyImg {
1313
'imgAlt': string;
14+
'imgErrorSrc': string;
1415
'imgSizes': string;
1516
'imgSrc': string;
1617
'imgSrcSet': string;
@@ -36,6 +37,7 @@ declare global {
3637
declare namespace LocalJSX {
3738
interface DeckgoLazyImg extends JSXBase.HTMLAttributes<HTMLDeckgoLazyImgElement> {
3839
'imgAlt'?: string;
40+
'imgErrorSrc'?: string;
3941
'imgSizes'?: string;
4042
'imgSrc'?: string;
4143
'imgSrcSet'?: string;

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

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

33
@Component({
44
tag: 'deckgo-lazy-img',
@@ -29,8 +29,14 @@ export class DeckdeckgoLazyImg {
2929
@Prop()
3030
observerThreshold: number | number[];
3131

32+
@Prop()
33+
imgErrorSrc: string;
34+
3235
private observer: IntersectionObserver;
3336

37+
@State()
38+
private imgLoaded: boolean = false;
39+
3440
async componentDidLoad() {
3541
await this.init();
3642

@@ -120,7 +126,30 @@ export class DeckdeckgoLazyImg {
120126
});
121127
}
122128

129+
private loadError(): Promise<void> {
130+
return new Promise<void>((resolve) => {
131+
const img: HTMLImageElement = this.el.shadowRoot.querySelector('img');
132+
133+
if (!img) {
134+
resolve();
135+
return;
136+
}
137+
138+
if (!this.imgErrorSrc || img.src === this.imgErrorSrc) {
139+
resolve();
140+
return;
141+
}
142+
143+
if (img.src === this.imgSrc || img.srcset === this.imgSrcSet) {
144+
img.src = this.imgErrorSrc;
145+
}
146+
147+
resolve();
148+
});
149+
}
150+
123151
render() {
124-
return <img alt={this.imgAlt ? this.imgAlt : this.imgSrc} sizes={this.imgSizes ? this.imgSizes : undefined}/>;
152+
return <img alt={this.imgLoaded ? (this.imgAlt ? this.imgAlt : this.imgSrc) : ''} sizes={this.imgSizes ? this.imgSizes : undefined}
153+
onLoad={() => this.imgLoaded = true} onError={() => this.loadError()}/>;
125154
}
126155
}

webcomponents/lazy-img/src/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
</head>
2020
<body>
2121

22-
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgo.png"></deckgo-lazy-img>
23-
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgo.png"></deckgo-lazy-img>
22+
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgoadsasd.png" img-error-src="https://deckdeckgo.com/assets/img/projector.svg"></deckgo-lazy-img>
23+
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgasdadso.png"></deckgo-lazy-img>
2424
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgo.png"></deckgo-lazy-img>
2525
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgo.png"></deckgo-lazy-img>
2626
<deckgo-lazy-img img-src="https://deckdeckgo.com/assets/img/deckdeckgo.png"></deckgo-lazy-img>

0 commit comments

Comments
 (0)