feat(overlays): close overlay with back-button

This commit is contained in:
Manu Mtz.-Almeida
2018-09-04 01:09:29 +02:00
parent dfac9dc4e9
commit 4ccbefa4c6
4 changed files with 18 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import { AnimationBuilder, HTMLIonOverlayElement, IonicConfig, OverlayInterface } from '../interface';
import { AnimationBuilder, HTMLIonOverlayElement, IonicConfig, OverlayInterface, BackButtonEvent } from '../interface';
let lastId = 0;
@ -19,19 +19,28 @@ export function createOverlay<T extends HTMLIonOverlayElement>(element: T, opts:
// append the overlay element to the document body
getAppRoot(doc).appendChild(element);
doc.body.addEventListener('ionBackButton', ev => {
(ev as BackButtonEvent).detail.register(100, () => closeTopOverlay(doc));
});
doc.body.addEventListener('keyup', ev => {
if (ev.key === 'Escape') {
const lastOverlay = getOverlay(doc);
if (lastOverlay && lastOverlay.backdropDismiss) {
// tslint:disable-next-line:no-floating-promises
lastOverlay.dismiss(null, BACKDROP);
}
// tslint:disable-next-line:no-floating-promises
closeTopOverlay(doc);
}
});
return element.componentOnReady();
}
function closeTopOverlay(doc: Document) {
const lastOverlay = getOverlay(doc);
if (lastOverlay && lastOverlay.backdropDismiss) {
return lastOverlay.dismiss(null, BACKDROP);
}
return Promise.resolve();
}
export function connectListeners(doc: Document) {
if (lastId === 0) {
lastId = 1;