-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix(overlays): focus is returned to last focus element when focusing toast #28950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ad3675b
74d0727
e11bb39
a388847
81be6c7
0883475
86eea70
5d0121b
1a770e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,13 @@ import type { | |
|
||
import { CoreDelegate } from './framework-delegate'; | ||
import { OVERLAY_BACK_BUTTON_PRIORITY } from './hardware-back-button'; | ||
import { addEventListener, componentOnReady, focusElement, getElementRoot, removeEventListener } from './helpers'; | ||
import { | ||
addEventListener, | ||
componentOnReady, | ||
focusVisibleElement, | ||
getElementRoot, | ||
removeEventListener, | ||
} from './helpers'; | ||
import { printIonWarning } from './logging'; | ||
|
||
let lastOverlayIndex = 0; | ||
|
@@ -131,38 +137,55 @@ export const createOverlay = <T extends HTMLIonOverlayElement>( | |
*/ | ||
const focusableQueryString = | ||
'[tabindex]:not([tabindex^="-"]):not([hidden]):not([disabled]), input:not([type=hidden]):not([tabindex^="-"]):not([hidden]):not([disabled]), textarea:not([tabindex^="-"]):not([hidden]):not([disabled]), button:not([tabindex^="-"]):not([hidden]):not([disabled]), select:not([tabindex^="-"]):not([hidden]):not([disabled]), .ion-focusable:not([tabindex^="-"]):not([hidden]):not([disabled]), .ion-focusable[disabled="false"]:not([tabindex^="-"]):not([hidden])'; | ||
const isOverlayHidden = (overlay: Element) => overlay.classList.contains('overlay-hidden'); | ||
|
||
/** | ||
* Focuses the first descendant in an overlay | ||
* that can receive focus. If none exists, | ||
* the entire overlay will be focused. | ||
*/ | ||
export const focusFirstDescendant = (ref: Element, overlay: HTMLIonOverlayElement) => { | ||
let firstInput = ref.querySelector(focusableQueryString) as HTMLElement | null; | ||
|
||
const shadowRoot = firstInput?.shadowRoot; | ||
if (shadowRoot) { | ||
// If there are no inner focusable elements, just focus the host element. | ||
firstInput = shadowRoot.querySelector(focusableQueryString) || firstInput; | ||
} | ||
const firstInput = ref.querySelector(focusableQueryString) as HTMLElement | null; | ||
|
||
if (firstInput) { | ||
focusElement(firstInput); | ||
} else { | ||
// Focus overlay instead of letting focus escape | ||
overlay.focus(); | ||
} | ||
focusElementInOverlay(firstInput, overlay); | ||
}; | ||
|
||
const isOverlayHidden = (overlay: Element) => overlay.classList.contains('overlay-hidden'); | ||
|
||
/** | ||
* Focuses the last descendant in an overlay | ||
* that can receive focus. If none exists, | ||
* the entire overlay will be focused. | ||
*/ | ||
const focusLastDescendant = (ref: Element, overlay: HTMLIonOverlayElement) => { | ||
const inputs = Array.from(ref.querySelectorAll(focusableQueryString)) as HTMLElement[]; | ||
let lastInput = inputs.length > 0 ? inputs[inputs.length - 1] : null; | ||
const lastInput = inputs.length > 0 ? inputs[inputs.length - 1] : null; | ||
|
||
focusElementInOverlay(lastInput, overlay); | ||
}; | ||
|
||
/** | ||
* Focuses a particular element in an overlay. If the element | ||
* doesn't have anything focusable associated with it then | ||
* the overlay itself will be focused. | ||
* This should be used instead of the focus() method | ||
* on most elements because the focusable element | ||
* may not be the host element. | ||
* | ||
* For example, if an ion-button should be focused | ||
* then we should actually focus the native <button> | ||
* element inside of ion-button's shadow root, not | ||
* the host element itself. | ||
*/ | ||
const focusElementInOverlay = (hostToFocus: HTMLElement | null | undefined, overlay: HTMLIonOverlayElement) => { | ||
let elementToFocus = hostToFocus; | ||
|
||
const shadowRoot = lastInput?.shadowRoot; | ||
const shadowRoot = hostToFocus?.shadowRoot; | ||
if (shadowRoot) { | ||
// If there are no inner focusable elements, just focus the host element. | ||
lastInput = shadowRoot.querySelector(focusableQueryString) || lastInput; | ||
elementToFocus = shadowRoot.querySelector<HTMLElement>(focusableQueryString) || hostToFocus; | ||
} | ||
|
||
if (lastInput) { | ||
lastInput.focus(); | ||
if (elementToFocus) { | ||
focusVisibleElement(elementToFocus); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing I noticed was that However, focusVisibleElement also calls |
||
} else { | ||
// Focus overlay instead of letting focus escape | ||
overlay.focus(); | ||
|
@@ -219,6 +242,20 @@ const trapKeyboardFocus = (ev: Event, doc: Document) => { | |
*/ | ||
if (lastOverlay === target) { | ||
lastOverlay.lastFocus = undefined; | ||
/** | ||
* Toasts can be presented from an overlay. | ||
* However, focus should still be returned to | ||
* the overlay when clicking a toast. Normally, | ||
* focus would be returned to the last focusable | ||
* descendant in the overlay which may not always be | ||
* the button that the toast was presented from. In this case, | ||
* the focus may be returned to an unexpected element. | ||
* To account for this, we make sure to return focus to the | ||
* last focused element in the overlay if focus is | ||
* moved to the toast. | ||
*/ | ||
} else if (target.tagName === 'ION-TOAST') { | ||
focusElementInOverlay(lastOverlay.lastFocus, lastOverlay); | ||
|
||
/** | ||
* Otherwise, we must be focusing an element | ||
|
@@ -295,6 +332,20 @@ const trapKeyboardFocus = (ev: Event, doc: Document) => { | |
*/ | ||
if (lastOverlay.contains(target)) { | ||
lastOverlay.lastFocus = target; | ||
/** | ||
* Toasts can be presented from an overlay. | ||
* However, focus should still be returned to | ||
* the overlay when clicking a toast. Normally, | ||
* focus would be returned to the last focusable | ||
* descendant in the overlay which may not always be | ||
* the button that the toast was presented from. In this case, | ||
* the focus may be returned to an unexpected element. | ||
* To account for this, we make sure to return focus to the | ||
* last focused element in the overlay if focus is | ||
* moved to the toast. | ||
*/ | ||
} else if (target.tagName === 'ION-TOAST') { | ||
focusElementInOverlay(lastOverlay.lastFocus, lastOverlay); | ||
} else { | ||
/** | ||
* Otherwise, we are about to have focus | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should be no behavior change to focusFirstDescendant and focusLastDescendant. All I did here was abstract the focusing logic to
focusElementInOverlay
and had those functions call it instead. I needed to usefocusElementInOverlay
to implement the fix, so I figured I should consolidate otherwise we're writing the same code 3x.