A resize while the options were disabled cleared the highlight, and enabling them again did not bring it back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
425 lines
16 KiB
TypeScript
425 lines
16 KiB
TypeScript
import * as Menu from '@radix-ui/react-dropdown-menu'
|
|
import { Check, ChevronsUpDown, CircleAlert, Lock, RotateCcw } from 'lucide-react'
|
|
import React, { useEffect, useId, useLayoutEffect, useRef, useState } from 'react'
|
|
import { IconButton } from './controls'
|
|
|
|
// Settings page row shell and shared form controls (see docs/design-guidelines.md).
|
|
|
|
/** A labelled block of rows, like a grouped list in macOS System Settings. */
|
|
export const SetGroup: React.FC<{ title?: string; note?: React.ReactNode; children: React.ReactNode }> = ({ title, note, children }) => (
|
|
<section className="sgroup">
|
|
{title && <h3 className="sgroup-title">{title}</h3>}
|
|
<div className="sgroup-list">{children}</div>
|
|
{note && <p className="sgroup-note">{note}</p>}
|
|
</section>
|
|
)
|
|
|
|
interface RowProps {
|
|
title: string
|
|
desc?: React.ReactNode
|
|
/** Control drawn on the right of the title. */
|
|
control?: React.ReactNode
|
|
/** Control drawn under the title at full width (sliders, cards). */
|
|
block?: React.ReactNode
|
|
/** The value differs from what the VM has now. */
|
|
dirty?: boolean
|
|
onReset?: () => void
|
|
resetLabel?: string
|
|
/** The row can't act right now; `reason` says why. */
|
|
disabled?: boolean
|
|
reason?: string
|
|
error?: string
|
|
/** Anchor so a validation error can scroll its row into view. */
|
|
field?: string
|
|
}
|
|
|
|
/** One setting: title, one line of description, a control, and its state marks. */
|
|
export const SetRow: React.FC<RowProps> = ({ title, desc, control, block, dirty, onReset, resetLabel, disabled, reason, error, field }) => {
|
|
const titleId = useId()
|
|
return (
|
|
<div
|
|
className="srow"
|
|
data-field={field}
|
|
data-dirty={dirty || undefined}
|
|
data-disabled={disabled || undefined}
|
|
data-error={error ? true : undefined}
|
|
role="group"
|
|
aria-labelledby={titleId}
|
|
>
|
|
<div className="srow-head">
|
|
<div className="srow-text">
|
|
<div className="srow-title" id={titleId}>
|
|
{title}
|
|
{dirty && <span className="dirty-dot" aria-hidden="true" />}
|
|
</div>
|
|
{desc && <div className="srow-desc">{desc}</div>}
|
|
{disabled && reason && (
|
|
<div className="srow-reason"><Lock size={12} aria-hidden="true" />{reason}</div>
|
|
)}
|
|
</div>
|
|
{(control || (dirty && onReset)) && (
|
|
<div className="srow-ctrl">
|
|
{dirty && onReset && !disabled && (
|
|
<IconButton small label={resetLabel ?? 'Reset'} icon={<RotateCcw size={13} />} onClick={onReset} className="srow-reset" tipSide="top" />
|
|
)}
|
|
{control && <fieldset className="srow-fieldset" disabled={disabled}>{control}</fieldset>}
|
|
</div>
|
|
)}
|
|
</div>
|
|
{block && <fieldset className="srow-fieldset srow-block" disabled={disabled}>{block}</fieldset>}
|
|
{error && <div className="srow-error" role="alert"><CircleAlert size={13} aria-hidden="true" />{error}</div>}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/** Read-only row: a label and its value. */
|
|
export const SetValue: React.FC<{ title: string; value: React.ReactNode; mono?: boolean }> = ({ title, value, mono }) => (
|
|
<div className="srow srow-kv">
|
|
<div className="srow-head">
|
|
<div className="srow-title">{title}</div>
|
|
<div className={`srow-value${mono ? ' mono' : ''}`}>{value}</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
|
|
export const Switch: React.FC<{ checked: boolean; onChange: (v: boolean) => void; label: string; disabled?: boolean }> = ({ checked, onChange, label, disabled }) => (
|
|
<label className="switch rg-p-switch">
|
|
<input type="checkbox" role="switch" aria-label={label} checked={checked} disabled={disabled} onChange={(e) => onChange(e.target.checked)} />
|
|
<span className="slider" />
|
|
</label>
|
|
)
|
|
|
|
interface SegOption<V> {
|
|
value: V
|
|
label: string
|
|
disabled?: boolean
|
|
}
|
|
|
|
/**
|
|
* Segmented control with a sliding highlight. A radio group under the hood:
|
|
* arrow keys move the choice, Tab leaves the group.
|
|
*/
|
|
export function Segmented<V extends string | number>({ options, value, onChange, label, wide }: {
|
|
options: SegOption<V>[]
|
|
value: V | undefined
|
|
onChange: (v: V) => void
|
|
label: string
|
|
/** Stretch to the row's width with equal segments. */
|
|
wide?: boolean
|
|
}) {
|
|
const box = useRef<HTMLDivElement>(null)
|
|
const [thumb, setThumb] = useState<{ x: number; w: number; value: V } | null>(null)
|
|
|
|
const measure = () => {
|
|
const el = box.current?.querySelector<HTMLElement>('[aria-checked="true"]')
|
|
setThumb((prev) => {
|
|
// Options disabled while something is saving keep the highlight in place.
|
|
if (!el || value === undefined) return prev?.value === value ? prev : null
|
|
if (prev?.value === value && prev.x === el.offsetLeft && prev.w === el.offsetWidth) return prev
|
|
return { x: el.offsetLeft, w: el.offsetWidth, value }
|
|
})
|
|
}
|
|
const latestMeasure = useRef(measure)
|
|
latestMeasure.current = measure
|
|
const optionsSig = options.map((o) => `${String(o.value)}\x1f${o.label}\x1f${!!o.disabled}`).join('\x1e')
|
|
const docDir = typeof document !== 'undefined' ? document.documentElement.dir : ''
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
useLayoutEffect(measure, [value, optionsSig, wide, docDir])
|
|
useEffect(() => {
|
|
if (!box.current || typeof ResizeObserver === 'undefined') return
|
|
let raf = 0
|
|
const ro = new ResizeObserver(() => {
|
|
if (raf) cancelAnimationFrame(raf)
|
|
raf = requestAnimationFrame(() => {
|
|
raf = 0
|
|
latestMeasure.current()
|
|
})
|
|
})
|
|
ro.observe(box.current)
|
|
return () => {
|
|
if (raf) cancelAnimationFrame(raf)
|
|
ro.disconnect()
|
|
}
|
|
}, [])
|
|
|
|
const onKey = (e: React.KeyboardEvent) => {
|
|
if (e.key !== 'ArrowRight' && e.key !== 'ArrowLeft' && e.key !== 'ArrowDown' && e.key !== 'ArrowUp') return
|
|
e.preventDefault()
|
|
const enabled = options.filter((o) => !o.disabled)
|
|
const i = enabled.findIndex((o) => o.value === value)
|
|
const isRTL = box.current ? getComputedStyle(box.current).direction === 'rtl' : false
|
|
const forward = e.key === 'ArrowDown' || (e.key === 'ArrowRight' && !isRTL) || (e.key === 'ArrowLeft' && isRTL)
|
|
const delta = forward ? 1 : -1
|
|
const next = enabled[(i + delta + enabled.length) % enabled.length]
|
|
if (next) {
|
|
onChange(next.value)
|
|
requestAnimationFrame(() => box.current?.querySelector<HTMLElement>('[aria-checked="true"]')?.focus())
|
|
}
|
|
}
|
|
|
|
const firstEnabled = options.findIndex((o) => !o.disabled)
|
|
const hasActive = options.some((o) => !o.disabled && o.value === value)
|
|
return (
|
|
<div ref={box} className={`seg${wide ? ' seg-wide' : ''}`} role="radiogroup" aria-label={label} onKeyDown={onKey}>
|
|
{thumb && <span className="seg-thumb" style={{ transform: `translateX(${thumb.x}px)`, width: thumb.w }} aria-hidden="true" />}
|
|
{options.map((o, i) => {
|
|
const on = !o.disabled && o.value === value
|
|
return (
|
|
<button
|
|
key={String(o.value)}
|
|
type="button"
|
|
role="radio"
|
|
aria-checked={on}
|
|
tabIndex={on || (!hasActive && i === firstEnabled) ? 0 : -1}
|
|
disabled={o.disabled}
|
|
onClick={() => onChange(o.value)}
|
|
>
|
|
{o.label}
|
|
</button>
|
|
)
|
|
})}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
interface NumberProps {
|
|
value: number
|
|
min: number
|
|
max: number
|
|
step?: number
|
|
unit?: string
|
|
label: string
|
|
onChange: (v: number) => void
|
|
/** Width in characters; the default fits five digits. */
|
|
size?: number
|
|
}
|
|
|
|
/**
|
|
* Number field with a unit suffix and no spinner. It keeps what is typed so
|
|
* far and commits a whole, clamped number on Enter or blur; ↑/↓ step it.
|
|
*/
|
|
export const NumberField: React.FC<NumberProps> = ({ value, min, max, step = 1, unit, label, onChange, size = 5 }) => {
|
|
const [text, setText] = useState(String(value))
|
|
useEffect(() => { setText(String(value)) }, [value])
|
|
const clamp = (v: number) => Math.min(max, Math.max(min, Math.round(v / step) * step))
|
|
const commit = () => {
|
|
const n = Number.parseFloat(text)
|
|
if (!Number.isFinite(n)) { setText(String(value)); return }
|
|
const c = clamp(n)
|
|
setText(String(c))
|
|
if (c !== value) onChange(c)
|
|
}
|
|
return (
|
|
<div className="num-field">
|
|
<input
|
|
className="num-input"
|
|
type="text"
|
|
inputMode="numeric"
|
|
aria-label={label}
|
|
style={{ width: `${size + 1.5}ch` }}
|
|
value={text}
|
|
onChange={(e) => setText(e.target.value.replace(/[^\d.]/g, ''))}
|
|
onBlur={commit}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter') { e.preventDefault(); commit() }
|
|
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
|
|
e.preventDefault()
|
|
onChange(clamp(value + (e.key === 'ArrowUp' ? step : -step)))
|
|
}
|
|
}}
|
|
/>
|
|
{unit && <span className="num-unit">{unit}</span>}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/** Drop marks that would sit closer than `gap` percent to the one before. */
|
|
function thinMarks(marks: RangeMark[], min: number, max: number, gap = 9): RangeMark[] {
|
|
const span = Math.max(max - min, 1)
|
|
const out: RangeMark[] = []
|
|
let last = -Infinity
|
|
for (const m of marks) {
|
|
if (m.value < min || m.value > max) continue
|
|
const p = ((m.value - min) / span) * 100
|
|
if (p - last < gap) continue
|
|
out.push(m)
|
|
last = p
|
|
}
|
|
return out
|
|
}
|
|
|
|
interface RangeMark {
|
|
value: number
|
|
label: string
|
|
}
|
|
|
|
interface RangeProps {
|
|
value: number
|
|
min: number
|
|
max: number
|
|
step?: number
|
|
unit: string
|
|
label: string
|
|
onChange: (v: number) => void
|
|
/** Clickable labels under the track. */
|
|
marks?: RangeMark[]
|
|
/** Highlighted "recommended" span of the track. */
|
|
recommend?: [number, number]
|
|
/** From here to the end the track turns amber. */
|
|
warnFrom?: number
|
|
}
|
|
|
|
/**
|
|
* Slider and number field bound to one value. The slider and the marks write
|
|
* straight away; the field commits on Enter or blur (NumberField).
|
|
*/
|
|
export const RangeField: React.FC<RangeProps> = ({ value, min, max, step = 1, unit, label, onChange, marks, recommend, warnFrom }) => {
|
|
const span = Math.max(max - min, 1)
|
|
const pct = (v: number) => `${((Math.min(max, Math.max(min, v)) - min) / span) * 100}%`
|
|
const shown = marks ? thinMarks(marks, min, max) : []
|
|
|
|
const over = warnFrom !== undefined && value >= warnFrom
|
|
return (
|
|
<div className="range-field" data-warn={over || undefined}>
|
|
<div className="range-main">
|
|
<div className="range-rail-wrap" dir="ltr">
|
|
<div className="range-rail" aria-hidden="true">
|
|
<div className="range-track" />
|
|
{recommend && (
|
|
<div className="range-band rec" style={{ left: pct(recommend[0]), width: `calc(${pct(recommend[1])} - ${pct(recommend[0])})` }} />
|
|
)}
|
|
{warnFrom !== undefined && warnFrom <= max && (
|
|
<div className="range-band warn" style={{ left: pct(warnFrom), right: 0 }} />
|
|
)}
|
|
<div className="range-fill" style={{ width: pct(value) }} />
|
|
</div>
|
|
<input
|
|
className="range"
|
|
type="range"
|
|
aria-label={label}
|
|
min={min}
|
|
max={max}
|
|
step={step}
|
|
value={value}
|
|
onChange={(e) => onChange(Number(e.target.value))}
|
|
/>
|
|
{shown.length > 0 && (
|
|
<div className="range-marks">
|
|
{shown.map((m) => (
|
|
<button
|
|
key={m.value}
|
|
type="button"
|
|
tabIndex={-1}
|
|
className="range-mark"
|
|
aria-pressed={m.value === value}
|
|
style={{ left: pct(m.value) }}
|
|
onClick={() => onChange(m.value)}
|
|
>
|
|
{m.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<NumberField value={value} min={min} max={max} step={step} unit={unit} label={label} onChange={onChange} size={3} />
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
interface CardOption<V> {
|
|
value: V
|
|
title: string
|
|
sub?: string
|
|
badges?: string[]
|
|
/** Small picture drawn above the title (e.g. a screen shape). */
|
|
art?: React.ReactNode
|
|
disabled?: boolean
|
|
}
|
|
|
|
/** A row of option cards, one picked: a radio group with larger targets. */
|
|
export function OptionCards<V extends string>({ options, value, onChange, label, columns }: {
|
|
options: CardOption<V>[]
|
|
value: V | undefined
|
|
onChange: (v: V) => void
|
|
label: string
|
|
columns?: number
|
|
}) {
|
|
const name = useId()
|
|
return (
|
|
<div className="opt-cards" role="radiogroup" aria-label={label} style={columns ? { gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))` } : undefined}>
|
|
{options.map((o) => (
|
|
<label key={o.value} className="opt-card" data-selected={o.value === value || undefined} data-disabled={o.disabled || undefined}>
|
|
<input type="radio" name={name} checked={o.value === value} disabled={o.disabled} onChange={() => onChange(o.value)} />
|
|
{o.art}
|
|
<span className="opt-card-title" title={o.title}>{o.title}</span>
|
|
{o.sub && <span className="opt-card-sub">{o.sub}</span>}
|
|
{o.badges && o.badges.length > 0 && (
|
|
<span className="opt-card-badges">{o.badges.map((b) => <span key={b} className="badge rg-p-badge badge-accent">{b}</span>)}</span>
|
|
)}
|
|
</label>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
interface PopupOption<V> {
|
|
value: V
|
|
label: string
|
|
/** Secondary text drawn after the label in grey, e.g. a build number. */
|
|
detail?: string
|
|
disabled?: boolean
|
|
}
|
|
|
|
/**
|
|
* Pop-up button (HIG pop-up-buttons.md): shows the current choice with ⌃⌄
|
|
* and opens a menu where the current item carries a checkmark. Replaces the
|
|
* native <select>, whose Windows list ignores the app's theme.
|
|
*/
|
|
export function PopupSelect<V extends string>({ value, options, onChange, label, placeholder, wide, id }: {
|
|
value: V | undefined
|
|
options: PopupOption<V>[]
|
|
onChange: (v: V) => void
|
|
label: string
|
|
placeholder?: string
|
|
/** Fill the container's width instead of sizing to the label. */
|
|
wide?: boolean
|
|
id?: string
|
|
}) {
|
|
const cur = options.find((o) => o.value === value)
|
|
const dir: 'ltr' | 'rtl' = typeof document !== 'undefined' && document.documentElement.dir === 'rtl' ? 'rtl' : 'ltr'
|
|
return (
|
|
<Menu.Root dir={dir} modal={false}>
|
|
<Menu.Trigger asChild>
|
|
<button type="button" id={id} className={`popup-btn${wide ? ' popup-wide' : ''}`} aria-label={label}>
|
|
<span className="popup-value">
|
|
{cur ? cur.label : <span className="muted">{placeholder ?? value ?? ''}</span>}
|
|
{cur?.detail && <span className="popup-detail">{cur.detail}</span>}
|
|
</span>
|
|
<ChevronsUpDown size={13} aria-hidden="true" className="popup-chevron" />
|
|
</button>
|
|
</Menu.Trigger>
|
|
<Menu.Portal>
|
|
<Menu.Content className="menu rg-p-menu popup-menu" align="start" sideOffset={4} collisionPadding={8}>
|
|
<Menu.RadioGroup value={value ?? ''} onValueChange={(v) => onChange(v as V)}>
|
|
{options.map((o) => (
|
|
<Menu.RadioItem key={o.value} value={o.value} disabled={o.disabled} className="menu-item rg-p-menu-item popup-item">
|
|
<Menu.ItemIndicator className="popup-check"><Check size={13} /></Menu.ItemIndicator>
|
|
<span className="popup-item-label">{o.label}</span>
|
|
{o.detail && <span className="popup-detail">{o.detail}</span>}
|
|
</Menu.RadioItem>
|
|
))}
|
|
</Menu.RadioGroup>
|
|
</Menu.Content>
|
|
</Menu.Portal>
|
|
</Menu.Root>
|
|
)
|
|
}
|
|
|
|
/** A screen outline with the resolution's aspect ratio. */
|
|
export const ScreenArt: React.FC<{ w: number; h: number }> = ({ w, h }) => {
|
|
const W = 44
|
|
const H = Math.max(18, Math.min(34, Math.round((W * h) / Math.max(w, 1))))
|
|
return <span className="screen-art" style={{ width: W, height: H }} aria-hidden="true" />
|
|
}
|