fix(app): crossing focus for any element in a hidden part
Focus held by any element that a crossing hides or unmounts (button, tool, canvas, link) goes to the shown tab, decided by the element leaving the page, not by tag; a field still commits first; visible focus stays. History pair: remembered field must be connected, and a pointer-down off a field keeps it. TextField.commit compares with the latest committed value. Open-panel axe settles and checks dialogs like the other places. R-24: no assertion rewritten or deleted; one Chromium case and three unit tests added.
This commit is contained in:
1 parent
a474ddaa40
commit
606f7ba268
8 files changed
+81
-18
No files matched your search
@@ -125,15 +125,33 @@ function activeTabButton(): HTMLElement | null {
|
||||
)
|
||||
}
|
||||
|
||||
/** Gives the focus to the shown tab's button: a field that lost the focus must not leave `body`. */
|
||||
/** Gives the focus to the shown tab's button: a lost focus must not leave on `body`. */
|
||||
export function focusActiveTab(): void {
|
||||
activeTabButton()?.focus()
|
||||
}
|
||||
|
||||
/** The element holding the focus, when it is something other than `body`. */
|
||||
export function heldElement(): HTMLElement | null {
|
||||
const active = document.activeElement
|
||||
return active instanceof HTMLElement && active !== document.body
|
||||
? active
|
||||
: null
|
||||
}
|
||||
|
||||
/**
|
||||
* The editor turned read-only: commits the text being typed; the field that held the focus
|
||||
* sits in a part that is now hidden, so the focus goes to the shown tab, never to `body`.
|
||||
* After the render that hides parts: `held` (taken before) that left the page or sits in a
|
||||
* hidden part hands its focus to the shown tab. An element still visible keeps its place.
|
||||
*/
|
||||
export function refocusIfHidden(held: HTMLElement | null): void {
|
||||
if (held && (!held.isConnected || held.closest('[hidden]'))) focusActiveTab()
|
||||
}
|
||||
|
||||
/**
|
||||
* The editor turned read-only: commits the text being typed, then whatever held the focus
|
||||
* in a now hidden part gives it to the shown tab, never to `body`.
|
||||
*/
|
||||
export function releaseEditFocus(): void {
|
||||
if (commitFocusedField()) focusActiveTab()
|
||||
const held = heldElement()
|
||||
commitFocusedField()
|
||||
refocusIfHidden(held)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { commitFocusedField, focusActiveTab } from './focusRequest'
|
||||
import { commitFocusedField, heldElement, refocusIfHidden } from './focusRequest'
|
||||
import { MIN_EDIT_WIDTH } from './mode'
|
||||
|
||||
/** The tab lists of board 05: all, the four of 640 px and over, the two below. */
|
||||
@@ -21,14 +21,15 @@ function crossesEditWidth(from: TabTier, to: TabTier): boolean {
|
||||
export function useTabTier(): TabTier {
|
||||
const [tier, setTier] = useState<TabTier>(() => tierOf(window.innerWidth))
|
||||
const previous = useRef(tier)
|
||||
const lostFocus = useRef(false)
|
||||
const held = useRef<HTMLElement | null>(null)
|
||||
useEffect(() => {
|
||||
const update = () => {
|
||||
const next = tierOf(window.innerWidth)
|
||||
// The typed text is committed before the editor turns read-only (or back): a blur
|
||||
// after the hide would be too late, a hidden field keeps no focus event.
|
||||
if (crossesEditWidth(previous.current, next)) {
|
||||
lostFocus.current = commitFocusedField()
|
||||
held.current = heldElement()
|
||||
commitFocusedField()
|
||||
}
|
||||
previous.current = next
|
||||
setTier(next)
|
||||
@@ -37,11 +38,10 @@ export function useTabTier(): TabTier {
|
||||
window.addEventListener('resize', update)
|
||||
return () => window.removeEventListener('resize', update)
|
||||
}, [])
|
||||
// The tab row of the new tier is rendered: the blurred field's focus goes to the shown tab.
|
||||
// The new tier is rendered: a focus left in a hidden part goes to the shown tab.
|
||||
useLayoutEffect(() => {
|
||||
if (!lostFocus.current) return
|
||||
lostFocus.current = false
|
||||
focusActiveTab()
|
||||
refocusIfHidden(held.current)
|
||||
held.current = null
|
||||
}, [tier])
|
||||
return tier
|
||||
}
|
||||
Reference in new issue
Block a user