@terpjs/react-core 0.16.0 → 0.17.0
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 +4 -3
- package/package.json +2 -2
- package/src/Disclosure.test.tsx +106 -0
- package/src/Disclosure.tsx +104 -0
- package/src/bootstrap.test.tsx +49 -0
- package/src/bootstrap.tsx +23 -0
- package/src/clipboard.test.ts +158 -0
- package/src/clipboard.ts +165 -0
- package/src/dataview/DataView.test.tsx +49 -0
- package/src/dataview/DataView.tsx +16 -0
- package/src/dataview/DataViewCardList.tsx +4 -2
- package/src/dataview/DataViewTable.tsx +19 -6
- package/src/index.ts +7 -2
- package/src/layout.test.tsx +80 -1
- package/src/layout.tsx +143 -6
- package/src/layoutContract.test.tsx +1 -0
- package/src/layoutContract.ts +1 -0
- package/src/markers.test.ts +4 -0
- package/src/styles.test.ts +193 -1
- package/src/styles.ts +254 -11
- package/src/theme.test.tsx +30 -0
- package/src/theme.tsx +17 -0
- package/src/tokens.guard.test.ts +1 -1
- package/src/useResource.test.tsx +56 -0
- package/src/useResource.ts +49 -4
package/src/clipboard.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Putting text on the clipboard, once, for everyone.
|
|
3
|
+
*
|
|
4
|
+
* `navigator.clipboard` is not the API it looks like. `lib.dom` types it as always
|
|
5
|
+
* present, and it is absent outside a **secure context** — so on a plain-http origin
|
|
6
|
+
* that is not localhost, `navigator.clipboard.writeText(...)` is a property access on
|
|
7
|
+
* `undefined`. That throws a **synchronous** `TypeError`, before any promise exists, so a
|
|
8
|
+
* `.catch` on the call does not run and neither does a `try` written around an `await`
|
|
9
|
+
* that was never reached. TypeScript reports nothing, because as far as its type says the
|
|
10
|
+
* property is there.
|
|
11
|
+
*
|
|
12
|
+
* The observed outcome is a button that does nothing and says nothing, found only by
|
|
13
|
+
* clicking it in a browser served over http — which is to say, found by a person, in a
|
|
14
|
+
* deployment, rather than by any check. Every app reaching for the API directly
|
|
15
|
+
* rediscovers that, and the icon set has shipped a `clipboard` glyph the whole time.
|
|
16
|
+
*
|
|
17
|
+
* Same shape as the download seam (ADR 0096 §3): a browser API with a footgun, wrapped
|
|
18
|
+
* once. Two exports, and they answer different questions -- {@link copyText} is the
|
|
19
|
+
* mechanism and can be called from anywhere, {@link useCopyToClipboard} adds the "Copied"
|
|
20
|
+
* acknowledgement a control needs and is the one a screen usually wants.
|
|
21
|
+
*
|
|
22
|
+
* **A refusal is reported, never swallowed.** `copyText` resolves to `false` when the
|
|
23
|
+
* text did not reach the clipboard, and the whole reason this module exists is that the
|
|
24
|
+
* failure was silent. A caller that ignores the answer has chosen to; a caller that had
|
|
25
|
+
* no answer to ignore had not.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
29
|
+
|
|
30
|
+
/** How long {@link useCopyToClipboard} keeps saying it worked, in milliseconds. */
|
|
31
|
+
export const COPIED_FEEDBACK_MS = 2000;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether the async Clipboard API is actually usable right now.
|
|
35
|
+
*
|
|
36
|
+
* Three separate ways it is not, and the type says none of them: no `navigator` at all
|
|
37
|
+
* (server-side render, a test environment without a DOM), `navigator.clipboard`
|
|
38
|
+
* undefined (an insecure context), and the object present without the method (older
|
|
39
|
+
* WebViews expose a partial shape). Checked as a property lookup rather than a
|
|
40
|
+
* `typeof navigator.clipboard` guard, because the second is what TypeScript already
|
|
41
|
+
* believes and therefore narrows away.
|
|
42
|
+
*/
|
|
43
|
+
function clipboardApi(): { writeText: (text: string) => Promise<void> } | null {
|
|
44
|
+
const candidate = (globalThis as { navigator?: { clipboard?: unknown } }).navigator
|
|
45
|
+
?.clipboard as { writeText?: unknown } | undefined;
|
|
46
|
+
return typeof candidate?.writeText === "function"
|
|
47
|
+
? (candidate as { writeText: (text: string) => Promise<void> })
|
|
48
|
+
: null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The pre-Clipboard-API path, for the insecure contexts where the API is absent.
|
|
53
|
+
*
|
|
54
|
+
* `document.execCommand("copy")` is deprecated and is deliberately still here: it is the
|
|
55
|
+
* only thing that works on an http origin, which is exactly the case that produced the
|
|
56
|
+
* silent failure. The textarea is positioned off-screen rather than hidden, because a
|
|
57
|
+
* `display: none` element cannot be selected and the copy then silently does nothing —
|
|
58
|
+
* the same class of quiet failure one layer down.
|
|
59
|
+
*
|
|
60
|
+
* `readOnly` keeps a mobile keyboard from appearing for the instant the element is
|
|
61
|
+
* focused, and the `finally` removes the node even when the command throws.
|
|
62
|
+
*/
|
|
63
|
+
function copyByExecCommand(text: string): boolean {
|
|
64
|
+
if (typeof document === "undefined") return false;
|
|
65
|
+
const area = document.createElement("textarea");
|
|
66
|
+
area.value = text;
|
|
67
|
+
area.setAttribute("readonly", "");
|
|
68
|
+
area.setAttribute("aria-hidden", "true");
|
|
69
|
+
area.style.position = "fixed";
|
|
70
|
+
area.style.top = "-1000px";
|
|
71
|
+
area.style.opacity = "0";
|
|
72
|
+
document.body.appendChild(area);
|
|
73
|
+
try {
|
|
74
|
+
area.select();
|
|
75
|
+
return document.execCommand("copy");
|
|
76
|
+
} catch {
|
|
77
|
+
return false;
|
|
78
|
+
} finally {
|
|
79
|
+
area.remove();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Put *text* on the clipboard. Resolves to whether it got there.
|
|
85
|
+
*
|
|
86
|
+
* Never rejects and never throws, including on the synchronous `TypeError` this module
|
|
87
|
+
* exists for: a copy affordance is not worth an unhandled rejection, and a caller that
|
|
88
|
+
* wants to react to failure has the boolean. Empty text is a successful no-op rather than
|
|
89
|
+
* a failure -- clearing a field and copying it is not an error, and reporting one would
|
|
90
|
+
* put a toast in front of somebody who did nothing wrong.
|
|
91
|
+
*/
|
|
92
|
+
export async function copyText(text: string): Promise<boolean> {
|
|
93
|
+
if (text === "") return true;
|
|
94
|
+
const api = clipboardApi();
|
|
95
|
+
if (api !== null) {
|
|
96
|
+
try {
|
|
97
|
+
await api.writeText(text);
|
|
98
|
+
return true;
|
|
99
|
+
} catch {
|
|
100
|
+
// Present and refused: a permissions policy, a document without focus, or a
|
|
101
|
+
// browser that asks and was declined. The fallback below sometimes still works,
|
|
102
|
+
// and trying it costs one detached element.
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return copyByExecCommand(text);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** What {@link useCopyToClipboard} hands back. */
|
|
109
|
+
export interface CopyToClipboard {
|
|
110
|
+
/** Copy *text*; resolves to whether it worked, and drives {@link copied}. */
|
|
111
|
+
copy: (text: string) => Promise<boolean>;
|
|
112
|
+
/** True for {@link COPIED_FEEDBACK_MS} after a copy that worked. */
|
|
113
|
+
copied: boolean;
|
|
114
|
+
/** True after a copy that did not, until the next attempt. Never both. */
|
|
115
|
+
failed: boolean;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* {@link copyText} plus the acknowledgement a control needs.
|
|
120
|
+
*
|
|
121
|
+
* A copy has no visible result — the clipboard is somewhere else — so a button that does
|
|
122
|
+
* not say "Copied" is indistinguishable from a button that is broken. That is the same
|
|
123
|
+
* confusion the silent failure caused, and it is why the flag is part of the seam instead
|
|
124
|
+
* of being left to each caller's own `setTimeout`.
|
|
125
|
+
*
|
|
126
|
+
* `failed` is separate rather than `copied === false`, because "not copied" is the
|
|
127
|
+
* resting state and "the copy was refused" is an event. Collapsing them would make a
|
|
128
|
+
* button that has never been pressed indistinguishable from one that just failed.
|
|
129
|
+
*
|
|
130
|
+
* Deliberately not a component. A copy affordance is a `Button` with an icon, a label the
|
|
131
|
+
* app writes, and a placement only the app knows — everything except the mechanism, and
|
|
132
|
+
* the mechanism is what was missing.
|
|
133
|
+
*/
|
|
134
|
+
export function useCopyToClipboard(): CopyToClipboard {
|
|
135
|
+
const [copied, setCopied] = useState(false);
|
|
136
|
+
const [failed, setFailed] = useState(false);
|
|
137
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
138
|
+
const live = useRef(true);
|
|
139
|
+
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
live.current = true;
|
|
142
|
+
return () => {
|
|
143
|
+
live.current = false;
|
|
144
|
+
if (timer.current !== null) clearTimeout(timer.current);
|
|
145
|
+
};
|
|
146
|
+
}, []);
|
|
147
|
+
|
|
148
|
+
const copy = useCallback(async (text: string) => {
|
|
149
|
+
const ok = await copyText(text);
|
|
150
|
+
// A screen can navigate away while the write is in flight; setting state on an
|
|
151
|
+
// unmounted hook is a warning in development and a leak of the timer below.
|
|
152
|
+
if (!live.current) return ok;
|
|
153
|
+
setCopied(ok);
|
|
154
|
+
setFailed(!ok);
|
|
155
|
+
if (timer.current !== null) clearTimeout(timer.current);
|
|
156
|
+
if (ok) {
|
|
157
|
+
timer.current = setTimeout(() => {
|
|
158
|
+
if (live.current) setCopied(false);
|
|
159
|
+
}, COPIED_FEEDBACK_MS);
|
|
160
|
+
}
|
|
161
|
+
return ok;
|
|
162
|
+
}, []);
|
|
163
|
+
|
|
164
|
+
return { copy, copied, failed };
|
|
165
|
+
}
|
|
@@ -350,6 +350,55 @@ describe("DataView expandable rows", () => {
|
|
|
350
350
|
fireEvent.click(screen.getByRole("button", { name: "Collapse row" }));
|
|
351
351
|
expect(screen.queryByText("Detail: Broken printer")).not.toBeInTheDocument();
|
|
352
352
|
});
|
|
353
|
+
|
|
354
|
+
it("draws a chevron only on the rows that have something behind it", async () => {
|
|
355
|
+
// `renderExpanded` is one prop for the whole view, so declaring it put a toggle on
|
|
356
|
+
// every row -- including the ones where the only honest thing left to show is a
|
|
357
|
+
// sentence saying there is nothing. Row actions, in the same interface, have taken a
|
|
358
|
+
// row all along.
|
|
359
|
+
render(
|
|
360
|
+
<DataView
|
|
361
|
+
repository={inMemoryRepo()}
|
|
362
|
+
columns={COLUMNS}
|
|
363
|
+
renderExpanded={(t) => <div>Detail: {t.title}</div>}
|
|
364
|
+
isRowExpandable={(t) => t.title === "Broken printer"}
|
|
365
|
+
/>,
|
|
366
|
+
);
|
|
367
|
+
await screen.findByText("Broken printer");
|
|
368
|
+
|
|
369
|
+
expect(screen.getAllByRole("button", { name: "Expand row" })).toHaveLength(1);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("keeps every row expandable when no predicate is given", async () => {
|
|
373
|
+
// The counter-case, so the assertion above cannot pass because the chevrons went away
|
|
374
|
+
// altogether: the default is unchanged and every row still has one.
|
|
375
|
+
render(
|
|
376
|
+
<DataView
|
|
377
|
+
repository={inMemoryRepo()}
|
|
378
|
+
columns={COLUMNS}
|
|
379
|
+
renderExpanded={(t) => <div>Detail: {t.title}</div>}
|
|
380
|
+
/>,
|
|
381
|
+
);
|
|
382
|
+
await screen.findByText("Broken printer");
|
|
383
|
+
|
|
384
|
+
expect(screen.getAllByRole("button", { name: "Expand row" }).length).toBeGreaterThan(1);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("drops the expand column when the predicate refuses every row", async () => {
|
|
388
|
+
// A column of empty cells is worse than no column: it takes width from the data and
|
|
389
|
+
// says nothing. One expandable row is enough to keep it, none is enough to lose it.
|
|
390
|
+
render(
|
|
391
|
+
<DataView
|
|
392
|
+
repository={inMemoryRepo()}
|
|
393
|
+
columns={COLUMNS}
|
|
394
|
+
renderExpanded={(t) => <div>Detail: {t.title}</div>}
|
|
395
|
+
isRowExpandable={() => false}
|
|
396
|
+
/>,
|
|
397
|
+
);
|
|
398
|
+
await screen.findByText("Broken printer");
|
|
399
|
+
|
|
400
|
+
expect(screen.queryAllByRole("button", { name: "Expand row" })).toHaveLength(0);
|
|
401
|
+
});
|
|
353
402
|
});
|
|
354
403
|
|
|
355
404
|
describe("DataView column resizing", () => {
|
|
@@ -73,6 +73,20 @@ interface DataViewBaseProps<T> {
|
|
|
73
73
|
pageSizeOptions?: number[];
|
|
74
74
|
initialPageSize?: number;
|
|
75
75
|
renderExpanded?: (row: T) => ReactNode;
|
|
76
|
+
/**
|
|
77
|
+
* Which rows actually have something behind the chevron; default: all of them.
|
|
78
|
+
*
|
|
79
|
+
* `renderExpanded` is one prop for the whole view, so declaring it drew a toggle on
|
|
80
|
+
* every row -- including the rows with nothing to show, where the only honest thing
|
|
81
|
+
* left to put behind it is a sentence saying so. Row actions, four lines up, have been
|
|
82
|
+
* `(row) => ...` all along; this closes an asymmetry that was in one interface rather
|
|
83
|
+
* than adding a new idea.
|
|
84
|
+
*
|
|
85
|
+
* A predicate rather than "call `renderExpanded` and see if it returns null": that
|
|
86
|
+
* would build a subtree, for every row, on every render, to answer a boolean -- and
|
|
87
|
+
* would run whatever the caller does in there as a side effect of drawing a chevron.
|
|
88
|
+
*/
|
|
89
|
+
isRowExpandable?: (row: T) => boolean;
|
|
76
90
|
/**
|
|
77
91
|
* Row-level status tone: the *row* is in that state (a refused link, a failed run),
|
|
78
92
|
* not one of its cells — the right altitude for a validation-driven table, where a
|
|
@@ -348,6 +362,7 @@ function DataViewInner<T>(props: DataViewProps<T>) {
|
|
|
348
362
|
isSelected={(id) => selectedIds.has(id)}
|
|
349
363
|
onToggleSelected={toggleSelected}
|
|
350
364
|
renderExpanded={props.renderExpanded}
|
|
365
|
+
isRowExpandable={props.isRowExpandable}
|
|
351
366
|
isExpanded={(id) => expandedIds.has(id)}
|
|
352
367
|
onToggleExpanded={toggleExpanded}
|
|
353
368
|
rowActions={props.rowActions}
|
|
@@ -375,6 +390,7 @@ function DataViewInner<T>(props: DataViewProps<T>) {
|
|
|
375
390
|
somePageSelected={somePageSelected}
|
|
376
391
|
onToggleSelectPage={toggleSelectPage}
|
|
377
392
|
renderExpanded={props.renderExpanded}
|
|
393
|
+
isRowExpandable={props.isRowExpandable}
|
|
378
394
|
isExpanded={(id) => expandedIds.has(id)}
|
|
379
395
|
onToggleExpanded={toggleExpanded}
|
|
380
396
|
rowActions={props.rowActions}
|
|
@@ -23,6 +23,7 @@ export interface DataViewCardListProps<T> {
|
|
|
23
23
|
onToggleSelected: (rowId: string) => void;
|
|
24
24
|
// Expansion
|
|
25
25
|
renderExpanded?: (row: T) => ReactNode;
|
|
26
|
+
isRowExpandable?: (row: T) => boolean;
|
|
26
27
|
isExpanded: (rowId: string) => boolean;
|
|
27
28
|
onToggleExpanded: (rowId: string) => void;
|
|
28
29
|
// Actions
|
|
@@ -58,7 +59,8 @@ export function DataViewCardList<T>(props: DataViewCardListProps<T>) {
|
|
|
58
59
|
<ul data-terp="dataview-card-list">
|
|
59
60
|
{props.rows.map((row) => {
|
|
60
61
|
const rowId = props.getRowId(row);
|
|
61
|
-
const
|
|
62
|
+
const expandable = props.isRowExpandable?.(row) ?? true;
|
|
63
|
+
const expanded = expandable && props.isExpanded(rowId);
|
|
62
64
|
const clickable = props.onRowClick !== undefined;
|
|
63
65
|
const tone = props.getRowTone?.(row) ?? null;
|
|
64
66
|
return (
|
|
@@ -83,7 +85,7 @@ export function DataViewCardList<T>(props: DataViewCardListProps<T>) {
|
|
|
83
85
|
/>
|
|
84
86
|
)}
|
|
85
87
|
<div data-terp="dataview-card-main">
|
|
86
|
-
{props.renderExpanded !== undefined && (
|
|
88
|
+
{props.renderExpanded !== undefined && expandable && (
|
|
87
89
|
<DataViewExpandToggle
|
|
88
90
|
expanded={expanded}
|
|
89
91
|
onToggle={() => props.onToggleExpanded(rowId)}
|
|
@@ -42,6 +42,7 @@ export interface DataViewTableProps<T> {
|
|
|
42
42
|
onToggleSelectPage: () => void;
|
|
43
43
|
// Expansion
|
|
44
44
|
renderExpanded?: (row: T) => ReactNode;
|
|
45
|
+
isRowExpandable?: (row: T) => boolean;
|
|
45
46
|
isExpanded: (rowId: string) => boolean;
|
|
46
47
|
onToggleExpanded: (rowId: string) => void;
|
|
47
48
|
// Actions
|
|
@@ -129,7 +130,14 @@ export function DataViewTable<T>(props: DataViewTableProps<T>) {
|
|
|
129
130
|
const stepOf = (column: DataViewColumn<T>): ColumnWidth | undefined =>
|
|
130
131
|
resizedWidthOf(column) === undefined ? column.meta?.width : undefined;
|
|
131
132
|
|
|
132
|
-
|
|
133
|
+
// Whether the view has an expand COLUMN at all: one row with something behind it is
|
|
134
|
+
// enough, because the column has to be there for the rows that do. Which rows get a
|
|
135
|
+
// toggle inside it is a separate question, asked per row below -- collapsing the two
|
|
136
|
+
// is what put a chevron on every row of a view where only some had anything.
|
|
137
|
+
const expandableRows = props.renderExpanded === undefined
|
|
138
|
+
? []
|
|
139
|
+
: props.rows.filter((row) => props.isRowExpandable?.(row) ?? true);
|
|
140
|
+
const hasExpand = expandableRows.length > 0;
|
|
133
141
|
const hasActions = props.rowActions !== undefined;
|
|
134
142
|
const columnCount =
|
|
135
143
|
props.columns.length + (hasExpand ? 1 : 0) + (props.selectionEnabled ? 1 : 0) + (hasActions ? 1 : 0);
|
|
@@ -217,7 +225,8 @@ export function DataViewTable<T>(props: DataViewTableProps<T>) {
|
|
|
217
225
|
<tbody>
|
|
218
226
|
{props.rows.map((row) => {
|
|
219
227
|
const rowId = props.getRowId(row);
|
|
220
|
-
const
|
|
228
|
+
const expandable = props.isRowExpandable?.(row) ?? true;
|
|
229
|
+
const expanded = expandable && props.isExpanded(rowId);
|
|
221
230
|
const clickable = props.onRowClick !== undefined;
|
|
222
231
|
const tone = props.getRowTone?.(row) ?? null;
|
|
223
232
|
return (
|
|
@@ -231,10 +240,14 @@ export function DataViewTable<T>(props: DataViewTableProps<T>) {
|
|
|
231
240
|
>
|
|
232
241
|
{hasExpand && (
|
|
233
242
|
<td data-terp="dataview-expand-cell">
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
243
|
+
{/* The cell is always rendered when the view has the column, so the
|
|
244
|
+
remaining cells stay aligned; only the control is conditional. */}
|
|
245
|
+
{expandable && (
|
|
246
|
+
<DataViewExpandToggle
|
|
247
|
+
expanded={expanded}
|
|
248
|
+
onToggle={() => props.onToggleExpanded(rowId)}
|
|
249
|
+
/>
|
|
250
|
+
)}
|
|
238
251
|
</td>
|
|
239
252
|
)}
|
|
240
253
|
{props.selectionEnabled && (
|
package/src/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type { AuthClientOptions, TokenGetter } from "./createAuthClient";
|
|
|
11
11
|
export { Authorized, useCan, useHasPermission, usePermissions } from "./Authorized";
|
|
12
12
|
export type { AuthorizedProps } from "./Authorized";
|
|
13
13
|
export { useResource } from "./useResource";
|
|
14
|
-
export type { Resource, ResourceSource } from "./useResource";
|
|
14
|
+
export type { Resource, ResourcePage, ResourceSource } from "./useResource";
|
|
15
15
|
export { useRecord } from "./useRecord";
|
|
16
16
|
export type { RecordResource, RecordSource } from "./useRecord";
|
|
17
17
|
export { useRealtimeChannel } from "./realtime";
|
|
@@ -111,6 +111,8 @@ export type { ErrorStateProps } from "./ErrorState";
|
|
|
111
111
|
export { ErrorMessagesProvider, useErrorMessage, DEFAULT_ERROR_MESSAGES } from "./errorMessages";
|
|
112
112
|
export type { ErrorMessages, ErrorMessagesProviderProps } from "./errorMessages";
|
|
113
113
|
export { ConfirmDialog } from "./ConfirmDialog";
|
|
114
|
+
export { Disclosure } from "./Disclosure";
|
|
115
|
+
export type { DisclosureProps } from "./Disclosure";
|
|
114
116
|
export type { ConfirmDialogProps } from "./ConfirmDialog";
|
|
115
117
|
export { LoadingState, InlineSpinner } from "./LoadingState";
|
|
116
118
|
export type { LoadingStateProps, InlineSpinnerProps } from "./LoadingState";
|
|
@@ -153,6 +155,8 @@ export type { AlertProps, AlertTone } from "./ui/Alert";
|
|
|
153
155
|
export { Markdown } from "./ui/Markdown";
|
|
154
156
|
export type { MarkdownProps } from "./ui/Markdown";
|
|
155
157
|
export { saveBlob, useEndpointDownload, fetchDownload, downloadUrl } from "./download";
|
|
158
|
+
export { copyText, useCopyToClipboard, COPIED_FEEDBACK_MS } from "./clipboard";
|
|
159
|
+
export type { CopyToClipboard } from "./clipboard";
|
|
156
160
|
export type { DownloadTarget } from "./download";
|
|
157
161
|
export { Heading, Text, Code, Link } from "./typography";
|
|
158
162
|
export type {
|
|
@@ -167,7 +171,7 @@ export type {
|
|
|
167
171
|
} from "./typography";
|
|
168
172
|
export { Field } from "./Field";
|
|
169
173
|
export type { FieldProps } from "./Field";
|
|
170
|
-
export { Stack, Grid, Divider, DetailList } from "./layout";
|
|
174
|
+
export { Stack, Grid, Divider, DetailList, DetailListGroup } from "./layout";
|
|
171
175
|
export type {
|
|
172
176
|
StackProps,
|
|
173
177
|
GridProps,
|
|
@@ -176,6 +180,7 @@ export type {
|
|
|
176
180
|
DividerProps,
|
|
177
181
|
Responsive,
|
|
178
182
|
DetailListProps,
|
|
183
|
+
DetailListGroupProps,
|
|
179
184
|
DetailListLayout,
|
|
180
185
|
DetailItem,
|
|
181
186
|
SpaceToken,
|
package/src/layout.test.tsx
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
3
|
import { afterEach, describe, expect, it } from "vitest";
|
|
4
4
|
|
|
5
|
-
import { DetailList, Divider, Grid, Stack } from "./layout";
|
|
5
|
+
import { DetailList, DetailListGroup, Divider, Grid, Stack } from "./layout";
|
|
6
6
|
|
|
7
7
|
afterEach(cleanup);
|
|
8
8
|
|
|
@@ -317,4 +317,83 @@ describe("DetailList", () => {
|
|
|
317
317
|
expect(screen.getByText("Ada").tagName).toBe("DD");
|
|
318
318
|
expect(screen.getByText(/Purchased/)).toBeInTheDocument();
|
|
319
319
|
});
|
|
320
|
+
|
|
321
|
+
it("marks the full row, and only the row that asked", () => {
|
|
322
|
+
// The escape from the shared column for one pair, and the attribute is the whole of the
|
|
323
|
+
// component's half: the span and the un-contents-ing are rules. Absent rather than "false"
|
|
324
|
+
// for the default, so the sheet needs no negative selector — the same idiom the density and
|
|
325
|
+
// collapsed attributes use, and the reason the marker scanner can read the rules at all.
|
|
326
|
+
render(
|
|
327
|
+
<DetailList
|
|
328
|
+
data-testid="dl"
|
|
329
|
+
layout="aligned"
|
|
330
|
+
items={[
|
|
331
|
+
{ label: "Owner", value: "Ada" },
|
|
332
|
+
{ label: "Report", value: "A paragraph too wide for a column." , full: true },
|
|
333
|
+
{ label: "Revision", value: "14" },
|
|
334
|
+
]}
|
|
335
|
+
/>,
|
|
336
|
+
);
|
|
337
|
+
const rows = [...screen.getByTestId("dl").querySelectorAll('[data-terp="detail-list-row"]')];
|
|
338
|
+
expect(rows.map((row) => row.getAttribute("data-full"))).toEqual([null, "true", null]);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("names auto as a column count, because it is one", () => {
|
|
342
|
+
// "auto" reaches the sheet through the same attribute as 1 and 2 and is a THIRD track list
|
|
343
|
+
// rather than a variant of the closed ones: it follows the container, so it needs no
|
|
344
|
+
// viewport cutover, which is the asymmetry it was added to close.
|
|
345
|
+
render(
|
|
346
|
+
<DetailList
|
|
347
|
+
data-testid="dl"
|
|
348
|
+
layout="aligned"
|
|
349
|
+
columns="auto"
|
|
350
|
+
items={[{ label: "Owner", value: "Ada" }]}
|
|
351
|
+
/>,
|
|
352
|
+
);
|
|
353
|
+
expect(screen.getByTestId("dl")).toHaveAttribute("data-columns", "auto");
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
describe("DetailListGroup", () => {
|
|
358
|
+
it("wraps its lists in one marked container with no inline styling", () => {
|
|
359
|
+
// The container is the mechanism: it owns the track list the lists inside subgrid into, so
|
|
360
|
+
// every label in every list is measured against one track. Nothing here can prove the
|
|
361
|
+
// alignment — that is a resolved layout fact and lives in the workbench's computed lane —
|
|
362
|
+
// but an unmarked or restyled wrapper would make the rule unreachable, and this is that.
|
|
363
|
+
render(
|
|
364
|
+
<DetailListGroup data-testid="group">
|
|
365
|
+
<DetailList layout="aligned" items={[{ label: "Owner", value: "Ada" }]} />
|
|
366
|
+
<DetailList layout="aligned" items={[{ label: "A much longer label", value: "14" }]} />
|
|
367
|
+
</DetailListGroup>,
|
|
368
|
+
);
|
|
369
|
+
const el = screen.getByTestId("group");
|
|
370
|
+
expect(el.tagName).toBe("DIV");
|
|
371
|
+
expect(el.querySelectorAll('[data-terp="detail-list"]')).toHaveLength(2);
|
|
372
|
+
expect(el.hasAttribute("data-gap")).toBe(false);
|
|
373
|
+
expect(el.getAttribute("style")).toBeNull();
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("stamps a gap only when one was asked for, step 0 included", () => {
|
|
377
|
+
// DetailList's condition, for its reason: an unset gap must leave the base rule standing.
|
|
378
|
+
// Step 0 is a real step, so the test is a defined-check rather than a falsy-check — "no
|
|
379
|
+
// distance between these lists" is a thing a caller can mean.
|
|
380
|
+
const { rerender } = render(
|
|
381
|
+
<DetailListGroup data-testid="group">
|
|
382
|
+
<DetailList layout="aligned" items={[{ label: "Owner", value: "Ada" }]} />
|
|
383
|
+
</DetailListGroup>,
|
|
384
|
+
);
|
|
385
|
+
expect(screen.getByTestId("group").hasAttribute("data-gap")).toBe(false);
|
|
386
|
+
rerender(
|
|
387
|
+
<DetailListGroup data-testid="group" gap={0}>
|
|
388
|
+
<DetailList layout="aligned" items={[{ label: "Owner", value: "Ada" }]} />
|
|
389
|
+
</DetailListGroup>,
|
|
390
|
+
);
|
|
391
|
+
expect(screen.getByTestId("group")).toHaveAttribute("data-gap", "0");
|
|
392
|
+
rerender(
|
|
393
|
+
<DetailListGroup data-testid="group" gap={6}>
|
|
394
|
+
<DetailList layout="aligned" items={[{ label: "Owner", value: "Ada" }]} />
|
|
395
|
+
</DetailListGroup>,
|
|
396
|
+
);
|
|
397
|
+
expect(screen.getByTestId("group")).toHaveAttribute("data-gap", "6");
|
|
398
|
+
});
|
|
320
399
|
});
|
package/src/layout.tsx
CHANGED
|
@@ -221,6 +221,28 @@ export interface DetailItem {
|
|
|
221
221
|
label: UiText;
|
|
222
222
|
/** The item's value (rendered as `<dd>`). */
|
|
223
223
|
value: ReactNode;
|
|
224
|
+
/**
|
|
225
|
+
* Give this pair the whole row, across every track (default `false`).
|
|
226
|
+
*
|
|
227
|
+
* The escape from the shared column, for the one pair that cannot live in it: a paragraph, a
|
|
228
|
+
* payload, a code block, a value whose label is short and whose content is a screen wide. The
|
|
229
|
+
* label goes above the value and the pair spans the list, exactly as the same pair renders
|
|
230
|
+
* below the viewport cutover — so this is the narrow shape, asked for at one row rather than
|
|
231
|
+
* imposed on all of them.
|
|
232
|
+
*
|
|
233
|
+
* It exists because the alternative was a SECOND list. A card holding eight aligned pairs and
|
|
234
|
+
* one wide value had to close the `<dl>`, render the wide thing, and open another — which
|
|
235
|
+
* gives the two lists two independently measured label columns, so the values in each land on
|
|
236
|
+
* a different vertical line. One `full` row keeps one list, one measure, one column.
|
|
237
|
+
*
|
|
238
|
+
* It spans TRACKS, so it does nothing where there are none to span: `layout="inline"` at
|
|
239
|
+
* `columns={1}`, and the closed counts below the viewport cutover, are already one full-width
|
|
240
|
+
* column. That is not a silent no-op to work around — it is the same pair rendering the same
|
|
241
|
+
* way for a different reason. `columns="auto"` is the exception to the second half and needs
|
|
242
|
+
* no exception in the code: it keeps its label column at every width, so a `full` row spans
|
|
243
|
+
* that column on a phone exactly as it spans four tracks on a desk.
|
|
244
|
+
*/
|
|
245
|
+
full?: boolean;
|
|
224
246
|
}
|
|
225
247
|
|
|
226
248
|
/** How a pair is arranged. */
|
|
@@ -239,8 +261,27 @@ export interface DetailListProps extends Omit<HTMLAttributes<HTMLDListElement>,
|
|
|
239
261
|
* contract's detail-page slot, so every governed detail screen already renders one.
|
|
240
262
|
*/
|
|
241
263
|
layout?: DetailListLayout;
|
|
242
|
-
/**
|
|
243
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Pairs per row: `1` (default), `2`, or `"auto"` to follow the available width.
|
|
266
|
+
*
|
|
267
|
+
* `"auto"` is the answer to the asymmetry this component's own notes below record: `Grid`
|
|
268
|
+
* publishes `columns="auto"` and a closed one-or-two had no such escape, so a detail list
|
|
269
|
+
* that wanted to be two columns on a desk and one on a phone got a hand-rolled reflow at the
|
|
270
|
+
* framework's single viewport cutover. `"auto"` needs no cutover at all — the track floor
|
|
271
|
+
* decides, so the list follows its CONTAINER rather than the window, which is the difference
|
|
272
|
+
* that matters for a list inside a card inside a split pane.
|
|
273
|
+
*
|
|
274
|
+
* The floors are published here because they are the whole behaviour: a pair is at least
|
|
275
|
+
* `9rem` of label and `13rem` of value, so 22rem, and each floor is additionally capped at a
|
|
276
|
+
* share of the track (30% and 60%) so that ONE pair always fits a narrow container instead of
|
|
277
|
+
* overflowing it. Measured across seven widths: three pairs at 1200px, two at 900px, one from
|
|
278
|
+
* 700px down, and no sideways scroll at 240px. `"auto"` on `inline` or `stacked` repeats one
|
|
279
|
+
* track per pair at `Grid`'s own `16rem` floor, since there is no label column to size.
|
|
280
|
+
*
|
|
281
|
+
* The closed counts keep the cutover reflow — `1` and `2` say a number, and a number that
|
|
282
|
+
* silently became three would not be one.
|
|
283
|
+
*/
|
|
284
|
+
columns?: 1 | 2 | "auto";
|
|
244
285
|
/**
|
|
245
286
|
* Distance **between pairs**, as a step on the token spacing scale.
|
|
246
287
|
*
|
|
@@ -291,12 +332,22 @@ export interface DetailListProps extends Omit<HTMLAttributes<HTMLDListElement>,
|
|
|
291
332
|
* distance within a pair equalled the distance between pairs, so nothing grouped. {@link
|
|
292
333
|
* DetailListProps.gap} makes it a caller's choice on the same scale as every other primitive.
|
|
293
334
|
* - **It reflows to one column below the framework's viewport cutover**, where `Grid`
|
|
294
|
-
* deliberately does not for a fixed `columns` count. The asymmetry
|
|
295
|
-
*
|
|
296
|
-
* no
|
|
335
|
+
* deliberately does not for a fixed `columns` count. The asymmetry was a decision and is now
|
|
336
|
+
* half retired: {@link DetailListProps.columns} takes `"auto"`, which follows the available
|
|
337
|
+
* width with no cutover at all, and the closed counts keep the reflow because a number that
|
|
338
|
+
* silently became three would not be a number. Above the cutover the shape is what it
|
|
297
339
|
* was, bar a label column now capped at `max-content` rather than floored at min-content;
|
|
298
340
|
* below it, four tracks in a phone's width met `overflow-wrap: anywhere` — correct for an
|
|
299
341
|
* unbreakable digest, wrong as a way to fit a label — and broke ordinary values mid-word.
|
|
342
|
+
*
|
|
343
|
+
* Two later additions are about the shared column rather than the pairs, and both exist because
|
|
344
|
+
* the alternative was splitting the list:
|
|
345
|
+
*
|
|
346
|
+
* - **{@link DetailItem.full} gives one pair the whole row.** A wide value — a paragraph, a
|
|
347
|
+
* payload — used to require closing the `<dl>` and opening another, which is two measured
|
|
348
|
+
* label columns and therefore two vertical lines for the values to land on.
|
|
349
|
+
* - **{@link DetailListGroup} shares one measure across several lists**, for the case where the
|
|
350
|
+
* lists must stay separate: their own headings, their own order, other content between them.
|
|
300
351
|
*/
|
|
301
352
|
export function DetailList({
|
|
302
353
|
items,
|
|
@@ -319,7 +370,13 @@ export function DetailList({
|
|
|
319
370
|
data-gap={gap === undefined ? undefined : String(gap)}
|
|
320
371
|
>
|
|
321
372
|
{items.map((item, index) => (
|
|
322
|
-
<div
|
|
373
|
+
<div
|
|
374
|
+
key={index}
|
|
375
|
+
data-terp="detail-list-row"
|
|
376
|
+
// Absent rather than "false", the sheet's own idiom: the default stamps nothing, so
|
|
377
|
+
// the rule needs no negative selector and the DOM says only what is true of it.
|
|
378
|
+
data-full={item.full === true ? "true" : undefined}
|
|
379
|
+
>
|
|
323
380
|
<dt data-terp="detail-list-term">{text(item.label)}</dt>
|
|
324
381
|
<dd data-terp="detail-list-value">{item.value}</dd>
|
|
325
382
|
</div>
|
|
@@ -327,3 +384,83 @@ export function DetailList({
|
|
|
327
384
|
</dl>
|
|
328
385
|
);
|
|
329
386
|
}
|
|
387
|
+
|
|
388
|
+
export interface DetailListGroupProps extends Omit<HTMLAttributes<HTMLDivElement>, "style"> {
|
|
389
|
+
/**
|
|
390
|
+
* Distance between the lists, as a step on the token spacing scale (default `--space-4`).
|
|
391
|
+
*
|
|
392
|
+
* The ROW gap only, like {@link DetailListProps.gap} and for the same reason: the column gap
|
|
393
|
+
* is the shared label-to-value distance, and a caller who could set it here would be moving
|
|
394
|
+
* every nested list's internal measure from the outside.
|
|
395
|
+
*/
|
|
396
|
+
gap?: SpaceToken;
|
|
397
|
+
/** The lists, and anything that belongs between them. */
|
|
398
|
+
children: ReactNode;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Several {@link DetailList}s that share ONE measured label column.
|
|
403
|
+
*
|
|
404
|
+
* The friction it removes: a card that shows a record in sections — "Identity", "Schedule",
|
|
405
|
+
* "What it reported" — renders a list per section, because the sections have their own headings
|
|
406
|
+
* and their own order and one `<dl>` cannot carry that. Each list then measures its own label
|
|
407
|
+
* column, so the values in each land on a different vertical line: four lists, four gutters,
|
|
408
|
+
* on one card. Nothing is misaligned by any single list's own rules, which is why it reads as
|
|
409
|
+
* sloppy rather than broken and why no test could have caught it.
|
|
410
|
+
*
|
|
411
|
+
* The mechanism is `subgrid`, and this wrapper is what makes it possible: the GROUP owns the
|
|
412
|
+
* track list, each aligned list inside becomes `grid-template-columns: subgrid`, and every
|
|
413
|
+
* label in every list is then measured against the same track. Measured: three lists whose
|
|
414
|
+
* labels differ in width put all three values at the same pixel.
|
|
415
|
+
*
|
|
416
|
+
* **Explicit rather than inferred**, which is the design decision worth stating. This could
|
|
417
|
+
* have been something `Card` did to the lists it happens to contain, and then a card that
|
|
418
|
+
* deliberately wants two differently-sized label columns would have no way to say so — and a
|
|
419
|
+
* card would know about `<dl>` tracks, which is a layer it has no business in.
|
|
420
|
+
*
|
|
421
|
+
* Three limits, each of them the honest kind:
|
|
422
|
+
*
|
|
423
|
+
* - **It shares the measure for `layout="aligned"` lists at the default single column**, since
|
|
424
|
+
* that is the only shape that HAS a shared label column. A `columns={2}` list keeps its own
|
|
425
|
+
* four tracks rather than being quietly folded into two — its pairs would otherwise reflow to
|
|
426
|
+
* one per row, which is a layout change disguised as an alignment fix.
|
|
427
|
+
* - **Above the viewport cutover only.** Below it every list is one column with the label above
|
|
428
|
+
* its value, so there is no column to share; the group is then a plain stack of lists.
|
|
429
|
+
* - **It degrades to exactly today's output.** Without `subgrid` each list keeps its own tracks,
|
|
430
|
+
* which is what it renders now, so this ships with no feature query and no fallback branch.
|
|
431
|
+
* Baseline-wide since Chrome 117 / Safari 16 / Firefox 71.
|
|
432
|
+
*
|
|
433
|
+
* Children other than lists are welcome and span the whole group — a `Heading` between two
|
|
434
|
+
* lists, a `Divider` — which is the other half of why this is a wrapper and not a prop. So the
|
|
435
|
+
* shape is flat, and it has to be:
|
|
436
|
+
*
|
|
437
|
+
* ```tsx
|
|
438
|
+
* <DetailListGroup>
|
|
439
|
+
* <Heading level={3}>Identity</Heading>
|
|
440
|
+
* <DetailList layout="aligned" items={identity} />
|
|
441
|
+
* <Heading level={3}>Schedule</Heading>
|
|
442
|
+
* <DetailList layout="aligned" items={schedule} />
|
|
443
|
+
* </DetailListGroup>
|
|
444
|
+
* ```
|
|
445
|
+
*
|
|
446
|
+
* **The lists must be the group's OWN children.** Wrapping each heading-and-list section in a
|
|
447
|
+
* `Stack` is the obvious tidy-up and it silently gives up the shared measure: `subgrid` needs
|
|
448
|
+
* the element to be a grid item of the box that owns the tracks, and a list inside a `Stack` is
|
|
449
|
+
* an item of the `Stack`. The rule is written with a child combinator for exactly that reason —
|
|
450
|
+
* as a descendant selector it would reach the nested list, compute to `none` because there is no
|
|
451
|
+
* parent grid to subgrid, and take the list's OWN label column with it, which is worse than not
|
|
452
|
+
* sharing. Nesting costs you the group; it does not break the list.
|
|
453
|
+
*/
|
|
454
|
+
export function DetailListGroup({ gap, children, ...rest }: DetailListGroupProps) {
|
|
455
|
+
return (
|
|
456
|
+
<div
|
|
457
|
+
{...rest}
|
|
458
|
+
data-terp="detail-list-group"
|
|
459
|
+
// The gap roll-call's idiom, and DetailList's: an unset gap stamps nothing and leaves the
|
|
460
|
+
// base rule standing, so there is no default to compare against.
|
|
461
|
+
data-gap={gap === undefined ? undefined : String(gap)}
|
|
462
|
+
>
|
|
463
|
+
{children}
|
|
464
|
+
</div>
|
|
465
|
+
);
|
|
466
|
+
}
|