MCPcopy Index your code
hub / github.com/ionic-team/ionic-framework / restoreElementFocus

Function restoreElementFocus

core/src/utils/overlays.ts:625–669  ·  view source on GitHub ↗
(overlayEl: any)

Source from the content-addressed store, hash-verified

623 * opened the overlay.
624 */
625const restoreElementFocus = async (overlayEl: any) => {
626 let previousElement = document.activeElement as HTMLElement | null;
627 if (!previousElement) {
628 return;
629 }
630
631 // Ensure active element is blurred to prevent a11y warning issues
632 previousElement.blur();
633
634 const shadowRoot = previousElement?.shadowRoot;
635 if (shadowRoot) {
636 // If there are no inner focusable elements, just focus the host element.
637 previousElement = shadowRoot.querySelector(focusableQueryString) || previousElement;
638 }
639
640 await overlayEl.onDidDismiss();
641
642 /**
643 * After onDidDismiss, the overlay loses focus
644 * because it is removed from the document
645 *
646 * > An element will also lose focus [...]
647 * > if the element is removed from the document)
648 *
649 * https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event
650 *
651 * Additionally, `document.activeElement` returns:
652 *
653 * > The Element which currently has focus,
654 * > `<body>` or null if there is
655 * > no focused element.
656 *
657 * https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement#value
658 *
659 * However, if the user has already focused
660 * an element sometime between onWillDismiss
661 * and onDidDismiss (for example, focusing a
662 * text box after tapping a button in an
663 * action sheet) then don't restore focus to
664 * previous element
665 */
666 if (document.activeElement === null || document.activeElement === document.body) {
667 previousElement.focus();
668 }
669};
670
671export const dismiss = async <OverlayDismissOptions>(
672 overlay: OverlayInterface,

Callers 1

presentFunction · 0.85

Calls 1

onDidDismissMethod · 0.45

Tested by

no test coverage detected