nish-ui 2.1.3 → 3.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 +6 -2
- package/dist/chart.js +1 -1
- package/dist/elements-all.js +58 -58
- package/dist/elements-charts.js +1 -1
- package/dist/elements-data.js +1 -1
- package/dist/elements-forms.js +1 -1
- package/dist/elements.js +4 -4
- package/dist/esm/components/accordion.js +3 -2
- package/dist/esm/components/app-shell.js +3 -2
- package/dist/esm/components/avatar.js +4 -3
- package/dist/esm/components/breadcrumbs.js +3 -2
- package/dist/esm/components/calendar-base.js +292 -0
- package/dist/esm/components/calendar-classes.js +78 -0
- package/dist/esm/components/calendar.js +22 -254
- package/dist/esm/components/canvas/legend.js +3 -2
- package/dist/esm/components/canvas/node.js +3 -2
- package/dist/esm/components/canvas/system-canvas.js +3 -2
- package/dist/esm/components/carousel.js +3 -2
- package/dist/esm/components/charts.js +8 -7
- package/dist/esm/components/combobox.js +3 -2
- package/dist/esm/components/command.js +4 -3
- package/dist/esm/components/context-menu.js +3 -2
- package/dist/esm/components/data.js +4 -3
- package/dist/esm/components/date-picker.js +4 -3
- package/dist/esm/components/date-range-picker.js +214 -0
- package/dist/esm/components/display.js +7 -6
- package/dist/esm/components/drawer.js +3 -2
- package/dist/esm/components/error-boundary.js +4 -2
- package/dist/esm/components/feedback.js +7 -6
- package/dist/esm/components/file-dropzone.js +3 -2
- package/dist/esm/components/filters.js +4 -3
- package/dist/esm/components/flow.js +6 -5
- package/dist/esm/components/forms.js +14 -8
- package/dist/esm/components/hooks.js +2 -2
- package/dist/esm/components/hover-card.js +2 -2
- package/dist/esm/components/inbox.js +4 -2
- package/dist/esm/components/layout.js +5 -4
- package/dist/esm/components/media.js +3 -2
- package/dist/esm/components/menu.js +5 -4
- package/dist/esm/components/modal.js +4 -3
- package/dist/esm/components/more-sheet.js +3 -2
- package/dist/esm/components/multi-select.js +3 -2
- package/dist/esm/components/nav-items.js +4 -3
- package/dist/esm/components/nav.js +5 -4
- package/dist/esm/components/overlay-dialog.js +3 -2
- package/dist/esm/components/pagination.js +3 -2
- package/dist/esm/components/popover.js +2 -2
- package/dist/esm/components/portal.js +2 -2
- package/dist/esm/components/progress.js +3 -2
- package/dist/esm/components/provider.js +34 -5
- package/dist/esm/components/range-calendar.js +123 -0
- package/dist/esm/components/service-tokens.js +4 -3
- package/dist/esm/components/settings.js +4 -3
- package/dist/esm/components/slider.js +3 -2
- package/dist/esm/components/split.js +4 -3
- package/dist/esm/components/strings.js +178 -60
- package/dist/esm/components/tag-input.js +3 -2
- package/dist/esm/components/theme-toggle.js +3 -2
- package/dist/esm/components/time-field-classes.js +110 -0
- package/dist/esm/components/time-field.js +309 -0
- package/dist/esm/components/toast-card.js +155 -0
- package/dist/esm/components/toast-store.js +136 -0
- package/dist/esm/components/toast.js +38 -241
- package/dist/esm/components/tooltip.js +2 -2
- package/dist/esm/components/tree-view.js +40 -46
- package/dist/esm/components/virtual-table.js +3 -2
- package/dist/esm/index.js +6 -20
- package/dist/esm/runtime.js +1 -1
- package/dist/esm/system-canvas.js +4 -0
- package/dist/esm/theme.js +7 -7
- package/dist/format.js +1 -1
- package/dist/index.d.ts +172 -24
- package/dist/system-canvas.d.ts +183 -2
- package/dist/theme.d.ts +21 -3
- package/dist/theme.global.js +21 -136
- package/dist/theme.js +21 -136
- package/package.json +1 -1
- package/dist/system-canvas-BLuWd69H.d.ts +0 -183
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/time-field.tsx
|
|
4
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
5
|
+
import { cn } from "../cn.js";
|
|
6
|
+
import { getDefaultLocale } from "../format.js";
|
|
7
|
+
import { resolveLocale } from "./calendar-classes.js";
|
|
8
|
+
import { useIsoLayoutEffect } from "./client.js";
|
|
9
|
+
import { setRef } from "./forms-classes.js";
|
|
10
|
+
import { fieldClass } from "./primitives-classes.js";
|
|
11
|
+
import { Select } from "./primitives.js";
|
|
12
|
+
import { useStrings, useNishContext } from "./provider.js";
|
|
13
|
+
import { TIME_STRINGS } from "./strings.js";
|
|
14
|
+
import {
|
|
15
|
+
dayPeriodNames,
|
|
16
|
+
draftFromTime,
|
|
17
|
+
draftTime,
|
|
18
|
+
from12,
|
|
19
|
+
hourAdvances,
|
|
20
|
+
hourCycleFor,
|
|
21
|
+
hourText,
|
|
22
|
+
inTimeRange,
|
|
23
|
+
lastMinute,
|
|
24
|
+
normalizeStep,
|
|
25
|
+
parseTime,
|
|
26
|
+
stepHour,
|
|
27
|
+
stepMinute
|
|
28
|
+
} from "./time-field-classes.js";
|
|
29
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
30
|
+
var pad2 = (n) => String(n).padStart(2, "0");
|
|
31
|
+
var warned = /* @__PURE__ */ new Set();
|
|
32
|
+
function warnOnce(message) {
|
|
33
|
+
if (process.env.NODE_ENV === "production" || warned.has(message)) return;
|
|
34
|
+
warned.add(message);
|
|
35
|
+
console.warn(message);
|
|
36
|
+
}
|
|
37
|
+
function boxNumber(text, lo, hi) {
|
|
38
|
+
if (!/^\d{1,2}$/.test(text)) return null;
|
|
39
|
+
const n = Number(text);
|
|
40
|
+
return n >= lo && n <= hi ? n : null;
|
|
41
|
+
}
|
|
42
|
+
function TimeField({
|
|
43
|
+
ref,
|
|
44
|
+
value: valueProp,
|
|
45
|
+
defaultValue = null,
|
|
46
|
+
onValueChange,
|
|
47
|
+
min,
|
|
48
|
+
max,
|
|
49
|
+
step,
|
|
50
|
+
hourCycle,
|
|
51
|
+
locale,
|
|
52
|
+
"aria-label": ariaLabel,
|
|
53
|
+
"aria-labelledby": ariaLabelledBy,
|
|
54
|
+
"aria-describedby": ariaDescribedBy,
|
|
55
|
+
"aria-invalid": ariaInvalid,
|
|
56
|
+
invalid,
|
|
57
|
+
id,
|
|
58
|
+
name,
|
|
59
|
+
form,
|
|
60
|
+
required,
|
|
61
|
+
disabled = false,
|
|
62
|
+
size = "md",
|
|
63
|
+
className,
|
|
64
|
+
...rest
|
|
65
|
+
}) {
|
|
66
|
+
const strings = useStrings(TIME_STRINGS);
|
|
67
|
+
const config = useNishContext();
|
|
68
|
+
const loc = resolveLocale(locale ?? config.locale ?? getDefaultLocale());
|
|
69
|
+
const cycle = hourCycle ?? hourCycleFor(loc);
|
|
70
|
+
const st = normalizeStep(step);
|
|
71
|
+
const [am, pm] = dayPeriodNames(loc);
|
|
72
|
+
const minT = parseTime(min) ? min : null;
|
|
73
|
+
const maxT = parseTime(max) ? max : null;
|
|
74
|
+
const auto = useId().replace(/[^a-zA-Z0-9_-]/g, "");
|
|
75
|
+
const inputId = id ?? `nish-tf-${auto}`;
|
|
76
|
+
const nameId = `nish-tf-${auto}-name`;
|
|
77
|
+
const hId = `nish-tf-${auto}-h`;
|
|
78
|
+
const mId = `nish-tf-${auto}-m`;
|
|
79
|
+
const pId = `nish-tf-${auto}-p`;
|
|
80
|
+
const derived = ariaLabelledBy == null && ariaLabel == null && id != null;
|
|
81
|
+
const [labelId, setLabelId] = useState(id != null ? `${id}-label` : null);
|
|
82
|
+
useIsoLayoutEffect(() => {
|
|
83
|
+
if (!derived) return;
|
|
84
|
+
if (document.getElementById(`${id}-label`)) {
|
|
85
|
+
setLabelId(`${id}-label`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const nativeLabel = document.getElementById(id)?.labels?.[0];
|
|
89
|
+
const native = nativeLabel?.id || null;
|
|
90
|
+
setLabelId(native);
|
|
91
|
+
if (!native) {
|
|
92
|
+
warnOnce(
|
|
93
|
+
nativeLabel ? "TimeField: give its <label> an id, or use a Field, an aria-label or aria-labelledby." : "TimeField: give it a Field, an aria-label or aria-labelledby."
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}, [derived, id]);
|
|
97
|
+
const src = ariaLabelledBy ?? (ariaLabel != null ? nameId : derived ? labelId ?? void 0 : void 0);
|
|
98
|
+
const names = (own2) => [src, own2].filter(Boolean).join(" ");
|
|
99
|
+
if (ariaLabel == null && ariaLabelledBy == null && id == null) {
|
|
100
|
+
warnOnce("TimeField: give it a Field, an aria-label or aria-labelledby.");
|
|
101
|
+
}
|
|
102
|
+
const controlled = valueProp !== void 0;
|
|
103
|
+
const okProp = (prop, v) => {
|
|
104
|
+
if (v == null) return null;
|
|
105
|
+
if (parseTime(v)) return v;
|
|
106
|
+
warnOnce(`TimeField: "${prop}" must be HH:mm`);
|
|
107
|
+
return null;
|
|
108
|
+
};
|
|
109
|
+
const [own, setOwn] = useState(() => okProp("defaultValue", defaultValue));
|
|
110
|
+
const value = controlled ? okProp("value", valueProp) : own;
|
|
111
|
+
const [draftState, setDraft] = useState(null);
|
|
112
|
+
const [bad, setBad] = useState(false);
|
|
113
|
+
if (draftState !== null && draftState.base !== value) {
|
|
114
|
+
setDraft(null);
|
|
115
|
+
setBad(false);
|
|
116
|
+
}
|
|
117
|
+
const draft = draftState !== null && draftState.base === value ? draftState.d : null;
|
|
118
|
+
const hourRef = useRef(null);
|
|
119
|
+
const minuteRef = useRef(null);
|
|
120
|
+
const shown = draft ?? draftFromTime(value, cycle);
|
|
121
|
+
const draftBad = () => {
|
|
122
|
+
if (draft === null) return false;
|
|
123
|
+
const t = draftTime(draft, cycle);
|
|
124
|
+
return t === void 0 || t !== null && !inTimeRange(t, minT, maxT);
|
|
125
|
+
};
|
|
126
|
+
const outside = value !== null && !inTimeRange(value, minT, maxT);
|
|
127
|
+
const isInvalid = invalid === true || ariaInvalid === true || ariaInvalid === "true" || bad || outside;
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
hourRef.current?.setCustomValidity(bad || outside ? strings.invalidTime : "");
|
|
130
|
+
}, [bad, outside, strings.invalidTime]);
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
const f = hourRef.current?.form;
|
|
133
|
+
if (!f || controlled) return;
|
|
134
|
+
const onReset = () => {
|
|
135
|
+
setOwn(okProp("defaultValue", defaultValue));
|
|
136
|
+
setDraft(null);
|
|
137
|
+
setBad(false);
|
|
138
|
+
};
|
|
139
|
+
f.addEventListener("reset", onReset);
|
|
140
|
+
return () => f.removeEventListener("reset", onReset);
|
|
141
|
+
}, [name, form, controlled, defaultValue]);
|
|
142
|
+
const commit = (v) => {
|
|
143
|
+
if (!controlled) setOwn(v);
|
|
144
|
+
onValueChange?.(v);
|
|
145
|
+
};
|
|
146
|
+
const update = (next) => {
|
|
147
|
+
const t = draftTime(next, cycle);
|
|
148
|
+
if (t === null) {
|
|
149
|
+
if (value !== null) commit(null);
|
|
150
|
+
setDraft({ d: next, base: null });
|
|
151
|
+
setBad(false);
|
|
152
|
+
} else if (typeof t === "string" && inTimeRange(t, minT, maxT)) {
|
|
153
|
+
if (t !== value) commit(t);
|
|
154
|
+
setDraft({ d: next, base: t });
|
|
155
|
+
setBad(false);
|
|
156
|
+
} else {
|
|
157
|
+
setDraft({ d: next, base: value });
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
const focusMinute = () => {
|
|
161
|
+
minuteRef.current?.focus();
|
|
162
|
+
minuteRef.current?.select();
|
|
163
|
+
};
|
|
164
|
+
const onKey = (which) => (e) => {
|
|
165
|
+
if (disabled) return;
|
|
166
|
+
const key = e.key;
|
|
167
|
+
if (key === "Enter") {
|
|
168
|
+
if (draftBad()) {
|
|
169
|
+
e.preventDefault();
|
|
170
|
+
setBad(true);
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (key === "ArrowUp" || key === "ArrowDown") {
|
|
175
|
+
e.preventDefault();
|
|
176
|
+
const dir = key === "ArrowUp" ? 1 : -1;
|
|
177
|
+
if (which === "h") {
|
|
178
|
+
const hn = boxNumber(shown.h, cycle === 12 ? 1 : 0, cycle === 12 ? 12 : 23);
|
|
179
|
+
const cur = hn === null ? null : cycle === 12 ? from12(hn, shown.pm) : hn;
|
|
180
|
+
const next = stepHour(cur, dir);
|
|
181
|
+
update({ ...shown, h: hourText(next, cycle), pm: next >= 12 });
|
|
182
|
+
} else {
|
|
183
|
+
const cur = boxNumber(shown.m, 0, 59);
|
|
184
|
+
update({ ...shown, m: pad2(stepMinute(cur, dir, st)) });
|
|
185
|
+
}
|
|
186
|
+
} else if (key === "Home") {
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
update(which === "h" ? { ...shown, h: hourText(0, cycle), pm: false } : { ...shown, m: "00" });
|
|
189
|
+
} else if (key === "End") {
|
|
190
|
+
e.preventDefault();
|
|
191
|
+
update(which === "h" ? { ...shown, h: hourText(23, cycle), pm: true } : { ...shown, m: pad2(lastMinute(st)) });
|
|
192
|
+
} else if (cycle === 12 && (key === "a" || key === "A" || key === "p" || key === "P")) {
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
update({ ...shown, pm: key === "p" || key === "P" });
|
|
195
|
+
} else if (which === "h" && (key === ":" || key === ".")) {
|
|
196
|
+
e.preventDefault();
|
|
197
|
+
focusMinute();
|
|
198
|
+
} else if (which === "m" && key === "Backspace" && shown.m === "") {
|
|
199
|
+
e.preventDefault();
|
|
200
|
+
hourRef.current?.focus();
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const hourNow = boxNumber(shown.h, cycle === 12 ? 1 : 0, cycle === 12 ? 12 : 23);
|
|
204
|
+
const minuteNow = boxNumber(shown.m, 0, 59);
|
|
205
|
+
const boxClass = fieldClass({
|
|
206
|
+
size,
|
|
207
|
+
invalid: isInvalid,
|
|
208
|
+
className: "w-12 ps-0 pe-0 text-center tabular-nums pointer-coarse:w-14"
|
|
209
|
+
});
|
|
210
|
+
const common = {
|
|
211
|
+
type: "text",
|
|
212
|
+
inputMode: "numeric",
|
|
213
|
+
autoComplete: "off",
|
|
214
|
+
spellCheck: false,
|
|
215
|
+
maxLength: 2,
|
|
216
|
+
role: "spinbutton",
|
|
217
|
+
placeholder: "--",
|
|
218
|
+
disabled,
|
|
219
|
+
required,
|
|
220
|
+
// The boxes have no name, so nothing extra posts; this puts them (and their validity) in the same form.
|
|
221
|
+
form,
|
|
222
|
+
"aria-required": required || void 0,
|
|
223
|
+
"aria-describedby": ariaDescribedBy,
|
|
224
|
+
"aria-invalid": isInvalid || void 0,
|
|
225
|
+
className: boxClass,
|
|
226
|
+
onFocus: (e) => e.currentTarget.select()
|
|
227
|
+
};
|
|
228
|
+
return /* @__PURE__ */ jsxs(
|
|
229
|
+
"div",
|
|
230
|
+
{
|
|
231
|
+
...rest,
|
|
232
|
+
role: "group",
|
|
233
|
+
"aria-labelledby": src,
|
|
234
|
+
"data-disabled": disabled ? "" : void 0,
|
|
235
|
+
className: cn("inline-flex items-center gap-2", className),
|
|
236
|
+
onBlur: (e) => {
|
|
237
|
+
rest.onBlur?.(e);
|
|
238
|
+
if (e.relatedTarget instanceof Node && e.currentTarget.contains(e.relatedTarget)) return;
|
|
239
|
+
const isBad = draftBad();
|
|
240
|
+
setBad(isBad);
|
|
241
|
+
if (!isBad) setDraft(null);
|
|
242
|
+
},
|
|
243
|
+
children: [
|
|
244
|
+
ariaLabel != null ? /* @__PURE__ */ jsx("span", { id: nameId, hidden: true, children: ariaLabel }) : null,
|
|
245
|
+
/* @__PURE__ */ jsx("span", { id: hId, hidden: true, children: strings.hour }),
|
|
246
|
+
/* @__PURE__ */ jsx("span", { id: mId, hidden: true, children: strings.minute }),
|
|
247
|
+
cycle === 12 ? /* @__PURE__ */ jsx("span", { id: pId, hidden: true, children: strings.dayPeriod }) : null,
|
|
248
|
+
/* @__PURE__ */ jsxs("span", { dir: "ltr", className: "inline-flex items-center gap-1", children: [
|
|
249
|
+
/* @__PURE__ */ jsx(
|
|
250
|
+
"input",
|
|
251
|
+
{
|
|
252
|
+
...common,
|
|
253
|
+
ref: (node) => {
|
|
254
|
+
hourRef.current = node;
|
|
255
|
+
setRef(ref, node);
|
|
256
|
+
},
|
|
257
|
+
id: inputId,
|
|
258
|
+
value: shown.h,
|
|
259
|
+
"aria-labelledby": names(hId),
|
|
260
|
+
"aria-valuemin": cycle === 12 ? 1 : 0,
|
|
261
|
+
"aria-valuemax": cycle === 12 ? 12 : 23,
|
|
262
|
+
"aria-valuenow": hourNow ?? void 0,
|
|
263
|
+
onChange: (e) => {
|
|
264
|
+
const d = e.target.value.replace(/\D/g, "").slice(0, 2);
|
|
265
|
+
update({ ...shown, h: d });
|
|
266
|
+
if (hourAdvances(d, cycle)) focusMinute();
|
|
267
|
+
},
|
|
268
|
+
onKeyDown: (e) => onKey("h")(e)
|
|
269
|
+
}
|
|
270
|
+
),
|
|
271
|
+
/* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "text-th-hi3", children: ":" }),
|
|
272
|
+
/* @__PURE__ */ jsx(
|
|
273
|
+
"input",
|
|
274
|
+
{
|
|
275
|
+
...common,
|
|
276
|
+
ref: minuteRef,
|
|
277
|
+
value: shown.m,
|
|
278
|
+
"aria-labelledby": names(mId),
|
|
279
|
+
"aria-valuemin": 0,
|
|
280
|
+
"aria-valuemax": 59,
|
|
281
|
+
"aria-valuenow": minuteNow ?? void 0,
|
|
282
|
+
onChange: (e) => update({ ...shown, m: e.target.value.replace(/\D/g, "").slice(0, 2) }),
|
|
283
|
+
onKeyDown: (e) => onKey("m")(e)
|
|
284
|
+
}
|
|
285
|
+
)
|
|
286
|
+
] }),
|
|
287
|
+
cycle === 12 ? /* @__PURE__ */ jsxs(
|
|
288
|
+
Select,
|
|
289
|
+
{
|
|
290
|
+
value: shown.pm ? "pm" : "am",
|
|
291
|
+
onChange: (e) => update({ ...shown, pm: e.target.value === "pm" }),
|
|
292
|
+
size,
|
|
293
|
+
disabled,
|
|
294
|
+
"aria-labelledby": names(pId),
|
|
295
|
+
className: "w-auto shrink-0",
|
|
296
|
+
children: [
|
|
297
|
+
/* @__PURE__ */ jsx("option", { value: "am", children: am }),
|
|
298
|
+
/* @__PURE__ */ jsx("option", { value: "pm", children: pm })
|
|
299
|
+
]
|
|
300
|
+
}
|
|
301
|
+
) : null,
|
|
302
|
+
name != null ? /* @__PURE__ */ jsx("input", { type: "hidden", name, value: value ?? "", form, disabled }) : null
|
|
303
|
+
]
|
|
304
|
+
}
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
export {
|
|
308
|
+
TimeField
|
|
309
|
+
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/toast-card.tsx
|
|
4
|
+
import { useRef } from "react";
|
|
5
|
+
import { cn } from "../cn.js";
|
|
6
|
+
import { AlertTriangleIcon, CheckCircleIcon, InfoIcon, XIcon } from "./icons.js";
|
|
7
|
+
import { useStrings } from "./provider.js";
|
|
8
|
+
import { DISPLAY_STRINGS } from "./strings.js";
|
|
9
|
+
import { dismissToast, pauseTimer, resumeTimer } from "./toast-store.js";
|
|
10
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
var TONE = {
|
|
12
|
+
success: {
|
|
13
|
+
icon: /* @__PURE__ */ jsx(CheckCircleIcon, { size: 15, className: "text-th-ok" }),
|
|
14
|
+
className: "border-th-ok/35"
|
|
15
|
+
},
|
|
16
|
+
error: {
|
|
17
|
+
icon: /* @__PURE__ */ jsx(AlertTriangleIcon, { size: 15, className: "text-th-loss" }),
|
|
18
|
+
className: "border-th-loss/40"
|
|
19
|
+
},
|
|
20
|
+
warn: {
|
|
21
|
+
icon: /* @__PURE__ */ jsx(AlertTriangleIcon, { size: 15, className: "text-th-warn" }),
|
|
22
|
+
className: "border-th-warn/40"
|
|
23
|
+
},
|
|
24
|
+
info: {
|
|
25
|
+
icon: /* @__PURE__ */ jsx(InfoIcon, { size: 15, className: "text-th-accent" }),
|
|
26
|
+
className: "border-th-line-strong"
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var SWIPE_DISMISS_FRACTION = 0.35;
|
|
30
|
+
var SWIPE_DISMISS_VELOCITY = 0.5;
|
|
31
|
+
var SWIPE_SLOP = 8;
|
|
32
|
+
function ToastCard({ t }) {
|
|
33
|
+
const strings = useStrings(DISPLAY_STRINGS);
|
|
34
|
+
const ref = useRef(null);
|
|
35
|
+
const drag = useRef(null);
|
|
36
|
+
const setOffset = (dx, width) => {
|
|
37
|
+
const el = ref.current;
|
|
38
|
+
if (!el) return;
|
|
39
|
+
el.style.transform = dx === 0 ? "" : `translateX(${dx}px)`;
|
|
40
|
+
el.style.opacity = dx === 0 ? "" : String(Math.max(0, 1 - Math.abs(dx) / width));
|
|
41
|
+
};
|
|
42
|
+
const onDown = (e) => {
|
|
43
|
+
if (e.pointerType === "mouse") return;
|
|
44
|
+
drag.current = {
|
|
45
|
+
pointerId: e.pointerId,
|
|
46
|
+
startX: e.clientX,
|
|
47
|
+
startY: e.clientY,
|
|
48
|
+
lastX: e.clientX,
|
|
49
|
+
lastT: e.timeStamp,
|
|
50
|
+
velocity: 0,
|
|
51
|
+
active: false
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const onMove = (e) => {
|
|
55
|
+
const d = drag.current;
|
|
56
|
+
const el = ref.current;
|
|
57
|
+
if (!d || !el || d.pointerId !== e.pointerId) return;
|
|
58
|
+
const dx = e.clientX - d.startX;
|
|
59
|
+
if (!d.active) {
|
|
60
|
+
if (Math.abs(dx) < SWIPE_SLOP || Math.abs(dx) < Math.abs(e.clientY - d.startY)) return;
|
|
61
|
+
d.active = true;
|
|
62
|
+
el.setPointerCapture(e.pointerId);
|
|
63
|
+
pauseTimer(t.id);
|
|
64
|
+
}
|
|
65
|
+
const dt = e.timeStamp - d.lastT;
|
|
66
|
+
if (dt > 0) d.velocity = (e.clientX - d.lastX) / dt;
|
|
67
|
+
d.lastX = e.clientX;
|
|
68
|
+
d.lastT = e.timeStamp;
|
|
69
|
+
setOffset(dx, el.offsetWidth);
|
|
70
|
+
};
|
|
71
|
+
const onUp = (e) => {
|
|
72
|
+
const d = drag.current;
|
|
73
|
+
const el = ref.current;
|
|
74
|
+
drag.current = null;
|
|
75
|
+
if (!d || !el || d.pointerId !== e.pointerId || !d.active) return;
|
|
76
|
+
const dx = e.clientX - d.startX;
|
|
77
|
+
const width = el.offsetWidth;
|
|
78
|
+
const flung = Math.abs(d.velocity) > SWIPE_DISMISS_VELOCITY && Math.sign(d.velocity) === Math.sign(dx);
|
|
79
|
+
if (Math.abs(dx) > width * SWIPE_DISMISS_FRACTION || flung) {
|
|
80
|
+
dismissToast(t.id);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
el.style.transition = "transform 160ms var(--nish-ease-out), opacity 160ms var(--nish-ease-out)";
|
|
84
|
+
setOffset(0, width);
|
|
85
|
+
window.setTimeout(() => {
|
|
86
|
+
if (ref.current) ref.current.style.transition = "";
|
|
87
|
+
}, 180);
|
|
88
|
+
resumeTimer(t.id);
|
|
89
|
+
};
|
|
90
|
+
const onCancel = (e) => {
|
|
91
|
+
const d = drag.current;
|
|
92
|
+
drag.current = null;
|
|
93
|
+
if (!d?.active || d.pointerId !== e.pointerId) return;
|
|
94
|
+
setOffset(0, ref.current?.offsetWidth || 1);
|
|
95
|
+
resumeTimer(t.id);
|
|
96
|
+
};
|
|
97
|
+
const leave = (e) => {
|
|
98
|
+
if (!e.currentTarget.contains(e.relatedTarget)) resumeTimer(t.id);
|
|
99
|
+
};
|
|
100
|
+
const enter = (e) => {
|
|
101
|
+
if (!e.currentTarget.contains(e.relatedTarget)) pauseTimer(t.id);
|
|
102
|
+
};
|
|
103
|
+
return /* @__PURE__ */ jsxs(
|
|
104
|
+
"div",
|
|
105
|
+
{
|
|
106
|
+
ref,
|
|
107
|
+
onPointerDown: onDown,
|
|
108
|
+
onPointerMove: onMove,
|
|
109
|
+
onPointerUp: onUp,
|
|
110
|
+
onPointerCancel: onCancel,
|
|
111
|
+
onPointerEnter: (e) => e.pointerType === "mouse" && pauseTimer(t.id),
|
|
112
|
+
onPointerLeave: (e) => e.pointerType === "mouse" && resumeTimer(t.id),
|
|
113
|
+
onFocus: enter,
|
|
114
|
+
onBlur: leave,
|
|
115
|
+
className: cn(
|
|
116
|
+
"pointer-events-auto flex w-full max-w-md items-start gap-2.5 rounded-xl border bg-th-elevated px-3.5 py-3 shadow-pop",
|
|
117
|
+
"touch-pan-y animate-[nish-rise-in_160ms_var(--nish-ease-out)]",
|
|
118
|
+
"max-sm:[html[data-nish-layer-open]_&]:animate-[nish-toast-in-top_200ms_var(--nish-ease-out)]",
|
|
119
|
+
TONE[t.tone].className
|
|
120
|
+
),
|
|
121
|
+
children: [
|
|
122
|
+
/* @__PURE__ */ jsx("span", { className: "mt-0.5 shrink-0", children: TONE[t.tone].icon }),
|
|
123
|
+
/* @__PURE__ */ jsxs("div", { className: "min-w-0 flex-1", children: [
|
|
124
|
+
/* @__PURE__ */ jsx("p", { className: "text-sm font-medium leading-snug text-th-hi", children: t.title }),
|
|
125
|
+
t.body ? /* @__PURE__ */ jsx("p", { className: "mt-0.5 break-words text-xs leading-snug text-th-hi2", children: t.body }) : null
|
|
126
|
+
] }),
|
|
127
|
+
t.action ? /* @__PURE__ */ jsx(
|
|
128
|
+
"button",
|
|
129
|
+
{
|
|
130
|
+
type: "button",
|
|
131
|
+
onClick: () => {
|
|
132
|
+
t.action?.onClick();
|
|
133
|
+
dismissToast(t.id);
|
|
134
|
+
},
|
|
135
|
+
className: "-my-1 inline-flex shrink-0 items-center justify-center rounded-md px-2 py-1 text-sm font-medium text-th-accent transition-colors hover:bg-th-accent-soft pointer-coarse:-my-2.5 pointer-coarse:min-h-11 pointer-coarse:min-w-11",
|
|
136
|
+
children: t.action.label
|
|
137
|
+
}
|
|
138
|
+
) : null,
|
|
139
|
+
/* @__PURE__ */ jsx(
|
|
140
|
+
"button",
|
|
141
|
+
{
|
|
142
|
+
type: "button",
|
|
143
|
+
onClick: () => dismissToast(t.id),
|
|
144
|
+
"aria-label": strings.dismiss,
|
|
145
|
+
className: "-m-1 inline-flex shrink-0 items-center justify-center rounded-md p-1 text-th-hi3 transition-colors hover:bg-th-overlay hover:text-th-hi pointer-coarse:-my-2 pointer-coarse:min-h-11 pointer-coarse:min-w-11",
|
|
146
|
+
children: /* @__PURE__ */ jsx(XIcon, { size: 14 })
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
export {
|
|
154
|
+
ToastCard
|
|
155
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/toast-store.ts
|
|
4
|
+
import { useSyncExternalStore } from "react";
|
|
5
|
+
import { shared } from "../runtime.js";
|
|
6
|
+
var AUTO_DISMISS_MS = 4e3;
|
|
7
|
+
var MAX_VISIBLE = 4;
|
|
8
|
+
var cachedStore;
|
|
9
|
+
function store() {
|
|
10
|
+
return cachedStore ??= shared("toast", () => ({
|
|
11
|
+
items: [],
|
|
12
|
+
nextId: 1,
|
|
13
|
+
subs: /* @__PURE__ */ new Set(),
|
|
14
|
+
timers: /* @__PURE__ */ new Map()
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
var emit = () => store().subs.forEach((fn) => fn());
|
|
18
|
+
function startTimer(id, ms) {
|
|
19
|
+
const t = {
|
|
20
|
+
handle: null,
|
|
21
|
+
remaining: ms,
|
|
22
|
+
started: Date.now(),
|
|
23
|
+
paused: 0
|
|
24
|
+
};
|
|
25
|
+
t.handle = setTimeout(() => dismissToast(id), ms);
|
|
26
|
+
store().timers.set(id, t);
|
|
27
|
+
}
|
|
28
|
+
function clearTimer(id) {
|
|
29
|
+
const { timers } = store();
|
|
30
|
+
const t = timers.get(id);
|
|
31
|
+
if (t?.handle != null) clearTimeout(t.handle);
|
|
32
|
+
timers.delete(id);
|
|
33
|
+
}
|
|
34
|
+
function pauseTimer(id) {
|
|
35
|
+
const t = store().timers.get(id);
|
|
36
|
+
if (!t) return;
|
|
37
|
+
t.paused += 1;
|
|
38
|
+
if (t.paused > 1 || t.handle == null) return;
|
|
39
|
+
clearTimeout(t.handle);
|
|
40
|
+
t.handle = null;
|
|
41
|
+
t.remaining = Math.max(0, t.remaining - (Date.now() - t.started));
|
|
42
|
+
}
|
|
43
|
+
function resumeTimer(id) {
|
|
44
|
+
const t = store().timers.get(id);
|
|
45
|
+
if (!t || t.paused === 0) return;
|
|
46
|
+
t.paused -= 1;
|
|
47
|
+
if (t.paused > 0) return;
|
|
48
|
+
t.started = Date.now();
|
|
49
|
+
t.handle = setTimeout(() => dismissToast(id), Math.max(t.remaining, 1e3));
|
|
50
|
+
}
|
|
51
|
+
function dismissToast(id) {
|
|
52
|
+
clearTimer(id);
|
|
53
|
+
const s = store();
|
|
54
|
+
const next = s.items.filter((t) => t.id !== id);
|
|
55
|
+
if (next.length === s.items.length) return;
|
|
56
|
+
s.items = next;
|
|
57
|
+
emit();
|
|
58
|
+
}
|
|
59
|
+
var isKept = (t) => t.duration === "sticky" || t.tone === "error";
|
|
60
|
+
function push(tone, title, bodyOrOptions) {
|
|
61
|
+
const o = typeof bodyOrOptions === "string" ? { body: bodyOrOptions } : bodyOrOptions ?? {};
|
|
62
|
+
const s = store();
|
|
63
|
+
const id = s.nextId++;
|
|
64
|
+
const asked = o.duration === Infinity ? "sticky" : o.duration;
|
|
65
|
+
const duration = asked ?? (tone === "error" ? "sticky" : AUTO_DISMISS_MS);
|
|
66
|
+
const item = {
|
|
67
|
+
id,
|
|
68
|
+
tone,
|
|
69
|
+
title,
|
|
70
|
+
body: o.body,
|
|
71
|
+
action: o.action,
|
|
72
|
+
slot: o.id,
|
|
73
|
+
duration
|
|
74
|
+
};
|
|
75
|
+
const old = o.id ? s.items.find((t) => t.slot === o.id) : void 0;
|
|
76
|
+
if (old) {
|
|
77
|
+
clearTimer(old.id);
|
|
78
|
+
s.items = s.items.map((t) => t === old ? item : t);
|
|
79
|
+
} else {
|
|
80
|
+
let excess = s.items.length + 1 - MAX_VISIBLE;
|
|
81
|
+
const kept = [];
|
|
82
|
+
for (const t of s.items) {
|
|
83
|
+
if (excess > 0 && !isKept(t)) {
|
|
84
|
+
excess--;
|
|
85
|
+
clearTimer(t.id);
|
|
86
|
+
} else {
|
|
87
|
+
kept.push(t);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
s.items = [...kept, item];
|
|
91
|
+
}
|
|
92
|
+
emit();
|
|
93
|
+
if (duration !== "sticky") startTimer(id, duration);
|
|
94
|
+
return id;
|
|
95
|
+
}
|
|
96
|
+
var toast = /* @__PURE__ */ Object.assign((title, o) => push("info", title, o), {
|
|
97
|
+
success: (title, o) => push("success", title, o),
|
|
98
|
+
error: (title, o) => push("error", title, o),
|
|
99
|
+
warn: (title, o) => push("warn", title, o),
|
|
100
|
+
info: (title, o) => push("info", title, o),
|
|
101
|
+
promise: (promise, messages) => {
|
|
102
|
+
const slot = "nish-promise-" + store().nextId;
|
|
103
|
+
push("info", messages.loading, { duration: "sticky", id: slot });
|
|
104
|
+
promise.then(
|
|
105
|
+
(value) => {
|
|
106
|
+
push("success", typeof messages.success === "function" ? messages.success(value) : messages.success, { id: slot });
|
|
107
|
+
},
|
|
108
|
+
(error) => {
|
|
109
|
+
push("error", typeof messages.error === "function" ? messages.error(error) : messages.error, { id: slot });
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
return promise;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
function useToast() {
|
|
116
|
+
return toast;
|
|
117
|
+
}
|
|
118
|
+
function useToasts() {
|
|
119
|
+
return useSyncExternalStore(
|
|
120
|
+
(fn) => {
|
|
121
|
+
const { subs } = store();
|
|
122
|
+
subs.add(fn);
|
|
123
|
+
return () => subs.delete(fn);
|
|
124
|
+
},
|
|
125
|
+
() => store().items,
|
|
126
|
+
() => store().items
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
export {
|
|
130
|
+
dismissToast,
|
|
131
|
+
pauseTimer,
|
|
132
|
+
resumeTimer,
|
|
133
|
+
toast,
|
|
134
|
+
useToast,
|
|
135
|
+
useToasts
|
|
136
|
+
};
|