Skip to content

Commit

Permalink
feat(overlay): adds lifecycle events
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Mar 10, 2018
1 parent 6383a54 commit 0b099ce
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
30 changes: 27 additions & 3 deletions packages/core/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import mdLeaveAnimation from './animations/md.leave';
})
export class Modal implements OverlayInterface {

private usersComponentElement: HTMLElement;
private usersElement: HTMLElement;

animation: Animation|undefined;
presented = false;
Expand Down Expand Up @@ -141,13 +141,30 @@ export class Modal implements OverlayInterface {
this.dismiss(null, BACKDROP);
}

@Listen('ionModalDidPresent')
@Listen('ionModalWillPresent')
@Listen('ionModalWillDismiss')
@Listen('ionModalDidDismiss')
protected lifecycle(modalEvent: CustomEvent) {
const el = this.usersElement;
const name = LIFECYCLE_MAP[modalEvent.type];
if (el && name) {
const event = new CustomEvent(name, {
bubbles: false,
cancelable: false,
detail: modalEvent.detail
});
el.dispatchEvent(event);
}
}

/**
* Present the modal overlay after it has been created.
*/
@Method()
present(): Promise<void> {
if (this.presented) {
return Promise.reject('df');
return Promise.resolve();
}
const container = this.el.querySelector(`.modal-wrapper`);
const data = {
Expand All @@ -159,7 +176,7 @@ export class Modal implements OverlayInterface {
'ion-page': true
};
return attachComponent(container, this.component, classes, data)
.then(el => this.usersComponentElement = el)
.then(el => this.usersElement = el)
.then(() => present(this, 'modalEnter', iosEnterAnimation, mdEnterAnimation, undefined));
}

Expand Down Expand Up @@ -189,6 +206,13 @@ export class Modal implements OverlayInterface {
}
}

const LIFECYCLE_MAP: any = {
'ionModalDidPresent': 'ionViewDidEnter',
'ionModalWillPresent': 'ionViewWillEnter',
'ionModalWillDismiss': 'ionViewWillDismiss',
'ionModalDidDismiss': 'ionViewDidDismiss',
};

export interface ModalOptions {
component: any;
data?: any;
Expand Down
30 changes: 28 additions & 2 deletions packages/core/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BACKDROP, OverlayInterface, attachComponent, dismiss, present } from '.

import iosEnterAnimation from './animations/ios.enter';
import iosLeaveAnimation from './animations/ios.leave';

import mdEnterAnimation from './animations/md.enter';
import mdLeaveAnimation from './animations/md.leave';

Expand All @@ -21,7 +22,7 @@ import mdLeaveAnimation from './animations/md.leave';
})
export class Popover implements OverlayInterface {

private usersComponentElement: HTMLElement;
private usersElement: HTMLElement;

presented = false;
animation: Animation;
Expand Down Expand Up @@ -150,6 +151,23 @@ export class Popover implements OverlayInterface {
this.dismiss(null, BACKDROP);
}

@Listen('ionModalDidPresent')
@Listen('ionModalWillPresent')
@Listen('ionModalWillDismiss')
@Listen('ionModalDidDismiss')
protected lifecycle(modalEvent: CustomEvent) {
const el = this.usersElement;
const name = LIFECYCLE_MAP[modalEvent.type];
if (el && name) {
const event = new CustomEvent(name, {
bubbles: false,
cancelable: false,
detail: modalEvent.detail
});
el.dispatchEvent(event);
}
}

/**
* Present the popover overlay after it has been created.
*/
Expand All @@ -168,7 +186,7 @@ export class Popover implements OverlayInterface {
'popover-viewport': true
};
return attachComponent(container, this.component, classes, data)
.then(el => this.usersComponentElement = el)
.then(el => this.usersElement = el)
.then(() => present(this, 'popoverEnter', iosEnterAnimation, mdEnterAnimation, this.ev));
}

Expand Down Expand Up @@ -236,6 +254,14 @@ export interface PopoverDismissEvent extends OverlayDismissEvent {
// keep this just for the sake of static types and potential future extensions
}

const LIFECYCLE_MAP: any = {
'ionPopoverDidPresent': 'ionViewDidEnter',
'ionPopoverWillPresent': 'ionViewWillEnter',
'ionPopoverWillDismiss': 'ionViewWillDismiss',
'ionPopoverDidDismiss': 'ionViewDidDismiss',
};


export const POPOVER_POSITION_PROPERTIES: any = {
ios: {
padding: 2,
Expand Down

0 comments on commit 0b099ce

Please sign in to comment.