Files
ArtPlayer/packages/artplayer/src/accessibility/moved-focus.ts
T

10 lines
473 B
TypeScript

/** Restore the same focused descendant after DOM reparenting. */
export function captureMovedFocus(root: HTMLElement): () => void {
const focused = root.ownerDocument?.activeElement as HTMLElement | null | undefined
const inside = focused && root.contains(focused)
return () => {
if (inside && focused.isConnected && root.contains(focused) && focused.ownerDocument.activeElement === focused.ownerDocument.body)
focused.focus({ preventScroll: true })
}
}