mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-06 08:02:04 +00:00
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.
This commit is contained in:
1 parent
a0caf0e161
commit
44efeedf92
3 files changed
+71
-70
No files matched your search
+1
-69
@@ -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<HTMLDivElement>(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<HTMLElement>('[data-selected]')?.focus();
|
||||
refocus.current = false;
|
||||
}, [ref, collapsed]);
|
||||
return (
|
||||
<div className="rp-segfit" data-collapsed={collapsed || undefined}>
|
||||
<ToggleButtonGroup
|
||||
ref={ref}
|
||||
className="rp-seg"
|
||||
aria-label={label}
|
||||
isDisabled={isDisabled}
|
||||
selectionMode="single"
|
||||
disallowEmptySelection
|
||||
selectedKeys={[value]}
|
||||
onSelectionChange={k => {
|
||||
const v = [...k][0];
|
||||
if (v != null) onChange(String(v));
|
||||
}}
|
||||
>
|
||||
{pos && <span className="rp-slider" data-still={pos.still || undefined} style={{left: pos.x, width: pos.w}} />}
|
||||
{items.map(([k, l]) => (
|
||||
<ToggleButton key={k} id={k} className="rp-btn">
|
||||
{l}
|
||||
</ToggleButton>
|
||||
))}
|
||||
</ToggleButtonGroup>
|
||||
{collapsed && (
|
||||
<div ref={pickRef} className="rp-segpick">
|
||||
<LabeledSelect bare label={label} value={value} onChange={onChange} isDisabled={isDisabled} items={items.map(([id, l]) => ({id, label: l}))} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// A switch in a labelled settings row has no text of its own, so it takes its name from `aria-label`.
|
||||
export function Switch({
|
||||
|
||||
@@ -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<HTMLDivElement>(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<HTMLElement>('[data-selected]')?.focus();
|
||||
refocus.current = false;
|
||||
}, [ref, collapsed]);
|
||||
return (
|
||||
<div className="rp-segfit" data-collapsed={collapsed || undefined}>
|
||||
<ToggleButtonGroup
|
||||
ref={ref}
|
||||
className="rp-seg"
|
||||
aria-label={label}
|
||||
isDisabled={isDisabled}
|
||||
selectionMode="single"
|
||||
disallowEmptySelection
|
||||
selectedKeys={[value]}
|
||||
onSelectionChange={k => {
|
||||
const v = [...k][0];
|
||||
if (v != null) onChange(String(v));
|
||||
}}
|
||||
>
|
||||
{pos && <span className="rp-slider" data-still={pos.still || undefined} style={{left: pos.x, width: pos.w}} />}
|
||||
{items.map(([k, l]) => (
|
||||
<ToggleButton key={k} id={k} className="rp-btn">
|
||||
{l}
|
||||
</ToggleButton>
|
||||
))}
|
||||
</ToggleButtonGroup>
|
||||
{collapsed && (
|
||||
<div ref={pickRef} className="rp-segpick">
|
||||
<LabeledSelect bare label={label} value={value} onChange={onChange} isDisabled={isDisabled} items={items.map(([id, l]) => ({id, label: l}))} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+2
-1
@@ -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';
|
||||
|
||||
Reference in new issue
Block a user