From 44efeedf92c2e01b82e79249cf83341dbdf64af0 Mon Sep 17 00:00:00 2001 From: Zakk Date: Wed, 30 Sep 2026 18:41:35 +1000 Subject: [PATCH] refactor(ui): move Segmented into its own module Fields imported Select only for Segmented's collapsed picker. The menus in Select now need TextField for the long-list search fallback, which would close an import cycle through Fields. --- src/ui/Fields.tsx | 70 +------------------------------------------- src/ui/Segmented.tsx | 68 ++++++++++++++++++++++++++++++++++++++++++ src/ui/ui.tsx | 3 +- 3 files changed, 71 insertions(+), 70 deletions(-) create mode 100644 src/ui/Segmented.tsx diff --git a/src/ui/Fields.tsx b/src/ui/Fields.tsx index a61856bb..7084f676 100644 --- a/src/ui/Fields.tsx +++ b/src/ui/Fields.tsx @@ -1,8 +1,6 @@ -import {useCallback, useId, useLayoutEffect, useRef, type ComponentProps, type ReactNode} from 'react'; +import {useId, type ComponentProps, type ReactNode} from 'react'; import { Button as RButton, - ToggleButton, - ToggleButtonGroup, Switch as RSwitch, TextField as RTextField, SearchField as RSearchField, @@ -18,72 +16,6 @@ import Visibility from './icons/Visibility'; import VisibilityOff from './icons/VisibilityOff'; import {useT} from '../i18n'; import {cx} from './cx'; -import {useOverflow, useSlider} from './hooks'; -import {LabeledSelect} from './Select'; - -// S2 does not scroll a segmented control: one too wide for its space collapses into a picker, as S2 Tabs do. The hidden -// track keeps its box, so the switch moves nothing, and is measured to tell when the items fit again. -export function Segmented({ - items, - value, - onChange, - label, - isDisabled -}: { - items: Array<[string, string]>; - value: string; - onChange: (k: string) => void; - label: string; - isDisabled?: boolean; -}) { - const [ref, pos] = useSlider(value); - const collapsed = useOverflow(ref, items.flat().join('\n')); - // Focus inside the control follows it across the switch, so it is neither hidden nor dropped to the page; focus - // elsewhere stays. The picker's ref detaches before the picker leaves the page, while it can still hold focus. - const pick = useRef(null); - const refocus = useRef(false); - const pickRef = useCallback((node: HTMLDivElement) => { - pick.current = node; - return () => { - refocus.current = node.contains(document.activeElement); - pick.current = null; - }; - }, []); - useLayoutEffect(() => { - if (collapsed && ref.current?.contains(document.activeElement)) pick.current?.querySelector('button')?.focus(); - if (!collapsed && refocus.current) ref.current?.querySelector('[data-selected]')?.focus(); - refocus.current = false; - }, [ref, collapsed]); - return ( -
- { - const v = [...k][0]; - if (v != null) onChange(String(v)); - }} - > - {pos && } - {items.map(([k, l]) => ( - - {l} - - ))} - - {collapsed && ( -
- ({id, label: l}))} /> -
- )} -
- ); -} // A switch in a labelled settings row has no text of its own, so it takes its name from `aria-label`. export function Switch({ diff --git a/src/ui/Segmented.tsx b/src/ui/Segmented.tsx new file mode 100644 index 00000000..11b8b98f --- /dev/null +++ b/src/ui/Segmented.tsx @@ -0,0 +1,68 @@ +import {useCallback, useLayoutEffect, useRef} from 'react'; +import {ToggleButton, ToggleButtonGroup} from 'react-aria-components'; +import {useOverflow, useSlider} from './hooks'; +import {LabeledSelect} from './Select'; + +// S2 does not scroll a segmented control: one too wide for its space collapses into a picker, as S2 Tabs do. The hidden +// track keeps its box, so the switch moves nothing, and is measured to tell when the items fit again. +export function Segmented({ + items, + value, + onChange, + label, + isDisabled +}: { + items: Array<[string, string]>; + value: string; + onChange: (k: string) => void; + label: string; + isDisabled?: boolean; +}) { + const [ref, pos] = useSlider(value); + const collapsed = useOverflow(ref, items.flat().join('\n')); + // Focus inside the control follows it across the switch, so it is neither hidden nor dropped to the page; focus + // elsewhere stays. The picker's ref detaches before the picker leaves the page, while it can still hold focus. + const pick = useRef(null); + const refocus = useRef(false); + const pickRef = useCallback((node: HTMLDivElement) => { + pick.current = node; + return () => { + refocus.current = node.contains(document.activeElement); + pick.current = null; + }; + }, []); + useLayoutEffect(() => { + if (collapsed && ref.current?.contains(document.activeElement)) pick.current?.querySelector('button')?.focus(); + if (!collapsed && refocus.current) ref.current?.querySelector('[data-selected]')?.focus(); + refocus.current = false; + }, [ref, collapsed]); + return ( +
+ { + const v = [...k][0]; + if (v != null) onChange(String(v)); + }} + > + {pos && } + {items.map(([k, l]) => ( + + {l} + + ))} + + {collapsed && ( +
+ ({id, label: l}))} /> +
+ )} +
+ ); +} diff --git a/src/ui/ui.tsx b/src/ui/ui.tsx index b16555a2..2af01976 100644 --- a/src/ui/ui.tsx +++ b/src/ui/ui.tsx @@ -15,7 +15,8 @@ export { } from './hooks'; export {exportName, downloadFile, csvLine} from './files'; export {ActionHelp, Button, TextTooltip, Link, buttonClass, type ButtonStyle} from './Button'; -export {TextField, StaticField, Switch, Segmented} from './Fields'; +export {TextField, StaticField, Switch} from './Fields'; +export {Segmented} from './Segmented'; export {Check} from './Check'; export {InlineSelect, LabeledSelect, MenuButton, MenuChoice, ChoiceMenu, pickMenuKey, type ChoiceSection, type ChoiceSubmenu} from './Select'; export {ModalDialog, PopoverDialog, ConfirmDialog, ConfirmButton, DetailPanel, Disclosure, DisclosureGroup, Tabs, useTabShown} from './Dialog';