@vitavision/ui 0.14.0 → 0.14.2
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 +2 -2
- package/dist/index.d.ts +94 -2
- package/dist/index.js +192 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/ui/DeltaCell.tsx +109 -0
- package/src/components/ui/ShortcutList.tsx +74 -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
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The keyboard shortcuts of a screen, listed for its help: the `?` dialog of a review tool, a
|
|
3
|
+
* shortcuts section in a side panel.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Fragment, useId, type ComponentProps } from "react";
|
|
7
|
+
|
|
8
|
+
import { cn } from "./cn";
|
|
9
|
+
import { Kbd } from "./Kbd";
|
|
10
|
+
|
|
11
|
+
/** One command and the keys that run it. */
|
|
12
|
+
export interface Shortcut {
|
|
13
|
+
/**
|
|
14
|
+
* The keys, as the keyboard labels them: each entry is one key or chord (`J`, `⇧ click`,
|
|
15
|
+
* `⌘K`), and several distinct entries are alternatives (`["J", "↓"]` reads "J or ↓").
|
|
16
|
+
*/
|
|
17
|
+
keys: readonly string[];
|
|
18
|
+
/** What it does, as a short phrase ("Next tile"), unique within its group. */
|
|
19
|
+
description: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A titled group of shortcuts ("Navigation", "Flags"). */
|
|
23
|
+
export interface ShortcutGroup {
|
|
24
|
+
/** The group's heading, unique within the list. */
|
|
25
|
+
title: string;
|
|
26
|
+
/** Its shortcuts, in the order they should read. */
|
|
27
|
+
shortcuts: readonly Shortcut[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Props of `ShortcutList`. */
|
|
31
|
+
export type ShortcutListProps = Omit<ComponentProps<"div">, "children"> & {
|
|
32
|
+
/** The groups, in the order they should read. Empty groups are left out. */
|
|
33
|
+
groups: readonly ShortcutGroup[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The keyboard shortcuts of a screen as titled groups: each row a command's keys, as `Kbd`
|
|
38
|
+
* caps, beside what it does. Groups flow into two columns where there is room.
|
|
39
|
+
*
|
|
40
|
+
* It only lists: binding the keys is the app's. Put it in a `Dialog` for a `?` help, or in a
|
|
41
|
+
* panel. Each group is a `<section>` labelled by its heading, and its rows are a description
|
|
42
|
+
* list, so a screen reader announces "J or ↓, Next tile".
|
|
43
|
+
*/
|
|
44
|
+
export function ShortcutList({ groups, className, ...rest }: ShortcutListProps) {
|
|
45
|
+
const baseId = useId();
|
|
46
|
+
return (
|
|
47
|
+
<div {...rest} className={cn("grid gap-x-8 gap-y-4 sm:grid-cols-2", className)}>
|
|
48
|
+
{groups.map((group, g) =>
|
|
49
|
+
group.shortcuts.length === 0 ? null : (
|
|
50
|
+
<section key={group.title} aria-labelledby={`${baseId}-${g}`} className="min-w-0">
|
|
51
|
+
<h3 id={`${baseId}-${g}`} className="mb-1.5 text-[11px] font-semibold tracking-wider text-fg-muted uppercase">
|
|
52
|
+
{group.title}
|
|
53
|
+
</h3>
|
|
54
|
+
<dl className="grid grid-cols-[auto_1fr] items-baseline gap-x-3 gap-y-1 text-sm">
|
|
55
|
+
{group.shortcuts.map((shortcut) => (
|
|
56
|
+
<Fragment key={shortcut.description}>
|
|
57
|
+
<dt className="flex flex-wrap items-center gap-1 text-xs text-fg-muted">
|
|
58
|
+
{shortcut.keys.map((key, k) => (
|
|
59
|
+
<Fragment key={key}>
|
|
60
|
+
{k > 0 && <span>or</span>}
|
|
61
|
+
<Kbd>{key}</Kbd>
|
|
62
|
+
</Fragment>
|
|
63
|
+
))}
|
|
64
|
+
</dt>
|
|
65
|
+
<dd className="text-fg">{shortcut.description}</dd>
|
|
66
|
+
</Fragment>
|
|
67
|
+
))}
|
|
68
|
+
</dl>
|
|
69
|
+
</section>
|
|
70
|
+
),
|
|
71
|
+
)}
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
* Practices: the header's content is a button, and only the sorted header carries
|
|
10
10
|
* `aria-sort`. The order itself lives in `tableSort.ts`.
|
|
11
11
|
*
|
|
12
|
+
* `group` on a column adds a header row above the column headers: adjacent columns that share a
|
|
13
|
+
* label sit under one cell that spans them (`tableGroups.ts`). `pinFirstColumn` keeps the first
|
|
14
|
+
* column in view while the rest scroll sideways.
|
|
15
|
+
*
|
|
12
16
|
* `windowed` renders only the rows in view, between two spacer rows that keep the scroll
|
|
13
17
|
* height, so a table of thousands of rows scrolls as fast as one of thirty. It stays a real
|
|
14
18
|
* `<table>`: the rows that are rendered carry `aria-rowindex` and the table `aria-rowcount`, so
|
|
@@ -20,6 +24,7 @@ import { type KeyboardEvent, type MouseEvent, type ReactNode, useLayoutEffect, u
|
|
|
20
24
|
|
|
21
25
|
import { byDensity, useDensity } from "./Density";
|
|
22
26
|
import { cn, focusRing, focusRingInset } from "./cn";
|
|
27
|
+
import { columnGroups } from "./tableGroups";
|
|
23
28
|
import { isSortable, nextSort, type SortDirection, sortOrder, type TableSort } from "./tableSort";
|
|
24
29
|
import { navigate, rowWindow, scrollTopFor } from "./tableWindow";
|
|
25
30
|
|
|
@@ -33,6 +38,12 @@ export type Column<Row> = {
|
|
|
33
38
|
numeric?: boolean;
|
|
34
39
|
/** A CSS width for the column, e.g. `"6rem"`. */
|
|
35
40
|
width?: string;
|
|
41
|
+
/**
|
|
42
|
+
* The label of the header group the column belongs to. Adjacent columns with the same label
|
|
43
|
+
* are headed by one cell that spans them, in a row above the column headers; columns with no
|
|
44
|
+
* group get an empty cell there. Any column with a `group` makes the row appear.
|
|
45
|
+
*/
|
|
46
|
+
group?: string | undefined;
|
|
36
47
|
/** The cell's content for a row. */
|
|
37
48
|
cell: (row: Row) => ReactNode;
|
|
38
49
|
/**
|
|
@@ -84,6 +95,25 @@ export interface TableWindowing {
|
|
|
84
95
|
/** Rows per screen before a windowed table has measured itself (a server render, the first paint). */
|
|
85
96
|
const INITIAL_ROWS = 20;
|
|
86
97
|
|
|
98
|
+
/**
|
|
99
|
+
* A pinned cell sticks to the left edge of the scroll area on `surface`, with a rule on its
|
|
100
|
+
* right edge so a cell scrolling under it reads as under it.
|
|
101
|
+
*/
|
|
102
|
+
const pinned = "sticky left-0 z-[2] bg-surface shadow-[inset_-1px_0_0_var(--line)]";
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A pinned body cell: it must be opaque, so a row's hover and active tint is painted on the
|
|
106
|
+
* cell itself, mixed over `surface`, to match what the row's other cells show through.
|
|
107
|
+
*/
|
|
108
|
+
function pinnedCell(clickable: boolean, active: boolean): string {
|
|
109
|
+
return cn(
|
|
110
|
+
"sticky left-0 z-[1] bg-surface shadow-[inset_-1px_0_0_var(--line)]",
|
|
111
|
+
clickable && "group-hover/row:bg-raised",
|
|
112
|
+
active &&
|
|
113
|
+
"bg-[color-mix(in_oklab,var(--surface),var(--signal)_10%)] group-hover/row:bg-[color-mix(in_oklab,var(--raised),var(--signal)_10%)]",
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
87
117
|
/** The header icon for a sort state: the direction when sorted, both arrows when only sortable. */
|
|
88
118
|
const SORT_ICONS = { ascending: ArrowUp, descending: ArrowDown, none: ArrowUpDown } as const;
|
|
89
119
|
|
|
@@ -123,6 +153,7 @@ export function Table<Row>({
|
|
|
123
153
|
manualSort = false,
|
|
124
154
|
windowed,
|
|
125
155
|
selectionFollowsFocus = false,
|
|
156
|
+
pinFirstColumn = false,
|
|
126
157
|
}: {
|
|
127
158
|
/** The columns, in order. */
|
|
128
159
|
columns: Column<Row>[];
|
|
@@ -181,6 +212,13 @@ export function Table<Row>({
|
|
|
181
212
|
* choose one. Needs `onRowClick`.
|
|
182
213
|
*/
|
|
183
214
|
selectionFollowsFocus?: boolean | undefined;
|
|
215
|
+
/**
|
|
216
|
+
* Keep the first column in view while the others scroll sideways, for a table wider than its
|
|
217
|
+
* container; the table is then as wide as its content (`min-w-max`) rather than squeezed to fit.
|
|
218
|
+
* The column is drawn on `surface`, the panel colour, with a rule on its right edge;
|
|
219
|
+
* give it a `width`. Put the table on a `Panel` or another `surface`.
|
|
220
|
+
*/
|
|
221
|
+
pinFirstColumn?: boolean | undefined;
|
|
184
222
|
}) {
|
|
185
223
|
const density = useDensity();
|
|
186
224
|
const [ownSort, setOwnSort] = useState<TableSort | null>(defaultSort);
|
|
@@ -201,13 +239,18 @@ export function Table<Row>({
|
|
|
201
239
|
const rowHeight = windowed?.rowHeight ?? 0;
|
|
202
240
|
const scrollRef = useRef<HTMLDivElement>(null);
|
|
203
241
|
const headRef = useRef<HTMLTableSectionElement>(null);
|
|
242
|
+
const groupRowRef = useRef<HTMLTableRowElement>(null);
|
|
243
|
+
const groups = useMemo(() => columnGroups(columns), [columns]);
|
|
244
|
+
// The group row and the column headers are two rows: the ARIA row numbers of the body start after both.
|
|
245
|
+
const headerRows = groups ? 2 : 1;
|
|
204
246
|
const bodyRef = useRef<HTMLTableSectionElement>(null);
|
|
205
247
|
// The row to focus once it is rendered: a key press may scroll to a row that is not in the DOM yet.
|
|
206
248
|
const pendingFocus = useRef<number | null>(null);
|
|
207
249
|
const [scrollTop, setScrollTop] = useState(0);
|
|
208
|
-
const [measured, setMeasured] = useState<{ viewport: number; header: number } | null>(null);
|
|
250
|
+
const [measured, setMeasured] = useState<{ viewport: number; header: number; groupRow: number } | null>(null);
|
|
209
251
|
const viewportHeight = measured?.viewport ?? windowed?.height ?? INITIAL_ROWS * rowHeight;
|
|
210
252
|
const headerHeight = measured?.header ?? 0;
|
|
253
|
+
const groupRowHeight = measured?.groupRow ?? 0;
|
|
211
254
|
const range = windowed
|
|
212
255
|
? rowWindow({ rowCount: order.length, rowHeight, scrollTop, viewportHeight, headerHeight, overscan: windowed.overscan ?? 8 })
|
|
213
256
|
: { start: 0, end: order.length, before: 0, after: 0 };
|
|
@@ -222,8 +265,14 @@ export function Table<Row>({
|
|
|
222
265
|
const measure = () => {
|
|
223
266
|
// A detached element measures 0, which would shrink the window to a row or two.
|
|
224
267
|
if (!scroller.isConnected) return;
|
|
225
|
-
const next = {
|
|
226
|
-
|
|
268
|
+
const next = {
|
|
269
|
+
viewport: scroller.clientHeight,
|
|
270
|
+
header: headRef.current?.offsetHeight ?? 0,
|
|
271
|
+
groupRow: groupRowRef.current?.offsetHeight ?? 0,
|
|
272
|
+
};
|
|
273
|
+
setMeasured((last) =>
|
|
274
|
+
last?.viewport === next.viewport && last.header === next.header && last.groupRow === next.groupRow ? last : next,
|
|
275
|
+
);
|
|
227
276
|
};
|
|
228
277
|
measure();
|
|
229
278
|
// A new scroll area starts at the top, whatever the last one was scrolled to.
|
|
@@ -298,14 +347,48 @@ export function Table<Row>({
|
|
|
298
347
|
{...(scrollStop ? { tabIndex: 0, role: "region", "aria-label": caption ?? "Table" } : {})}
|
|
299
348
|
>
|
|
300
349
|
<table
|
|
301
|
-
className={cn("w-full text-left", byDensity(density, "text-sm", "text-[11px]"))}
|
|
350
|
+
className={cn("w-full text-left", pinFirstColumn && "min-w-max", byDensity(density, "text-sm", "text-[11px]"))}
|
|
302
351
|
style={windowed ? { tableLayout: "fixed" } : undefined}
|
|
303
|
-
aria-rowcount={windowed ? order.length +
|
|
352
|
+
aria-rowcount={windowed ? order.length + headerRows : undefined}
|
|
304
353
|
>
|
|
305
354
|
{caption && <caption className="sr-only">{caption}</caption>}
|
|
306
355
|
<thead ref={headRef}>
|
|
307
|
-
|
|
308
|
-
{
|
|
356
|
+
{groups && (
|
|
357
|
+
<tr ref={groupRowRef} aria-rowindex={windowed ? 1 : undefined}>
|
|
358
|
+
{groups.map((cell) =>
|
|
359
|
+
cell.label === null ? (
|
|
360
|
+
// An empty `<td>`: an empty `<th>` is a header that names nothing.
|
|
361
|
+
<td
|
|
362
|
+
key={`gap-${cell.start}`}
|
|
363
|
+
colSpan={cell.span}
|
|
364
|
+
className={cn(windowed && "bg-surface", pinFirstColumn && cell.start === 0 && cell.span === 1 && pinned)}
|
|
365
|
+
style={windowed ? { position: "sticky", top: 0, zIndex: pinFirstColumn && cell.start === 0 ? 3 : 1 } : undefined}
|
|
366
|
+
/>
|
|
367
|
+
) : (
|
|
368
|
+
<th
|
|
369
|
+
key={`group-${cell.start}`}
|
|
370
|
+
scope="colgroup"
|
|
371
|
+
colSpan={cell.span}
|
|
372
|
+
className={cn(
|
|
373
|
+
"font-medium text-fg-muted",
|
|
374
|
+
byDensity(density, "pb-1 text-xs", "pb-0.5 text-[10px]"),
|
|
375
|
+
cell.start + cell.span === columns.length ? "pr-0" : byDensity(density, "pr-3", "pr-2"),
|
|
376
|
+
windowed && "bg-surface",
|
|
377
|
+
pinFirstColumn && cell.start === 0 && cell.span === 1 && pinned,
|
|
378
|
+
)}
|
|
379
|
+
style={windowed ? { position: "sticky", top: 0, zIndex: pinFirstColumn && cell.start === 0 ? 3 : 1 } : undefined}
|
|
380
|
+
>
|
|
381
|
+
{/* The rule under the label is the group's extent: it spans its columns and stops short of the next group. */}
|
|
382
|
+
<span className={cn("block border-b border-line pb-1 text-left", byDensity(density, "pb-1", "pb-0.5"))}>
|
|
383
|
+
{cell.label}
|
|
384
|
+
</span>
|
|
385
|
+
</th>
|
|
386
|
+
),
|
|
387
|
+
)}
|
|
388
|
+
</tr>
|
|
389
|
+
)}
|
|
390
|
+
<tr className="border-b border-line" aria-rowindex={windowed ? headerRows : undefined}>
|
|
391
|
+
{columns.map((column, columnIndex) => {
|
|
309
392
|
const sortable = isSortable(column);
|
|
310
393
|
const direction = sortable && sort?.key === column.key ? sort.direction : undefined;
|
|
311
394
|
const SortIcon = SORT_ICONS[direction ?? "none"];
|
|
@@ -315,7 +398,12 @@ export function Table<Row>({
|
|
|
315
398
|
scope="col"
|
|
316
399
|
style={
|
|
317
400
|
windowed
|
|
318
|
-
? {
|
|
401
|
+
? {
|
|
402
|
+
position: "sticky",
|
|
403
|
+
top: groupRowHeight,
|
|
404
|
+
zIndex: pinFirstColumn && columnIndex === 0 ? 3 : 1,
|
|
405
|
+
...(column.width ? { width: column.width } : {}),
|
|
406
|
+
}
|
|
319
407
|
: column.width
|
|
320
408
|
? { width: column.width }
|
|
321
409
|
: undefined
|
|
@@ -332,6 +420,9 @@ export function Table<Row>({
|
|
|
332
420
|
column.numeric && "text-right",
|
|
333
421
|
// A sticky cell leaves its row's border behind: it draws its own rule.
|
|
334
422
|
windowed && "bg-surface shadow-[inset_0_-1px_0_var(--line)]",
|
|
423
|
+
pinFirstColumn && columnIndex === 0 && pinned,
|
|
424
|
+
// A pinned header cell of a table that does not scroll vertically is only sticky to the left.
|
|
425
|
+
pinFirstColumn && columnIndex === 0 && !windowed && "bg-surface",
|
|
335
426
|
)}
|
|
336
427
|
>
|
|
337
428
|
{sortable ? (
|
|
@@ -376,10 +467,11 @@ export function Table<Row>({
|
|
|
376
467
|
<tr
|
|
377
468
|
key={rowKey(row, index)}
|
|
378
469
|
data-position={position}
|
|
379
|
-
aria-rowindex={windowed ? position +
|
|
470
|
+
aria-rowindex={windowed ? position + 1 + headerRows : undefined}
|
|
380
471
|
style={windowed ? { height: rowHeight } : undefined}
|
|
381
472
|
data-state={active ? "active" : undefined}
|
|
382
473
|
className={cn(
|
|
474
|
+
"group/row",
|
|
383
475
|
// A windowed row's rule is a shadow on its cells: a collapsed border would add
|
|
384
476
|
// half a pixel to every row, and the arithmetic counts whole rows.
|
|
385
477
|
!windowed && "border-b border-line/60 last:border-0",
|
|
@@ -409,11 +501,12 @@ export function Table<Row>({
|
|
|
409
501
|
onPointerEnter={onRowHover ? () => onRowHover(row, index) : undefined}
|
|
410
502
|
onPointerLeave={onRowHover ? () => onRowHover(null, null) : undefined}
|
|
411
503
|
>
|
|
412
|
-
{columns.map((column) => (
|
|
504
|
+
{columns.map((column, columnIndex) => (
|
|
413
505
|
<td
|
|
414
506
|
key={column.key}
|
|
415
507
|
className={cn(
|
|
416
508
|
"align-middle text-fg last:pr-0",
|
|
509
|
+
pinFirstColumn && columnIndex === 0 && pinnedCell(Boolean(onRowClick), active),
|
|
417
510
|
byDensity(density, "py-2 pr-3", "py-0.5 pr-2"),
|
|
418
511
|
column.numeric && "text-right font-mono tabular-nums",
|
|
419
512
|
windowed && "shadow-[inset_0_-1px_0_color-mix(in_oklab,var(--line)_60%,transparent)]",
|
|
@@ -17,6 +17,7 @@ export {
|
|
|
17
17
|
type BreakdownTone,
|
|
18
18
|
} from "./Breakdown";
|
|
19
19
|
export { Button, ButtonLink, buttonClasses, type ButtonSize, type ButtonVariant } from "./Button";
|
|
20
|
+
export { DeltaCell, deltaStrength, type DeltaCellProps, type DeltaStrength } from "./DeltaCell";
|
|
20
21
|
export { DensityProvider, byDensity, useDensity, type Density } from "./Density";
|
|
21
22
|
export { ConfirmDialog, Dialog, DialogClose } from "./Dialog";
|
|
22
23
|
export { Disclosure } from "./Disclosure";
|
|
@@ -44,6 +45,7 @@ export { Popover, PopoverClose, type PopoverProps } from "./Popover";
|
|
|
44
45
|
export { PoseInput, type PoseInputProps, type PoseValue, type Quaternion, type RotationView, type Vec3 } from "./PoseInput";
|
|
45
46
|
export { SegmentedControl } from "./SegmentedControl";
|
|
46
47
|
export { Select, type SelectOption } from "./Select";
|
|
48
|
+
export { ShortcutList, type Shortcut, type ShortcutGroup, type ShortcutListProps } from "./ShortcutList";
|
|
47
49
|
export { Slider } from "./Slider";
|
|
48
50
|
export { Table, type Column, type TableWindowing } from "./Table";
|
|
49
51
|
export { sortRows, type SortDirection, type TableSort } from "./tableSort";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Column groups: a header row above the column headers whose cells each span the adjacent
|
|
3
|
+
* columns that share a `group` label. Pure, so the spans are tested without a DOM.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** One cell of the group header row. */
|
|
7
|
+
export interface ColumnGroupSpan {
|
|
8
|
+
/** The label the spanned columns share, or `null` for columns with no group (an empty cell). */
|
|
9
|
+
label: string | null;
|
|
10
|
+
/** How many columns it spans. */
|
|
11
|
+
span: number;
|
|
12
|
+
/** The position of its first column. */
|
|
13
|
+
start: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The cells of the group header row.
|
|
18
|
+
*
|
|
19
|
+
* Adjacent columns with the same `group` label are one cell; a column with no group, and
|
|
20
|
+
* adjacent such columns, are one empty cell. The same label twice with another between
|
|
21
|
+
* them is two groups.
|
|
22
|
+
*
|
|
23
|
+
* @param columns - The columns, in order.
|
|
24
|
+
* @returns The cells, left to right, spanning every column; `null` when no column has a group, so no row is drawn.
|
|
25
|
+
*/
|
|
26
|
+
export function columnGroups(columns: readonly { group?: string | undefined }[]): ColumnGroupSpan[] | null {
|
|
27
|
+
if (!columns.some((column) => column.group)) return null;
|
|
28
|
+
const cells: ColumnGroupSpan[] = [];
|
|
29
|
+
columns.forEach((column, start) => {
|
|
30
|
+
const label = column.group || null;
|
|
31
|
+
const last = cells.at(-1);
|
|
32
|
+
if (last && last.label === label) last.span += 1;
|
|
33
|
+
else cells.push({ label, span: 1, start });
|
|
34
|
+
});
|
|
35
|
+
return cells;
|
|
36
|
+
}
|