src/components/post-img/post-img.tsx
tag | post-img |
Properties |
Methods |
addIntersectionObserver |
addIntersectionObserver()
|
Defined in src/components/post-img/post-img.tsx:47
|
Returns :
void
|
componentDidLoad |
componentDidLoad()
|
Defined in src/components/post-img/post-img.tsx:21
|
Returns :
void
|
componentWillUpdate |
componentWillUpdate()
|
Defined in src/components/post-img/post-img.tsx:25
|
Returns :
void
|
Async handleImage |
handleImage()
|
Defined in src/components/post-img/post-img.tsx:32
|
Returns :
any
|
removeIntersectionObserver |
removeIntersectionObserver()
|
Defined in src/components/post-img/post-img.tsx:69
|
Returns :
void
|
render |
render()
|
Defined in src/components/post-img/post-img.tsx:76
|
Returns :
any
|
alt |
alt:
|
Type : string
|
Decorators :
@Prop()
|
Defined in src/components/post-img/post-img.tsx:12
|
el |
el:
|
Type : HTMLElement
|
Decorators :
@Element()
|
Defined in src/components/post-img/post-img.tsx:9
|
firebaseSrc |
firebaseSrc:
|
Type : string
|
Decorators :
@State()
|
Defined in src/components/post-img/post-img.tsx:15
|
io |
io:
|
Type : IntersectionObserver
|
Defined in src/components/post-img/post-img.tsx:19
|
lazyImgloaded |
lazyImgloaded:
|
Type : EventEmitter<HTMLImageElement>
|
Decorators :
@Event()
|
Defined in src/components/post-img/post-img.tsx:17
|
oldSrc |
oldSrc:
|
Type : string
|
Decorators :
@State()
|
Defined in src/components/post-img/post-img.tsx:14
|
src |
src:
|
Type : string
|
Decorators :
@Prop()
|
Defined in src/components/post-img/post-img.tsx:11
|
import { Component, Element, Event, EventEmitter, Prop, State } from '@stencil/core';
declare var firebase: any;
@Component({
tag: 'post-img'
})
export class PostImg {
@Element() el: HTMLElement;
@Prop() src: string;
@Prop() alt: string;
@State() oldSrc: string;
@State() firebaseSrc: string;
@Event() lazyImgloaded: EventEmitter<HTMLImageElement>;
io: IntersectionObserver;
componentDidLoad() {
this.addIntersectionObserver();
}
componentWillUpdate() {
console.log('componentWillUpdate called', this.src, this.oldSrc);
if (this.src !== this.el.querySelector('img').getAttribute('data-src')) {
this.addIntersectionObserver();
}
}
async handleImage() {
const image: HTMLImageElement = this.el.querySelector('img');
const storage = firebase.storage();
const imagePath = storage.ref(this.src);
const url = await imagePath.getDownloadURL();
this.firebaseSrc = url;
image.onload = () => {
this.lazyImgloaded.emit(image);
};
}
addIntersectionObserver() {
if (!this.src) {
return;
}
if ('IntersectionObserver' in window) {
this.io = new IntersectionObserver((data: IntersectionObserverEntry[]) => {
// because there will only ever be one instance
// of the element we are observing
// we can just use data[0]
if (data[0].isIntersecting) {
this.handleImage();
this.removeIntersectionObserver();
}
})
this.io.observe(this.el.querySelector('img'));
} else {
// fall back to just loading the image for Safari and IE
this.handleImage();
}
}
removeIntersectionObserver() {
if (this.io) {
this.io.disconnect();
this.io = null;
}
}
render() {
return (
<img src={this.firebaseSrc} alt={this.alt}></img>
);
}
}