@vitavision/ui 0.14.1 → 0.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +147 -3
- package/dist/index.js +325 -30
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/ui/DeltaCell.tsx +109 -0
- package/src/components/ui/Field.tsx +46 -13
- package/src/components/ui/IconButton.tsx +236 -0
- package/src/components/ui/Table.tsx +103 -10
- package/src/components/ui/index.ts +2 -0
- package/src/components/ui/tableGroups.ts +36 -0
package/package.json
CHANGED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A number with its difference from another: a value in a table cell, the signed change from
|
|
3
|
+
* a reference beside it, and a tint that says how much that change matters.
|
|
4
|
+
*
|
|
5
|
+
* It is generic on purpose. What the reference is (a default, a previous run, a design value),
|
|
6
|
+
* and how a change is judged large (against a tolerance, a spread, the other rows), is the
|
|
7
|
+
* caller's: it hands over `magnitude`, already normalised to `[0, 1]`, and the cell steps it
|
|
8
|
+
* into four strengths. The tint is the accent or a neutral, never a verdict colour: a change
|
|
9
|
+
* is not a pass or a fail.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { cn } from "./cn";
|
|
13
|
+
|
|
14
|
+
/** How strongly a `DeltaCell` is highlighted. */
|
|
15
|
+
export type DeltaStrength = "none" | "low" | "medium" | "high";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The strength a normalised magnitude is shown at.
|
|
19
|
+
*
|
|
20
|
+
* @param magnitude - Where the change falls on the caller's scale, `0` (no change) to `1` (the
|
|
21
|
+
* largest worth showing). Clamped. `undefined`, `null` and `NaN` give `none`.
|
|
22
|
+
* @returns `none` below 0.1, `low` below 0.4, `medium` below 0.7, else `high`.
|
|
23
|
+
*/
|
|
24
|
+
export function deltaStrength(magnitude: number | null | undefined): DeltaStrength {
|
|
25
|
+
if (magnitude === null || magnitude === undefined || Number.isNaN(magnitude)) return "none";
|
|
26
|
+
const m = Math.min(1, Math.max(0, magnitude));
|
|
27
|
+
if (m < 0.1) return "none";
|
|
28
|
+
if (m < 0.4) return "low";
|
|
29
|
+
if (m < 0.7) return "medium";
|
|
30
|
+
return "high";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const TINT = {
|
|
34
|
+
signal: { none: "", low: "bg-signal/8", medium: "bg-signal/16", high: "bg-signal/26" },
|
|
35
|
+
neutral: { none: "", low: "bg-fg/6", medium: "bg-fg/12", high: "bg-fg/20" },
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
const defaultFormat = (value: number) => String(Number(value.toPrecision(4)));
|
|
39
|
+
|
|
40
|
+
/** Props of `DeltaCell`. */
|
|
41
|
+
export interface DeltaCellProps {
|
|
42
|
+
/** The number. A missing or non-finite one shows an em dash. */
|
|
43
|
+
value: number | null | undefined;
|
|
44
|
+
/** Its signed difference from the reference, in the value's own units. Missing or non-finite shows no difference. */
|
|
45
|
+
delta?: number | null | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* How much the difference matters, normalised to `[0, 1]` by the caller: `0.25` is a quarter
|
|
48
|
+
* of the largest difference worth showing. Steps into a tint (see `deltaStrength`); leave it
|
|
49
|
+
* out for no tint.
|
|
50
|
+
*/
|
|
51
|
+
magnitude?: number | null | undefined;
|
|
52
|
+
/** The tint: the accent (`signal`, the default) or a neutral grey. Never a verdict colour. */
|
|
53
|
+
tone?: "signal" | "neutral" | undefined;
|
|
54
|
+
/** Formats the value. Defaults to at most 4 significant figures, without trailing zeros. */
|
|
55
|
+
format?: ((value: number) => string) | undefined;
|
|
56
|
+
/** Formats the difference's size, without its sign. Defaults to `format`. */
|
|
57
|
+
deltaFormat?: ((delta: number) => string) | undefined;
|
|
58
|
+
/** The unit, written after the value. */
|
|
59
|
+
unit?: string | undefined;
|
|
60
|
+
/** What the difference is from, for assistive technology: "from default". Defaults to "change". */
|
|
61
|
+
deltaLabel?: string | undefined;
|
|
62
|
+
/** Merged onto the cell with `cn`. */
|
|
63
|
+
className?: string | undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A value with its signed difference and a strength tint, for a numeric `Table` cell.
|
|
68
|
+
*
|
|
69
|
+
* The difference is written with an explicit sign (`+0.012`, `−0.4`; a zero has none) in muted
|
|
70
|
+
* text beside the value, and is announced as "change +0.012" or with `deltaLabel`. The tint
|
|
71
|
+
* carries `data-strength` (`none`, `low`, `medium`, `high`); it is never the only cue, since the
|
|
72
|
+
* difference is also text. Right-aligned and in mono when it sits in a `numeric` column.
|
|
73
|
+
*/
|
|
74
|
+
export function DeltaCell({
|
|
75
|
+
value,
|
|
76
|
+
delta,
|
|
77
|
+
magnitude,
|
|
78
|
+
tone = "signal",
|
|
79
|
+
format = defaultFormat,
|
|
80
|
+
deltaFormat,
|
|
81
|
+
unit,
|
|
82
|
+
deltaLabel = "change",
|
|
83
|
+
className,
|
|
84
|
+
}: DeltaCellProps) {
|
|
85
|
+
const present = typeof value === "number" && Number.isFinite(value);
|
|
86
|
+
const hasDelta = typeof delta === "number" && Number.isFinite(delta);
|
|
87
|
+
const strength = deltaStrength(magnitude);
|
|
88
|
+
const sign = !hasDelta || delta === 0 ? "" : delta > 0 ? "+" : "−";
|
|
89
|
+
const size = hasDelta ? (deltaFormat ?? format)(Math.abs(delta)) : "";
|
|
90
|
+
return (
|
|
91
|
+
<span
|
|
92
|
+
data-delta-cell=""
|
|
93
|
+
data-strength={strength}
|
|
94
|
+
className={cn("inline-flex items-baseline justify-end gap-1.5 rounded-control px-1.5 py-0.5", TINT[tone][strength], className)}
|
|
95
|
+
>
|
|
96
|
+
<span>
|
|
97
|
+
{present ? format(value) : "—"}
|
|
98
|
+
{present && unit && <span className={cn("ml-0.5", strength === "none" ? "text-fg-muted" : "text-fg")}>{unit}</span>}
|
|
99
|
+
</span>
|
|
100
|
+
{hasDelta && (
|
|
101
|
+
<span className={cn("text-[0.85em]", strength === "none" ? "text-fg-muted" : "text-fg")}>
|
|
102
|
+
<span className="sr-only">{deltaLabel} </span>
|
|
103
|
+
{sign}
|
|
104
|
+
{size}
|
|
105
|
+
</span>
|
|
106
|
+
)}
|
|
107
|
+
</span>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
@@ -17,6 +17,12 @@ import { createContext, use, useId, useMemo, type ReactNode } from "react";
|
|
|
17
17
|
import { byDensity, useDensity } from "./Density";
|
|
18
18
|
import { cn } from "./cn";
|
|
19
19
|
|
|
20
|
+
/** The `side` row: a label column and the control, centred on one line. */
|
|
21
|
+
const SIDE_ROW = "grid min-w-0 grid-cols-[var(--field-label-w,8.5rem)_minmax(0,1fr)] items-center gap-x-3";
|
|
22
|
+
|
|
23
|
+
/** Lines the description and the error up under the control of a `side` row. */
|
|
24
|
+
const SIDE_UNDER = "ml-[calc(var(--field-label-w,8.5rem)+0.75rem)]";
|
|
25
|
+
|
|
20
26
|
/** What a `Field` tells the control inside it: the ids that describe it, and whether it is invalid. */
|
|
21
27
|
interface FieldControl {
|
|
22
28
|
describedBy: string | undefined;
|
|
@@ -57,6 +63,7 @@ export function Field({
|
|
|
57
63
|
required = false,
|
|
58
64
|
annotation,
|
|
59
65
|
as = "label",
|
|
66
|
+
layout = "top",
|
|
60
67
|
className,
|
|
61
68
|
children,
|
|
62
69
|
}: {
|
|
@@ -75,6 +82,14 @@ export function Field({
|
|
|
75
82
|
* `role="group"` named by `label`, for a set of radios or a segmented control.
|
|
76
83
|
*/
|
|
77
84
|
as?: "label" | "group";
|
|
85
|
+
/**
|
|
86
|
+
* `top` (the default) puts the label above the control. `side` is the property-sheet row:
|
|
87
|
+
* the label in a fixed column on the left (one line, truncated, the full text in its
|
|
88
|
+
* `title`), the control on the right, the annotation after the label. The column's width
|
|
89
|
+
* is the CSS variable `--field-label-w` (default `8.5rem`), so a parent can set it once
|
|
90
|
+
* for every row. The description and the error line up under the control.
|
|
91
|
+
*/
|
|
92
|
+
layout?: "top" | "side";
|
|
78
93
|
/** Merged with the field's own classes through `cn`. */
|
|
79
94
|
className?: string | undefined;
|
|
80
95
|
/** The control. */
|
|
@@ -95,41 +110,59 @@ export function Field({
|
|
|
95
110
|
|
|
96
111
|
const gap = byDensity(density, "gap-1.5", "gap-1");
|
|
97
112
|
|
|
98
|
-
const
|
|
113
|
+
const side = layout === "side";
|
|
114
|
+
const labelText = (
|
|
99
115
|
<>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
116
|
+
{label}
|
|
117
|
+
{required && (
|
|
118
|
+
<span className="ml-0.5 text-defect" title="Required">
|
|
119
|
+
*
|
|
120
|
+
</span>
|
|
121
|
+
)}
|
|
122
|
+
</>
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const labelled = side ? (
|
|
126
|
+
<>
|
|
127
|
+
<span className="flex min-w-0 items-center gap-1.5">
|
|
128
|
+
<span className={cn("min-w-0 truncate font-medium text-fg", byDensity(density, "text-xs", "text-[11px]"))} title={label}>
|
|
129
|
+
{labelText}
|
|
108
130
|
</span>
|
|
131
|
+
{annotation && <span className="shrink-0 font-mono text-[11px] text-fg-subtle">{annotation}</span>}
|
|
132
|
+
</span>
|
|
133
|
+
<FieldContext value={control}>{children}</FieldContext>
|
|
134
|
+
</>
|
|
135
|
+
) : (
|
|
136
|
+
<>
|
|
137
|
+
<span className="flex items-baseline gap-2">
|
|
138
|
+
<span className={cn("font-medium text-fg", byDensity(density, "text-xs", "text-[11px]"))}>{labelText}</span>
|
|
109
139
|
{annotation && <span className="ml-auto font-mono text-[11px] text-fg-subtle">{annotation}</span>}
|
|
110
140
|
</span>
|
|
111
141
|
<FieldContext value={control}>{children}</FieldContext>
|
|
112
142
|
</>
|
|
113
143
|
);
|
|
144
|
+
const rowClasses = side ? SIDE_ROW : cn("flex min-w-0 flex-col", gap);
|
|
145
|
+
const under = side ? SIDE_UNDER : undefined;
|
|
114
146
|
|
|
115
147
|
return (
|
|
116
148
|
<div
|
|
117
|
-
className={cn("flex min-w-0 flex-col", gap, className)}
|
|
149
|
+
className={cn("flex min-w-0 flex-col", side ? "gap-0.5" : gap, className)}
|
|
150
|
+
data-layout={side ? "side" : undefined}
|
|
118
151
|
data-invalid={hasError ? "" : undefined}
|
|
119
152
|
data-required={required ? "" : undefined}
|
|
120
153
|
{...(as === "group" ? { role: "group", "aria-label": label, "aria-describedby": describedBy } : {})}
|
|
121
154
|
>
|
|
122
155
|
{/* The description and the error sit outside the <label>: inside it they would be read
|
|
123
156
|
as part of the control's name as well as its description. */}
|
|
124
|
-
{as === "label" ? <label className={
|
|
157
|
+
{as === "label" ? <label className={rowClasses}>{labelled}</label> : side ? <div className={rowClasses}>{labelled}</div> : labelled}
|
|
125
158
|
|
|
126
159
|
{description && (
|
|
127
|
-
<span id={descriptionId} className="text-xs leading-snug text-fg-muted">
|
|
160
|
+
<span id={descriptionId} className={cn("text-xs leading-snug text-fg-muted", under)}>
|
|
128
161
|
{description}
|
|
129
162
|
</span>
|
|
130
163
|
)}
|
|
131
164
|
{hasError && (
|
|
132
|
-
<span id={errorId} role="alert" className="text-xs leading-snug text-defect">
|
|
165
|
+
<span id={errorId} role="alert" className={cn("text-xs leading-snug text-defect", under)}>
|
|
133
166
|
{error}
|
|
134
167
|
</span>
|
|
135
168
|
)}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* An action or toggle that is only an icon, named and explained by its tooltip.
|
|
3
|
+
*
|
|
4
|
+
* The icon-first spelling of `Button` for dense toolbars: the label is not shown, it is the
|
|
5
|
+
* accessible name and the tooltip, so the two can never disagree. The look is the stage
|
|
6
|
+
* toolbar's — a quiet square that tints with the accent while pressed.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ComponentProps, ReactNode } from "react";
|
|
10
|
+
import { useRef, useState } from "react";
|
|
11
|
+
|
|
12
|
+
import { type ButtonSize, type ButtonVariant, buttonClasses } from "./Button";
|
|
13
|
+
import { byDensity, useDensity } from "./Density";
|
|
14
|
+
import { Kbd } from "./Kbd";
|
|
15
|
+
import { Tooltip } from "./Tooltip";
|
|
16
|
+
import { cn } from "./cn";
|
|
17
|
+
|
|
18
|
+
/** The square box of each size: `sm` 28 px, `md` 32 px — the heights of `Button`. */
|
|
19
|
+
const BOX_CLASSES: Record<ButtonSize, string> = {
|
|
20
|
+
sm: "size-7 p-0 [&>svg]:size-4",
|
|
21
|
+
md: "size-8 p-0 [&>svg]:size-4",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** What the tooltip shows: the label, an optional hint line, an optional key cap. */
|
|
25
|
+
function TipContent({ label, hint, shortcut }: { label: string; hint?: ReactNode; shortcut?: string | undefined }) {
|
|
26
|
+
return (
|
|
27
|
+
<span className="flex items-center gap-2">
|
|
28
|
+
<span>
|
|
29
|
+
<span className="font-medium">{label}</span>
|
|
30
|
+
{hint !== undefined && <span className="block text-fg-muted">{hint}</span>}
|
|
31
|
+
</span>
|
|
32
|
+
{shortcut && <Kbd>{shortcut}</Kbd>}
|
|
33
|
+
</span>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* An icon-only button. `label` is its accessible name *and* its tooltip, so it is required;
|
|
39
|
+
* `shortcut` adds a key cap to the tooltip and `aria-keyshortcuts`. `pressed` makes it a
|
|
40
|
+
* toggle (`aria-pressed`, `data-state="on"` / `"off"`).
|
|
41
|
+
*
|
|
42
|
+
* Needs a `TooltipProvider` above it. Takes every `<button>` prop, `ref` included. A
|
|
43
|
+
* disabled button shows no tooltip (browsers send it no pointer events), so say why in the
|
|
44
|
+
* surrounding UI when the reason is not obvious.
|
|
45
|
+
*/
|
|
46
|
+
export function IconButton({
|
|
47
|
+
label,
|
|
48
|
+
icon,
|
|
49
|
+
hint,
|
|
50
|
+
shortcut,
|
|
51
|
+
pressed,
|
|
52
|
+
variant = "ghost",
|
|
53
|
+
size,
|
|
54
|
+
type = "button",
|
|
55
|
+
className,
|
|
56
|
+
...rest
|
|
57
|
+
}: Omit<ComponentProps<"button">, "children" | "title" | "aria-label" | "aria-pressed"> & {
|
|
58
|
+
/** The accessible name, also the tooltip's text. */
|
|
59
|
+
label: string;
|
|
60
|
+
/** The icon, decorative (`aria-hidden`). Sized to 16 px. */
|
|
61
|
+
icon: ReactNode;
|
|
62
|
+
/** A second tooltip line after the label, e.g. why it is the way it is. */
|
|
63
|
+
hint?: ReactNode;
|
|
64
|
+
/** The key that does the same, shown in the tooltip. Display only: binding it is yours. */
|
|
65
|
+
shortcut?: string | undefined;
|
|
66
|
+
/** A toggle's state; omit for a plain action. */
|
|
67
|
+
pressed?: boolean | undefined;
|
|
68
|
+
/** Same variants as `Button`. Defaults to `ghost`, the toolbar look. */
|
|
69
|
+
variant?: ButtonVariant | undefined;
|
|
70
|
+
/** Defaults to the density in force — `md` comfortable, `sm` compact. */
|
|
71
|
+
size?: ButtonSize | undefined;
|
|
72
|
+
}) {
|
|
73
|
+
const density = useDensity();
|
|
74
|
+
const resolved = size ?? byDensity(density, "md", "sm");
|
|
75
|
+
return (
|
|
76
|
+
<Tooltip content={<TipContent label={label} hint={hint} shortcut={shortcut} />}>
|
|
77
|
+
<button
|
|
78
|
+
{...rest}
|
|
79
|
+
type={type}
|
|
80
|
+
aria-label={label}
|
|
81
|
+
aria-keyshortcuts={shortcut}
|
|
82
|
+
aria-pressed={pressed}
|
|
83
|
+
data-variant={variant}
|
|
84
|
+
data-state={pressed === undefined ? undefined : pressed ? "on" : "off"}
|
|
85
|
+
className={buttonClasses({
|
|
86
|
+
variant,
|
|
87
|
+
size: resolved,
|
|
88
|
+
className: cn(
|
|
89
|
+
BOX_CLASSES[resolved],
|
|
90
|
+
pressed && variant === "ghost" && "bg-signal/15 text-signal hover:bg-signal/20 hover:text-signal",
|
|
91
|
+
className,
|
|
92
|
+
),
|
|
93
|
+
})}
|
|
94
|
+
>
|
|
95
|
+
<span aria-hidden className="contents">
|
|
96
|
+
{icon}
|
|
97
|
+
</span>
|
|
98
|
+
</button>
|
|
99
|
+
</Tooltip>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** One item of an `IconToggleGroup`. */
|
|
104
|
+
export interface IconToggleItem {
|
|
105
|
+
/** Its identity, reported through `onValueChange`. */
|
|
106
|
+
value: string;
|
|
107
|
+
/** The accessible name and tooltip text. */
|
|
108
|
+
label: string;
|
|
109
|
+
/** The icon, decorative. */
|
|
110
|
+
icon: ReactNode;
|
|
111
|
+
/** A key that does the same, shown in the tooltip. */
|
|
112
|
+
shortcut?: string | undefined;
|
|
113
|
+
/** Blocks this item only. */
|
|
114
|
+
disabled?: boolean | undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
type GroupCommon = {
|
|
118
|
+
/** The items, in order. */
|
|
119
|
+
items: readonly IconToggleItem[];
|
|
120
|
+
/** Names the group. */
|
|
121
|
+
"aria-label": string;
|
|
122
|
+
/** Defaults to the density in force. */
|
|
123
|
+
size?: ButtonSize | undefined;
|
|
124
|
+
/** Blocks every item. */
|
|
125
|
+
disabled?: boolean | undefined;
|
|
126
|
+
/** Merged onto the group with `cn`. */
|
|
127
|
+
className?: string | undefined;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** Props of `IconToggleGroup`: one selected value, or any number. */
|
|
131
|
+
export type IconToggleGroupProps = GroupCommon &
|
|
132
|
+
(
|
|
133
|
+
| {
|
|
134
|
+
/** One item at a time. Selecting the selected one keeps it (`""` means none). */
|
|
135
|
+
type?: "single" | undefined;
|
|
136
|
+
value?: string | undefined;
|
|
137
|
+
defaultValue?: string | undefined;
|
|
138
|
+
onValueChange?: ((value: string) => void) | undefined;
|
|
139
|
+
}
|
|
140
|
+
| {
|
|
141
|
+
/** Any subset. */
|
|
142
|
+
type: "multiple";
|
|
143
|
+
value?: readonly string[] | undefined;
|
|
144
|
+
defaultValue?: readonly string[] | undefined;
|
|
145
|
+
onValueChange?: ((value: string[]) => void) | undefined;
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A strip of icon toggles — a tool picker, a view-mode switch, text-style toggles — each
|
|
151
|
+
* named by its tooltip. `type="single"` (the default) keeps exactly the chosen item
|
|
152
|
+
* pressed; `type="multiple"` lets each toggle on its own. Controlled with `value` or
|
|
153
|
+
* uncontrolled with `defaultValue`.
|
|
154
|
+
*
|
|
155
|
+
* Each item is a button with `aria-pressed` inside a `role="group"`. One item is a tab stop
|
|
156
|
+
* (the pressed one, else the first); Arrow keys, Home and End move focus along the strip.
|
|
157
|
+
* Needs a `TooltipProvider` above it.
|
|
158
|
+
*/
|
|
159
|
+
export function IconToggleGroup(props: IconToggleGroupProps) {
|
|
160
|
+
const { items, size, disabled = false, className, "aria-label": ariaLabel } = props;
|
|
161
|
+
const multiple = props.type === "multiple";
|
|
162
|
+
const density = useDensity();
|
|
163
|
+
const resolved = size ?? byDensity(density, "md", "sm");
|
|
164
|
+
const [internal, setInternal] = useControllable(props.value, props.defaultValue);
|
|
165
|
+
const selected: readonly string[] = toArray(internal);
|
|
166
|
+
const stripRef = useRef<HTMLDivElement>(null);
|
|
167
|
+
|
|
168
|
+
const toggle = (value: string) => {
|
|
169
|
+
if (props.type === "multiple") {
|
|
170
|
+
const next = selected.includes(value) ? selected.filter((v) => v !== value) : [...selected, value];
|
|
171
|
+
setInternal(next);
|
|
172
|
+
props.onValueChange?.(next);
|
|
173
|
+
} else {
|
|
174
|
+
if (selected.includes(value)) return;
|
|
175
|
+
setInternal(value);
|
|
176
|
+
props.onValueChange?.(value);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const enabled = items.filter((item) => !item.disabled && !disabled);
|
|
181
|
+
const stop = enabled.find((item) => selected.includes(item.value)) ?? enabled[0];
|
|
182
|
+
|
|
183
|
+
const onKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
184
|
+
const keys = ["ArrowRight", "ArrowDown", "ArrowLeft", "ArrowUp", "Home", "End"];
|
|
185
|
+
if (!keys.includes(event.key)) return;
|
|
186
|
+
const buttons = Array.from(stripRef.current?.querySelectorAll<HTMLButtonElement>("button:not(:disabled)") ?? []);
|
|
187
|
+
const at = buttons.indexOf(document.activeElement as HTMLButtonElement);
|
|
188
|
+
if (at < 0 || buttons.length === 0) return;
|
|
189
|
+
event.preventDefault();
|
|
190
|
+
const target =
|
|
191
|
+
event.key === "Home"
|
|
192
|
+
? 0
|
|
193
|
+
: event.key === "End"
|
|
194
|
+
? buttons.length - 1
|
|
195
|
+
: (at + (event.key === "ArrowRight" || event.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length;
|
|
196
|
+
buttons[target]?.focus();
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<div
|
|
201
|
+
ref={stripRef}
|
|
202
|
+
role="group"
|
|
203
|
+
aria-label={ariaLabel}
|
|
204
|
+
data-type={multiple ? "multiple" : "single"}
|
|
205
|
+
data-disabled={disabled ? "" : undefined}
|
|
206
|
+
onKeyDown={onKeyDown}
|
|
207
|
+
className={cn("inline-flex shrink-0 items-center gap-0.5 rounded-control border border-line-strong bg-raised p-0.5", className)}
|
|
208
|
+
>
|
|
209
|
+
{items.map((item) => (
|
|
210
|
+
<IconButton
|
|
211
|
+
key={item.value}
|
|
212
|
+
label={item.label}
|
|
213
|
+
icon={item.icon}
|
|
214
|
+
shortcut={item.shortcut}
|
|
215
|
+
size={resolved}
|
|
216
|
+
pressed={selected.includes(item.value)}
|
|
217
|
+
disabled={disabled || item.disabled}
|
|
218
|
+
tabIndex={stop?.value === item.value ? 0 : -1}
|
|
219
|
+
onClick={() => toggle(item.value)}
|
|
220
|
+
className={cn("rounded-[0.3rem]", selected.includes(item.value) && "bg-surface text-signal shadow-sm")}
|
|
221
|
+
/>
|
|
222
|
+
))}
|
|
223
|
+
</div>
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function toArray(value: string | readonly string[] | undefined): readonly string[] {
|
|
228
|
+
if (value === undefined || value === "") return [];
|
|
229
|
+
return typeof value === "string" ? [value] : value;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Minimal controlled/uncontrolled state: `value` wins when given. */
|
|
233
|
+
function useControllable<T>(value: T | undefined, defaultValue: T | undefined) {
|
|
234
|
+
const [state, setState] = useState<T | undefined>(defaultValue);
|
|
235
|
+
return [value !== undefined ? value : state, setState] as const;
|
|
236
|
+
}
|