strata-react 1.0.38 → 1.0.40
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 -4
- package/UPGRADING.md +5 -0
- package/dist/components/button.d.ts +3 -0
- package/dist/components/checkbox.d.ts +3 -2
- package/dist/components/secret-field.d.ts +34 -0
- package/dist/components/selection.d.ts +45 -1
- package/dist/components/transcript-view.d.ts +18 -2
- package/dist/components/tree.d.ts +16 -6
- package/dist/components/value-controls.d.ts +20 -4
- package/dist/{query-CJ1pSPiC.js → errors-BunUBqpA.js} +5 -176
- package/dist/errors-BunUBqpA.js.map +1 -0
- package/dist/{field-7Z4N5r5g.js → field-DeKNvGox.js} +74 -5
- package/dist/field-DeKNvGox.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +735 -327
- package/dist/index.js.map +1 -1
- package/dist/lib/choice.d.ts +29 -0
- package/dist/lib/transcript.d.ts +16 -0
- package/dist/map.js +2 -2
- package/dist/panorama.js +2 -2
- package/dist/query-D163gXXZ.js +178 -0
- package/dist/query-D163gXXZ.js.map +1 -0
- package/dist/{run-DzyLrZlA.js → run-DEf1Vh0x.js} +10 -6
- package/dist/run-DEf1Vh0x.js.map +1 -0
- package/dist/testing.js +2 -2
- package/dist/{theme-CFNi6WUO.js → theme-DVaIxyNn.js} +6 -75
- package/dist/theme-DVaIxyNn.js.map +1 -0
- package/dist/value-controls-FO4zQiHP.js +735 -0
- package/dist/value-controls-FO4zQiHP.js.map +1 -0
- package/dist/visual-cli.js +1 -1
- package/dist/visual.js +1 -1
- package/package.json +1 -1
- package/dist/field-7Z4N5r5g.js.map +0 -1
- package/dist/query-CJ1pSPiC.js.map +0 -1
- package/dist/run-DzyLrZlA.js.map +0 -1
- package/dist/theme-CFNi6WUO.js.map +0 -1
- package/dist/value-controls-D6O9TsM6.js +0 -356
- package/dist/value-controls-D6O9TsM6.js.map +0 -1
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
import { l as KeyText, o as useFieldControl, s as controlHeight, u as cn } from "./field-DeKNvGox.js";
|
|
2
|
+
import { i as format } from "./format-B32-PVi2.js";
|
|
3
|
+
import { i as fieldErrorsOf, n as describeActionError, o as Button$1 } from "./errors-BunUBqpA.js";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import { I18nProvider, Input, NumberField, Separator, Slider, SliderOutput, SliderThumb, SliderTrack, Toolbar } from "react-aria-components";
|
|
6
|
+
import { Check, CircleAlert, LoaderCircle } from "lucide-react";
|
|
7
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
// src/lib/in-view.ts
|
|
9
|
+
/**
|
|
10
|
+
* Whether an element is within `rootMargin` of its scroll region (the nearest
|
|
11
|
+
* `[data-strata-scroll]` ancestor, else the viewport). Returns a callback ref, so it follows the
|
|
12
|
+
* element as it mounts and unmounts.
|
|
13
|
+
*/
|
|
14
|
+
function useInView({ active = true, rootMargin = "0px", once = false, unsupported = false } = {}) {
|
|
15
|
+
const [inView, setInView] = React.useState(false);
|
|
16
|
+
const [el, setEl] = React.useState(null);
|
|
17
|
+
const seen = once && inView;
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
if (seen) return;
|
|
20
|
+
if (!active || !el) {
|
|
21
|
+
setInView(false);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
25
|
+
setInView(unsupported);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const root = el.parentElement?.closest("[data-strata-scroll]") ?? null;
|
|
29
|
+
const observer = new IntersectionObserver((entries) => setInView(entries[entries.length - 1].isIntersecting), {
|
|
30
|
+
root,
|
|
31
|
+
rootMargin
|
|
32
|
+
});
|
|
33
|
+
observer.observe(el);
|
|
34
|
+
return () => observer.disconnect();
|
|
35
|
+
}, [
|
|
36
|
+
active,
|
|
37
|
+
el,
|
|
38
|
+
rootMargin,
|
|
39
|
+
seen,
|
|
40
|
+
unsupported
|
|
41
|
+
]);
|
|
42
|
+
return {
|
|
43
|
+
ref: setEl,
|
|
44
|
+
inView
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// src/components/hotkeys.tsx
|
|
48
|
+
var TEXT_INPUTS_EXEMPT = /* @__PURE__ */ new Set([
|
|
49
|
+
"checkbox",
|
|
50
|
+
"radio",
|
|
51
|
+
"button",
|
|
52
|
+
"submit",
|
|
53
|
+
"reset",
|
|
54
|
+
"range",
|
|
55
|
+
"color",
|
|
56
|
+
"file"
|
|
57
|
+
]);
|
|
58
|
+
/** True when a key pressed on this element is the person typing, so single-key shortcuts must leave it alone. */
|
|
59
|
+
function isTypingTarget(target) {
|
|
60
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
61
|
+
if (target.isContentEditable || target.closest("[contenteditable=''],[contenteditable='true']")) return true;
|
|
62
|
+
if (target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement) return true;
|
|
63
|
+
if (target instanceof HTMLInputElement) return !TEXT_INPUTS_EXEMPT.has(target.type);
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
function toShortcuts(input) {
|
|
67
|
+
return Array.isArray(input) ? input : Object.entries(input).map(([key, run]) => ({
|
|
68
|
+
keys: [key],
|
|
69
|
+
label: key,
|
|
70
|
+
run
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Binds single-key shortcuts on the window, ignoring keys typed into fields, chords with Ctrl, Meta
|
|
75
|
+
* or Alt, IME composition and (unless `repeat`) held keys. Pass the same list to `KeysLegend`.
|
|
76
|
+
*/
|
|
77
|
+
function useHotkeys(shortcuts, { enabled = true } = {}) {
|
|
78
|
+
const latest = React.useRef([]);
|
|
79
|
+
latest.current = toShortcuts(shortcuts);
|
|
80
|
+
React.useEffect(() => {
|
|
81
|
+
if (!enabled) return;
|
|
82
|
+
const onKeyDown = (e) => {
|
|
83
|
+
if (e.defaultPrevented || e.isComposing || e.ctrlKey || e.metaKey || e.altKey) return;
|
|
84
|
+
if (isTypingTarget(e.target)) return;
|
|
85
|
+
const hit = latest.current.find((s) => !s.disabled && s.keys.includes(e.key));
|
|
86
|
+
if (!hit || e.repeat && !hit.repeat) return;
|
|
87
|
+
e.preventDefault();
|
|
88
|
+
hit.run();
|
|
89
|
+
};
|
|
90
|
+
window.addEventListener("keydown", onKeyDown);
|
|
91
|
+
return () => window.removeEventListener("keydown", onKeyDown);
|
|
92
|
+
}, [enabled]);
|
|
93
|
+
}
|
|
94
|
+
var KEY_NAMES = {
|
|
95
|
+
ArrowDown: "↓",
|
|
96
|
+
ArrowUp: "↑",
|
|
97
|
+
ArrowLeft: "←",
|
|
98
|
+
ArrowRight: "→",
|
|
99
|
+
Escape: "Esc",
|
|
100
|
+
Enter: "Enter",
|
|
101
|
+
Backspace: "Backspace",
|
|
102
|
+
Delete: "Delete",
|
|
103
|
+
" ": "Space"
|
|
104
|
+
};
|
|
105
|
+
/** A quiet legend of the screen's shortcuts, from the same list `useHotkeys` binds. Hidden on touch screens. */
|
|
106
|
+
function KeysLegend({ shortcuts, label = "Keyboard shortcuts", className }) {
|
|
107
|
+
return /* @__PURE__ */ jsx("dl", {
|
|
108
|
+
"aria-label": label,
|
|
109
|
+
className: cn("flex flex-wrap gap-x-4 gap-y-1 type-caption text-muted-foreground pointer-coarse:hidden", className),
|
|
110
|
+
children: shortcuts.map((s) => /* @__PURE__ */ jsxs("div", {
|
|
111
|
+
className: cn("flex items-center gap-1.5", s.disabled && "opacity-50"),
|
|
112
|
+
children: [/* @__PURE__ */ jsx("dt", {
|
|
113
|
+
className: "flex items-center gap-1",
|
|
114
|
+
children: s.keys.map((k, i) => /* @__PURE__ */ jsxs(React.Fragment, { children: [i > 0 && /* @__PURE__ */ jsx("span", {
|
|
115
|
+
className: "sr-only",
|
|
116
|
+
children: " or "
|
|
117
|
+
}), /* @__PURE__ */ jsx("kbd", {
|
|
118
|
+
className: "min-w-5 rounded-control border border-border bg-surface-sunken px-1 text-center font-mono text-foreground",
|
|
119
|
+
children: /* @__PURE__ */ jsx(KeyText, { children: KEY_NAMES[k] ?? k })
|
|
120
|
+
})] }, k))
|
|
121
|
+
}), /* @__PURE__ */ jsx("dd", { children: s.label })]
|
|
122
|
+
}, s.keys.join(" ")))
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// src/components/toolbar.tsx
|
|
126
|
+
/** A group of controls with one tab stop and arrow-key movement (the WAI-ARIA toolbar pattern). Needs a `label`. */
|
|
127
|
+
function Toolbar$1({ label, orientation = "horizontal", children, className }) {
|
|
128
|
+
const jump = (e) => {
|
|
129
|
+
if (e.key !== "Home" && e.key !== "End" || isTypingTarget(e.target) || !e.currentTarget.contains(e.target)) return;
|
|
130
|
+
const items = [...e.currentTarget.querySelectorAll("button, input, select, textarea, a[href], [tabindex]")].filter((el) => !el.hasAttribute("disabled") && el.getAttribute("aria-disabled") !== "true" && el.tabIndex >= -1 && el.getAttribute("role") !== "separator");
|
|
131
|
+
const target = e.key === "Home" ? items[0] : items[items.length - 1];
|
|
132
|
+
if (!target) return;
|
|
133
|
+
e.preventDefault();
|
|
134
|
+
e.stopPropagation();
|
|
135
|
+
target.focus();
|
|
136
|
+
};
|
|
137
|
+
return /* @__PURE__ */ jsx("div", {
|
|
138
|
+
className: "contents",
|
|
139
|
+
onKeyDownCapture: jump,
|
|
140
|
+
children: /* @__PURE__ */ jsx(Toolbar, {
|
|
141
|
+
"aria-label": label,
|
|
142
|
+
orientation,
|
|
143
|
+
className: cn("flex flex-wrap items-center gap-1", orientation === "vertical" && "flex-col items-stretch", className),
|
|
144
|
+
children
|
|
145
|
+
})
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** A hairline between groups of controls inside a `Toolbar`. */
|
|
149
|
+
function ToolbarSeparator({ className }) {
|
|
150
|
+
return /* @__PURE__ */ jsx(Separator, { className: cn("mx-1 h-5 w-px self-center border-0 bg-border", className) });
|
|
151
|
+
}
|
|
152
|
+
// src/components/async-button.tsx
|
|
153
|
+
/** One button through idle, pending, done and error, with the label changing in place and its width fixed by the longest label. The outcome is announced politely, with the error's own words when `error` is given. */
|
|
154
|
+
function AsyncButton({ phase, labels, error, disabled, ...props }) {
|
|
155
|
+
const errorLabel = labels.error ?? "Try again";
|
|
156
|
+
const current = {
|
|
157
|
+
idle: labels.idle,
|
|
158
|
+
pending: labels.pending,
|
|
159
|
+
done: labels.done,
|
|
160
|
+
error: errorLabel
|
|
161
|
+
}[phase];
|
|
162
|
+
const Icon = {
|
|
163
|
+
idle: null,
|
|
164
|
+
pending: LoaderCircle,
|
|
165
|
+
done: Check,
|
|
166
|
+
error: CircleAlert
|
|
167
|
+
}[phase];
|
|
168
|
+
const announcement = phase === "done" ? labels.done : phase === "error" ? error ? `${error} ${errorLabel}` : errorLabel : "";
|
|
169
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(Button$1, {
|
|
170
|
+
...props,
|
|
171
|
+
"data-phase": phase,
|
|
172
|
+
disabled: disabled || phase === "pending",
|
|
173
|
+
"aria-disabled": phase === "pending" || void 0,
|
|
174
|
+
children: [Icon && /* @__PURE__ */ jsx(Icon, {
|
|
175
|
+
"aria-hidden": true,
|
|
176
|
+
className: phase === "pending" ? "motion-safe:animate-spin" : void 0
|
|
177
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
178
|
+
className: "grid",
|
|
179
|
+
children: [
|
|
180
|
+
labels.idle,
|
|
181
|
+
labels.pending,
|
|
182
|
+
labels.done,
|
|
183
|
+
errorLabel
|
|
184
|
+
].map((l) => /* @__PURE__ */ jsx("span", {
|
|
185
|
+
"aria-hidden": l !== current,
|
|
186
|
+
className: l === current ? "[grid-area:1/1]" : "invisible [grid-area:1/1]",
|
|
187
|
+
children: l
|
|
188
|
+
}, l))
|
|
189
|
+
})]
|
|
190
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
191
|
+
role: "status",
|
|
192
|
+
className: "sr-only",
|
|
193
|
+
children: announcement
|
|
194
|
+
})] });
|
|
195
|
+
}
|
|
196
|
+
/** Runs an async action and reports its phase, its error and any field messages. `done` returns to `idle` after `settleMs`; `run` never changes identity. */
|
|
197
|
+
function useAsyncAction(action, { settleMs = 2e3, onError, describeError = describeActionError, validate, onInvalid } = {}) {
|
|
198
|
+
const [state, setState] = React.useState(idle);
|
|
199
|
+
const timer = React.useRef(void 0);
|
|
200
|
+
const unreported = React.useRef(void 0);
|
|
201
|
+
const latest = React.useRef({
|
|
202
|
+
action,
|
|
203
|
+
onError,
|
|
204
|
+
settleMs,
|
|
205
|
+
describeError,
|
|
206
|
+
validate,
|
|
207
|
+
onInvalid
|
|
208
|
+
});
|
|
209
|
+
React.useLayoutEffect(() => {
|
|
210
|
+
latest.current = {
|
|
211
|
+
action,
|
|
212
|
+
onError,
|
|
213
|
+
settleMs,
|
|
214
|
+
describeError,
|
|
215
|
+
validate,
|
|
216
|
+
onInvalid
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
React.useEffect(() => () => window.clearTimeout(timer.current), []);
|
|
220
|
+
const run = React.useCallback(async (...args) => {
|
|
221
|
+
window.clearTimeout(timer.current);
|
|
222
|
+
const invalid = messages(latest.current.validate?.(...args));
|
|
223
|
+
if (invalid) {
|
|
224
|
+
unreported.current = { invalid };
|
|
225
|
+
setState({
|
|
226
|
+
...idle,
|
|
227
|
+
fieldErrors: invalid
|
|
228
|
+
});
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
setState({
|
|
232
|
+
...idle,
|
|
233
|
+
phase: "pending"
|
|
234
|
+
});
|
|
235
|
+
try {
|
|
236
|
+
await latest.current.action(...args);
|
|
237
|
+
setState({
|
|
238
|
+
...idle,
|
|
239
|
+
phase: "done"
|
|
240
|
+
});
|
|
241
|
+
timer.current = window.setTimeout(() => setState(idle), latest.current.settleMs);
|
|
242
|
+
} catch (thrown) {
|
|
243
|
+
unreported.current = { thrown };
|
|
244
|
+
setState({
|
|
245
|
+
phase: "error",
|
|
246
|
+
error: latest.current.describeError(thrown),
|
|
247
|
+
thrown,
|
|
248
|
+
fieldErrors: fieldErrorsOf(thrown)
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}, []);
|
|
252
|
+
React.useEffect(() => {
|
|
253
|
+
const note = unreported.current;
|
|
254
|
+
if (!note) return;
|
|
255
|
+
if ("thrown" in note && state.phase === "error") latest.current.onError?.(note.thrown);
|
|
256
|
+
else if ("invalid" in note) latest.current.onInvalid?.(note.invalid);
|
|
257
|
+
else return;
|
|
258
|
+
unreported.current = void 0;
|
|
259
|
+
}, [state]);
|
|
260
|
+
const clearFieldError = React.useCallback((field) => setState((s) => {
|
|
261
|
+
if (field === void 0) return Object.keys(s.fieldErrors).length ? {
|
|
262
|
+
...s,
|
|
263
|
+
fieldErrors: {}
|
|
264
|
+
} : s;
|
|
265
|
+
if (!(field in s.fieldErrors)) return s;
|
|
266
|
+
const { [field]: _gone, ...rest } = s.fieldErrors;
|
|
267
|
+
return {
|
|
268
|
+
...s,
|
|
269
|
+
fieldErrors: rest
|
|
270
|
+
};
|
|
271
|
+
}), []);
|
|
272
|
+
return {
|
|
273
|
+
phase: state.phase,
|
|
274
|
+
run,
|
|
275
|
+
error: state.error,
|
|
276
|
+
thrown: state.thrown,
|
|
277
|
+
fieldErrors: state.fieldErrors,
|
|
278
|
+
clearFieldError
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
var idle = {
|
|
282
|
+
phase: "idle",
|
|
283
|
+
error: void 0,
|
|
284
|
+
thrown: void 0,
|
|
285
|
+
fieldErrors: {}
|
|
286
|
+
};
|
|
287
|
+
/** The messages `validate` returned, without the empty ones; undefined when there are none. */
|
|
288
|
+
function messages(found) {
|
|
289
|
+
if (!found) return void 0;
|
|
290
|
+
const kept = Object.fromEntries(Object.entries(found).filter((e) => Boolean(e[1])));
|
|
291
|
+
return Object.keys(kept).length ? kept : void 0;
|
|
292
|
+
}
|
|
293
|
+
// src/lib/number-input.ts
|
|
294
|
+
/** Reads `#rgb`, `#rrggbb` (with or without the #) as lower-case `#rrggbb`, or null. */
|
|
295
|
+
function parseHex(text) {
|
|
296
|
+
const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(text.trim());
|
|
297
|
+
if (!m) return null;
|
|
298
|
+
const h = m[1].toLowerCase();
|
|
299
|
+
return "#" + (h.length === 3 ? [...h].map((c) => c + c).join("") : h);
|
|
300
|
+
}
|
|
301
|
+
function luminance(hex) {
|
|
302
|
+
const v = [
|
|
303
|
+
1,
|
|
304
|
+
3,
|
|
305
|
+
5
|
|
306
|
+
].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255).map((c) => c <= .03928 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4);
|
|
307
|
+
return .2126 * v[0] + .7152 * v[1] + .0722 * v[2];
|
|
308
|
+
}
|
|
309
|
+
/** WCAG contrast ratio between two `#rrggbb` colours, 1 to 21. */
|
|
310
|
+
function contrastRatio(a, b) {
|
|
311
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
312
|
+
return (hi + .05) / (lo + .05);
|
|
313
|
+
}
|
|
314
|
+
// src/components/value-controls.tsx
|
|
315
|
+
var box = "flex w-full min-w-0 items-center rounded-control border border-input bg-background px-3 type-body text-foreground transition-state disabled:cursor-not-allowed disabled:bg-muted disabled:opacity-70 has-[input:disabled]:cursor-not-allowed has-[input:disabled]:bg-muted has-[input:disabled]:opacity-70 aria-invalid:border-danger has-[input:focus-visible]:outline-2 has-[input:focus-visible]:outline-offset-2 has-[input:focus-visible]:outline-ring";
|
|
316
|
+
/** A number with its unit joined by a non-breaking space; `%` and `°` stay attached. */
|
|
317
|
+
function withUnit(text, unit) {
|
|
318
|
+
if (!unit) return text;
|
|
319
|
+
return unit === "%" || unit === "°" ? text + unit : text + "\xA0" + unit;
|
|
320
|
+
}
|
|
321
|
+
var intlUnits = {
|
|
322
|
+
"%": "percent",
|
|
323
|
+
"°": "degree",
|
|
324
|
+
ms: "millisecond",
|
|
325
|
+
s: "second",
|
|
326
|
+
kB: "kilobyte",
|
|
327
|
+
MB: "megabyte",
|
|
328
|
+
GB: "gigabyte",
|
|
329
|
+
kbps: "kilobit-per-second",
|
|
330
|
+
Mbps: "megabit-per-second"
|
|
331
|
+
};
|
|
332
|
+
function decimalsOf(n) {
|
|
333
|
+
const s = String(n);
|
|
334
|
+
if (s.includes("e-")) return Number(s.split("e-")[1]) + (s.split("e-")[0].split(".")[1]?.length ?? 0);
|
|
335
|
+
return s.split(".")[1]?.length ?? 0;
|
|
336
|
+
}
|
|
337
|
+
function useNumberText(locale, places) {
|
|
338
|
+
return React.useMemo(() => {
|
|
339
|
+
const f = new Intl.NumberFormat(locale, {
|
|
340
|
+
maximumFractionDigits: places,
|
|
341
|
+
minimumFractionDigits: 0
|
|
342
|
+
});
|
|
343
|
+
return (n) => f.format(n);
|
|
344
|
+
}, [locale, places]);
|
|
345
|
+
}
|
|
346
|
+
function formatOptionsFor(places, unit) {
|
|
347
|
+
const base = {
|
|
348
|
+
maximumFractionDigits: places,
|
|
349
|
+
minimumFractionDigits: 0
|
|
350
|
+
};
|
|
351
|
+
const known = unit ? intlUnits[unit] : void 0;
|
|
352
|
+
return known ? {
|
|
353
|
+
...base,
|
|
354
|
+
style: "unit",
|
|
355
|
+
unit: known,
|
|
356
|
+
unitDisplay: "narrow"
|
|
357
|
+
} : base;
|
|
358
|
+
}
|
|
359
|
+
function describeRange(min, max, show, unit) {
|
|
360
|
+
const a = min === void 0 ? "" : withUnit(show(min), unit);
|
|
361
|
+
const b = max === void 0 ? "" : withUnit(show(max), unit);
|
|
362
|
+
if (min !== void 0 && max !== void 0) return `Enter a value from ${a} to ${b}.`;
|
|
363
|
+
if (min !== void 0) return `Enter ${a} or more.`;
|
|
364
|
+
if (max !== void 0) return `Enter ${b} or less.`;
|
|
365
|
+
return "Enter a number.";
|
|
366
|
+
}
|
|
367
|
+
var REPEAT_AFTER_MS = 400;
|
|
368
|
+
var REPEAT_EVERY_MS = 60;
|
|
369
|
+
var round = (n, places) => Number(n.toFixed(places));
|
|
370
|
+
var signed = (n) => (n < 0 ? "−" : "+") + String(Math.abs(n));
|
|
371
|
+
/** A number with a unit on React Aria's number field: typed input is validated, not clamped, so an out-of-range value is explained in the surrounding `Field` and never passed on; empty stays `null`. `nudge` adds step buttons that repeat when held, `suggestion` a value the app proposes with its reason, and `preview` holds changes back behind Apply and Revert. */
|
|
372
|
+
function NumberField$1({ value, onChange, min, max, step = 1, places, unit, locale = format.locale, required = false, disabled, id: idProp, name, size = "default", nudge, suggestion, preview, applyLabels = {
|
|
373
|
+
idle: "Apply",
|
|
374
|
+
pending: "Applying…",
|
|
375
|
+
done: "Applied"
|
|
376
|
+
}, className, "aria-label": ariaLabel }) {
|
|
377
|
+
const { field, report } = useFieldControl();
|
|
378
|
+
const digits = places ?? Math.max(decimalsOf(step), min === void 0 ? 0 : decimalsOf(min), ...(nudge ?? []).map(decimalsOf));
|
|
379
|
+
const show = useNumberText(locale, digits);
|
|
380
|
+
const [own, setOwn] = React.useState(false);
|
|
381
|
+
const unitId = React.useId();
|
|
382
|
+
const [local, setLocal] = React.useState(value ?? NaN);
|
|
383
|
+
const [seen, setSeen] = React.useState(value);
|
|
384
|
+
const [pending, setPending] = React.useState(void 0);
|
|
385
|
+
if (seen !== value) {
|
|
386
|
+
setSeen(value);
|
|
387
|
+
setLocal(value ?? NaN);
|
|
388
|
+
setPending(void 0);
|
|
389
|
+
}
|
|
390
|
+
const held = preview !== void 0;
|
|
391
|
+
const latest = React.useRef(local);
|
|
392
|
+
latest.current = local;
|
|
393
|
+
const fail = (message) => {
|
|
394
|
+
setOwn(true);
|
|
395
|
+
report(message);
|
|
396
|
+
};
|
|
397
|
+
const ok = () => {
|
|
398
|
+
setOwn(false);
|
|
399
|
+
report(void 0);
|
|
400
|
+
};
|
|
401
|
+
const accept = (n) => {
|
|
402
|
+
if (held) setPending(n === value ? void 0 : n);
|
|
403
|
+
else if (n !== value) onChange(n);
|
|
404
|
+
};
|
|
405
|
+
const handle = (n) => {
|
|
406
|
+
latest.current = n;
|
|
407
|
+
setLocal(n);
|
|
408
|
+
if (Number.isNaN(n)) {
|
|
409
|
+
if (required) return fail("Enter a value.");
|
|
410
|
+
ok();
|
|
411
|
+
accept(null);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (min !== void 0 && n < min || max !== void 0 && n > max) return fail(describeRange(min, max, show, unit));
|
|
415
|
+
ok();
|
|
416
|
+
accept(n);
|
|
417
|
+
};
|
|
418
|
+
const apply = useAsyncAction(async () => {
|
|
419
|
+
if (pending === void 0) return;
|
|
420
|
+
await onChange(pending);
|
|
421
|
+
});
|
|
422
|
+
const revert = () => {
|
|
423
|
+
setLocal(value ?? NaN);
|
|
424
|
+
setPending(void 0);
|
|
425
|
+
ok();
|
|
426
|
+
};
|
|
427
|
+
const timers = React.useRef({});
|
|
428
|
+
const stopNudge = React.useCallback(() => {
|
|
429
|
+
window.clearTimeout(timers.current.first);
|
|
430
|
+
window.clearInterval(timers.current.every);
|
|
431
|
+
}, []);
|
|
432
|
+
React.useEffect(() => stopNudge, [stopNudge]);
|
|
433
|
+
const stepBy = (delta) => {
|
|
434
|
+
const base = Number.isNaN(latest.current) ? 0 : latest.current;
|
|
435
|
+
let next = round(base + delta, digits);
|
|
436
|
+
if (min !== void 0) next = Math.max(next, min);
|
|
437
|
+
if (max !== void 0) next = Math.min(next, max);
|
|
438
|
+
if (next === base) return stopNudge();
|
|
439
|
+
handle(next);
|
|
440
|
+
};
|
|
441
|
+
const startNudge = (delta) => {
|
|
442
|
+
stopNudge();
|
|
443
|
+
stepBy(delta);
|
|
444
|
+
timers.current.first = window.setTimeout(() => {
|
|
445
|
+
timers.current.every = window.setInterval(() => stepBy(delta), REPEAT_EVERY_MS);
|
|
446
|
+
}, REPEAT_AFTER_MS);
|
|
447
|
+
};
|
|
448
|
+
const invalid = field?.invalid || own;
|
|
449
|
+
const name_ = ariaLabel ?? field?.label ?? "value";
|
|
450
|
+
const current = Number.isNaN(local) ? null : local;
|
|
451
|
+
const field_ = /* @__PURE__ */ jsx(I18nProvider, {
|
|
452
|
+
locale,
|
|
453
|
+
children: /* @__PURE__ */ jsxs(NumberField, {
|
|
454
|
+
value: local,
|
|
455
|
+
onChange: handle,
|
|
456
|
+
minValue: min,
|
|
457
|
+
maxValue: max,
|
|
458
|
+
step,
|
|
459
|
+
formatOptions: {
|
|
460
|
+
maximumFractionDigits: digits,
|
|
461
|
+
minimumFractionDigits: 0
|
|
462
|
+
},
|
|
463
|
+
commitBehavior: "validate",
|
|
464
|
+
isDisabled: disabled,
|
|
465
|
+
isInvalid: invalid || void 0,
|
|
466
|
+
name,
|
|
467
|
+
"aria-label": ariaLabel ?? field?.label,
|
|
468
|
+
className: cn(box, controlHeight[size], nudge ? "min-w-32 flex-1" : void 0, !nudge && !suggestion && !held && className),
|
|
469
|
+
children: [/* @__PURE__ */ jsx(Input, {
|
|
470
|
+
id: idProp ?? field?.id,
|
|
471
|
+
placeholder: format.missing,
|
|
472
|
+
"aria-describedby": [field?.describedBy, unit ? unitId : void 0].filter(Boolean).join(" ") || void 0,
|
|
473
|
+
className: "min-w-0 flex-1 bg-transparent tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed"
|
|
474
|
+
}), unit && /* @__PURE__ */ jsxs("span", {
|
|
475
|
+
id: unitId,
|
|
476
|
+
className: "ml-2 shrink-0 type-body-sm text-muted-foreground",
|
|
477
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
478
|
+
"aria-hidden": true,
|
|
479
|
+
children: unit
|
|
480
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
481
|
+
className: "sr-only",
|
|
482
|
+
children: intlUnits[unit] ? intlUnits[unit].replace(/-/g, " ") : unit
|
|
483
|
+
})]
|
|
484
|
+
})]
|
|
485
|
+
})
|
|
486
|
+
});
|
|
487
|
+
if (!nudge && !suggestion && !held) return field_;
|
|
488
|
+
const sizes = [...new Set(nudge ?? [])].filter((n) => n > 0).sort((a, b) => a - b);
|
|
489
|
+
const steps = [...sizes.slice().reverse().map((n) => -n), ...sizes];
|
|
490
|
+
const row = /* @__PURE__ */ jsxs("div", {
|
|
491
|
+
className: "flex min-w-0 flex-wrap items-center gap-2",
|
|
492
|
+
children: [field_, steps.length > 0 && /* @__PURE__ */ jsx(Toolbar$1, {
|
|
493
|
+
label: `Nudge ${name_}`,
|
|
494
|
+
className: "gap-1",
|
|
495
|
+
children: steps.map((delta) => {
|
|
496
|
+
const base = current ?? 0;
|
|
497
|
+
return /* @__PURE__ */ jsx(Button$1, {
|
|
498
|
+
size: size === "sm" ? "sm" : "default",
|
|
499
|
+
className: "tabular-nums",
|
|
500
|
+
disabled: disabled || (delta < 0 ? min !== void 0 && base <= min : max !== void 0 && base >= max),
|
|
501
|
+
"aria-label": `${delta < 0 ? "Subtract" : "Add"} ${withUnit(show(Math.abs(delta)), unit)}`,
|
|
502
|
+
onPressStart: () => startNudge(delta),
|
|
503
|
+
onPressEnd: stopNudge,
|
|
504
|
+
children: signed(delta)
|
|
505
|
+
}, delta);
|
|
506
|
+
})
|
|
507
|
+
})]
|
|
508
|
+
});
|
|
509
|
+
const shown = suggestion && current !== suggestion.value ? suggestion : void 0;
|
|
510
|
+
const suggestionId = `${unitId}-suggestion`;
|
|
511
|
+
const label = shown && (shown.label ?? withUnit(show(shown.value), unit));
|
|
512
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
513
|
+
className: cn("grid min-w-0 gap-2", className),
|
|
514
|
+
children: [
|
|
515
|
+
row,
|
|
516
|
+
shown && /* @__PURE__ */ jsxs("div", {
|
|
517
|
+
className: "flex flex-wrap items-center gap-x-3 gap-y-1",
|
|
518
|
+
children: [/* @__PURE__ */ jsxs("p", {
|
|
519
|
+
id: suggestionId,
|
|
520
|
+
className: "min-w-0 flex-1 type-body-sm text-muted-foreground",
|
|
521
|
+
children: [
|
|
522
|
+
/* @__PURE__ */ jsxs("span", {
|
|
523
|
+
className: "text-foreground",
|
|
524
|
+
children: [
|
|
525
|
+
"Suggested ",
|
|
526
|
+
label,
|
|
527
|
+
":"
|
|
528
|
+
]
|
|
529
|
+
}),
|
|
530
|
+
" ",
|
|
531
|
+
shown.reason
|
|
532
|
+
]
|
|
533
|
+
}), /* @__PURE__ */ jsx(Button$1, {
|
|
534
|
+
size: "sm",
|
|
535
|
+
disabled,
|
|
536
|
+
"aria-describedby": suggestionId,
|
|
537
|
+
onPress: () => handle(shown.value),
|
|
538
|
+
children: "Use this value"
|
|
539
|
+
})]
|
|
540
|
+
}),
|
|
541
|
+
held && pending !== void 0 && /* @__PURE__ */ jsxs("div", {
|
|
542
|
+
role: "group",
|
|
543
|
+
"aria-label": "Change not applied yet",
|
|
544
|
+
className: "grid gap-3 rounded-surface bg-surface-sunken p-3",
|
|
545
|
+
children: [typeof preview === "function" ? preview(pending) : preview, /* @__PURE__ */ jsxs("div", {
|
|
546
|
+
className: "flex flex-wrap items-center gap-2",
|
|
547
|
+
children: [/* @__PURE__ */ jsx(AsyncButton, {
|
|
548
|
+
variant: "default",
|
|
549
|
+
size: "sm",
|
|
550
|
+
phase: apply.phase,
|
|
551
|
+
error: apply.error,
|
|
552
|
+
labels: applyLabels,
|
|
553
|
+
disabled: disabled || invalid,
|
|
554
|
+
onPress: () => void apply.run()
|
|
555
|
+
}), /* @__PURE__ */ jsx(Button$1, {
|
|
556
|
+
size: "sm",
|
|
557
|
+
variant: "ghost",
|
|
558
|
+
disabled: apply.phase === "pending",
|
|
559
|
+
onPress: revert,
|
|
560
|
+
children: "Revert"
|
|
561
|
+
})]
|
|
562
|
+
})]
|
|
563
|
+
})
|
|
564
|
+
]
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
/** A slider on React Aria with its current value always printed beside the track (and spoken with the unit when `Intl` knows it); `editable` adds a number field to type an exact value. */
|
|
568
|
+
function Slider$1({ value, onChange, min, max, step = 1, unit, locale = format.locale, editable = false, hideValue = false, places, disabled, className, "aria-label": ariaLabel }) {
|
|
569
|
+
const { field } = useFieldControl();
|
|
570
|
+
const digits = places ?? Math.max(decimalsOf(step), decimalsOf(min));
|
|
571
|
+
const show = useNumberText(locale, digits);
|
|
572
|
+
const name = ariaLabel ?? field?.label ?? "Value";
|
|
573
|
+
const text = withUnit(show(value), unit);
|
|
574
|
+
return /* @__PURE__ */ jsx(I18nProvider, {
|
|
575
|
+
locale,
|
|
576
|
+
children: /* @__PURE__ */ jsxs(Slider, {
|
|
577
|
+
value,
|
|
578
|
+
onChange: (v) => onChange(v),
|
|
579
|
+
minValue: min,
|
|
580
|
+
maxValue: max,
|
|
581
|
+
step,
|
|
582
|
+
formatOptions: formatOptionsFor(digits, unit),
|
|
583
|
+
isDisabled: disabled,
|
|
584
|
+
"aria-label": name,
|
|
585
|
+
"aria-describedby": field?.describedBy,
|
|
586
|
+
className: cn("flex min-w-0 items-center gap-3", className),
|
|
587
|
+
children: [/* @__PURE__ */ jsx(SliderTrack, {
|
|
588
|
+
className: "relative flex h-(--control-h) min-w-0 flex-1 cursor-pointer items-center disabled:cursor-not-allowed disabled:opacity-50",
|
|
589
|
+
children: ({ state }) => /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
590
|
+
/* @__PURE__ */ jsx("span", {
|
|
591
|
+
"aria-hidden": true,
|
|
592
|
+
className: "absolute inset-x-0 h-1.5 rounded-pill bg-muted"
|
|
593
|
+
}),
|
|
594
|
+
/* @__PURE__ */ jsx("span", {
|
|
595
|
+
"aria-hidden": true,
|
|
596
|
+
className: "absolute left-0 h-1.5 rounded-pill bg-primary",
|
|
597
|
+
style: { width: `${state.getThumbPercent(0) * 100}%` }
|
|
598
|
+
}),
|
|
599
|
+
/* @__PURE__ */ jsx(SliderThumb, { className: "top-1/2 size-5 rounded-pill border-2 border-primary bg-background focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring" })
|
|
600
|
+
] })
|
|
601
|
+
}), editable ? /* @__PURE__ */ jsx(NumberField$1, {
|
|
602
|
+
className: "w-28 shrink-0",
|
|
603
|
+
"aria-label": `${name}, exact value`,
|
|
604
|
+
value,
|
|
605
|
+
onChange: (v) => {
|
|
606
|
+
if (v !== null) onChange(v);
|
|
607
|
+
},
|
|
608
|
+
min,
|
|
609
|
+
max,
|
|
610
|
+
step,
|
|
611
|
+
unit,
|
|
612
|
+
locale,
|
|
613
|
+
places: digits,
|
|
614
|
+
required: true,
|
|
615
|
+
disabled
|
|
616
|
+
}) : !hideValue && /* @__PURE__ */ jsx(SliderOutput, {
|
|
617
|
+
"aria-hidden": true,
|
|
618
|
+
className: "w-16 shrink-0 text-right type-body-sm tabular-nums text-foreground",
|
|
619
|
+
children: text
|
|
620
|
+
})]
|
|
621
|
+
})
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
/** Choose a colour with the platform picker, see and type its hex value, pick from the app's suggestions, and get a note when it would be hard to see on `against`. A half-typed value is never passed on. */
|
|
625
|
+
function ColorField({ value, onChange, suggestions, against, againstLabel = "the background", minContrast = 3, disabled, id: idProp, className, "aria-label": ariaLabel }) {
|
|
626
|
+
const { field, report } = useFieldControl();
|
|
627
|
+
const label = ariaLabel ?? field?.label;
|
|
628
|
+
const pickerName = label ? `${label}, picker` : "Colour picker";
|
|
629
|
+
const [draft, setDraft] = React.useState(null);
|
|
630
|
+
const [own, setOwn] = React.useState(false);
|
|
631
|
+
const text = draft ?? value ?? "";
|
|
632
|
+
const ratio = value && against ? contrastRatio(value, against) : null;
|
|
633
|
+
const commitText = (t) => {
|
|
634
|
+
if (t.trim() === "") {
|
|
635
|
+
setOwn(false);
|
|
636
|
+
report(void 0);
|
|
637
|
+
if (value !== null) onChange(null);
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
const hex = parseHex(t);
|
|
641
|
+
if (!hex) {
|
|
642
|
+
setOwn(true);
|
|
643
|
+
report("Enter a colour like #1a7f64.");
|
|
644
|
+
return false;
|
|
645
|
+
}
|
|
646
|
+
setOwn(false);
|
|
647
|
+
report(void 0);
|
|
648
|
+
if (hex !== value) onChange(hex);
|
|
649
|
+
return true;
|
|
650
|
+
};
|
|
651
|
+
const pick = (hex) => {
|
|
652
|
+
setDraft(null);
|
|
653
|
+
setOwn(false);
|
|
654
|
+
report(void 0);
|
|
655
|
+
if (hex !== value) onChange(hex);
|
|
656
|
+
};
|
|
657
|
+
const invalid = field?.invalid || own;
|
|
658
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
659
|
+
className: cn("grid min-w-0 gap-2", className),
|
|
660
|
+
children: [
|
|
661
|
+
/* @__PURE__ */ jsxs("div", {
|
|
662
|
+
className: cn(box, controlHeight.default, "gap-2 px-2"),
|
|
663
|
+
"aria-invalid": invalid || void 0,
|
|
664
|
+
children: [/* @__PURE__ */ jsxs("span", {
|
|
665
|
+
className: "relative size-6 shrink-0 overflow-hidden rounded-control border border-input",
|
|
666
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
667
|
+
"aria-hidden": true,
|
|
668
|
+
className: cn("absolute inset-0", value === null && "bg-muted"),
|
|
669
|
+
style: value ? { background: value } : void 0
|
|
670
|
+
}), /* @__PURE__ */ jsx("input", {
|
|
671
|
+
type: "color",
|
|
672
|
+
"aria-label": pickerName,
|
|
673
|
+
value: value ?? "#000000",
|
|
674
|
+
disabled,
|
|
675
|
+
onChange: (e) => pick(e.target.value),
|
|
676
|
+
className: "absolute inset-0 size-full cursor-pointer opacity-0 disabled:cursor-not-allowed"
|
|
677
|
+
})]
|
|
678
|
+
}), /* @__PURE__ */ jsx("input", {
|
|
679
|
+
id: idProp ?? field?.id,
|
|
680
|
+
type: "text",
|
|
681
|
+
autoComplete: "off",
|
|
682
|
+
spellCheck: false,
|
|
683
|
+
disabled,
|
|
684
|
+
"aria-label": ariaLabel,
|
|
685
|
+
"aria-describedby": field?.describedBy,
|
|
686
|
+
"aria-invalid": invalid || void 0,
|
|
687
|
+
placeholder: format.missing,
|
|
688
|
+
value: text,
|
|
689
|
+
onChange: (e) => setDraft(e.target.value),
|
|
690
|
+
onBlur: () => {
|
|
691
|
+
if (draft !== null && commitText(draft)) setDraft(null);
|
|
692
|
+
},
|
|
693
|
+
onKeyDown: (e) => {
|
|
694
|
+
if (e.key === "Enter" && draft !== null && commitText(draft)) setDraft(null);
|
|
695
|
+
if (e.key === "Escape" && draft !== null) {
|
|
696
|
+
setDraft(null);
|
|
697
|
+
setOwn(false);
|
|
698
|
+
report(void 0);
|
|
699
|
+
}
|
|
700
|
+
},
|
|
701
|
+
className: "min-w-0 flex-1 bg-transparent tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed"
|
|
702
|
+
})]
|
|
703
|
+
}),
|
|
704
|
+
suggestions && suggestions.length > 0 && /* @__PURE__ */ jsx("div", {
|
|
705
|
+
role: "group",
|
|
706
|
+
"aria-label": "Suggested colours",
|
|
707
|
+
className: "flex flex-wrap gap-1.5",
|
|
708
|
+
children: suggestions.map((hex) => /* @__PURE__ */ jsx("button", {
|
|
709
|
+
type: "button",
|
|
710
|
+
disabled,
|
|
711
|
+
"aria-label": hex,
|
|
712
|
+
"aria-pressed": value === hex,
|
|
713
|
+
onClick: () => pick(hex),
|
|
714
|
+
className: "size-6 cursor-pointer rounded-control border border-input transition-state aria-pressed:outline-2 aria-pressed:outline-offset-2 aria-pressed:outline-foreground disabled:cursor-not-allowed disabled:opacity-50",
|
|
715
|
+
style: { background: hex }
|
|
716
|
+
}, hex))
|
|
717
|
+
}),
|
|
718
|
+
ratio !== null && ratio < minContrast && /* @__PURE__ */ jsxs("p", {
|
|
719
|
+
className: "type-body-sm text-warning",
|
|
720
|
+
children: [
|
|
721
|
+
"Hard to see on ",
|
|
722
|
+
againstLabel,
|
|
723
|
+
": contrast ",
|
|
724
|
+
format.decimal(ratio),
|
|
725
|
+
":1, aim for ",
|
|
726
|
+
format.decimal(minContrast),
|
|
727
|
+
":1 or more."
|
|
728
|
+
]
|
|
729
|
+
})
|
|
730
|
+
]
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
export { useAsyncAction as a, KeysLegend as c, useInView as d, AsyncButton as i, isTypingTarget as l, NumberField$1 as n, Toolbar$1 as o, Slider$1 as r, ToolbarSeparator as s, ColorField as t, useHotkeys as u };
|
|
734
|
+
|
|
735
|
+
//# sourceMappingURL=value-controls-FO4zQiHP.js.map
|