Files
TREK/client/src/components/shared/dialogFocus.ts
T
Maurice a1f063c703 fix(collections): the add-place search reads as one field and takes the focus
The search runs from a small button inside the field, dark once there is
something to look for, instead of a greyed-out button beside it, and the row
keeps its distance from the address box below. A dialog can now mark the field
it starts in, so this one opens in the search rather than in the name above it.
2026-11-15 12:11:43 +01:00

86 lines
3.6 KiB
TypeScript

import type { KeyboardEvent as ReactKeyboardEvent } from 'react'
import { isPhoneViewport } from '../../mobile/useIsPhone'
/**
* Keyboard focus for the app's dialogs (#1302): the first Tab lands in the dialog
* instead of running through the page behind it, Tab wraps around inside it, and
* on a desktop the first text field is ready to type into.
*
* Fields that open a list as soon as they get the focus (an address or airport
* search) carry role="combobox" and are skipped, so a dialog never opens with a
* dropdown already hanging off it. A field can opt out with data-no-autofocus.
*/
const TEXT_FIELD = [
'input:not([type]),input[type="text"],input[type="search"],input[type="email"],input[type="url"],input[type="tel"],input[type="number"],input[type="password"]',
'textarea',
].join(',')
const TABBABLE = [
'a[href]', 'button', 'input:not([type="hidden"])', 'select', 'textarea',
'[tabindex]', '[contenteditable="true"]',
].join(',')
function usable(el: HTMLElement): boolean {
if ((el as HTMLInputElement).disabled) return false
if (el.closest('[hidden],[aria-hidden="true"],[inert]')) return false
return el.getAttribute('tabindex') !== '-1'
}
/** The first field a user would type into, or null when the dialog has none. */
export function firstTextField(panel: HTMLElement): HTMLElement | null {
for (const el of panel.querySelectorAll<HTMLElement>(TEXT_FIELD)) {
if (!usable(el)) continue
if ((el as HTMLInputElement).readOnly) continue
if (el.getAttribute('role') === 'combobox' || el.hasAttribute('aria-haspopup') || el.hasAttribute('data-no-autofocus')) continue
return el
}
return null
}
/** Only with a mouse or trackpad: on a phone a focused field would throw the keyboard up. */
function typingDevice(): boolean {
if (typeof window.matchMedia !== 'function') return false
return window.matchMedia('(pointer: fine)').matches && !isPhoneViewport()
}
/**
* Puts the focus into a dialog that just opened, unless something inside already
* took it (a field with autoFocus, a dialog's own choice). The panel itself is
* the fallback, so the next Tab starts inside the dialog either way.
*/
export function focusDialog(panel: HTMLElement): void {
if (panel.contains(document.activeElement)) return
// A dialog that starts somewhere other than its first field marks that field,
// as the add-place dialog does with its search below the name in its head.
const marked = panel.querySelector<HTMLElement>('[data-initial-focus]')
const field = typingDevice() ? (marked ?? firstTextField(panel)) : null
;(field ?? panel).focus({ preventScroll: true })
}
/**
* Keeps Tab inside the dialog: past the last control it wraps to the first, and
* Shift+Tab from the first (or from the panel) to the last. A key pressed in a
* picker list the dialog opened in a portal on the body is left alone, because
* that list is not inside the panel and handles its own keys.
*/
export function trapTab(e: ReactKeyboardEvent<HTMLElement>, panel: HTMLElement): void {
if (e.key !== 'Tab' || e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey) return
const target = e.target as Node
if (target !== panel && !panel.contains(target)) return
const items = Array.from(panel.querySelectorAll<HTMLElement>(TABBABLE)).filter(usable)
if (items.length === 0) {
e.preventDefault()
panel.focus({ preventScroll: true })
return
}
const first = items[0]
const last = items[items.length - 1]
if (e.shiftKey && (target === first || target === panel)) {
e.preventDefault()
last.focus()
} else if (!e.shiftKey && target === last) {
e.preventDefault()
first.focus()
}
}