fix(app): focus the shown tab after a crossing, one commit per typed field
- A field blurred by a 1 024 px crossing or a read-only switch hands its focus to the shown tab (or Plus), never to body (focusRequest, useViewport). - TextField no longer commits a blurred value twice when it unmounts before the render that brings the new value: one undo step, one count. - Annuler and Retablir share the field left by Tab, so Shift+Tab to Annuler and Enter still skips the undo of a refused field. - Adding a reference clears the finding filter and the search. Unit tests: no-loss (focus on tab and on Plus, one undo entry), history buttons (field kept across the pair), references panel (add clears, refused add keeps).
This commit is contained in:
1 parent
9c03cf2413
commit
0bb150f860
9 files changed
+149
-29
No files matched your search
@@ -5,7 +5,7 @@ import type {
|
||||
EditorTarget,
|
||||
FocusRequest,
|
||||
} from './navigation'
|
||||
import type { EditorTabId } from './tabs'
|
||||
import { MORE_BUTTON_ID, type EditorTabId } from './tabs'
|
||||
|
||||
export type { FocusRequest }
|
||||
|
||||
@@ -105,25 +105,35 @@ export function useRowFocus(focus: FocusRequest | undefined): void {
|
||||
/**
|
||||
* Commits the text being typed before a place is left: blurs the focused field, so its
|
||||
* `TextField` commits on blur. Safari and iPadOS do not move the focus to a tapped button,
|
||||
* so the blur is forced rather than assumed.
|
||||
* so the blur is forced rather than assumed. Answers whether a field was blurred.
|
||||
*/
|
||||
export function commitFocusedField(): void {
|
||||
export function commitFocusedField(): boolean {
|
||||
const active = document.activeElement
|
||||
if (
|
||||
const isField =
|
||||
active instanceof HTMLInputElement ||
|
||||
active instanceof HTMLTextAreaElement ||
|
||||
active instanceof HTMLSelectElement
|
||||
) {
|
||||
active.blur()
|
||||
}
|
||||
if (isField) active.blur()
|
||||
return isField
|
||||
}
|
||||
|
||||
/** The tab shown, or « Plus » while the shown tab sits in the closed list. */
|
||||
function activeTabButton(): HTMLElement | null {
|
||||
return (
|
||||
document.querySelector<HTMLElement>('[role="tab"][aria-selected="true"]') ??
|
||||
document.getElementById(MORE_BUTTON_ID)
|
||||
)
|
||||
}
|
||||
|
||||
/** Gives the focus to the shown tab's button: a field that lost the focus must not leave `body`. */
|
||||
export function focusActiveTab(): void {
|
||||
activeTabButton()?.focus()
|
||||
}
|
||||
|
||||
/**
|
||||
* The editor turned read-only: commits the text being typed, then releases any focus left
|
||||
* inside a hidden part, so no field is focused in a place that left the page.
|
||||
* 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`.
|
||||
*/
|
||||
export function releaseEditFocus(): void {
|
||||
commitFocusedField()
|
||||
const active = document.activeElement
|
||||
if (active instanceof HTMLElement && active.closest('[hidden]')) active.blur()
|
||||
if (commitFocusedField()) focusActiveTab()
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { commitFocusedField } from './focusRequest'
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { commitFocusedField, focusActiveTab } 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,12 +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)
|
||||
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)) commitFocusedField()
|
||||
if (crossesEditWidth(previous.current, next)) {
|
||||
lostFocus.current = commitFocusedField()
|
||||
}
|
||||
previous.current = next
|
||||
setTier(next)
|
||||
}
|
||||
@@ -34,5 +37,11 @@ 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.
|
||||
useLayoutEffect(() => {
|
||||
if (!lostFocus.current) return
|
||||
lostFocus.current = false
|
||||
focusActiveTab()
|
||||
}, [tier])
|
||||
return tier
|
||||
}
|
||||
Reference in new issue
Block a user