| 623 | * opened the overlay. |
| 624 | */ |
| 625 | const 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 | |
| 671 | export const dismiss = async <OverlayDismissOptions>( |
| 672 | overlay: OverlayInterface, |