@terpjs/react-core 0.30.0 → 0.31.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 +35 -14
- package/package.json +5 -5
- package/src/DashboardPage.tsx +34 -0
- package/src/Field.test.tsx +26 -1
- package/src/Field.tsx +15 -5
- package/src/HubPage.tsx +7 -2
- package/src/Page.test.tsx +192 -0
- package/src/Page.tsx +141 -55
- package/src/PageActions.test.tsx +64 -3
- package/src/PageActions.tsx +40 -15
- package/src/PageSequence.tsx +114 -0
- package/src/Stat.test.tsx +341 -0
- package/src/Stat.tsx +314 -0
- package/src/Timeline.test.tsx +74 -0
- package/src/Timeline.tsx +71 -0
- package/src/admin/AdminHub.tsx +11 -3
- package/src/admin/AuditLogAdmin.tsx +3 -1
- package/src/admin/GroupCreate.tsx +3 -1
- package/src/admin/GroupDetail.tsx +72 -67
- package/src/admin/GroupsAdmin.tsx +3 -1
- package/src/admin/ModuleAccessPanel.tsx +13 -12
- package/src/admin/RecordHistory.tsx +83 -0
- package/src/admin/UserCreate.tsx +3 -1
- package/src/admin/UserDetail.tsx +8 -1
- package/src/admin/UsersAdmin.tsx +3 -0
- package/src/admin/admin.test.tsx +208 -5
- package/src/bootstrap.test.tsx +31 -7
- package/src/bootstrap.tsx +30 -4
- package/src/charts/BarChart.tsx +74 -0
- package/src/charts/ProportionBar.tsx +118 -0
- package/src/charts/StatusHistory.tsx +75 -0
- package/src/charts/TrendChart.tsx +232 -0
- package/src/charts/charts.test.tsx +409 -0
- package/src/charts/scale.ts +103 -0
- package/src/dataview/DataView.test.tsx +255 -0
- package/src/dataview/DataView.tsx +35 -0
- package/src/dataview/DataViewCardList.tsx +21 -13
- package/src/dataview/DataViewTable.tsx +5 -6
- package/src/dataview/README.md +18 -0
- package/src/dataview/index.ts +1 -0
- package/src/dataview/internal.tsx +112 -5
- package/src/dataview/types.ts +39 -10
- package/src/errorMessages.test.tsx +17 -0
- package/src/errorMessages.tsx +18 -0
- package/src/format.ts +15 -0
- package/src/index.ts +24 -1
- package/src/layout.manifest.json +2 -9
- package/src/layout.manifest.test.ts +4 -2
- package/src/layout.test.tsx +74 -6
- package/src/layout.tsx +85 -25
- package/src/layoutContract.test.tsx +239 -1
- package/src/layoutContract.ts +131 -5
- package/src/layoutDeclaration.test.ts +31 -10
- package/src/layoutDeclaration.ts +9 -4
- package/src/locale.tsx +20 -3
- package/src/markers.test.ts +70 -2
- package/src/router.test.tsx +15 -3
- package/src/router.tsx +11 -2
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +222 -190
- package/src/styles.ts +1268 -328
- package/src/theme.test.tsx +44 -18
- package/src/theme.themes.test.ts +22 -2
- package/src/theme.tsx +22 -14
- package/src/themes.ts +39 -4
- package/src/tokens.guard.test.ts +13 -15
- package/src/typography.tsx +5 -1
- package/src/ui/Alert.test.tsx +24 -1
- package/src/ui/Alert.tsx +10 -1
- package/src/ui/Card.tsx +5 -4
- package/src/ui/Tooltip.test.tsx +171 -2
- package/src/ui/Tooltip.tsx +144 -8
- package/src/ui/controlMessages.tsx +20 -7
- package/src/uiText.test.tsx +16 -0
- package/src/uiText.tsx +76 -7
package/src/Page.tsx
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
|
-
import { Fragment, useContext, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { Fragment, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
|
|
4
4
|
import { Breadcrumbs } from "./Breadcrumbs";
|
|
5
5
|
import type { BreadcrumbItem, RenderBreadcrumbLink } from "./Breadcrumbs";
|
|
6
6
|
import { ErrorState } from "./ErrorState";
|
|
7
7
|
import {
|
|
8
|
+
HeadlineContext,
|
|
8
9
|
LayoutSlotContext,
|
|
10
|
+
headlineViolationMessage,
|
|
9
11
|
useLayoutContract,
|
|
12
|
+
verifyPageSummary,
|
|
10
13
|
verifySlotChildren,
|
|
11
14
|
} from "./layoutContract";
|
|
15
|
+
import type { HeadlineRegistry } from "./layoutContract";
|
|
12
16
|
import { LoadingState } from "./LoadingState";
|
|
13
17
|
import { usePageMarker } from "./pageMarker";
|
|
18
|
+
import { PageSequenceBar } from "./PageSequence";
|
|
19
|
+
import type { PageSequence } from "./PageSequence";
|
|
14
20
|
import { resolveUiTextNode, useUiText } from "./uiText";
|
|
15
21
|
import type { UiText, UiTextNode } from "./uiText";
|
|
16
22
|
|
|
@@ -46,6 +52,20 @@ export interface PageProps {
|
|
|
46
52
|
* source language.
|
|
47
53
|
*/
|
|
48
54
|
description?: UiTextNode;
|
|
55
|
+
/**
|
|
56
|
+
* The page's own figures, in a band under the title band (ADR 0169 §4): a `StatGroup` of
|
|
57
|
+
* what the page is about, a headline `Stat`, a status `Badge`, a line of `Text`.
|
|
58
|
+
*
|
|
59
|
+
* Full-bleed inside a shell, on the summary fill (`--color-bg-summary`, the brand's soft tint
|
|
60
|
+
* unless the theme says otherwise), so the figures are the first thing on the page after its
|
|
61
|
+
* name. One per page because it is a slot. Under a layout contract it admits exactly those
|
|
62
|
+
* four components, on every page including the plain `Page`, and the page carries at most
|
|
63
|
+
* one headline figure anywhere in it.
|
|
64
|
+
*
|
|
65
|
+
* It is data, so it follows the body: it is not rendered while the page loads or shows its
|
|
66
|
+
* error, where the band and the trail stay.
|
|
67
|
+
*/
|
|
68
|
+
summary?: ReactNode;
|
|
49
69
|
/**
|
|
50
70
|
* Cap the whole frame — header included — at a readable measure (default `"full"`).
|
|
51
71
|
*
|
|
@@ -76,6 +96,16 @@ export interface PageProps {
|
|
|
76
96
|
error?: unknown;
|
|
77
97
|
/** Error slot; defaults to {@link ErrorState} rendering `error` through the error-code map. */
|
|
78
98
|
errorState?: ReactNode;
|
|
99
|
+
/**
|
|
100
|
+
* The ordered series this page is one item of — its previous and next neighbours and its
|
|
101
|
+
* place among them — rendered as a bar that stays at the bottom of the viewport.
|
|
102
|
+
*
|
|
103
|
+
* For a page that is worked through one after another: the records a search returned, the
|
|
104
|
+
* columns of a table, the steps of a procedure. Inside a shell the bar sits at the bottom of
|
|
105
|
+
* the content column on a short page and pins to the bottom of the viewport on a long one,
|
|
106
|
+
* so "next" is at the same place on every page of the series.
|
|
107
|
+
*/
|
|
108
|
+
sequence?: PageSequence;
|
|
79
109
|
/** The page body. */
|
|
80
110
|
children: ReactNode;
|
|
81
111
|
}
|
|
@@ -95,10 +125,13 @@ export interface PageProps {
|
|
|
95
125
|
* frame used to append `title` to the trail *and* render it as an `h1`, so every `DetailPage`
|
|
96
126
|
* printed its own name twice, a couple of dozen pixels apart.
|
|
97
127
|
*
|
|
98
|
-
* `
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
128
|
+
* `actions` sit at the right end of the title's line when the whole trail fits beside them, and
|
|
129
|
+
* drop to a line of their own when it does not, wrapping their own buttons there; `badges` and
|
|
130
|
+
* `description` take a line under both. Every line is a control tall with one inset above,
|
|
131
|
+
* between and below them, so a one-line band is the app header's own height. Inside a shell
|
|
132
|
+
* the band bleeds to the content column's edge with a bottom border, so the two read as one
|
|
133
|
+
* piece of chrome; standalone — the workbench, the unit tests — it is the same band without the
|
|
134
|
+
* bleed. A `measure="narrow"`
|
|
102
135
|
* frame (`FormPage`, `SettingsPage`) keeps the row and drops the chrome, because a form is
|
|
103
136
|
* capped with its header (ADR 0098 §3).
|
|
104
137
|
*
|
|
@@ -109,6 +142,10 @@ export interface PageProps {
|
|
|
109
142
|
* query never hides behind a spinner) then `isLoading` replace the body while the
|
|
110
143
|
* header stays put, so the user keeps their place in the layers.
|
|
111
144
|
*
|
|
145
|
+
* A page that is one item of a series takes `sequence`, and the frame renders its neighbours
|
|
146
|
+
* as a bar after the article that stays at the bottom of the viewport (see `PageSequenceBar`,
|
|
147
|
+
* ADR 0168) — the band says where the page is, the bar what comes before and after it.
|
|
148
|
+
*
|
|
112
149
|
* It renders no inline styles: the frame's geometry and the title's type come from the
|
|
113
150
|
* injected react-core sheet, matched on the `data-terp` markers stamped below (ADR 0094).
|
|
114
151
|
*/
|
|
@@ -119,11 +156,13 @@ export function Page({
|
|
|
119
156
|
actions,
|
|
120
157
|
badges,
|
|
121
158
|
description,
|
|
159
|
+
summary,
|
|
122
160
|
measure = "full",
|
|
123
161
|
isLoading,
|
|
124
162
|
loadingState,
|
|
125
163
|
error,
|
|
126
164
|
errorState,
|
|
165
|
+
sequence,
|
|
127
166
|
children,
|
|
128
167
|
}: PageProps) {
|
|
129
168
|
const resolve = useUiText();
|
|
@@ -134,13 +173,16 @@ export function Page({
|
|
|
134
173
|
// (OverviewPage / DetailPage), the rendered body's DOM children must each carry an
|
|
135
174
|
// allowed component's data-terp marker — verified one macrotask after mount (like the
|
|
136
175
|
// page-archetype check) and refused fail closed with the lint rule's directive message.
|
|
176
|
+
// The frame's own two rules (ADR 0169 §4) are checked on every page under a contract, the
|
|
177
|
+
// plain Page included: the summary band holds figures, checked here with the body; and one
|
|
178
|
+
// figure at most is the headline, counted as figures register below.
|
|
137
179
|
const contract = useLayoutContract();
|
|
138
180
|
const slotOwner = useContext(LayoutSlotContext);
|
|
139
181
|
const articleRef = useRef<HTMLElement>(null);
|
|
140
182
|
const [slotViolation, setSlotViolation] = useState<string | null>(null);
|
|
141
183
|
const showsBody = (error === null || error === undefined) && !isLoading;
|
|
142
184
|
useEffect(() => {
|
|
143
|
-
if (contract === null ||
|
|
185
|
+
if (contract === null || !showsBody) {
|
|
144
186
|
return;
|
|
145
187
|
}
|
|
146
188
|
const timer = setTimeout(() => {
|
|
@@ -149,13 +191,37 @@ export function Page({
|
|
|
149
191
|
return;
|
|
150
192
|
}
|
|
151
193
|
const body = [...article.children].filter((child) => child.tagName !== "HEADER");
|
|
152
|
-
setSlotViolation(
|
|
194
|
+
setSlotViolation(
|
|
195
|
+
(slotOwner === null ? null : verifySlotChildren(contract, slotOwner, body)) ??
|
|
196
|
+
verifyPageSummary(contract, article),
|
|
197
|
+
);
|
|
153
198
|
}, 0);
|
|
154
199
|
return () => clearTimeout(timer);
|
|
155
200
|
});
|
|
156
201
|
if (slotViolation !== null) {
|
|
157
202
|
throw new Error(slotViolation);
|
|
158
203
|
}
|
|
204
|
+
// Every figure marked headline under this page registers here when it mounts, so one that
|
|
205
|
+
// renders after its own data arrives is counted then; a nested page keeps a registry of its
|
|
206
|
+
// own. One registry for the page's life, so a figure's effect is not re-run by the page's.
|
|
207
|
+
const [headlines, setHeadlines] = useState(0);
|
|
208
|
+
const headlineRegistry = useMemo<HeadlineRegistry>(() => {
|
|
209
|
+
const registered = new Set<symbol>();
|
|
210
|
+
return {
|
|
211
|
+
register() {
|
|
212
|
+
const id = Symbol("headline");
|
|
213
|
+
registered.add(id);
|
|
214
|
+
setHeadlines(registered.size);
|
|
215
|
+
return () => {
|
|
216
|
+
registered.delete(id);
|
|
217
|
+
setHeadlines(registered.size);
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}, []);
|
|
222
|
+
if (contract !== null && headlines > 1) {
|
|
223
|
+
throw new Error(headlineViolationMessage(contract, `${headlines} figures marked headline`));
|
|
224
|
+
}
|
|
159
225
|
// ALWAYS a trail, even of one, and that is the point rather than a simplification.
|
|
160
226
|
// An overview's title has to be the same node in the same boxes as a detail's, or the name
|
|
161
227
|
// moves the moment you open a record: one path rendered a bare h1 and the other rendered
|
|
@@ -181,13 +247,13 @@ export function Page({
|
|
|
181
247
|
description !== null &&
|
|
182
248
|
description !== false &&
|
|
183
249
|
description !== "";
|
|
184
|
-
// The band's
|
|
185
|
-
//
|
|
186
|
-
//
|
|
250
|
+
// The band's meta line exists when there is meta to put on it. A page with none, and a cluster
|
|
251
|
+
// that fits beside its trail, keeps the single line, and with it the measurement the chrome is
|
|
252
|
+
// held to: the band matches the app header above it.
|
|
187
253
|
const hasMeta = badgeList.length > 0 || hasDescription;
|
|
188
|
-
// WHICH meta, because the two are visible at different widths and the
|
|
254
|
+
// WHICH meta, because the two are visible at different widths and the line must follow what
|
|
189
255
|
// is visible: badges show everywhere, a lead line only above the second cutover. A band whose
|
|
190
|
-
// only meta is a lead line therefore has an empty
|
|
256
|
+
// only meta is a lead line therefore has an empty meta line below that width unless the
|
|
191
257
|
// sheet can tell it apart, and it could not while this said "true" for both.
|
|
192
258
|
// Hoisted, the density/collapsed idiom: the default stamps nothing, so the expression has a
|
|
193
259
|
// branch rather than a boolean React would render as the string "false".
|
|
@@ -205,49 +271,69 @@ export function Page({
|
|
|
205
271
|
// branch, and a conditional written at the attribute is the form the marker scanner reads
|
|
206
272
|
// every string literal out of.
|
|
207
273
|
const measureAttribute = measure === "narrow" ? "narrow" : undefined;
|
|
274
|
+
// The same renderability test as the band's meta: `summary={figures && <StatGroup/>}` hands
|
|
275
|
+
// this `false` before the figures arrive, and an empty band would still take its padding.
|
|
276
|
+
const hasSummary =
|
|
277
|
+
showsBody && summary !== undefined && summary !== null && summary !== false && summary !== "";
|
|
208
278
|
return (
|
|
209
|
-
<
|
|
210
|
-
{
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
<
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
279
|
+
<HeadlineContext.Provider value={headlineRegistry}>
|
|
280
|
+
<article ref={articleRef} data-terp="page" data-measure={measureAttribute}>
|
|
281
|
+
{/* A <header> ELEMENT, and it has to stay one. The slot check above drops the header
|
|
282
|
+
from the body set by tagName, so re-rendering this as a marked <div> would put it
|
|
283
|
+
back in and fail every governed OverviewPage / DetailPage closed. The marker is
|
|
284
|
+
additive; the tag is load-bearing. For the same reason the body below takes no
|
|
285
|
+
wrapper of its own — not even a display: contents one, since article.children is a
|
|
286
|
+
DOM traversal and would see it. */}
|
|
287
|
+
<header data-terp="page-header" data-has-meta={metaAttribute}>
|
|
288
|
+
<div data-terp="page-heading">
|
|
289
|
+
{/* The trail carries the h1 as its leaf. No wrapper of its own any more: the crumb
|
|
290
|
+
row it used to sit in existed to hold a 2rem floor above the title row, and
|
|
291
|
+
there is no title row to be above. */}
|
|
292
|
+
<Breadcrumbs items={trail} renderLink={renderLink} currentAs="h1" />
|
|
293
|
+
{/* Badges and the lead line travel together as one item: a group with a full-line
|
|
294
|
+
basis, so they take a line of their own under the trail and the cluster rather
|
|
295
|
+
than competing with either. Rendered only when there is something in it, because
|
|
296
|
+
the empty box would still take a line. */}
|
|
297
|
+
{hasMeta && (
|
|
298
|
+
<div data-terp="page-meta">
|
|
299
|
+
{badgeList.length > 0 && (
|
|
300
|
+
<div data-terp="page-badges">
|
|
301
|
+
{badgeList.map((badge, index) => (
|
|
302
|
+
<Fragment key={index}>{badge}</Fragment>
|
|
303
|
+
))}
|
|
304
|
+
</div>
|
|
305
|
+
)}
|
|
306
|
+
{hasDescription && (
|
|
307
|
+
<p data-terp="page-description">{resolveUiTextNode(description, resolve)}</p>
|
|
308
|
+
)}
|
|
309
|
+
</div>
|
|
310
|
+
)}
|
|
311
|
+
</div>
|
|
312
|
+
{/* Always a group, never the caller's nodes loose in the band. A page that passed a
|
|
313
|
+
fragment of buttons used to put each one in the band as its own item, each wrapping
|
|
314
|
+
on its own. One wrapper means the cluster is one item -- beside the trail or on its
|
|
315
|
+
own line, wrapping its buttons inside itself -- whether or not the page reached
|
|
316
|
+
for PageActions (which also collapses to icons and a menu as room runs out). */}
|
|
317
|
+
{hasActions && <div data-terp="page-actions">{actions}</div>}
|
|
318
|
+
</header>
|
|
319
|
+
{/* The summary band: a second <header>, so the body slot check above drops it by tag
|
|
320
|
+
as it drops the band, and the frame check reads its children against the summary's
|
|
321
|
+
own table instead. Introductory content for the page is what a header is; inside an
|
|
322
|
+
article it is no landmark, so it adds nothing to a screen reader's landmark list. */}
|
|
323
|
+
{hasSummary && (
|
|
324
|
+
<header data-terp="page-summary">
|
|
325
|
+
<LayoutSlotContext.Provider value={null}>{summary}</LayoutSlotContext.Provider>
|
|
326
|
+
</header>
|
|
327
|
+
)}
|
|
328
|
+
{/* Reset the slot for the body's own subtree, so nested content is never judged
|
|
329
|
+
by an ancestor archetype's slot. */}
|
|
330
|
+
<LayoutSlotContext.Provider value={null}>{body}</LayoutSlotContext.Provider>
|
|
331
|
+
</article>
|
|
332
|
+
{/* After the article rather than inside it, so the slot check above never reads it as
|
|
333
|
+
body (see PageSequenceBar). Rendered while the body loads or fails too: the series is
|
|
334
|
+
the page's place, like the band, and stepping past a record that failed to load is
|
|
335
|
+
exactly when a reader wants it. */}
|
|
336
|
+
{sequence !== undefined && <PageSequenceBar sequence={sequence} />}
|
|
337
|
+
</HeadlineContext.Provider>
|
|
252
338
|
);
|
|
253
339
|
}
|
package/src/PageActions.test.tsx
CHANGED
|
@@ -140,12 +140,16 @@ describe("PageActions", () => {
|
|
|
140
140
|
expect(screen.queryByRole("button", { name: "More actions" })).toBeNull();
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
-
it("folds supporting actions into the menu below the first cutover", () => {
|
|
143
|
+
it("folds supporting actions into the menu below the first cutover when there is a crowd", () => {
|
|
144
|
+
// More than two actions in the slot: the primary and two supporting ones.
|
|
144
145
|
stubRegion("narrow");
|
|
145
146
|
render(
|
|
146
147
|
<PageActions
|
|
147
148
|
primary={<Button>Publish</Button>}
|
|
148
|
-
secondaryActions={
|
|
149
|
+
secondaryActions={[
|
|
150
|
+
...SUPPORTING,
|
|
151
|
+
{ label: "Duplicate", icon: <svg aria-hidden="true" focusable={false} />, onSelect: () => {} },
|
|
152
|
+
]}
|
|
149
153
|
overflow={[{ label: "Delete", variant: "destructive", onSelect: () => {} }]}
|
|
150
154
|
/>,
|
|
151
155
|
);
|
|
@@ -154,7 +158,64 @@ describe("PageActions", () => {
|
|
|
154
158
|
// ...and present in the menu, above the page's own rare items.
|
|
155
159
|
fireEvent.click(screen.getByRole("button", { name: "More actions" }));
|
|
156
160
|
const items = screen.getAllByRole("menuitem").map((item) => item.textContent);
|
|
157
|
-
expect(items).toEqual(["Discard", "Delete"]);
|
|
161
|
+
expect(items).toEqual(["Discard", "Duplicate", "Delete"]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("keeps one supporting action beside the primary on a phone, as an icon, not in a menu", () => {
|
|
165
|
+
// Two actions fit a phone's line as a button and an icon; folding the second into a menu
|
|
166
|
+
// of one made a tap into two. Mutation: fold on density alone again, and Discard is gone.
|
|
167
|
+
stubRegion("narrow");
|
|
168
|
+
render(<PageActions primary={<Button>Publish</Button>} secondaryActions={SUPPORTING} />);
|
|
169
|
+
const discard = screen.getByRole("button", { name: "Discard" });
|
|
170
|
+
expect(discard).toHaveTextContent("");
|
|
171
|
+
expect(screen.queryByRole("button", { name: "More actions" })).toBeNull();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("keeps a lone action's label below the widest region", () => {
|
|
175
|
+
// An icon saves room a cluster of one does not need, and costs the reader a guess.
|
|
176
|
+
// Mutation: drop the count from the icon condition, and the label goes.
|
|
177
|
+
for (const region of ["medium", "narrow"] as const) {
|
|
178
|
+
stubRegion(region);
|
|
179
|
+
const { unmount } = render(<PageActions secondaryActions={SUPPORTING} />);
|
|
180
|
+
expect(screen.getByRole("button", { name: "Discard" })).toHaveTextContent("Discard");
|
|
181
|
+
unmount();
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("counts only what renders: a primary that is false or null is not an action", () => {
|
|
186
|
+
// `primary={canCreate && <Button>New</Button>}` with canCreate false. Counted as an
|
|
187
|
+
// action, it made the lone supporting action a bare icon in the middle region.
|
|
188
|
+
// Mutation: count `primary !== undefined` again, and Discard loses its label.
|
|
189
|
+
stubRegion("medium");
|
|
190
|
+
for (const primary of [false, null] as const) {
|
|
191
|
+
const { unmount } = render(<PageActions primary={primary} secondaryActions={SUPPORTING} />);
|
|
192
|
+
expect(screen.getByRole("button", { name: "Discard" })).toHaveTextContent("Discard");
|
|
193
|
+
unmount();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("does not fold two supporting actions into a menu beside a primary that is not rendered", () => {
|
|
198
|
+
// Two actions fit a phone's line; a primary of null made the count three, and folded both
|
|
199
|
+
// into a menu of two. Mutation: count `primary !== undefined` again, and Discard is gone.
|
|
200
|
+
stubRegion("narrow");
|
|
201
|
+
render(
|
|
202
|
+
<PageActions
|
|
203
|
+
primary={null}
|
|
204
|
+
secondaryActions={[
|
|
205
|
+
...SUPPORTING,
|
|
206
|
+
{ label: "Duplicate", icon: <svg aria-hidden="true" focusable={false} />, onSelect: () => {} },
|
|
207
|
+
]}
|
|
208
|
+
/>,
|
|
209
|
+
);
|
|
210
|
+
expect(screen.getByRole("button", { name: "Discard" })).toBeInTheDocument();
|
|
211
|
+
expect(screen.queryByRole("button", { name: "More actions" })).toBeNull();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("returns no wrapper when every slot is empty or not rendered", () => {
|
|
215
|
+
// Mutation: test `primary === undefined` again in the early return, and an empty cluster
|
|
216
|
+
// div is left in the band.
|
|
217
|
+
const { container } = render(<PageActions primary={false} secondary={null} />);
|
|
218
|
+
expect(container.firstChild).toBeNull();
|
|
158
219
|
});
|
|
159
220
|
|
|
160
221
|
it("keeps the primary action labelled at every width", () => {
|
package/src/PageActions.tsx
CHANGED
|
@@ -87,18 +87,42 @@ function useActionDensity(): ActionDensity {
|
|
|
87
87
|
return density;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Whether a slot draws anything. React draws nothing for `false`, `null`, `undefined` or an
|
|
92
|
+
* empty string, so `primary={canCreate && <Button>…</Button>}` with `canCreate` false is not an
|
|
93
|
+
* action — counted as one, it turned a lone supporting action into a bare icon, or folded two
|
|
94
|
+
* of them into the menu on a phone.
|
|
95
|
+
*/
|
|
96
|
+
function renders(node: ReactNode): boolean {
|
|
97
|
+
return node !== undefined && node !== null && typeof node !== "boolean" && node !== "";
|
|
98
|
+
}
|
|
99
|
+
|
|
90
100
|
function actionKey(action: OverflowAction): string {
|
|
91
101
|
return typeof action.label === "string" ? action.label : action.label.id;
|
|
92
102
|
}
|
|
93
103
|
|
|
94
104
|
/**
|
|
95
|
-
* Standard right-aligned action cluster for page headers
|
|
105
|
+
* Standard right-aligned action cluster for page headers — the way a page's actions should be
|
|
106
|
+
* written, because it is the one that survives a narrow window.
|
|
96
107
|
*
|
|
97
108
|
* The primary action keeps its label at every width, and that is a decision rather than an
|
|
98
109
|
* oversight: it is the one thing the page is for, and a glyph or a menu line makes the main
|
|
99
|
-
* act of the screen a guess or a second tap. Everything around it gives way instead
|
|
100
|
-
*
|
|
101
|
-
*
|
|
110
|
+
* act of the screen a guess or a second tap. Everything around it gives way instead, by WIDTH
|
|
111
|
+
* and by COUNT:
|
|
112
|
+
*
|
|
113
|
+
* - **Icons when there is company.** In the middle region and on a phone, a supporting action
|
|
114
|
+
* that has an icon drops its label for it — but only when the slot holds more than one
|
|
115
|
+
* action. A lone action keeps its words: an icon saves space a cluster of one does not need,
|
|
116
|
+
* and costs the reader a guess. The label is never dropped, only moved into the accessible
|
|
117
|
+
* name and the tooltip.
|
|
118
|
+
* - **A menu when there is a crowd.** On a phone the supporting actions fold into the overflow
|
|
119
|
+
* menu, above the page's own rare and destructive items — but only when the slot holds more
|
|
120
|
+
* than two actions. A primary and one supporting action fit a phone's line as a button and
|
|
121
|
+
* an icon, and folding the second into a menu of one turned a tap into two.
|
|
122
|
+
*
|
|
123
|
+
* Counted: the primary, the `secondary` node, and each of `secondaryActions`. `overflow` is
|
|
124
|
+
* already behind the menu and does not count. Whatever the form, the cluster wraps rather than
|
|
125
|
+
* running off the edge (see page-actions in the sheet).
|
|
102
126
|
*/
|
|
103
127
|
export function PageActions({
|
|
104
128
|
primary,
|
|
@@ -112,18 +136,19 @@ export function PageActions({
|
|
|
112
136
|
const density = useActionDensity();
|
|
113
137
|
const supporting = secondaryActions ?? [];
|
|
114
138
|
const rare = overflow ?? [];
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
139
|
+
// How many actions the slot shows, which decides the form as much as the width does.
|
|
140
|
+
const count = (renders(primary) ? 1 : 0) + (renders(secondary) ? 1 : 0) + supporting.length;
|
|
141
|
+
// On a phone, and only with more than two, the supporting actions stop being buttons and
|
|
142
|
+
// become the top of the menu. Order is deliberate: what the page offers first, then what it
|
|
143
|
+
// keeps back.
|
|
144
|
+
const fold = density === "menu" && count > 2;
|
|
145
|
+
const inMenu = fold ? [...supporting, ...rare] : rare;
|
|
146
|
+
const asButtons = fold ? [] : supporting;
|
|
119
147
|
const hasMenu = inMenu.length > 0;
|
|
148
|
+
// Below the widest region, an icon stands in for a label only beside other actions.
|
|
149
|
+
const iconOnly = density !== "labels" && count > 1;
|
|
120
150
|
|
|
121
|
-
if (
|
|
122
|
-
primary === undefined &&
|
|
123
|
-
secondary === undefined &&
|
|
124
|
-
supporting.length === 0 &&
|
|
125
|
-
rare.length === 0
|
|
126
|
-
) {
|
|
151
|
+
if (!renders(primary) && !renders(secondary) && supporting.length === 0 && rare.length === 0) {
|
|
127
152
|
return null;
|
|
128
153
|
}
|
|
129
154
|
|
|
@@ -155,7 +180,7 @@ export function PageActions({
|
|
|
155
180
|
// Icon-only still has to SAY what it is: the label moves from the box to the
|
|
156
181
|
// accessible name and the tooltip rather than being dropped, so the control reads the
|
|
157
182
|
// same to a screen reader at every width and hovers legibly for everyone else.
|
|
158
|
-
return
|
|
183
|
+
return iconOnly && action.icon !== undefined ? (
|
|
159
184
|
<Button
|
|
160
185
|
key={actionKey(action)}
|
|
161
186
|
variant={action.variant === "destructive" ? "danger" : "secondary"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { AnchorHTMLAttributes, ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import { Icon } from "./icons";
|
|
4
|
+
import { useNavLink } from "./navLink";
|
|
5
|
+
import { fillPlaceholders, useStrings, useUiText } from "./uiText";
|
|
6
|
+
import type { UiText } from "./uiText";
|
|
7
|
+
|
|
8
|
+
/** One neighbour of the current page: what it is called, and where it lives. */
|
|
9
|
+
export interface PageSequenceLink {
|
|
10
|
+
/** The neighbour's own name — the record, the column, the step — shown on the link. */
|
|
11
|
+
label: UiText;
|
|
12
|
+
/** The neighbour's in-app path. */
|
|
13
|
+
to: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The ordered series a page is one item of, so a reader can step through it without going
|
|
18
|
+
* back to the list: a record among the results that opened it, a column among a table's
|
|
19
|
+
* columns, a step among a procedure's steps.
|
|
20
|
+
*
|
|
21
|
+
* Data rather than elements, and that is the point of the prop. Handed descriptions, the
|
|
22
|
+
* frame renders the neighbours as real links (the router's, so middle-click and "open in a
|
|
23
|
+
* new tab" work and `rel` says which way each one points), puts them in the same places on
|
|
24
|
+
* every page of the series, and words the position in the active locale. Handed elements, it
|
|
25
|
+
* could do none of those — the same reason `PageActions` takes `secondaryActions` as tuples.
|
|
26
|
+
*/
|
|
27
|
+
export interface PageSequence {
|
|
28
|
+
/**
|
|
29
|
+
* What the series is, as the accessible name of its `nav` landmark — "Columns",
|
|
30
|
+
* "Invoices". Required: a page already has a breadcrumb landmark, and two navigation
|
|
31
|
+
* landmarks a screen reader cannot tell apart are one too many.
|
|
32
|
+
*/
|
|
33
|
+
label: UiText;
|
|
34
|
+
/** The item before this one. Absent on the first: its place stays empty rather than closing up. */
|
|
35
|
+
previous?: PageSequenceLink;
|
|
36
|
+
/** The item after this one. Absent on the last. */
|
|
37
|
+
next?: PageSequenceLink;
|
|
38
|
+
/**
|
|
39
|
+
* This page's place in the series, counted from 1 — rendered as "4 of 23". Omit it when the
|
|
40
|
+
* total is not known; an unknown total is shown as nothing, never guessed.
|
|
41
|
+
*/
|
|
42
|
+
position?: { current: number; total: number };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The sequence bar `Page` renders after its article when given a `sequence`.
|
|
47
|
+
*
|
|
48
|
+
* **A sibling of the article, not a child of it.** The layout contract's runtime check reads
|
|
49
|
+
* the article's children as the body and exempts only the header; a bar inside it would fail
|
|
50
|
+
* every governed `DetailPage` closed, and exempting it there by tag would let any app `<nav>`
|
|
51
|
+
* through the contract. Outside the article the check never sees it.
|
|
52
|
+
*
|
|
53
|
+
* **A `nav`, not a `footer`.** Outside a sectioning element a `<footer>` is the page's
|
|
54
|
+
* `contentinfo` landmark, which belongs to the shell's own footer. A bar of links to sibling
|
|
55
|
+
* pages is navigation, and is named for its series.
|
|
56
|
+
*
|
|
57
|
+
* **Three cells, always.** The previous link, the position and the next link each keep their
|
|
58
|
+
* cell whether or not they are present, so "next" sits at the same spot on the first page of
|
|
59
|
+
* a series as on the fortieth — a reader stepping through it never has to find the control
|
|
60
|
+
* again.
|
|
61
|
+
*/
|
|
62
|
+
export function PageSequenceBar({ sequence }: { sequence: PageSequence }) {
|
|
63
|
+
const strings = useStrings();
|
|
64
|
+
const resolve = useUiText();
|
|
65
|
+
const navLink = useNavLink();
|
|
66
|
+
const { previous, next, position } = sequence;
|
|
67
|
+
|
|
68
|
+
const renderStep = (step: PageSequenceLink, direction: "prev" | "next"): ReactNode => {
|
|
69
|
+
const label = resolve(step.label);
|
|
70
|
+
// The direction is in the accessible name, and the visible label is inside it (WCAG 2.5.3):
|
|
71
|
+
// a screen reader hears "Previous: Order 1016", a voice user can still say "Order 1016".
|
|
72
|
+
const attributes: Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href"> = {
|
|
73
|
+
rel: direction,
|
|
74
|
+
"aria-label": fillPlaceholders(
|
|
75
|
+
direction === "prev" ? strings.pageSequencePrevious : strings.pageSequenceNext,
|
|
76
|
+
{ label },
|
|
77
|
+
),
|
|
78
|
+
};
|
|
79
|
+
const children = (
|
|
80
|
+
<>
|
|
81
|
+
{direction === "prev" && <Icon name="chevron-left" />}
|
|
82
|
+
<span data-terp="page-sequence-label">{label}</span>
|
|
83
|
+
{direction === "next" && <Icon name="chevron-right" />}
|
|
84
|
+
</>
|
|
85
|
+
);
|
|
86
|
+
// The router's link where there is one, so a step is a client-side navigation; a plain
|
|
87
|
+
// anchor outside a Terp router, like every other layout component that renders a link.
|
|
88
|
+
return navLink === null ? (
|
|
89
|
+
<a {...attributes} href={step.to}>
|
|
90
|
+
{children}
|
|
91
|
+
</a>
|
|
92
|
+
) : (
|
|
93
|
+
navLink({ to: step.to, children, attributes })
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<nav data-terp="page-sequence" aria-label={resolve(sequence.label)}>
|
|
99
|
+
<span data-terp="page-sequence-previous">
|
|
100
|
+
{previous !== undefined && renderStep(previous, "prev")}
|
|
101
|
+
</span>
|
|
102
|
+
<span data-terp="page-sequence-position">
|
|
103
|
+
{position !== undefined &&
|
|
104
|
+
fillPlaceholders(strings.pageSequencePosition, {
|
|
105
|
+
current: position.current,
|
|
106
|
+
total: position.total,
|
|
107
|
+
})}
|
|
108
|
+
</span>
|
|
109
|
+
<span data-terp="page-sequence-next">
|
|
110
|
+
{next !== undefined && renderStep(next, "next")}
|
|
111
|
+
</span>
|
|
112
|
+
</nav>
|
|
113
|
+
);
|
|
114
|
+
}
|