@terpjs/react-core 0.27.0 → 0.29.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 +31 -7
- package/package.json +4 -3
- package/src/Field.test.tsx +95 -1
- package/src/Field.tsx +70 -1
- package/src/LoginView.test.tsx +33 -0
- package/src/LoginView.tsx +36 -23
- package/src/Meter.test.tsx +304 -0
- package/src/Meter.tsx +159 -0
- package/src/QuietActions.test.tsx +118 -0
- package/src/QuietActions.tsx +75 -0
- package/src/UserMenu.test.tsx +23 -0
- package/src/UserMenu.tsx +9 -1
- package/src/admin/AccessAdmin.tsx +6 -2
- package/src/admin/AuditLogAdmin.tsx +1 -1
- package/src/admin/access.test.tsx +2 -1
- package/src/admin/admin.test.tsx +57 -5
- package/src/admin/module.tsx +9 -6
- package/src/bootstrap.test.tsx +1 -1
- package/src/bootstrap.tsx +2 -1
- package/src/dataview/DataView.test.tsx +99 -0
- package/src/dataview/DataViewPagination.tsx +2 -2
- package/src/dataview/DataViewToolbar.tsx +2 -2
- package/src/dataview/README.md +5 -3
- package/src/dataview/index.ts +1 -1
- package/src/dataview/internal.tsx +102 -21
- package/src/dataview/types.ts +11 -37
- package/src/errorMessages.test.tsx +21 -0
- package/src/errorMessages.tsx +33 -19
- package/src/index.ts +9 -3
- package/src/locale.test.tsx +164 -4
- package/src/locale.tsx +105 -10
- package/src/markers.test.ts +33 -11
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +265 -14
- package/src/styles.ts +424 -49
- package/src/testing.test.tsx +162 -0
- package/src/testing.ts +84 -0
- package/src/tokens.guard.test.ts +11 -6
- package/src/ui/controlMessages.tsx +27 -14
- package/src/uiText.literals.test.ts +19 -6
- package/src/uiText.test.tsx +143 -2
- package/src/uiText.tsx +287 -15
- package/vitest.setup.ts +7 -20
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import { Component } from "react";
|
|
4
|
+
import type { ReactNode } from "react";
|
|
5
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
6
|
+
|
|
7
|
+
// The pairings the contrast gate measures, read from the contract package that owns them.
|
|
8
|
+
import tokenPairs from "../../contract/token-pairs.json";
|
|
9
|
+
import { DetailPage } from "./DetailPage";
|
|
10
|
+
import { DetailList } from "./layout";
|
|
11
|
+
import { LayoutContractContext, slotViolationMessage } from "./layoutContract";
|
|
12
|
+
import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "./locale";
|
|
13
|
+
import { Meter } from "./Meter";
|
|
14
|
+
import type { MeterProps } from "./Meter";
|
|
15
|
+
import { TERP_STYLES_CSS } from "./styles";
|
|
16
|
+
import { Card } from "./ui/Card";
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
cleanup();
|
|
20
|
+
window.localStorage.clear();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** Render under a fixed app locale, so a formatting assertion never depends on the runner's. */
|
|
24
|
+
function renderIn(locale: "nl" | "en", node: ReactNode) {
|
|
25
|
+
return render(
|
|
26
|
+
<LocaleProvider locales={{ nl: LOCALE_NL, en: LOCALE_EN }} defaultLocale={locale}>
|
|
27
|
+
{node}
|
|
28
|
+
</LocaleProvider>,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const meterOf = () => document.querySelector("meter")!;
|
|
33
|
+
const printed = () => document.querySelector('[data-terp="meter-value"]')!.textContent;
|
|
34
|
+
|
|
35
|
+
describe("Meter's name and value", () => {
|
|
36
|
+
it("is named by its label, which it does not print", () => {
|
|
37
|
+
// The name is what a screen reader says the number measures; without it a meter is
|
|
38
|
+
// announced as a bare value. Not printed, because the caption beside it already is.
|
|
39
|
+
renderIn("en", <Meter label="Storage" value={0.62} />);
|
|
40
|
+
expect(screen.getByRole("meter", { name: "Storage" })).toBe(meterOf());
|
|
41
|
+
expect(document.querySelector('[data-terp="meter"]')!.textContent).toBe("62%");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("prints the value in the app's locale, and announces exactly what it prints", () => {
|
|
45
|
+
// A value with a decimal is the one that tells the locales apart: nl writes the comma.
|
|
46
|
+
const format = { style: "percent", maximumFractionDigits: 1 } as const;
|
|
47
|
+
renderIn("nl", <Meter label="Opslag" value={0.625} format={format} />);
|
|
48
|
+
expect(printed()).toBe("62,5%");
|
|
49
|
+
expect(meterOf().getAttribute("aria-valuetext")).toBe("62,5%");
|
|
50
|
+
cleanup();
|
|
51
|
+
renderIn("en", <Meter label="Storage" value={0.625} format={format} />);
|
|
52
|
+
expect(printed()).toBe("62.5%");
|
|
53
|
+
expect(meterOf().getAttribute("aria-valuetext")).toBe("62.5%");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("hands the printed value to the meter and hides the copy, so it is one element", () => {
|
|
57
|
+
// Measured in Chromium: a meter inside a HubCard's link adds its value text to the link's
|
|
58
|
+
// name, so with the printed copy left in the tree the link read its value twice.
|
|
59
|
+
renderIn("en", <Meter label="Storage" value={0.62} />);
|
|
60
|
+
expect(document.querySelector('[data-terp="meter-value"]')).toHaveAttribute(
|
|
61
|
+
"aria-hidden",
|
|
62
|
+
"true",
|
|
63
|
+
);
|
|
64
|
+
expect(screen.getAllByRole("meter")).toHaveLength(1);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("what Meter prints", () => {
|
|
69
|
+
it("prints a percentage as the share of the range, never Intl's hundredfold", () => {
|
|
70
|
+
renderIn("en", <Meter label="Score" value={74} max={100} />);
|
|
71
|
+
expect(printed(), "74 of 100 is 74%, where the bare Intl option says 7,400%").toBe("74%");
|
|
72
|
+
cleanup();
|
|
73
|
+
renderIn("en", <Meter label="Score" value={30} min={20} max={70} />);
|
|
74
|
+
expect(printed(), "and the share is measured from min, not from zero").toBe("20%");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("prints the value itself for any other style", () => {
|
|
78
|
+
renderIn(
|
|
79
|
+
"nl",
|
|
80
|
+
<Meter
|
|
81
|
+
label="Opslag"
|
|
82
|
+
value={7.4}
|
|
83
|
+
max={10}
|
|
84
|
+
format={{ style: "unit", unit: "gigabyte", maximumFractionDigits: 1 }}
|
|
85
|
+
/>,
|
|
86
|
+
);
|
|
87
|
+
expect(printed()).toBe("7,4 GB");
|
|
88
|
+
cleanup();
|
|
89
|
+
renderIn("en", <Meter label="Score" value={72} max={100} format={{}} />);
|
|
90
|
+
expect(printed(), "an empty format is a plain number").toBe("72");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("prints an overrun as it is, while the element clamps the bar", () => {
|
|
94
|
+
// The text is where a reader learns the bar has run out of room, so it is never clamped.
|
|
95
|
+
renderIn("en", <Meter label="Storage" value={12} max={10} />);
|
|
96
|
+
expect(printed()).toBe("120%");
|
|
97
|
+
expect(meterOf().getAttribute("value"), "the element gets the real value to clamp").toBe("12");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("prints the framework's dash for a range with no width, never an infinity", () => {
|
|
101
|
+
// Chromium paints no fill for either shape (measured), and the division would print
|
|
102
|
+
// "∞%" for the second.
|
|
103
|
+
renderIn("en", <Meter label="Storage" value={5} min={5} max={5} />);
|
|
104
|
+
expect(printed()).toBe("—");
|
|
105
|
+
cleanup();
|
|
106
|
+
renderIn("en", <Meter label="Storage" value={0.5} min={1} max={0.5} />);
|
|
107
|
+
expect(printed()).toBe("—");
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
type Case = Pick<MeterProps, "value" | "min" | "max" | "low" | "high" | "optimum"> & {
|
|
112
|
+
region: "optimum" | "suboptimum" | "even-less-good";
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The region Chromium itself paints, per case — read off a rendered meter whose three value
|
|
117
|
+
* pseudo-elements were given three different colours, not derived from the standard's prose.
|
|
118
|
+
*
|
|
119
|
+
* Chosen to make every boundary observable: each value sitting exactly ON a band edge, a band
|
|
120
|
+
* declared outside the range, and a `high` below `low` with the optimum ABOVE the bands, which
|
|
121
|
+
* is the one shape where clamping `high` up to `low` changes the answer (without it, 0.5 is
|
|
122
|
+
* read as optimum; Chromium paints it even-less-good).
|
|
123
|
+
*/
|
|
124
|
+
const CHROMIUM_REGIONS: Case[] = [
|
|
125
|
+
{ low: 0.3, high: 0.7, optimum: 0.1, value: 0.3, region: "optimum" },
|
|
126
|
+
{ low: 0.3, high: 0.7, optimum: 0.1, value: 0.5, region: "suboptimum" },
|
|
127
|
+
{ low: 0.3, high: 0.7, optimum: 0.1, value: 0.7, region: "suboptimum" },
|
|
128
|
+
{ low: 0.3, high: 0.7, optimum: 0.1, value: 0.9, region: "even-less-good" },
|
|
129
|
+
{ low: 0.3, high: 0.7, optimum: 0.1, value: 1.5, region: "even-less-good" },
|
|
130
|
+
{ low: 0.3, high: 0.7, optimum: 0.9, value: 0.2, region: "even-less-good" },
|
|
131
|
+
{ low: 0.3, high: 0.7, optimum: 0.9, value: 0.3, region: "suboptimum" },
|
|
132
|
+
{ low: 0.3, high: 0.7, optimum: 0.9, value: 0.7, region: "optimum" },
|
|
133
|
+
{ low: 0.3, high: 0.7, optimum: 0.5, value: 0.2, region: "suboptimum" },
|
|
134
|
+
{ low: 0.3, high: 0.7, optimum: 0.5, value: 0.3, region: "optimum" },
|
|
135
|
+
{ low: 0.3, high: 0.7, optimum: 0.5, value: 0.7, region: "optimum" },
|
|
136
|
+
{ low: 0.3, high: 0.7, optimum: 0.5, value: 0.9, region: "suboptimum" },
|
|
137
|
+
{ low: 0.3, high: 0.7, optimum: 0.3, value: 0.2, region: "suboptimum" },
|
|
138
|
+
{ low: 0.3, high: 0.7, optimum: 0.7, value: 0.9, region: "suboptimum" },
|
|
139
|
+
{ low: 0.3, value: 0.2, region: "suboptimum" },
|
|
140
|
+
{ low: 0.3, value: 0.3, region: "optimum" },
|
|
141
|
+
{ high: 0.7, value: 0.9, region: "suboptimum" },
|
|
142
|
+
{ optimum: 0.1, value: 0.9, region: "optimum" },
|
|
143
|
+
{ low: -1, high: 5, optimum: 0.1, value: 0.9, region: "optimum" },
|
|
144
|
+
{ low: 0.8, high: 0.3, optimum: 0.1, value: 0.9, region: "even-less-good" },
|
|
145
|
+
{ low: 0.8, high: 0.3, optimum: 0.9, value: 0.5, region: "even-less-good" },
|
|
146
|
+
{ low: 0.8, high: 0.3, optimum: 0.9, value: 0.8, region: "optimum" },
|
|
147
|
+
{ min: 20, max: 70, low: 30, high: 60, optimum: 25, value: 30, region: "optimum" },
|
|
148
|
+
{ min: 20, max: 70, low: 30, high: 60, optimum: 25, value: 60, region: "suboptimum" },
|
|
149
|
+
{ min: 20, max: 70, low: 30, high: 60, optimum: 25, value: 65, region: "even-less-good" },
|
|
150
|
+
{ min: 20, max: 70, low: 30, high: 60, optimum: 65, value: 25, region: "even-less-good" },
|
|
151
|
+
{ min: 20, max: 70, low: 30, high: 60, optimum: 65, value: 60, region: "optimum" },
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
describe("Meter's bands", () => {
|
|
155
|
+
it("stamps no region when no band is declared", () => {
|
|
156
|
+
// Unbanded, the browser still files every value under "optimum" (measured) — so a region
|
|
157
|
+
// stamped here would paint a bare quota in the success tone, a judgement nobody made.
|
|
158
|
+
renderIn("en", <Meter label="Storage" value={0.95} />);
|
|
159
|
+
expect(meterOf().hasAttribute("data-region")).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it.each(CHROMIUM_REGIONS)(
|
|
163
|
+
"puts $value (low $low, high $high, optimum $optimum) where Chromium does: $region",
|
|
164
|
+
({ region, ...bands }) => {
|
|
165
|
+
renderIn("en", <Meter label="Storage" {...bands} />);
|
|
166
|
+
expect(meterOf().getAttribute("data-region")).toBe(region);
|
|
167
|
+
},
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/** Every rule in the sheet, as `[selectors, declarations]`, comments stripped. */
|
|
172
|
+
function rules(): Array<[string[], string]> {
|
|
173
|
+
const css = TERP_STYLES_CSS.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
174
|
+
return [...css.matchAll(/([^{}]+)\{([^{}]*)\}/g)].map((match) => [
|
|
175
|
+
match[1]!.split(",").map((part) => part.trim().replace(/\s+/g, " ")),
|
|
176
|
+
match[2]!,
|
|
177
|
+
]);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The `var()` a rule's `background` names, for the one rule whose selectors include `selector`. */
|
|
181
|
+
function backgroundOf(selector: string): string | undefined {
|
|
182
|
+
const found = rules().filter(([selectors]) => selectors.includes(selector));
|
|
183
|
+
expect(found, `exactly one rule targets ${selector}`).toHaveLength(1);
|
|
184
|
+
return /background:\s*var\((--[a-z0-9-]+)\)/.exec(found[0]![1])?.[1];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const VALUE_PSEUDOS = [
|
|
188
|
+
"::-webkit-meter-optimum-value",
|
|
189
|
+
"::-webkit-meter-suboptimum-value",
|
|
190
|
+
"::-webkit-meter-even-less-good-value",
|
|
191
|
+
"::-moz-meter-bar",
|
|
192
|
+
];
|
|
193
|
+
const TONES = {
|
|
194
|
+
optimum: "--color-status-success",
|
|
195
|
+
suboptimum: "--color-status-warning",
|
|
196
|
+
"even-less-good": "--color-status-danger",
|
|
197
|
+
} as const;
|
|
198
|
+
|
|
199
|
+
describe("Meter's rules in the sheet", () => {
|
|
200
|
+
it("paints the unbanded fill in the accent and each band in its own tone, in every engine's rule", () => {
|
|
201
|
+
// The Gecko rules are the half no lane can see — the workbench runs one browser and it
|
|
202
|
+
// drops them — so this is the only thing that notices one being deleted or retoned.
|
|
203
|
+
for (const pseudo of VALUE_PSEUDOS) {
|
|
204
|
+
expect(backgroundOf(`[data-terp="meter-bar"]${pseudo}`), pseudo).toBe("--color-fg-accent");
|
|
205
|
+
for (const [region, tone] of Object.entries(TONES)) {
|
|
206
|
+
expect(
|
|
207
|
+
backgroundOf(`[data-terp="meter-bar"][data-region="${region}"]${pseudo}`),
|
|
208
|
+
`${region} ${pseudo}`,
|
|
209
|
+
).toBe(tone);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("keeps each engine's pseudo-elements in rules of their own", () => {
|
|
215
|
+
// A selector list naming a pseudo-element an engine does not know is invalid as a whole,
|
|
216
|
+
// so one mixed list would cost Chromium every meter rule in it rather than the Gecko half.
|
|
217
|
+
for (const [selectors] of rules()) {
|
|
218
|
+
const webkit = selectors.some((selector) => selector.includes("::-webkit-"));
|
|
219
|
+
const gecko = selectors.some((selector) => selector.includes("::-moz-"));
|
|
220
|
+
expect(webkit && gecko, selectors.join(", ")).toBe(false);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("paints only fills the contrast gate holds against the track they sit in", () => {
|
|
225
|
+
// The sheet chooses the tokens; token-pairs.json is what the contrast gate measures in all
|
|
226
|
+
// five themes. This joins the two, so retoning a fill to one nobody declared — the brand
|
|
227
|
+
// fill, say, which falls under 3:1 against this track in the three dark themes — fails
|
|
228
|
+
// here instead of shipping unmeasured.
|
|
229
|
+
const track = backgroundOf('[data-terp="meter-bar"]');
|
|
230
|
+
expect(track).toBe("--color-bg-inset");
|
|
231
|
+
expect(backgroundOf('[data-terp="meter-bar"]::-webkit-meter-bar'), "both tracks agree").toBe(
|
|
232
|
+
track,
|
|
233
|
+
);
|
|
234
|
+
const fills = new Set(
|
|
235
|
+
rules()
|
|
236
|
+
.filter(([selectors]) => selectors.some((s) => VALUE_PSEUDOS.some((p) => s.endsWith(p))))
|
|
237
|
+
.map(([, body]) => /background:\s*var\((--[a-z0-9-]+)\)/.exec(body)?.[1]),
|
|
238
|
+
);
|
|
239
|
+
expect(fills.size).toBeGreaterThan(1);
|
|
240
|
+
for (const fill of fills) {
|
|
241
|
+
expect(
|
|
242
|
+
tokenPairs.nonTextPairs.some((pair) => pair.fg === fill && pair.bg === track),
|
|
243
|
+
`${fill} on ${track} is not a declared non-text pairing`,
|
|
244
|
+
).toBe(true);
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
class CatchBoundary extends Component<{ children: ReactNode }, { message: string | null }> {
|
|
250
|
+
state = { message: null };
|
|
251
|
+
static getDerivedStateFromError(error: Error) {
|
|
252
|
+
return { message: error.message };
|
|
253
|
+
}
|
|
254
|
+
render() {
|
|
255
|
+
return this.state.message === null ? (
|
|
256
|
+
this.props.children
|
|
257
|
+
) : (
|
|
258
|
+
<p data-testid="refused">{this.state.message}</p>
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Mount under the standard contract and let the archetype's slot check run once. */
|
|
264
|
+
async function underContract(node: ReactNode) {
|
|
265
|
+
render(
|
|
266
|
+
<CatchBoundary>
|
|
267
|
+
<LayoutContractContext.Provider value="standard">{node}</LayoutContractContext.Provider>
|
|
268
|
+
</CatchBoundary>,
|
|
269
|
+
);
|
|
270
|
+
// The slot check runs a macrotask after mount; flushing one inside act is what makes an
|
|
271
|
+
// acceptance assertion wait for it rather than pass before it has run.
|
|
272
|
+
await act(async () => {
|
|
273
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
describe("where a Meter sits on a governed page", () => {
|
|
278
|
+
it("is a value inside a section, and a section it is not", async () => {
|
|
279
|
+
// Inside a section it is composition, which the contract never governs.
|
|
280
|
+
await underContract(
|
|
281
|
+
<DetailPage title="Project" parents={[{ label: "Projects", to: "/projects" }]}>
|
|
282
|
+
<Card title="Usage">
|
|
283
|
+
<DetailList
|
|
284
|
+
items={[{ label: "Storage", value: <Meter label="Storage" value={0.62} /> }]}
|
|
285
|
+
/>
|
|
286
|
+
</Card>
|
|
287
|
+
</DetailPage>,
|
|
288
|
+
);
|
|
289
|
+
expect(screen.queryByTestId("refused")).toBeNull();
|
|
290
|
+
cleanup();
|
|
291
|
+
|
|
292
|
+
// Loose in the body it is refused. The label is an accessible name and not printed, so a
|
|
293
|
+
// bare meter there is a bar with no visible caption at all — the section around it is
|
|
294
|
+
// what supplies one.
|
|
295
|
+
await underContract(
|
|
296
|
+
<DetailPage title="Project" parents={[{ label: "Projects", to: "/projects" }]}>
|
|
297
|
+
<Meter label="Storage" value={0.62} />
|
|
298
|
+
</DetailPage>,
|
|
299
|
+
);
|
|
300
|
+
expect(screen.getByTestId("refused").textContent).toBe(
|
|
301
|
+
slotViolationMessage("standard", "DetailPage", '<span data-terp="meter">'),
|
|
302
|
+
);
|
|
303
|
+
});
|
|
304
|
+
});
|
package/src/Meter.tsx
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { useFormatNumber } from "./format";
|
|
2
|
+
import { injectTerpStyles } from "./styles";
|
|
3
|
+
import { useUiText } from "./uiText";
|
|
4
|
+
import type { UiText } from "./uiText";
|
|
5
|
+
|
|
6
|
+
injectTerpStyles();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Which part of its range a banded meter's value sits in, in the HTML standard's own words.
|
|
10
|
+
*
|
|
11
|
+
* `optimum` is where the caller said good values lie, `suboptimum` is next to it, and
|
|
12
|
+
* `even-less-good` is the far side — the words the standard uses for the three regions that
|
|
13
|
+
* `low`, `high` and `optimum` define, and the words WebKit and Blink name their value
|
|
14
|
+
* pseudo-elements after.
|
|
15
|
+
*/
|
|
16
|
+
type MeterRegion = "optimum" | "suboptimum" | "even-less-good";
|
|
17
|
+
|
|
18
|
+
export interface MeterProps {
|
|
19
|
+
/**
|
|
20
|
+
* The measured value.
|
|
21
|
+
*
|
|
22
|
+
* Drawn clamped to the range, as the element itself draws it, and printed as it is: a quota
|
|
23
|
+
* overrun prints `120%` against a full bar, because the text is where a reader learns the
|
|
24
|
+
* bar has run out of room.
|
|
25
|
+
*/
|
|
26
|
+
value: number;
|
|
27
|
+
/** The bottom of the range (default `0`). */
|
|
28
|
+
min?: number;
|
|
29
|
+
/** The top of the range (default `1`, the element's own default — so a bare value is a fraction). */
|
|
30
|
+
max?: number;
|
|
31
|
+
/** The top of the low band. Declaring any band colours the bar by the region its value falls in. */
|
|
32
|
+
low?: number;
|
|
33
|
+
/** The bottom of the high band. */
|
|
34
|
+
high?: number;
|
|
35
|
+
/** Where the good values lie: below `low`, above `high`, or between them. */
|
|
36
|
+
optimum?: number;
|
|
37
|
+
/**
|
|
38
|
+
* The meter's accessible name. Required — an unnamed meter is announced as a number with
|
|
39
|
+
* nothing to say what it measures.
|
|
40
|
+
*
|
|
41
|
+
* It is not printed. Every place a quantity sits already carries a visible caption of its own
|
|
42
|
+
* — a `DetailList` term, a `Card` title, a `HubCard` title — and printing the label as well
|
|
43
|
+
* puts the same word twice on one line. Name the meter with the words that caption uses.
|
|
44
|
+
*/
|
|
45
|
+
label: UiText;
|
|
46
|
+
/**
|
|
47
|
+
* How the printed value is formatted, as the `Intl.NumberFormatOptions` the `format`
|
|
48
|
+
* helpers take (default: a percentage).
|
|
49
|
+
*
|
|
50
|
+
* A percentage is always the value's **share of the range**, so `value={74} max={100}`
|
|
51
|
+
* prints `74%` where the bare Intl option would print `7,400%`. Every other style prints the
|
|
52
|
+
* value itself: `{ style: "unit", unit: "gigabyte" }` for a quota, `{}` for a plain score.
|
|
53
|
+
* The locale is the app's, never the browser's.
|
|
54
|
+
*/
|
|
55
|
+
format?: Intl.NumberFormatOptions;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The default format, hoisted so the formatter cache sees one options object rather than one per render. */
|
|
59
|
+
const PERCENT: Intl.NumberFormatOptions = { style: "percent" };
|
|
60
|
+
|
|
61
|
+
const clamp = (value: number, floor: number, ceiling: number) =>
|
|
62
|
+
Math.min(Math.max(value, floor), ceiling);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The region a banded value sits in — the HTML standard's gauge-region algorithm, written out.
|
|
66
|
+
*
|
|
67
|
+
* Written out rather than read from the browser, because the browser does not say: the element
|
|
68
|
+
* has no DOM property for its region, and a stylesheet can only see it through a
|
|
69
|
+
* different vendor hook per engine, so a rule keyed on the attribute this returns is the one
|
|
70
|
+
* way a single sheet can colour every engine alike. The boundaries are inclusive exactly where
|
|
71
|
+
* Chromium's are: measured against the region Chromium itself paints, for every shape the
|
|
72
|
+
* standard distinguishes — an optimum below, between and above the bands, each boundary
|
|
73
|
+
* value, bands outside the range, a `high` below `low` on either side of the optimum, a range
|
|
74
|
+
* that is not 0 to 1 — with no disagreement. `Meter.test.tsx` holds this function to cases
|
|
75
|
+
* taken from that measurement.
|
|
76
|
+
*/
|
|
77
|
+
function meterRegion({
|
|
78
|
+
value,
|
|
79
|
+
min = 0,
|
|
80
|
+
max = 1,
|
|
81
|
+
low,
|
|
82
|
+
high,
|
|
83
|
+
optimum,
|
|
84
|
+
}: Pick<MeterProps, "value" | "min" | "max" | "low" | "high" | "optimum">): MeterRegion {
|
|
85
|
+
// The standard's clamping, in its own order: a max below the min becomes the min, the value
|
|
86
|
+
// and each boundary are pulled inside the range, and a high below the low becomes the low.
|
|
87
|
+
const top = Math.max(max, min);
|
|
88
|
+
const at = clamp(value, min, top);
|
|
89
|
+
const lowEdge = clamp(low ?? min, min, top);
|
|
90
|
+
const highEdge = clamp(high ?? top, lowEdge, top);
|
|
91
|
+
const best = clamp(optimum ?? (min + top) / 2, min, top);
|
|
92
|
+
if (best < lowEdge) {
|
|
93
|
+
return at <= lowEdge ? "optimum" : at <= highEdge ? "suboptimum" : "even-less-good";
|
|
94
|
+
}
|
|
95
|
+
if (best > highEdge) {
|
|
96
|
+
return at >= highEdge ? "optimum" : at >= lowEdge ? "suboptimum" : "even-less-good";
|
|
97
|
+
}
|
|
98
|
+
return at >= lowEdge && at <= highEdge ? "optimum" : "suboptimum";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* One bounded value — a quota used, a score against its range — as a bar with the value
|
|
103
|
+
* printed beside it.
|
|
104
|
+
*
|
|
105
|
+
* The bar IS the native `<meter>`, styled from the sheet: the element carries its own
|
|
106
|
+
* semantics, the browser draws the proportion from its attributes, and so there is no inline
|
|
107
|
+
* width and no second element pretending to be the first. Its fill is `--color-fg-accent` on a
|
|
108
|
+
* `--color-bg-inset` track — the accent that is ink on the app's own surfaces, not the brand
|
|
109
|
+
* fill, which falls under 3:1 against the track in all three dark themes.
|
|
110
|
+
*
|
|
111
|
+
* **Bands are opt-in.** With none declared the bar is one colour, because "a value in its range"
|
|
112
|
+
* is not a judgement. Declare `low`, `high` or `optimum` and the fill takes the region's tone —
|
|
113
|
+
* success, warning, danger. The colour reinforces the printed value and never replaces it: if
|
|
114
|
+
* the judgement itself matters, say it in words beside the meter (a `Badge`), because the
|
|
115
|
+
* region is not in the accessibility tree — measured in Chromium, whose tree carries the
|
|
116
|
+
* value and the range and nothing about the bands.
|
|
117
|
+
*
|
|
118
|
+
* **One accessible element.** The printed value is `aria-hidden` and handed to the meter as its
|
|
119
|
+
* `aria-valuetext`, so a screen reader hears the same `62%` a sighted reader sees, once — and a
|
|
120
|
+
* meter inside a `HubCard` stat adds that text to the card link's name rather than the raw
|
|
121
|
+
* `0.62`. Measured in Chromium: with the printed value left in the tree the link read its value
|
|
122
|
+
* twice.
|
|
123
|
+
*
|
|
124
|
+
* ```tsx
|
|
125
|
+
* <DetailList items={[{ label: storage, value: <Meter label={storage} value={used} max={quota} /> }]} />
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export function Meter({ value, min = 0, max = 1, low, high, optimum, label, format }: MeterProps) {
|
|
129
|
+
const resolve = useUiText();
|
|
130
|
+
const formatNumber = useFormatNumber();
|
|
131
|
+
const options = format ?? PERCENT;
|
|
132
|
+
const top = Math.max(max, min);
|
|
133
|
+
// A range with no width has no share, and the browser paints no fill for it (measured):
|
|
134
|
+
// NaN prints the framework's dash, where the division would print an infinity.
|
|
135
|
+
const share = top > min ? (value - min) / (top - min) : Number.NaN;
|
|
136
|
+
const text = formatNumber(options.style === "percent" ? share : value, options);
|
|
137
|
+
const banded = low !== undefined || high !== undefined || optimum !== undefined;
|
|
138
|
+
return (
|
|
139
|
+
<span data-terp="meter">
|
|
140
|
+
<meter
|
|
141
|
+
data-terp="meter-bar"
|
|
142
|
+
data-region={
|
|
143
|
+
banded ? meterRegion({ value, min, max, low, high, optimum }) : undefined
|
|
144
|
+
}
|
|
145
|
+
value={value}
|
|
146
|
+
min={min}
|
|
147
|
+
max={max}
|
|
148
|
+
low={low}
|
|
149
|
+
high={high}
|
|
150
|
+
optimum={optimum}
|
|
151
|
+
aria-label={resolve(label)}
|
|
152
|
+
aria-valuetext={text}
|
|
153
|
+
/>
|
|
154
|
+
<span data-terp="meter-value" aria-hidden="true">
|
|
155
|
+
{text}
|
|
156
|
+
</span>
|
|
157
|
+
</span>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { QuietActions } from "./QuietActions";
|
|
6
|
+
import { TERP_STYLES_CSS } from "./styles";
|
|
7
|
+
import { Button } from "./ui/Button";
|
|
8
|
+
import { Code } from "./typography";
|
|
9
|
+
|
|
10
|
+
afterEach(cleanup);
|
|
11
|
+
|
|
12
|
+
/** The rules inside the sheet's one hover-capability query. */
|
|
13
|
+
function hoverBlock(): string {
|
|
14
|
+
const opener = "@media (hover: hover) {";
|
|
15
|
+
const at = TERP_STYLES_CSS.indexOf(opener);
|
|
16
|
+
if (at === -1) throw new Error("the sheet declares no hover-capability query");
|
|
17
|
+
const first = TERP_STYLES_CSS.indexOf("{", at);
|
|
18
|
+
let depth = 0;
|
|
19
|
+
for (let i = first; i < TERP_STYLES_CSS.length; i += 1) {
|
|
20
|
+
if (TERP_STYLES_CSS[i] === "{") depth += 1;
|
|
21
|
+
else if (TERP_STYLES_CSS[i] === "}") {
|
|
22
|
+
depth -= 1;
|
|
23
|
+
if (depth === 0) return TERP_STYLES_CSS.slice(first + 1, i);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
throw new Error("the hover-capability query is not brace-balanced");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("QuietActions", () => {
|
|
30
|
+
it("renders the value and the action, with the action in its own slot", () => {
|
|
31
|
+
render(
|
|
32
|
+
<QuietActions actions={<Button aria-label="Copy the fingerprint" />}>
|
|
33
|
+
<Code>9f2c1b7a</Code>
|
|
34
|
+
</QuietActions>,
|
|
35
|
+
);
|
|
36
|
+
const row = document.querySelector('[data-terp="quiet-actions"]')!;
|
|
37
|
+
expect(row.textContent).toContain("9f2c1b7a");
|
|
38
|
+
const slot = row.querySelector('[data-terp="quiet-actions-slot"]')!;
|
|
39
|
+
expect(
|
|
40
|
+
slot.querySelector("button"),
|
|
41
|
+
"the action goes in the slot, which is the box that fades",
|
|
42
|
+
).toBe(screen.getByRole("button", { name: "Copy the fingerprint" }));
|
|
43
|
+
// The value is NOT in the slot, or it would fade with the action.
|
|
44
|
+
expect(slot.textContent).not.toContain("9f2c1b7a");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("leaves the action in the accessibility tree at rest", () => {
|
|
48
|
+
// The property that separates this from hiding something. Opacity is not `visibility`
|
|
49
|
+
// and not `display`, so the action is announced, named and operable at every moment --
|
|
50
|
+
// what changes is only whether a sighted pointer user has to look at it. Asserted
|
|
51
|
+
// through the accessible-name query rather than by reading a style, because that is
|
|
52
|
+
// what a screen reader and a voice-control user actually resolve.
|
|
53
|
+
render(
|
|
54
|
+
<QuietActions actions={<Button aria-label="Copy the fingerprint" />}>
|
|
55
|
+
<Code>9f2c1b7a</Code>
|
|
56
|
+
</QuietActions>,
|
|
57
|
+
);
|
|
58
|
+
const action = screen.getByRole("button", { name: "Copy the fingerprint" });
|
|
59
|
+
expect(action).toBeInTheDocument();
|
|
60
|
+
expect(action, "never hidden from assistive tech").not.toHaveAttribute("aria-hidden");
|
|
61
|
+
expect(action, "and never taken out of the tab order").not.toHaveAttribute("tabindex", "-1");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("fades only where hovering exists, and reveals on focus as well", () => {
|
|
65
|
+
// Three declarations, one mechanism, and each one closes a different way this pattern is
|
|
66
|
+
// got wrong. jsdom resolves no media queries, so these are read out of the sheet rather
|
|
67
|
+
// than off the element -- the alternative is a browser, which the workbench lane is.
|
|
68
|
+
const hover = hoverBlock().replace(/\s+/g, " ");
|
|
69
|
+
|
|
70
|
+
// 1. The fade is BEHIND the capability query. Unconditional, a touch screen -- which has
|
|
71
|
+
// no hover to reveal anything with -- would get an action that is permanently
|
|
72
|
+
// invisible and permanently tappable.
|
|
73
|
+
expect(hover, "the resting state is inside the hover query").toContain(
|
|
74
|
+
'[data-terp="quiet-actions"] [data-terp="quiet-actions-slot"] { opacity: 0; }',
|
|
75
|
+
);
|
|
76
|
+
const outside = TERP_STYLES_CSS.replace(hoverBlock(), "").replace(/\s+/g, " ");
|
|
77
|
+
expect(
|
|
78
|
+
outside,
|
|
79
|
+
"and nowhere else, or a touch device gets an invisible control",
|
|
80
|
+
).not.toContain('[data-terp="quiet-actions-slot"] { opacity: 0');
|
|
81
|
+
|
|
82
|
+
// 2. Focus reveals it too. Opacity does not remove an element from the tab order, so
|
|
83
|
+
// without this a keyboard user tabs to a control they cannot see.
|
|
84
|
+
expect(hover, "hover reveals it").toContain('[data-terp="quiet-actions"]:hover');
|
|
85
|
+
expect(hover, "and so does focus, or a keyboard lands somewhere invisible").toContain(
|
|
86
|
+
'[data-terp="quiet-actions"]:focus-within',
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// 3. The slot's box is declared OUTSIDE the query, so the space is reserved in every
|
|
90
|
+
// pointer mode. Inside it, the row would reflow under the cursor.
|
|
91
|
+
expect(outside, "the slot reserves its space unconditionally").toContain(
|
|
92
|
+
'[data-terp="quiet-actions-slot"] {',
|
|
93
|
+
);
|
|
94
|
+
expect(hover, "the fade changes opacity only, never layout").not.toContain("display:");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("stamps a gap the sheet has a rule for", () => {
|
|
98
|
+
render(
|
|
99
|
+
<QuietActions gap={3} actions={<Button aria-label="Copy" />}>
|
|
100
|
+
<Code>abc</Code>
|
|
101
|
+
</QuietActions>,
|
|
102
|
+
);
|
|
103
|
+
expect(document.querySelector('[data-terp="quiet-actions"]')!.getAttribute("data-gap")).toBe(
|
|
104
|
+
"3",
|
|
105
|
+
);
|
|
106
|
+
expect(TERP_STYLES_CSS).toContain('[data-terp="quiet-actions"][data-gap="3"]');
|
|
107
|
+
// The default stamps nothing, so the base rule stands rather than being restated.
|
|
108
|
+
cleanup();
|
|
109
|
+
render(
|
|
110
|
+
<QuietActions actions={<Button aria-label="Copy" />}>
|
|
111
|
+
<Code>abc</Code>
|
|
112
|
+
</QuietActions>,
|
|
113
|
+
);
|
|
114
|
+
expect(
|
|
115
|
+
document.querySelector('[data-terp="quiet-actions"]')!.getAttribute("data-gap"),
|
|
116
|
+
).toBeNull();
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { HTMLAttributes, ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import type { SpaceToken } from "./layout";
|
|
4
|
+
import { injectTerpStyles } from "./styles";
|
|
5
|
+
|
|
6
|
+
injectTerpStyles();
|
|
7
|
+
|
|
8
|
+
export interface QuietActionsProps extends Omit<HTMLAttributes<HTMLDivElement>, "style"> {
|
|
9
|
+
/**
|
|
10
|
+
* The action, or actions, that stay quiet until someone reaches for them.
|
|
11
|
+
*
|
|
12
|
+
* A slot rather than a convention about which children are actions, and it is the same
|
|
13
|
+
* slot `Card` publishes, for the same reason: "every Button inside" would be a rule a
|
|
14
|
+
* reader has to know before they can predict what a row will do, and it would capture a
|
|
15
|
+
* button that happened to be part of the value.
|
|
16
|
+
*/
|
|
17
|
+
actions: ReactNode;
|
|
18
|
+
/** Distance between the value and its actions, as a step on the token spacing scale (default `1`). */
|
|
19
|
+
gap?: SpaceToken;
|
|
20
|
+
/** The value the actions belong to. */
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A value with an action attached, where the action is quiet until someone reaches for it.
|
|
26
|
+
*
|
|
27
|
+
* The case it was written for is a row of identifiers. A revision card carries five copyable
|
|
28
|
+
* digests, so it also carried five copy buttons, and the affordance was then repeated more
|
|
29
|
+
* often than the values it applied to -- five controls competing with five twelve-character
|
|
30
|
+
* chips that are themselves the thing a reader came to look at. Below the viewport cutover
|
|
31
|
+
* each of those buttons took a line of its own. The same shape turns up wherever a list row
|
|
32
|
+
* owns an action: a remove, an open, a pin.
|
|
33
|
+
*
|
|
34
|
+
* **The space is reserved, not collapsed.** The action fades rather than appearing, so
|
|
35
|
+
* nothing on the row moves when a pointer arrives -- a value that reflows under the cursor is
|
|
36
|
+
* a value you cannot click, and a row of them makes a card twitch as the mouse crosses it.
|
|
37
|
+
*
|
|
38
|
+
* Three things make it safe rather than merely quiet, and each closes a way this pattern is
|
|
39
|
+
* usually got wrong:
|
|
40
|
+
*
|
|
41
|
+
* - **It reveals on focus as well as hover**, through `:focus-within` on the row. Opacity does
|
|
42
|
+
* not remove an element from the tab order -- which is correct, the action must stay
|
|
43
|
+
* reachable -- so without this a keyboard user would tab to a control they cannot see.
|
|
44
|
+
* - **It only hides where hovering is possible at all.** On a touch screen there is no hover
|
|
45
|
+
* to reveal anything, so the action would be permanently invisible and permanently
|
|
46
|
+
* tappable. The rule sits behind `@media (hover: hover)` and a touch device simply gets a
|
|
47
|
+
* visible action.
|
|
48
|
+
* - **It is invisible to the accessibility tree either way.** Opacity is not `visibility`, so
|
|
49
|
+
* the action is announced, named and operable at every moment; what changes is only whether
|
|
50
|
+
* a sighted pointer user has to look at it.
|
|
51
|
+
*
|
|
52
|
+
* ```tsx
|
|
53
|
+
* <QuietActions
|
|
54
|
+
* actions={
|
|
55
|
+
* <Button variant="ghost" size="sm" icon={<Icon name="clipboard" />} aria-label={copyThis} />
|
|
56
|
+
* }
|
|
57
|
+
* >
|
|
58
|
+
* <Code>{shortDigest(value)}</Code>
|
|
59
|
+
* </QuietActions>
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function QuietActions({ actions, gap, children, ...rest }: QuietActionsProps) {
|
|
63
|
+
return (
|
|
64
|
+
<div
|
|
65
|
+
{...rest}
|
|
66
|
+
data-terp="quiet-actions"
|
|
67
|
+
// The gap roll-call's idiom: an unset gap stamps nothing and leaves the base rule
|
|
68
|
+
// standing rather than restating the default in the DOM.
|
|
69
|
+
data-gap={gap === undefined ? undefined : String(gap)}
|
|
70
|
+
>
|
|
71
|
+
{children}
|
|
72
|
+
<div data-terp="quiet-actions-slot">{actions}</div>
|
|
73
|
+
</div>
|
|
74
|
+
);
|
|
75
|
+
}
|