nish-ui 2.0.2 → 2.1.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 +18 -8
- package/dist/chart.js +1 -1
- package/dist/elements-all.js +13 -13
- 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 +3 -3
- package/dist/esm/components/app-shell.js +145 -45
- package/dist/esm/components/breadcrumbs.js +1 -1
- package/dist/esm/components/calendar-classes.js +325 -0
- package/dist/esm/components/calendar.js +263 -0
- package/dist/esm/components/canvas/route.js +56 -20
- package/dist/esm/components/carousel-classes.js +27 -0
- package/dist/esm/components/carousel.js +317 -0
- package/dist/esm/components/command.js +2 -27
- package/dist/esm/components/context-menu.js +199 -0
- package/dist/esm/components/date-picker.js +208 -0
- package/dist/esm/components/display-classes.js +10 -13
- package/dist/esm/components/display.js +22 -19
- package/dist/esm/components/drawer.js +139 -0
- package/dist/esm/components/filters.js +1 -1
- package/dist/esm/components/flow.js +1 -1
- package/dist/esm/components/hover-card.js +221 -0
- package/dist/esm/components/icons.js +17 -0
- package/dist/esm/components/inbox.js +2 -1
- package/dist/esm/components/lazy-module.js +53 -0
- package/dist/esm/components/menu.js +344 -0
- package/dist/esm/components/modal.js +173 -0
- package/dist/esm/components/more-sheet.js +52 -0
- package/dist/esm/components/multi-select.js +420 -0
- package/dist/esm/components/nav-items.js +138 -0
- package/dist/esm/components/nav.js +13 -174
- package/dist/esm/components/navigate.js +33 -0
- package/dist/esm/components/overlay-core.js +221 -0
- package/dist/esm/components/overlay-dialog.js +255 -0
- package/dist/esm/components/overlay-position.js +163 -0
- package/dist/esm/components/overlay.js +7 -1441
- package/dist/esm/components/popover.js +92 -0
- package/dist/esm/components/segmented.js +2 -1
- package/dist/esm/components/split.js +1 -1
- package/dist/esm/components/strings.js +25 -0
- package/dist/esm/components/tooltip.js +177 -0
- package/dist/esm/components/tree-view-classes.js +57 -0
- package/dist/esm/components/tree-view.js +319 -0
- package/dist/esm/components/virtual-table.js +245 -0
- package/dist/esm/components/virtual.js +291 -0
- package/dist/esm/inbox.js +12 -0
- package/dist/esm/index.js +61 -2
- package/dist/esm/runtime.js +1 -1
- package/dist/esm/service-tokens.js +6 -0
- package/dist/esm/theme.js +2 -2
- package/dist/esm/virtual-range.js +73 -0
- package/dist/format.js +1 -1
- package/dist/inbox.d.ts +238 -0
- package/dist/index.d.ts +1030 -777
- package/dist/nish-components.css +2 -0
- package/dist/nish-components.min.css +1 -1
- package/dist/service-tokens.d.ts +60 -0
- package/dist/theme.d.ts +50 -0
- package/dist/theme.global.js +28 -3
- package/dist/theme.js +28 -3
- package/package.json +17 -1
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/multi-select.tsx
|
|
4
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { cn } from "../cn.js";
|
|
6
|
+
import { CheckIcon, XIcon } from "./icons.js";
|
|
7
|
+
import { Spinner } from "./feedback.js";
|
|
8
|
+
import { Chip } from "./forms.js";
|
|
9
|
+
import { fieldClass } from "./primitives.js";
|
|
10
|
+
import { useNishStrings } from "./provider.js";
|
|
11
|
+
import { indexComboboxOptions, setRef } from "./forms-classes.js";
|
|
12
|
+
import { COMBOBOX_DEBOUNCE_MS, COMBOBOX_MAX_SHOWN } from "../combobox-filter.js";
|
|
13
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
14
|
+
var defaultResultsText = (n) => `${n} ${n === 1 ? "result" : "results"}`;
|
|
15
|
+
function MultiSelect({
|
|
16
|
+
ref,
|
|
17
|
+
options,
|
|
18
|
+
value: valueProp,
|
|
19
|
+
defaultValue,
|
|
20
|
+
onValueChange,
|
|
21
|
+
onChange,
|
|
22
|
+
filter,
|
|
23
|
+
maxSelected,
|
|
24
|
+
placeholder,
|
|
25
|
+
"aria-label": ariaLabel,
|
|
26
|
+
"aria-labelledby": ariaLabelledBy,
|
|
27
|
+
"aria-describedby": ariaDescribedBy,
|
|
28
|
+
"aria-invalid": ariaInvalid,
|
|
29
|
+
invalid,
|
|
30
|
+
id,
|
|
31
|
+
name,
|
|
32
|
+
form,
|
|
33
|
+
required,
|
|
34
|
+
disabled = false,
|
|
35
|
+
emptyText,
|
|
36
|
+
onQueryChange,
|
|
37
|
+
loading = false,
|
|
38
|
+
loadingText,
|
|
39
|
+
resultsText = defaultResultsText,
|
|
40
|
+
size = "md",
|
|
41
|
+
clearLabel,
|
|
42
|
+
className,
|
|
43
|
+
...rest
|
|
44
|
+
}) {
|
|
45
|
+
const strings = useNishStrings();
|
|
46
|
+
const autoId = useId().replace(/[^a-zA-Z0-9_-]/g, "");
|
|
47
|
+
const inputId = id ?? `nish-ms-${autoId}`;
|
|
48
|
+
const listId = `nish-ms-list-${autoId}`;
|
|
49
|
+
const empty = emptyText ?? strings.noMatches;
|
|
50
|
+
const searching = loadingText ?? strings.searching;
|
|
51
|
+
const defaultRef = useRef(defaultValue);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
defaultRef.current = defaultValue;
|
|
54
|
+
});
|
|
55
|
+
const [own, setOwn] = useState(defaultValue ?? []);
|
|
56
|
+
const controlled = valueProp !== void 0;
|
|
57
|
+
const values = controlled ? valueProp : own;
|
|
58
|
+
const [open, setOpen] = useState(false);
|
|
59
|
+
const [text, setText] = useState("");
|
|
60
|
+
const [active, setActive] = useState(-1);
|
|
61
|
+
const [armed, setArmed] = useState(false);
|
|
62
|
+
const [announcement, setAnnouncement] = useState("");
|
|
63
|
+
const inputRef = useRef(null);
|
|
64
|
+
const listRef = useRef(null);
|
|
65
|
+
const debounceRef = useRef(void 0);
|
|
66
|
+
const [seen, setSeen] = useState({});
|
|
67
|
+
const indexed = useMemo(() => indexComboboxOptions(options), [options]);
|
|
68
|
+
{
|
|
69
|
+
const cur = valueProp ?? own;
|
|
70
|
+
let next = null;
|
|
71
|
+
const done = /* @__PURE__ */ new Set();
|
|
72
|
+
for (const o of options) {
|
|
73
|
+
if (done.has(o.value)) continue;
|
|
74
|
+
done.add(o.value);
|
|
75
|
+
if (cur.includes(o.value) && seen[o.value]?.label !== o.label) (next ??= { ...seen })[o.value] = o;
|
|
76
|
+
}
|
|
77
|
+
if (next) setSeen(next);
|
|
78
|
+
}
|
|
79
|
+
const visible = useMemo(() => {
|
|
80
|
+
const serverFiltered = onQueryChange != null && filter == null;
|
|
81
|
+
const needle = text.trim().toLowerCase();
|
|
82
|
+
const out = [];
|
|
83
|
+
for (const it of indexed) {
|
|
84
|
+
if (out.length >= COMBOBOX_MAX_SHOWN) break;
|
|
85
|
+
if (needle && !serverFiltered) {
|
|
86
|
+
if (filter) {
|
|
87
|
+
if (!filter(it.option, text)) continue;
|
|
88
|
+
} else if (!it.label.includes(needle) && !(it.hint?.includes(needle) ?? false)) continue;
|
|
89
|
+
}
|
|
90
|
+
out.push(it.option);
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}, [indexed, text, filter, onQueryChange]);
|
|
94
|
+
const current = armed ? visible.findIndex((o) => !o.disabled) : active;
|
|
95
|
+
const atLimit = maxSelected != null && values.length >= maxSelected;
|
|
96
|
+
useEffect(() => () => window.clearTimeout(debounceRef.current), []);
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (process.env.NODE_ENV !== "production" && !ariaLabel && !ariaLabelledBy && id == null) {
|
|
99
|
+
console.warn("MultiSelect needs a name: pass aria-label, aria-labelledby, or an id with a <label htmlFor>.");
|
|
100
|
+
}
|
|
101
|
+
}, [ariaLabel, ariaLabelledBy, id]);
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
const f = inputRef.current?.form;
|
|
104
|
+
if (!f || controlled) return;
|
|
105
|
+
const onReset = () => setOwn(defaultRef.current ?? []);
|
|
106
|
+
f.addEventListener("reset", onReset);
|
|
107
|
+
return () => f.removeEventListener("reset", onReset);
|
|
108
|
+
}, [controlled, form]);
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
if (!open || current < 0) return;
|
|
111
|
+
const el = listRef.current?.querySelector(`[data-index="${current}"]`);
|
|
112
|
+
el?.scrollIntoView?.({ block: "nearest" });
|
|
113
|
+
}, [open, current]);
|
|
114
|
+
const toOption = (v) => options.find((o) => o.value === v) ?? seen[v] ?? { value: v, label: v };
|
|
115
|
+
const labelOf = (v) => toOption(v).label;
|
|
116
|
+
const close = useCallback(() => {
|
|
117
|
+
setOpen(false);
|
|
118
|
+
setActive(-1);
|
|
119
|
+
setArmed(false);
|
|
120
|
+
}, []);
|
|
121
|
+
const queryChanged = (q) => {
|
|
122
|
+
if (!onQueryChange) return;
|
|
123
|
+
window.clearTimeout(debounceRef.current);
|
|
124
|
+
debounceRef.current = window.setTimeout(() => onQueryChange(q), COMBOBOX_DEBOUNCE_MS);
|
|
125
|
+
};
|
|
126
|
+
const commit = (next) => {
|
|
127
|
+
if (!controlled) setOwn(next);
|
|
128
|
+
onValueChange?.(next);
|
|
129
|
+
onChange?.(next, next.map(toOption));
|
|
130
|
+
};
|
|
131
|
+
const toggle = (o) => {
|
|
132
|
+
if (o.disabled || disabled) return;
|
|
133
|
+
const picked = values.includes(o.value);
|
|
134
|
+
if (picked) {
|
|
135
|
+
commit(values.filter((v) => v !== o.value));
|
|
136
|
+
setAnnouncement(strings.optionRemoved(o.label));
|
|
137
|
+
} else {
|
|
138
|
+
if (maxSelected != null && values.length >= maxSelected) {
|
|
139
|
+
setAnnouncement(strings.maxSelected(maxSelected));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
setSeen((s) => ({ ...s, [o.value]: o }));
|
|
143
|
+
commit([...values, o.value]);
|
|
144
|
+
setAnnouncement(strings.optionAdded(o.label));
|
|
145
|
+
}
|
|
146
|
+
if (text !== "") {
|
|
147
|
+
setText("");
|
|
148
|
+
queryChanged("");
|
|
149
|
+
}
|
|
150
|
+
setArmed(false);
|
|
151
|
+
const at = options.findIndex((x) => x.value === o.value);
|
|
152
|
+
setActive(at >= 0 && at < COMBOBOX_MAX_SHOWN ? at : -1);
|
|
153
|
+
};
|
|
154
|
+
const removeValue = (v) => {
|
|
155
|
+
commit(values.filter((x) => x !== v));
|
|
156
|
+
setAnnouncement(strings.optionRemoved(labelOf(v)));
|
|
157
|
+
inputRef.current?.focus();
|
|
158
|
+
};
|
|
159
|
+
const step = (from, dir) => {
|
|
160
|
+
const n = visible.length;
|
|
161
|
+
if (n === 0) return -1;
|
|
162
|
+
let i = from;
|
|
163
|
+
for (let k = 0; k < n; k++) {
|
|
164
|
+
i = (i + dir + n) % n;
|
|
165
|
+
if (!visible[i].disabled) return i;
|
|
166
|
+
}
|
|
167
|
+
return -1;
|
|
168
|
+
};
|
|
169
|
+
const edge = (dir) => dir === 1 ? step(-1, 1) : step(0, -1);
|
|
170
|
+
const firstPickedAt = () => visible.findIndex((o) => !o.disabled && values.includes(o.value));
|
|
171
|
+
function openList() {
|
|
172
|
+
if (open || disabled) return;
|
|
173
|
+
setOpen(true);
|
|
174
|
+
setArmed(false);
|
|
175
|
+
setAnnouncement("");
|
|
176
|
+
setActive(firstPickedAt());
|
|
177
|
+
}
|
|
178
|
+
function onInput(next) {
|
|
179
|
+
setText(next);
|
|
180
|
+
setOpen(true);
|
|
181
|
+
setActive(-1);
|
|
182
|
+
setArmed(next.trim() !== "");
|
|
183
|
+
setAnnouncement("");
|
|
184
|
+
queryChanged(next);
|
|
185
|
+
}
|
|
186
|
+
function onKeyDown(e) {
|
|
187
|
+
if (disabled || e.nativeEvent.isComposing) return;
|
|
188
|
+
switch (e.key) {
|
|
189
|
+
case "ArrowDown":
|
|
190
|
+
case "ArrowUp": {
|
|
191
|
+
e.preventDefault();
|
|
192
|
+
const dir = e.key === "ArrowDown" ? 1 : -1;
|
|
193
|
+
setArmed(false);
|
|
194
|
+
if (!open) {
|
|
195
|
+
setOpen(true);
|
|
196
|
+
setAnnouncement("");
|
|
197
|
+
const at = firstPickedAt();
|
|
198
|
+
setActive(at >= 0 ? at : edge(dir));
|
|
199
|
+
} else {
|
|
200
|
+
setActive(current < 0 ? edge(dir) : step(current, dir));
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "Home":
|
|
205
|
+
case "End":
|
|
206
|
+
if (open) {
|
|
207
|
+
e.preventDefault();
|
|
208
|
+
setArmed(false);
|
|
209
|
+
setActive(edge(e.key === "Home" ? 1 : -1));
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
case "Enter":
|
|
213
|
+
if (open && current >= 0 && visible[current]) {
|
|
214
|
+
e.preventDefault();
|
|
215
|
+
toggle(visible[current]);
|
|
216
|
+
} else if (open) {
|
|
217
|
+
e.preventDefault();
|
|
218
|
+
}
|
|
219
|
+
break;
|
|
220
|
+
case "Escape":
|
|
221
|
+
if (open) {
|
|
222
|
+
e.preventDefault();
|
|
223
|
+
e.stopPropagation();
|
|
224
|
+
close();
|
|
225
|
+
} else if (text !== "") {
|
|
226
|
+
e.stopPropagation();
|
|
227
|
+
setText("");
|
|
228
|
+
queryChanged("");
|
|
229
|
+
}
|
|
230
|
+
break;
|
|
231
|
+
case "Backspace": {
|
|
232
|
+
const el = e.currentTarget;
|
|
233
|
+
if (text === "" && el.selectionStart === 0 && el.selectionEnd === 0 && values.length > 0) {
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
removeValue(values[values.length - 1]);
|
|
236
|
+
}
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
case "Tab":
|
|
240
|
+
close();
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function clearAll() {
|
|
245
|
+
commit([]);
|
|
246
|
+
setAnnouncement("");
|
|
247
|
+
inputRef.current?.focus();
|
|
248
|
+
}
|
|
249
|
+
function onBoxClick(e) {
|
|
250
|
+
if (disabled) return;
|
|
251
|
+
if (e.target.closest('button, input, [role="option"]') === null) {
|
|
252
|
+
inputRef.current?.focus();
|
|
253
|
+
openList();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const activeId = open && current >= 0 && visible[current] ? `${listId}-opt-${current}` : void 0;
|
|
257
|
+
const isInvalid = invalid || ariaInvalid === true || ariaInvalid === "true";
|
|
258
|
+
const status = announcement || (open ? loading ? searching : visible.length === 0 ? empty : resultsText(visible.length) : "");
|
|
259
|
+
return /* @__PURE__ */ jsxs(
|
|
260
|
+
"div",
|
|
261
|
+
{
|
|
262
|
+
...rest,
|
|
263
|
+
"data-state": open ? "open" : "closed",
|
|
264
|
+
"data-disabled": disabled ? "" : void 0,
|
|
265
|
+
"data-invalid": isInvalid ? "" : void 0,
|
|
266
|
+
onBlur: (e) => {
|
|
267
|
+
rest.onBlur?.(e);
|
|
268
|
+
if (!e.currentTarget.contains(e.relatedTarget)) {
|
|
269
|
+
close();
|
|
270
|
+
setText("");
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
className: cn("relative w-full min-w-0", className),
|
|
274
|
+
children: [
|
|
275
|
+
/* @__PURE__ */ jsxs(
|
|
276
|
+
"div",
|
|
277
|
+
{
|
|
278
|
+
"data-disabled": disabled ? "" : void 0,
|
|
279
|
+
onClick: onBoxClick,
|
|
280
|
+
className: fieldClass({
|
|
281
|
+
size,
|
|
282
|
+
invalid: isInvalid,
|
|
283
|
+
className: cn(
|
|
284
|
+
"relative flex h-auto cursor-text flex-wrap items-center gap-1.5 py-1 pe-11",
|
|
285
|
+
"focus-within:border-th-accent focus-within:ring-2 focus-within:ring-th-accent/25",
|
|
286
|
+
"data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50",
|
|
287
|
+
"forced-colors:focus-within:outline-solid forced-colors:focus-within:outline-2 forced-colors:focus-within:outline-[Highlight]"
|
|
288
|
+
)
|
|
289
|
+
}),
|
|
290
|
+
children: [
|
|
291
|
+
values.map((v) => /* @__PURE__ */ jsx(
|
|
292
|
+
Chip,
|
|
293
|
+
{
|
|
294
|
+
variant: "tag",
|
|
295
|
+
size: "sm",
|
|
296
|
+
removeLabel: strings.removeNamed(labelOf(v)),
|
|
297
|
+
onRemove: disabled ? void 0 : () => removeValue(v),
|
|
298
|
+
children: labelOf(v)
|
|
299
|
+
},
|
|
300
|
+
v
|
|
301
|
+
)),
|
|
302
|
+
/* @__PURE__ */ jsx(
|
|
303
|
+
"input",
|
|
304
|
+
{
|
|
305
|
+
ref: (node) => {
|
|
306
|
+
inputRef.current = node;
|
|
307
|
+
setRef(ref, node);
|
|
308
|
+
},
|
|
309
|
+
id: inputId,
|
|
310
|
+
type: "text",
|
|
311
|
+
role: "combobox",
|
|
312
|
+
"aria-expanded": open,
|
|
313
|
+
"aria-controls": open ? listId : void 0,
|
|
314
|
+
"aria-autocomplete": "list",
|
|
315
|
+
"aria-activedescendant": activeId,
|
|
316
|
+
"aria-label": ariaLabel,
|
|
317
|
+
"aria-labelledby": ariaLabelledBy,
|
|
318
|
+
"aria-describedby": ariaDescribedBy,
|
|
319
|
+
"aria-invalid": isInvalid || void 0,
|
|
320
|
+
"aria-required": required || void 0,
|
|
321
|
+
required: required && values.length === 0,
|
|
322
|
+
autoComplete: "off",
|
|
323
|
+
autoCapitalize: "off",
|
|
324
|
+
spellCheck: false,
|
|
325
|
+
placeholder: values.length === 0 ? placeholder : void 0,
|
|
326
|
+
disabled,
|
|
327
|
+
value: text,
|
|
328
|
+
onChange: (e) => onInput(e.target.value),
|
|
329
|
+
onKeyDown,
|
|
330
|
+
onClick: openList,
|
|
331
|
+
className: "min-w-24 flex-1 bg-transparent py-0.5 text-th-hi outline-none placeholder:text-th-hi3"
|
|
332
|
+
}
|
|
333
|
+
),
|
|
334
|
+
values.length > 0 && !disabled ? /* @__PURE__ */ jsx(
|
|
335
|
+
"button",
|
|
336
|
+
{
|
|
337
|
+
type: "button",
|
|
338
|
+
"aria-label": clearLabel ?? strings.clearAll,
|
|
339
|
+
tabIndex: -1,
|
|
340
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
341
|
+
onClick: clearAll,
|
|
342
|
+
className: "absolute -inset-y-px -end-px flex w-11 items-center justify-center text-th-hi3 hover:text-th-hi",
|
|
343
|
+
children: /* @__PURE__ */ jsx("span", { className: "flex size-8 items-center justify-center rounded-md hover:bg-th-elevated", children: /* @__PURE__ */ jsx(XIcon, { size: 16 }) })
|
|
344
|
+
}
|
|
345
|
+
) : null
|
|
346
|
+
]
|
|
347
|
+
}
|
|
348
|
+
),
|
|
349
|
+
name != null ? values.map((v) => /* @__PURE__ */ jsx("input", { type: "hidden", name, value: v, form, disabled }, v)) : null,
|
|
350
|
+
/* @__PURE__ */ jsx("div", { role: "status", "aria-live": "polite", className: "sr-only", children: status }),
|
|
351
|
+
open ? /* @__PURE__ */ jsxs(
|
|
352
|
+
"ul",
|
|
353
|
+
{
|
|
354
|
+
ref: listRef,
|
|
355
|
+
id: listId,
|
|
356
|
+
role: "listbox",
|
|
357
|
+
"aria-multiselectable": "true",
|
|
358
|
+
"aria-label": ariaLabel,
|
|
359
|
+
"aria-labelledby": ariaLabel ? void 0 : ariaLabelledBy ?? inputId,
|
|
360
|
+
className: "absolute inset-x-0 top-full z-[var(--nish-z-dropdown,40)] mt-1 max-h-[min(16rem,40dvh)] overflow-auto rounded-xl border border-th-line bg-th-elevated shadow-pop",
|
|
361
|
+
children: [
|
|
362
|
+
visible.map((o, i) => {
|
|
363
|
+
const picked = values.includes(o.value);
|
|
364
|
+
const limited = atLimit && !picked;
|
|
365
|
+
const off = o.disabled || limited;
|
|
366
|
+
return /* @__PURE__ */ jsxs(
|
|
367
|
+
"li",
|
|
368
|
+
{
|
|
369
|
+
id: `${listId}-opt-${i}`,
|
|
370
|
+
role: "option",
|
|
371
|
+
"data-index": i,
|
|
372
|
+
"data-state": i === current ? "active" : "inactive",
|
|
373
|
+
"data-disabled": off ? "" : void 0,
|
|
374
|
+
"aria-selected": picked,
|
|
375
|
+
"aria-disabled": off || void 0,
|
|
376
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
377
|
+
onMouseMove: () => {
|
|
378
|
+
if (o.disabled || !armed && active === i) return;
|
|
379
|
+
setArmed(false);
|
|
380
|
+
setActive(i);
|
|
381
|
+
},
|
|
382
|
+
onClick: () => toggle(o),
|
|
383
|
+
className: cn(
|
|
384
|
+
"flex min-h-11 cursor-pointer items-center gap-2 px-3 text-sm text-th-hi2",
|
|
385
|
+
i === current && "bg-th-accent-soft text-th-hi forced-colors:forced-color-adjust-none forced-colors:bg-[color:Highlight] forced-colors:text-[color:HighlightText]",
|
|
386
|
+
off && "cursor-not-allowed opacity-50 forced-colors:text-[color:GrayText]"
|
|
387
|
+
),
|
|
388
|
+
children: [
|
|
389
|
+
/* @__PURE__ */ jsx(
|
|
390
|
+
"span",
|
|
391
|
+
{
|
|
392
|
+
"aria-hidden": "true",
|
|
393
|
+
className: cn(
|
|
394
|
+
"flex size-4 shrink-0 items-center justify-center rounded border",
|
|
395
|
+
picked ? "border-th-accent bg-th-accent text-th-on-accent forced-colors:bg-[Highlight]" : "border-th-line-control"
|
|
396
|
+
),
|
|
397
|
+
children: picked ? /* @__PURE__ */ jsx(CheckIcon, { size: 12 }) : null
|
|
398
|
+
}
|
|
399
|
+
),
|
|
400
|
+
/* @__PURE__ */ jsx("span", { className: "min-w-0 flex-1 truncate", children: o.label }),
|
|
401
|
+
o.hint ? /* @__PURE__ */ jsx("span", { className: cn("shrink-0 truncate text-xs text-th-hi3", i === current && "forced-colors:text-[color:HighlightText]"), children: o.hint }) : null
|
|
402
|
+
]
|
|
403
|
+
},
|
|
404
|
+
o.value
|
|
405
|
+
);
|
|
406
|
+
}),
|
|
407
|
+
loading ? /* @__PURE__ */ jsxs("li", { role: "presentation", className: "flex min-h-11 items-center gap-2 px-3 text-sm text-th-hi3", children: [
|
|
408
|
+
/* @__PURE__ */ jsx(Spinner, { size: "sm" }),
|
|
409
|
+
searching
|
|
410
|
+
] }) : visible.length === 0 ? /* @__PURE__ */ jsx("li", { role: "presentation", className: "flex min-h-11 items-center px-3 text-sm text-th-hi3", children: empty }) : null
|
|
411
|
+
]
|
|
412
|
+
}
|
|
413
|
+
) : null
|
|
414
|
+
]
|
|
415
|
+
}
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
export {
|
|
419
|
+
MultiSelect
|
|
420
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/nav-items.tsx
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { safeUrl } from "../url.js";
|
|
6
|
+
import { isRtl, useIsoLayoutEffect } from "./client.js";
|
|
7
|
+
import { navigateFromLayer } from "./navigate.js";
|
|
8
|
+
import { warnDev } from "./display-classes.js";
|
|
9
|
+
import { useNishConfig, useNishStrings } from "./provider.js";
|
|
10
|
+
import { jsx } from "react/jsx-runtime";
|
|
11
|
+
function useEndSide(ref) {
|
|
12
|
+
const dir = useNishConfig().dir;
|
|
13
|
+
const [rtl, setRtl] = useState(dir === "rtl");
|
|
14
|
+
useIsoLayoutEffect(() => {
|
|
15
|
+
setRtl(dir ? dir === "rtl" : isRtl(ref.current));
|
|
16
|
+
}, [dir, ref]);
|
|
17
|
+
return rtl ? "left" : "right";
|
|
18
|
+
}
|
|
19
|
+
function NavBadge({ value }) {
|
|
20
|
+
const strings = useNishStrings();
|
|
21
|
+
if (value === void 0 || value === false || value === 0) return null;
|
|
22
|
+
if (value === true) {
|
|
23
|
+
return /* @__PURE__ */ jsx(
|
|
24
|
+
"span",
|
|
25
|
+
{
|
|
26
|
+
"aria-hidden": "true",
|
|
27
|
+
className: "absolute -end-0.5 -top-0.5 size-2 rounded-full bg-th-loss ring-2 ring-th-surface forced-colors:forced-color-adjust-none forced-colors:bg-[Highlight]"
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return /* @__PURE__ */ jsx(
|
|
32
|
+
"span",
|
|
33
|
+
{
|
|
34
|
+
"aria-label": strings.newCount(value),
|
|
35
|
+
className: "absolute -end-2.5 -top-1.5 inline-flex h-4 min-w-4 items-center justify-center rounded-full bg-th-loss px-1 text-3xs font-semibold leading-none tabular-nums text-th-on-loss ring-2 ring-th-surface forced-colors:border forced-colors:border-[CanvasText]",
|
|
36
|
+
children: value > 99 ? "99+" : value
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
var layerOf = () => history.state?.nishLayer;
|
|
41
|
+
function closeAfterRouterLink(onAfter) {
|
|
42
|
+
const layer = layerOf();
|
|
43
|
+
if (typeof window === "undefined" || !layer) {
|
|
44
|
+
onAfter();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
let decided = false;
|
|
48
|
+
const decide = (routerTookIt) => {
|
|
49
|
+
if (decided) return;
|
|
50
|
+
decided = true;
|
|
51
|
+
window.removeEventListener("click", onWindowClick);
|
|
52
|
+
if (!routerTookIt) {
|
|
53
|
+
onAfter();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const t0 = Date.now();
|
|
57
|
+
const wait = () => {
|
|
58
|
+
if (layerOf() !== layer || Date.now() - t0 > 3e3) onAfter();
|
|
59
|
+
else window.setTimeout(wait, 16);
|
|
60
|
+
};
|
|
61
|
+
wait();
|
|
62
|
+
};
|
|
63
|
+
const onWindowClick = (e) => decide(e.defaultPrevented);
|
|
64
|
+
window.addEventListener("click", onWindowClick);
|
|
65
|
+
window.setTimeout(() => decide(true), 0);
|
|
66
|
+
}
|
|
67
|
+
function renderItem(item, className, children, renderLink, onAfter) {
|
|
68
|
+
const current = item.active ? "page" : void 0;
|
|
69
|
+
const state = item.active ? "active" : "inactive";
|
|
70
|
+
if (item.href != null) {
|
|
71
|
+
const href = safeUrl(item.href);
|
|
72
|
+
if (href === null) return null;
|
|
73
|
+
if (renderLink) {
|
|
74
|
+
return renderLink(item, {
|
|
75
|
+
className,
|
|
76
|
+
children,
|
|
77
|
+
"aria-current": current,
|
|
78
|
+
"data-state": state,
|
|
79
|
+
...onAfter ? { replace: true } : {},
|
|
80
|
+
onClick: () => {
|
|
81
|
+
item.onSelect?.();
|
|
82
|
+
if (onAfter) closeAfterRouterLink(onAfter);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return /* @__PURE__ */ jsx(
|
|
87
|
+
"a",
|
|
88
|
+
{
|
|
89
|
+
href,
|
|
90
|
+
className,
|
|
91
|
+
"aria-current": current,
|
|
92
|
+
"data-state": state,
|
|
93
|
+
onClick: (e) => {
|
|
94
|
+
item.onSelect?.();
|
|
95
|
+
const inLayer = !!history.state?.nishLayer;
|
|
96
|
+
const plain = e.button === 0 && !e.metaKey && !e.ctrlKey && !e.shiftKey && !e.altKey && !e.defaultPrevented;
|
|
97
|
+
if (onAfter && inLayer && plain) {
|
|
98
|
+
e.preventDefault();
|
|
99
|
+
onAfter();
|
|
100
|
+
navigateFromLayer(href);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
onAfter?.();
|
|
104
|
+
},
|
|
105
|
+
children
|
|
106
|
+
}
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return /* @__PURE__ */ jsx(
|
|
110
|
+
"button",
|
|
111
|
+
{
|
|
112
|
+
type: "button",
|
|
113
|
+
className,
|
|
114
|
+
"aria-current": current,
|
|
115
|
+
"data-state": state,
|
|
116
|
+
onClick: () => {
|
|
117
|
+
item.onSelect?.();
|
|
118
|
+
onAfter?.();
|
|
119
|
+
},
|
|
120
|
+
children
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
var FOCUS = "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-th-accent";
|
|
125
|
+
function visibleNavItems(items) {
|
|
126
|
+
return items.filter((item) => {
|
|
127
|
+
if (item.href == null || safeUrl(item.href) !== null) return true;
|
|
128
|
+
warnDev(`nish-ui: nav item "${item.key}" was not rendered because its href is not safe (${item.href.slice(0, 40)}).`);
|
|
129
|
+
return false;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
export {
|
|
133
|
+
FOCUS,
|
|
134
|
+
NavBadge,
|
|
135
|
+
renderItem,
|
|
136
|
+
useEndSide,
|
|
137
|
+
visibleNavItems
|
|
138
|
+
};
|