mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-08 23:58:13 +08:00
fix(toast): allow input focus while toast is visible (#24572)
Resolves #24571
This commit is contained in:
@ -140,7 +140,9 @@ export class Toast implements ComponentInterface, OverlayInterface {
|
||||
@Event({ eventName: 'ionToastDidDismiss' }) didDismiss!: EventEmitter<OverlayEventDetail>;
|
||||
|
||||
connectedCallback() {
|
||||
prepareOverlay(this.el);
|
||||
prepareOverlay(this.el, {
|
||||
trapKeyboardFocus: false
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -47,10 +47,16 @@ export const pickerController = /*@__PURE__*/createController<PickerOptions, HTM
|
||||
export const popoverController = /*@__PURE__*/createController<PopoverOptions, HTMLIonPopoverElement>('ion-popover', Popover, [{ tagName: 'ion-backdrop', customElement: Backdrop }]);
|
||||
export const toastController = /*@__PURE__*/createController<ToastOptions, HTMLIonToastElement>('ion-toast', Toast, [{ tagName: 'ion-ripple-effect', customElement: RippleEffect }]);
|
||||
|
||||
export const prepareOverlay = <T extends HTMLIonOverlayElement>(el: T) => {
|
||||
export interface OverlayListenerOptions {
|
||||
trapKeyboardFocus: boolean;
|
||||
}
|
||||
|
||||
export const prepareOverlay = <T extends HTMLIonOverlayElement>(el: T, options: OverlayListenerOptions = {
|
||||
trapKeyboardFocus: true
|
||||
}) => {
|
||||
/* tslint:disable-next-line */
|
||||
if (typeof document !== 'undefined') {
|
||||
connectListeners(document);
|
||||
connectListeners(document, options);
|
||||
}
|
||||
const overlayIndex = lastId++;
|
||||
el.overlayIndex = overlayIndex;
|
||||
@ -161,8 +167,7 @@ const trapKeyboardFocus = (ev: Event, doc: Document) => {
|
||||
* We need to add a listener to the shadow root
|
||||
* itself to ensure the focus trap works.
|
||||
*/
|
||||
if (!lastOverlay || !target
|
||||
) { return; }
|
||||
if (!lastOverlay || !target) { return; }
|
||||
|
||||
const trapScopedFocus = () => {
|
||||
/**
|
||||
@ -286,10 +291,12 @@ const trapKeyboardFocus = (ev: Event, doc: Document) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const connectListeners = (doc: Document) => {
|
||||
const connectListeners = (doc: Document, options: OverlayListenerOptions) => {
|
||||
if (lastId === 0) {
|
||||
lastId = 1;
|
||||
if (options.trapKeyboardFocus) {
|
||||
doc.addEventListener('focus', ev => trapKeyboardFocus(ev, doc), true);
|
||||
}
|
||||
|
||||
// handle back-button click
|
||||
doc.addEventListener('ionBackButton', ev => {
|
||||
|
||||
Reference in New Issue
Block a user