nish-ui 1.8.0 → 2.0.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 +223 -61
- package/dist/base-tailwind.css +1757 -0
- package/dist/base.css +1590 -1445
- package/dist/chart.d.ts +4 -1
- package/dist/chart.js +122 -49
- package/dist/elements-all.d.ts +8 -0
- package/dist/elements-all.js +178 -0
- package/dist/elements-charts.d.ts +124 -2
- package/dist/elements-charts.js +131 -1
- package/dist/elements-data.d.ts +166 -2
- package/dist/elements-data.js +46 -1
- package/dist/elements-forms.d.ts +71 -0
- package/dist/elements-forms.js +24 -25
- package/dist/elements.d.ts +71 -0
- package/dist/elements.js +4 -5
- package/dist/esm/chart-values.js +58 -0
- package/dist/esm/chart.js +10 -0
- package/dist/esm/cn.js +19 -0
- package/dist/esm/combobox-filter.js +13 -0
- package/dist/esm/components/accordion.js +166 -0
- package/dist/esm/components/app-shell.js +198 -0
- package/dist/esm/components/avatar-classes.js +28 -0
- package/dist/esm/components/avatar.js +113 -0
- package/dist/esm/components/breadcrumbs.js +154 -0
- package/dist/esm/components/canvas/follow-caption.js +32 -0
- package/dist/esm/components/canvas/follow.js +119 -0
- package/dist/esm/components/canvas/icons.js +70 -0
- package/dist/esm/components/canvas/index.js +10 -0
- package/dist/esm/components/canvas/layout.js +258 -0
- package/dist/esm/components/canvas/legend.js +93 -0
- package/dist/esm/components/canvas/minimap.js +127 -0
- package/dist/esm/components/canvas/node.js +87 -0
- package/dist/esm/components/canvas/route.js +262 -0
- package/dist/esm/components/canvas/system-canvas.js +837 -0
- package/dist/esm/components/canvas/types.js +10 -0
- package/dist/esm/components/canvas/view.js +47 -0
- package/dist/esm/components/chart-hooks.js +19 -0
- package/dist/esm/components/charts.js +467 -0
- package/dist/esm/components/client.js +24 -0
- package/dist/esm/components/combobox.js +343 -0
- package/dist/esm/components/command.js +403 -0
- package/dist/esm/components/data-classes.js +159 -0
- package/dist/esm/components/data.js +517 -0
- package/dist/esm/components/display-classes.js +130 -0
- package/dist/esm/components/display.js +755 -0
- package/dist/esm/components/error-boundary.js +64 -0
- package/dist/esm/components/feedback.js +136 -0
- package/dist/esm/components/file-dropzone.js +184 -0
- package/dist/esm/components/filters.js +108 -0
- package/dist/esm/components/flow.js +746 -0
- package/dist/esm/components/forms-classes.js +43 -0
- package/dist/esm/components/forms.js +581 -0
- package/dist/esm/components/hooks-classes.js +9 -0
- package/dist/esm/components/hooks.js +236 -0
- package/dist/esm/components/icons.js +96 -0
- package/dist/esm/components/inbox-classes.js +29 -0
- package/dist/esm/components/inbox.js +257 -0
- package/dist/esm/components/latest.js +13 -0
- package/dist/esm/components/layout-vars.js +96 -0
- package/dist/esm/components/layout.js +272 -0
- package/dist/esm/components/media.js +143 -0
- package/dist/esm/components/nav.js +614 -0
- package/dist/esm/components/overlay-classes.js +66 -0
- package/dist/esm/components/overlay.js +1447 -0
- package/dist/esm/components/pagination-classes.js +29 -0
- package/dist/esm/components/pagination.js +154 -0
- package/dist/esm/components/portal.js +16 -0
- package/dist/esm/components/primitives-classes.js +198 -0
- package/dist/esm/components/primitives.js +268 -0
- package/dist/esm/components/progress.js +77 -0
- package/dist/esm/components/provider.js +32 -0
- package/dist/esm/components/radio-group.js +186 -0
- package/dist/esm/components/resource-classes.js +133 -0
- package/dist/esm/components/resource.js +209 -0
- package/dist/esm/components/segmented.js +178 -0
- package/dist/esm/components/separator.js +38 -0
- package/dist/esm/components/service-tokens.js +221 -0
- package/dist/esm/components/settings.js +301 -0
- package/dist/esm/components/slider.js +262 -0
- package/dist/esm/components/split-classes.js +8 -0
- package/dist/esm/components/split.js +411 -0
- package/dist/esm/components/strings.js +119 -0
- package/dist/esm/components/table.js +144 -0
- package/dist/esm/components/tabs-classes.js +21 -0
- package/dist/esm/components/tabs.js +201 -0
- package/dist/esm/components/tag-input.js +257 -0
- package/dist/esm/components/theme-toggle.js +177 -0
- package/dist/esm/components/toast.js +361 -0
- package/dist/esm/components/url-state.js +118 -0
- package/dist/esm/components/visually-hidden.js +20 -0
- package/dist/esm/format.js +320 -0
- package/dist/esm/inbox-client.js +2 -0
- package/dist/esm/inbox-types.js +2 -0
- package/dist/esm/index.js +320 -0
- package/dist/esm/runtime.js +37 -0
- package/dist/esm/server/inbox/client.js +297 -0
- package/dist/esm/server/inbox/types.js +24 -0
- package/dist/esm/sparkline-path.js +59 -0
- package/dist/esm/split-layout.js +419 -0
- package/dist/esm/system-canvas.js +2 -0
- package/dist/esm/theme.js +804 -0
- package/dist/esm/tokens.mjs +425 -0
- package/dist/esm/url.js +20 -0
- package/dist/files.json +5 -1
- package/dist/fonts/LICENSE-Inter.txt +93 -0
- package/dist/format.d.ts +32 -18
- package/dist/format.js +227 -82
- package/dist/inbox-client.js +3 -1
- package/dist/index.d.ts +1689 -667
- package/dist/nish-components.css +4701 -3169
- package/dist/nish-components.min.css +1 -0
- package/dist/nish.css +1603 -1450
- package/dist/nish.min.css +1 -0
- package/dist/node/eslint.js +29 -14
- package/dist/node/th-check.js +29 -14
- package/dist/node/vite.d.ts +12 -0
- package/dist/node/vite.js +12 -2
- package/dist/server/hono.d.ts +15 -6
- package/dist/server/hono.js +30 -25
- package/dist/server/index.d.ts +3 -10
- package/dist/server/index.js +71 -38
- package/dist/{spa-DuVB5O7S.d.ts → spa-BLlMc0eI.d.ts} +38 -5
- package/dist/system-canvas-BLuWd69H.d.ts +183 -0
- package/dist/system-canvas.d.ts +2 -168
- package/dist/tailwind-core.css +114 -0
- package/dist/tailwind.css +3 -7
- package/dist/theme-init.js +23 -9
- package/dist/theme-init.sha256 +1 -0
- package/dist/theme.d.ts +246 -4
- package/dist/theme.global.js +357 -90
- package/dist/theme.js +338 -68
- package/dist/tokens.css +14 -6
- package/dist/tokens.dtcg.json +1513 -0
- package/dist/tokens.js +29 -14
- package/dist/tokens.json +512 -0
- package/package.json +51 -13
- package/dist/chart.js.map +0 -1
- package/dist/elements-charts.js.map +0 -1
- package/dist/elements-data.js.map +0 -1
- package/dist/elements-forms.js.map +0 -1
- package/dist/elements.js.map +0 -1
- package/dist/format.js.map +0 -1
- package/dist/inbox-client.js.map +0 -1
- package/dist/index.js +0 -10260
- package/dist/index.js.map +0 -1
- package/dist/server/hono.js.map +0 -1
- package/dist/server/index.js.map +0 -1
- package/dist/split-layout.js.map +0 -1
- package/dist/system-canvas.js +0 -2204
- package/dist/system-canvas.js.map +0 -1
- package/dist/theme.global.js.map +0 -1
- package/dist/theme.js.map +0 -1
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/settings.tsx
|
|
4
|
+
import {
|
|
5
|
+
createContext,
|
|
6
|
+
useCallback,
|
|
7
|
+
useContext,
|
|
8
|
+
useEffect,
|
|
9
|
+
useId,
|
|
10
|
+
useRef,
|
|
11
|
+
useState
|
|
12
|
+
} from "react";
|
|
13
|
+
import { cn } from "../cn.js";
|
|
14
|
+
import { Spinner } from "./feedback.js";
|
|
15
|
+
import { Card, Input } from "./primitives.js";
|
|
16
|
+
import { useNishStrings } from "./provider.js";
|
|
17
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
18
|
+
var CompactSettings = /* @__PURE__ */ createContext(false);
|
|
19
|
+
function SettingsSection({ title, description, id, actions, compact = false, children, className, ref, ...rest }) {
|
|
20
|
+
const auto = useId();
|
|
21
|
+
const headingId = `${id ?? auto}-heading`;
|
|
22
|
+
return /* @__PURE__ */ jsxs("section", { ...rest, ref, id, "aria-labelledby": rest["aria-labelledby"] ?? headingId, className: cn("min-w-0", className), children: [
|
|
23
|
+
/* @__PURE__ */ jsxs("div", { className: cn("flex flex-wrap items-end justify-between gap-x-4 gap-y-2", compact ? "mb-1" : "mb-3"), children: [
|
|
24
|
+
/* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
|
|
25
|
+
/* @__PURE__ */ jsx("h2", { id: headingId, className: compact ? "text-sm font-semibold text-th-hi2" : "text-base font-semibold text-th-hi", children: title }),
|
|
26
|
+
description != null ? /* @__PURE__ */ jsx("p", { className: cn("mt-0.5 text-th-hi3", compact ? "text-xs" : "text-sm"), children: description }) : null
|
|
27
|
+
] }),
|
|
28
|
+
actions != null ? /* @__PURE__ */ jsx("div", { className: "flex shrink-0 items-center gap-2", children: actions }) : null
|
|
29
|
+
] }),
|
|
30
|
+
/* @__PURE__ */ jsx(CompactSettings.Provider, { value: compact, children: compact ? /* @__PURE__ */ jsx("div", { className: "divide-y divide-th-line", children }) : /* @__PURE__ */ jsx(Card, { padding: "none", className: "divide-y divide-th-line", children }) })
|
|
31
|
+
] });
|
|
32
|
+
}
|
|
33
|
+
var STATUS_TONE = { muted: "text-th-hi3", error: "text-th-loss", ok: "text-th-ok" };
|
|
34
|
+
function SettingRow({
|
|
35
|
+
label,
|
|
36
|
+
description,
|
|
37
|
+
children,
|
|
38
|
+
htmlFor,
|
|
39
|
+
stacked = false,
|
|
40
|
+
inline = false,
|
|
41
|
+
compact: compactProp,
|
|
42
|
+
status,
|
|
43
|
+
statusTone = "muted",
|
|
44
|
+
className,
|
|
45
|
+
ref,
|
|
46
|
+
...rest
|
|
47
|
+
}) {
|
|
48
|
+
const inCompactSection = useContext(CompactSettings);
|
|
49
|
+
const compact = compactProp ?? inCompactSection;
|
|
50
|
+
const labelClass = "block text-sm font-medium text-th-hi";
|
|
51
|
+
const auto = useId();
|
|
52
|
+
const labelId = `${auto}-label`;
|
|
53
|
+
const descId = `${auto}-desc`;
|
|
54
|
+
const side = inline || compact && !stacked;
|
|
55
|
+
const layout = stacked ? "flex flex-col gap-2" : side ? "grid grid-cols-[1fr_auto] items-center gap-4" : "flex flex-col gap-2 sm:grid sm:grid-cols-[1fr_auto] sm:items-center sm:gap-4";
|
|
56
|
+
return /* @__PURE__ */ jsxs(
|
|
57
|
+
"div",
|
|
58
|
+
{
|
|
59
|
+
...rest,
|
|
60
|
+
ref,
|
|
61
|
+
className: cn(compact ? cn("min-h-11 py-2", !inCompactSection && "px-4") : "min-h-[56px] px-4 py-3", layout, className),
|
|
62
|
+
children: [
|
|
63
|
+
/* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
|
|
64
|
+
htmlFor ? /* @__PURE__ */ jsx("label", { htmlFor, className: labelClass, children: label }) : /* @__PURE__ */ jsx("span", { id: labelId, className: labelClass, children: label }),
|
|
65
|
+
description != null ? /* @__PURE__ */ jsx("p", { id: descId, className: cn("mt-0.5 text-th-hi3", compact ? "text-xs" : "text-sm"), children: description }) : null
|
|
66
|
+
] }),
|
|
67
|
+
/* @__PURE__ */ jsxs(
|
|
68
|
+
"div",
|
|
69
|
+
{
|
|
70
|
+
...htmlFor ? {} : { role: "group", "aria-labelledby": labelId, "aria-describedby": description != null ? descId : void 0 },
|
|
71
|
+
className: cn("flex min-w-0 items-center gap-2", stacked ? "w-full" : side ? "justify-end" : "w-full sm:w-auto sm:justify-end"),
|
|
72
|
+
children: [
|
|
73
|
+
children,
|
|
74
|
+
/* @__PURE__ */ jsx("span", { role: "status", "aria-live": "polite", className: cn("shrink-0 text-sm empty:hidden", STATUS_TONE[statusTone]), children: status })
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
var SAVED_MS = 1500;
|
|
83
|
+
var ERROR_MS = 3e3;
|
|
84
|
+
function useSaveStatus() {
|
|
85
|
+
const strings = useNishStrings();
|
|
86
|
+
const [status, setStatus] = useState("idle");
|
|
87
|
+
const [error, setError] = useState(null);
|
|
88
|
+
const timer = useRef(void 0);
|
|
89
|
+
const token = useRef(0);
|
|
90
|
+
const mounted = useRef(true);
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
mounted.current = true;
|
|
93
|
+
return () => {
|
|
94
|
+
mounted.current = false;
|
|
95
|
+
clearTimeout(timer.current);
|
|
96
|
+
};
|
|
97
|
+
}, []);
|
|
98
|
+
const run = useCallback(async (p) => {
|
|
99
|
+
const mine = ++token.current;
|
|
100
|
+
clearTimeout(timer.current);
|
|
101
|
+
setError(null);
|
|
102
|
+
setStatus("saving");
|
|
103
|
+
const settle = (next, ms) => {
|
|
104
|
+
if (!mounted.current || token.current !== mine) return;
|
|
105
|
+
setStatus(next);
|
|
106
|
+
timer.current = setTimeout(() => {
|
|
107
|
+
if (!mounted.current || token.current !== mine) return;
|
|
108
|
+
setStatus("idle");
|
|
109
|
+
setError(null);
|
|
110
|
+
}, ms);
|
|
111
|
+
};
|
|
112
|
+
try {
|
|
113
|
+
const out = await p;
|
|
114
|
+
settle("saved", SAVED_MS);
|
|
115
|
+
return out;
|
|
116
|
+
} catch (e) {
|
|
117
|
+
if (mounted.current && token.current === mine) setError(e instanceof Error ? e.message : String(e));
|
|
118
|
+
settle("error", ERROR_MS);
|
|
119
|
+
return void 0;
|
|
120
|
+
}
|
|
121
|
+
}, []);
|
|
122
|
+
const label = status === "saving" ? strings.saving : status === "saved" ? strings.saved : status === "error" ? error || strings.saveFailed : null;
|
|
123
|
+
const tone = status === "saved" ? "ok" : status === "error" ? "error" : "muted";
|
|
124
|
+
return { status, error, label, tone, run };
|
|
125
|
+
}
|
|
126
|
+
var parseDraft = (text) => Number(text.trim().replace(/,/g, "").replace(/\u2212/g, "-"));
|
|
127
|
+
var fmt = (v) => v == null ? "" : String(v);
|
|
128
|
+
var decimals = (n) => String(n).split(".")[1]?.length ?? 0;
|
|
129
|
+
function NumberField({
|
|
130
|
+
value,
|
|
131
|
+
onCommit,
|
|
132
|
+
min,
|
|
133
|
+
max,
|
|
134
|
+
step = 1,
|
|
135
|
+
nullable = false,
|
|
136
|
+
suffix,
|
|
137
|
+
precision,
|
|
138
|
+
className,
|
|
139
|
+
onBlur,
|
|
140
|
+
onKeyDown,
|
|
141
|
+
id,
|
|
142
|
+
invalid,
|
|
143
|
+
"aria-describedby": describedBy,
|
|
144
|
+
ref,
|
|
145
|
+
...rest
|
|
146
|
+
}) {
|
|
147
|
+
const strings = useNishStrings();
|
|
148
|
+
const auto = useId();
|
|
149
|
+
const errId = `${id ?? auto}-error`;
|
|
150
|
+
const [draft, setDraftState] = useState(fmt(value));
|
|
151
|
+
const [error, setError] = useState(null);
|
|
152
|
+
const [pending, setPending] = useState(false);
|
|
153
|
+
const draftRef = useRef(draft);
|
|
154
|
+
const lastSent = useRef(null);
|
|
155
|
+
const mounted = useRef(true);
|
|
156
|
+
const skipCommit = useRef(false);
|
|
157
|
+
const setDraft = (next) => {
|
|
158
|
+
draftRef.current = next;
|
|
159
|
+
setDraftState(next);
|
|
160
|
+
};
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
mounted.current = true;
|
|
163
|
+
return () => {
|
|
164
|
+
mounted.current = false;
|
|
165
|
+
};
|
|
166
|
+
}, []);
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
lastSent.current = null;
|
|
169
|
+
setDraft(fmt(value));
|
|
170
|
+
}, [value]);
|
|
171
|
+
const clamp = (n) => {
|
|
172
|
+
let out = n;
|
|
173
|
+
if (min != null) out = Math.max(min, out);
|
|
174
|
+
if (max != null) out = Math.min(max, out);
|
|
175
|
+
return out;
|
|
176
|
+
};
|
|
177
|
+
const fail = (e) => {
|
|
178
|
+
lastSent.current = null;
|
|
179
|
+
setDraft(fmt(value));
|
|
180
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
181
|
+
};
|
|
182
|
+
const commit = () => {
|
|
183
|
+
const text = draftRef.current.trim();
|
|
184
|
+
let next;
|
|
185
|
+
if (text === "") {
|
|
186
|
+
if (!nullable) {
|
|
187
|
+
setDraft(fmt(value));
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
next = null;
|
|
191
|
+
} else {
|
|
192
|
+
const n = parseDraft(text);
|
|
193
|
+
if (Number.isNaN(n)) {
|
|
194
|
+
setDraft(fmt(value));
|
|
195
|
+
setError(strings.enterNumber);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
let c = clamp(n);
|
|
199
|
+
if (precision != null) c = Number(c.toFixed(precision));
|
|
200
|
+
next = c;
|
|
201
|
+
}
|
|
202
|
+
setDraft(fmt(next));
|
|
203
|
+
if (next === value) return;
|
|
204
|
+
if (lastSent.current && lastSent.current.v === next) return;
|
|
205
|
+
setError(null);
|
|
206
|
+
lastSent.current = { v: next };
|
|
207
|
+
let result;
|
|
208
|
+
try {
|
|
209
|
+
result = onCommit(next);
|
|
210
|
+
} catch (e) {
|
|
211
|
+
fail(e);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (result && typeof result.then === "function") {
|
|
215
|
+
setPending(true);
|
|
216
|
+
result.then(
|
|
217
|
+
() => {
|
|
218
|
+
if (mounted.current) setPending(false);
|
|
219
|
+
},
|
|
220
|
+
(e) => {
|
|
221
|
+
if (!mounted.current) return;
|
|
222
|
+
setPending(false);
|
|
223
|
+
fail(e);
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const nudge = (dir) => {
|
|
229
|
+
const base = parseDraft(draftRef.current);
|
|
230
|
+
const from = Number.isNaN(base) || draftRef.current.trim() === "" ? value ?? min ?? 0 : base;
|
|
231
|
+
const n = Number(clamp(from + dir * step).toFixed(Math.max(precision ?? 0, decimals(step), decimals(from))));
|
|
232
|
+
setDraft(String(n));
|
|
233
|
+
lastSent.current = null;
|
|
234
|
+
};
|
|
235
|
+
const handleKeyDown = (e) => {
|
|
236
|
+
onKeyDown?.(e);
|
|
237
|
+
if (e.defaultPrevented) return;
|
|
238
|
+
if (e.key === "Enter") {
|
|
239
|
+
e.preventDefault();
|
|
240
|
+
commit();
|
|
241
|
+
} else if (e.key === "Escape") {
|
|
242
|
+
e.stopPropagation();
|
|
243
|
+
setDraft(fmt(value));
|
|
244
|
+
setError(null);
|
|
245
|
+
skipCommit.current = true;
|
|
246
|
+
e.currentTarget.blur();
|
|
247
|
+
skipCommit.current = false;
|
|
248
|
+
} else if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
|
249
|
+
e.preventDefault();
|
|
250
|
+
nudge(e.key === "ArrowUp" ? 1 : -1);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const shown = parseDraft(draft);
|
|
254
|
+
const now = draft.trim() !== "" && !Number.isNaN(shown) ? shown : value ?? void 0;
|
|
255
|
+
const negatives = min != null && min < 0;
|
|
256
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-col gap-1", children: [
|
|
257
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
258
|
+
/* @__PURE__ */ jsx(
|
|
259
|
+
Input,
|
|
260
|
+
{
|
|
261
|
+
inputMode: negatives ? "text" : "decimal",
|
|
262
|
+
...rest,
|
|
263
|
+
ref,
|
|
264
|
+
id,
|
|
265
|
+
type: "text",
|
|
266
|
+
role: "spinbutton",
|
|
267
|
+
"aria-valuemin": min,
|
|
268
|
+
"aria-valuemax": max,
|
|
269
|
+
"aria-valuenow": now,
|
|
270
|
+
"aria-valuetext": now != null && typeof suffix === "string" ? `${now} ${suffix}` : void 0,
|
|
271
|
+
autoComplete: "off",
|
|
272
|
+
value: draft,
|
|
273
|
+
invalid: invalid || error != null,
|
|
274
|
+
"aria-describedby": cn(describedBy, error != null && errId) || void 0,
|
|
275
|
+
"aria-busy": pending || void 0,
|
|
276
|
+
className: cn("w-28", className),
|
|
277
|
+
onChange: (e) => {
|
|
278
|
+
setDraft(e.target.value);
|
|
279
|
+
lastSent.current = null;
|
|
280
|
+
if (error != null) setError(null);
|
|
281
|
+
},
|
|
282
|
+
onBlur: (e) => {
|
|
283
|
+
onBlur?.(e);
|
|
284
|
+
if (skipCommit.current) return;
|
|
285
|
+
commit();
|
|
286
|
+
},
|
|
287
|
+
onKeyDown: handleKeyDown
|
|
288
|
+
}
|
|
289
|
+
),
|
|
290
|
+
suffix != null ? /* @__PURE__ */ jsx("span", { className: "text-sm text-th-hi3", children: suffix }) : null,
|
|
291
|
+
pending ? /* @__PURE__ */ jsx(Spinner, { size: "sm", label: strings.saving, className: "text-th-hi3" }) : null
|
|
292
|
+
] }),
|
|
293
|
+
error != null ? /* @__PURE__ */ jsx("span", { id: errId, role: "alert", className: "text-sm text-th-loss", children: error }) : null
|
|
294
|
+
] });
|
|
295
|
+
}
|
|
296
|
+
export {
|
|
297
|
+
NumberField,
|
|
298
|
+
SettingRow,
|
|
299
|
+
SettingsSection,
|
|
300
|
+
useSaveStatus
|
|
301
|
+
};
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/slider.tsx
|
|
4
|
+
import {
|
|
5
|
+
useId,
|
|
6
|
+
useRef,
|
|
7
|
+
useState
|
|
8
|
+
} from "react";
|
|
9
|
+
import { cn } from "../cn.js";
|
|
10
|
+
import { isRtl, useIsoLayoutEffect } from "./client.js";
|
|
11
|
+
import { useLatest } from "./latest.js";
|
|
12
|
+
import { useNishStrings } from "./provider.js";
|
|
13
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
14
|
+
var decimals = (n) => {
|
|
15
|
+
const s = String(n);
|
|
16
|
+
const dot = s.indexOf(".");
|
|
17
|
+
return dot < 0 ? 0 : s.length - dot - 1;
|
|
18
|
+
};
|
|
19
|
+
var clamp = (n, lo, hi) => Math.min(Math.max(n, lo), hi);
|
|
20
|
+
function snap(v, min, max, step) {
|
|
21
|
+
const s = step > 0 ? step : 1;
|
|
22
|
+
const n = Math.round((v - min) / s) * s + min;
|
|
23
|
+
return clamp(Number(n.toFixed(Math.max(decimals(s), decimals(min)))), min, max);
|
|
24
|
+
}
|
|
25
|
+
function setRef(ref, node) {
|
|
26
|
+
if (typeof ref === "function") ref(node);
|
|
27
|
+
else if (ref) ref.current = node;
|
|
28
|
+
}
|
|
29
|
+
function useExternalLabelId(root, id, skip) {
|
|
30
|
+
const [labelId, setLabelId] = useState(void 0);
|
|
31
|
+
useIsoLayoutEffect(() => {
|
|
32
|
+
const doc = root.current?.ownerDocument;
|
|
33
|
+
if (skip || !id || !doc) {
|
|
34
|
+
setLabelId(void 0);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const label = Array.from(doc.getElementsByTagName("label")).find((l) => l.htmlFor === id);
|
|
38
|
+
if (!label) {
|
|
39
|
+
setLabelId(void 0);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!label.id) label.id = `${id}-label`;
|
|
43
|
+
setLabelId(label.id);
|
|
44
|
+
}, [id, skip]);
|
|
45
|
+
return labelId;
|
|
46
|
+
}
|
|
47
|
+
function Slider({
|
|
48
|
+
value,
|
|
49
|
+
defaultValue,
|
|
50
|
+
onValueChange,
|
|
51
|
+
onValueCommit,
|
|
52
|
+
min = 0,
|
|
53
|
+
max = 100,
|
|
54
|
+
step = 1,
|
|
55
|
+
name,
|
|
56
|
+
disabled = false,
|
|
57
|
+
invalid = false,
|
|
58
|
+
formatValue,
|
|
59
|
+
className,
|
|
60
|
+
ref,
|
|
61
|
+
id,
|
|
62
|
+
"aria-label": ariaLabel,
|
|
63
|
+
"aria-labelledby": ariaLabelledBy,
|
|
64
|
+
"aria-describedby": ariaDescribedBy,
|
|
65
|
+
"aria-invalid": ariaInvalid,
|
|
66
|
+
onPointerDown,
|
|
67
|
+
onPointerMove,
|
|
68
|
+
onPointerUp,
|
|
69
|
+
onPointerCancel,
|
|
70
|
+
...rest
|
|
71
|
+
}) {
|
|
72
|
+
const strings = useNishStrings();
|
|
73
|
+
const auto = useId();
|
|
74
|
+
const rootRef = useRef(null);
|
|
75
|
+
const trackRef = useRef(null);
|
|
76
|
+
const thumbs = useRef([null, null]);
|
|
77
|
+
const dragging = useRef(null);
|
|
78
|
+
const [dragIndex, setDragIndex] = useState(null);
|
|
79
|
+
const controlled = value !== void 0;
|
|
80
|
+
const [inner, setInner] = useState(() => defaultValue ?? min);
|
|
81
|
+
const raw = controlled ? value : inner;
|
|
82
|
+
const range = Array.isArray(raw);
|
|
83
|
+
const vals = Array.isArray(raw) ? [snap(Math.min(raw[0], raw[1]), min, max, step), snap(Math.max(raw[0], raw[1]), min, max, step)] : [snap(raw, min, max, step)];
|
|
84
|
+
const live = useRef(vals);
|
|
85
|
+
useIsoLayoutEffect(() => {
|
|
86
|
+
live.current = vals;
|
|
87
|
+
});
|
|
88
|
+
const onChange = useLatest(onValueChange);
|
|
89
|
+
const onCommit = useLatest(onValueCommit);
|
|
90
|
+
const fieldLabelId = useExternalLabelId(rootRef, id, ariaLabel != null || ariaLabelledBy != null);
|
|
91
|
+
const pack = (a) => a.length === 2 ? [a[0], a[1]] : a[0];
|
|
92
|
+
const setThumb = (i, v) => {
|
|
93
|
+
const cur = live.current;
|
|
94
|
+
const lo = i === 1 ? cur[0] : min;
|
|
95
|
+
const hi = i === 0 && cur.length === 2 ? cur[1] : max;
|
|
96
|
+
const next = clamp(snap(v, min, max, step), lo, hi);
|
|
97
|
+
if (next === cur[i]) return false;
|
|
98
|
+
const arr = cur.slice();
|
|
99
|
+
arr[i] = next;
|
|
100
|
+
live.current = arr;
|
|
101
|
+
if (!controlled) setInner(pack(arr));
|
|
102
|
+
onChange.current?.(pack(arr));
|
|
103
|
+
return true;
|
|
104
|
+
};
|
|
105
|
+
const commit = () => onCommit.current?.(pack(live.current));
|
|
106
|
+
const valueAt = (clientX) => {
|
|
107
|
+
const rect = trackRef.current.getBoundingClientRect();
|
|
108
|
+
if (!rect.width) return min;
|
|
109
|
+
let frac = (clientX - rect.left) / rect.width;
|
|
110
|
+
if (isRtl(rootRef.current)) frac = 1 - frac;
|
|
111
|
+
return min + clamp(frac, 0, 1) * (max - min);
|
|
112
|
+
};
|
|
113
|
+
const handlePointerDown = (e) => {
|
|
114
|
+
onPointerDown?.(e);
|
|
115
|
+
if (e.defaultPrevented || disabled || e.button !== 0) return;
|
|
116
|
+
const v = valueAt(e.clientX);
|
|
117
|
+
const cur = live.current;
|
|
118
|
+
let i = 0;
|
|
119
|
+
if (cur.length === 2) {
|
|
120
|
+
if (v <= cur[0]) i = 0;
|
|
121
|
+
else if (v >= cur[1]) i = 1;
|
|
122
|
+
else i = v - cur[0] <= cur[1] - v ? 0 : 1;
|
|
123
|
+
}
|
|
124
|
+
dragging.current = i;
|
|
125
|
+
setDragIndex(i);
|
|
126
|
+
try {
|
|
127
|
+
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
128
|
+
} catch {
|
|
129
|
+
}
|
|
130
|
+
thumbs.current[i]?.focus();
|
|
131
|
+
setThumb(i, v);
|
|
132
|
+
};
|
|
133
|
+
const handlePointerMove = (e) => {
|
|
134
|
+
onPointerMove?.(e);
|
|
135
|
+
if (dragging.current === null) return;
|
|
136
|
+
setThumb(dragging.current, valueAt(e.clientX));
|
|
137
|
+
};
|
|
138
|
+
const endDrag = () => {
|
|
139
|
+
if (dragging.current === null) return;
|
|
140
|
+
dragging.current = null;
|
|
141
|
+
setDragIndex(null);
|
|
142
|
+
commit();
|
|
143
|
+
};
|
|
144
|
+
const handleKeyDown = (i) => (e) => {
|
|
145
|
+
if (disabled || e.altKey || e.ctrlKey || e.metaKey) return;
|
|
146
|
+
const cur = live.current[i];
|
|
147
|
+
const rtl = isRtl(rootRef.current);
|
|
148
|
+
const big = step * 10;
|
|
149
|
+
let next = null;
|
|
150
|
+
if (e.key === "ArrowRight") next = cur + (rtl ? -step : step);
|
|
151
|
+
else if (e.key === "ArrowLeft") next = cur + (rtl ? step : -step);
|
|
152
|
+
else if (e.key === "ArrowUp") next = cur + step;
|
|
153
|
+
else if (e.key === "ArrowDown") next = cur - step;
|
|
154
|
+
else if (e.key === "PageUp") next = cur + big;
|
|
155
|
+
else if (e.key === "PageDown") next = cur - big;
|
|
156
|
+
else if (e.key === "Home") next = min;
|
|
157
|
+
else if (e.key === "End") next = max;
|
|
158
|
+
if (next === null) return;
|
|
159
|
+
e.preventDefault();
|
|
160
|
+
if (setThumb(i, next)) commit();
|
|
161
|
+
};
|
|
162
|
+
const isInvalid = invalid || ariaInvalid === true || ariaInvalid === "true";
|
|
163
|
+
const pct = (v) => max > min ? (v - min) / (max - min) * 100 : 0;
|
|
164
|
+
const fillStart = range ? pct(vals[0]) : 0;
|
|
165
|
+
const fillEnd = pct(vals[vals.length - 1]);
|
|
166
|
+
const labelledBy = ariaLabelledBy ?? fieldLabelId;
|
|
167
|
+
return /* @__PURE__ */ jsxs(
|
|
168
|
+
"div",
|
|
169
|
+
{
|
|
170
|
+
...rest,
|
|
171
|
+
ref: (node) => {
|
|
172
|
+
rootRef.current = node;
|
|
173
|
+
setRef(ref, node);
|
|
174
|
+
},
|
|
175
|
+
id,
|
|
176
|
+
"data-disabled": disabled ? "" : void 0,
|
|
177
|
+
"data-invalid": isInvalid ? "" : void 0,
|
|
178
|
+
"data-dragging": dragIndex !== null ? "" : void 0,
|
|
179
|
+
onPointerDown: handlePointerDown,
|
|
180
|
+
onPointerMove: handlePointerMove,
|
|
181
|
+
onPointerUp: (e) => {
|
|
182
|
+
onPointerUp?.(e);
|
|
183
|
+
endDrag();
|
|
184
|
+
},
|
|
185
|
+
onPointerCancel: (e) => {
|
|
186
|
+
onPointerCancel?.(e);
|
|
187
|
+
endDrag();
|
|
188
|
+
},
|
|
189
|
+
className: cn(
|
|
190
|
+
"relative flex h-6 w-full touch-none select-none items-center px-2.5 pointer-coarse:h-11",
|
|
191
|
+
disabled && "cursor-not-allowed opacity-50",
|
|
192
|
+
!disabled && "cursor-pointer",
|
|
193
|
+
className
|
|
194
|
+
),
|
|
195
|
+
children: [
|
|
196
|
+
/* @__PURE__ */ jsxs(
|
|
197
|
+
"div",
|
|
198
|
+
{
|
|
199
|
+
ref: trackRef,
|
|
200
|
+
className: cn(
|
|
201
|
+
"relative h-1.5 w-full rounded-full border bg-th-elevated",
|
|
202
|
+
isInvalid ? "border-th-loss" : "border-th-line-control",
|
|
203
|
+
"forced-colors:border-[ButtonText] forced-colors:bg-[Canvas]"
|
|
204
|
+
),
|
|
205
|
+
children: [
|
|
206
|
+
/* @__PURE__ */ jsx(
|
|
207
|
+
"div",
|
|
208
|
+
{
|
|
209
|
+
"aria-hidden": "true",
|
|
210
|
+
className: "absolute -inset-y-px rounded-full bg-th-accent forced-colors:bg-[Highlight]",
|
|
211
|
+
style: { insetInlineStart: `${fillStart}%`, width: `${Math.max(fillEnd - fillStart, 0)}%` }
|
|
212
|
+
}
|
|
213
|
+
),
|
|
214
|
+
vals.map((v, i) => {
|
|
215
|
+
const name0 = range ? i === 0 ? strings.sliderMin : strings.sliderMax : void 0;
|
|
216
|
+
const thumbId = `${auto}-thumb-${i}`;
|
|
217
|
+
return /* @__PURE__ */ jsx(
|
|
218
|
+
"div",
|
|
219
|
+
{
|
|
220
|
+
ref: (node) => {
|
|
221
|
+
thumbs.current[i] = node;
|
|
222
|
+
},
|
|
223
|
+
id: range ? thumbId : void 0,
|
|
224
|
+
role: "slider",
|
|
225
|
+
tabIndex: disabled ? void 0 : 0,
|
|
226
|
+
"aria-valuemin": i === 1 ? vals[0] : min,
|
|
227
|
+
"aria-valuemax": i === 0 && range ? vals[1] : max,
|
|
228
|
+
"aria-valuenow": v,
|
|
229
|
+
"aria-valuetext": formatValue ? formatValue(v, i) : void 0,
|
|
230
|
+
"aria-disabled": disabled || void 0,
|
|
231
|
+
"aria-orientation": "horizontal",
|
|
232
|
+
"aria-label": range ? ariaLabel ? `${ariaLabel} ${name0}` : name0 : ariaLabel,
|
|
233
|
+
"aria-labelledby": labelledBy ? range ? `${labelledBy} ${thumbId}` : labelledBy : void 0,
|
|
234
|
+
"aria-describedby": ariaDescribedBy,
|
|
235
|
+
"data-thumb": i,
|
|
236
|
+
"data-dragging": dragIndex === i ? "" : void 0,
|
|
237
|
+
onKeyDown: handleKeyDown(i),
|
|
238
|
+
className: cn(
|
|
239
|
+
"absolute top-1/2 size-5 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 bg-th-surface shadow-card outline-none rtl:translate-x-1/2",
|
|
240
|
+
'before:absolute before:-inset-3 before:content-[""]',
|
|
241
|
+
isInvalid ? "border-th-loss" : "border-th-accent",
|
|
242
|
+
"focus-visible:ring-2 focus-visible:ring-th-accent/40 data-[dragging]:z-10 data-[dragging]:scale-110",
|
|
243
|
+
"transition-transform duration-100 motion-reduce:transition-none",
|
|
244
|
+
"forced-colors:forced-color-adjust-none forced-colors:border-[Highlight] forced-colors:bg-[Canvas]",
|
|
245
|
+
"forced-colors:focus-visible:border-[CanvasText] forced-colors:focus-visible:outline-solid forced-colors:focus-visible:outline-2 forced-colors:focus-visible:outline-[Highlight]"
|
|
246
|
+
),
|
|
247
|
+
style: { insetInlineStart: `${pct(v)}%` }
|
|
248
|
+
},
|
|
249
|
+
i
|
|
250
|
+
);
|
|
251
|
+
})
|
|
252
|
+
]
|
|
253
|
+
}
|
|
254
|
+
),
|
|
255
|
+
name ? vals.map((v, i) => /* @__PURE__ */ jsx("input", { type: "hidden", name, value: String(v), disabled }, i)) : null
|
|
256
|
+
]
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
export {
|
|
261
|
+
Slider
|
|
262
|
+
};
|