Skip to content
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

feat (img) emit ionImgDidLoad only when image is actually loaded #18159

Merged
merged 5 commits into from
May 1, 2019
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: 2 additions & 1 deletion angular/src/directives/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,14 @@ proxyInputs(IonIcon, ['ariaLabel', 'color', 'flipRtl', 'icon', 'ios', 'lazy', 'm
export declare interface IonImg extends StencilComponents<'IonImg'> {}
@Component({ selector: 'ion-img', changeDetection: 0, template: '<ng-content></ng-content>', inputs: ['alt', 'src'] })
export class IonImg {
ionImgWillLoad!: EventEmitter<CustomEvent>;
ionImgDidLoad!: EventEmitter<CustomEvent>;
ionError!: EventEmitter<CustomEvent>;
protected el: HTMLElement;
constructor(c: ChangeDetectorRef, r: ElementRef) {
c.detach();
this.el = r.nativeElement;
proxyOutputs(this, this.el, ['ionImgDidLoad', 'ionError']);
proxyOutputs(this, this.el, ['ionImgWillLoad', 'ionImgDidLoad', 'ionError']);
}
}
proxyInputs(IonImg, ['alt', 'src']);
Expand Down
1 change: 1 addition & 0 deletions core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ ion-img,prop,alt,string | undefined,undefined,false,false
ion-img,prop,src,string | undefined,undefined,false,false
ion-img,event,ionError,void,true
ion-img,event,ionImgDidLoad,void,true
ion-img,event,ionImgWillLoad,void,true

ion-infinite-scroll-content,none
ion-infinite-scroll-content,prop,loadingSpinner,"bubbles" | "circles" | "crescent" | "dots" | "lines" | "lines-small" | null | undefined,undefined,false,false
Expand Down
6 changes: 5 additions & 1 deletion core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,10 +1551,14 @@ export namespace Components {
*/
'onIonError'?: (event: CustomEvent<void>) => void;
/**
* Emitted when the img src has been set
* Emitted when the image has finished loading
*/
'onIonImgDidLoad'?: (event: CustomEvent<void>) => void;
/**
* Emitted when the img src has been set
*/
'onIonImgWillLoad'?: (event: CustomEvent<void>) => void;
/**
* The image URL. This attribute is mandatory for the <img> element.
*/
'src'?: string;
Expand Down
8 changes: 8 additions & 0 deletions core/src/components/img/img.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class Img implements ComponentInterface {
}

/** Emitted when the img src has been set */
@Event() ionImgWillLoad!: EventEmitter<void>;

/** Emitted when the image has finished loading */
@Event() ionImgDidLoad!: EventEmitter<void>;

/** Emitted when the img fails to load */
Expand Down Expand Up @@ -70,6 +73,10 @@ export class Img implements ComponentInterface {
private load() {
this.loadError = this.onError;
this.loadSrc = this.src;
this.ionImgWillLoad.emit();
}

private onLoad = () => {
this.ionImgDidLoad.emit();
}

Expand Down Expand Up @@ -98,6 +105,7 @@ export class Img implements ComponentInterface {
src={this.loadSrc}
alt={this.alt}
decoding="async"
onLoad={this.onLoad}
onError={this.loadError}
/>
);
Expand Down
9 changes: 5 additions & 4 deletions core/src/components/img/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ export default Example

## Events

| Event | Description | Type |
| --------------- | ------------------------------------- | ------------------- |
| `ionError` | Emitted when the img fails to load | `CustomEvent<void>` |
| `ionImgDidLoad` | Emitted when the img src has been set | `CustomEvent<void>` |
| Event | Description | Type |
| ---------------- | ------------------------------------------- | ------------------- |
| `ionError` | Emitted when the img fails to load | `CustomEvent<void>` |
| `ionImgDidLoad` | Emitted when the image has finished loading | `CustomEvent<void>` |
| `ionImgWillLoad` | Emitted when the img src has been set | `CustomEvent<void>` |


----------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions core/src/components/img/test/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
</style>
</ion-app>
<script>
document.body.addEventListener('ionImgWillLoad', (event) => {
console.log('image will load', event.target);
});

document.body.addEventListener('ionImgDidLoad', (event) => {
console.log('image did load', event.target);
});
Expand Down