mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
feat(overlays): close overlay with back-button
This commit is contained in:
@@ -38,6 +38,7 @@ If you're using Angular, please see [ion-router-outlet](../router-outlet) instea
|
||||
|
||||
| Method | Description |
|
||||
| ------------ | ----------------------------- |
|
||||
| `goBack` | |
|
||||
| `navChanged` | |
|
||||
| `printDebug` | |
|
||||
| `push` | Navigate to the specified URL |
|
||||
|
||||
2
core/src/interface.d.ts
vendored
2
core/src/interface.d.ts
vendored
@@ -41,7 +41,7 @@ export type ComponentRef = Function | HTMLElement | string;
|
||||
export type ComponentProps<T = null> = T extends ComponentTags ? StencilIntrinsicElements[T] : {[key: string]: any};
|
||||
export type CssClassMap = { [className: string]: boolean };
|
||||
export type BackButtonEvent = CustomEvent<{
|
||||
register(priority: number, handler: () => Promise<void> | void): void;
|
||||
register(priority: number, handler: () => Promise<any> | void): void;
|
||||
}>
|
||||
|
||||
declare global {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { BackButtonEvent } from '../interface';
|
||||
|
||||
type Handler = () => Promise<void> | void;
|
||||
type Handler = () => Promise<any> | void;
|
||||
|
||||
interface HandlerRegister {
|
||||
priority: number;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user