nish-ui 2.0.1 → 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/hooks.js +17 -6
- 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 +1032 -778
- 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,173 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/modal.tsx
|
|
4
|
+
import { useCallback, useEffect, useId, useRef } from "react";
|
|
5
|
+
import { cn } from "../cn.js";
|
|
6
|
+
import { XIcon } from "./icons.js";
|
|
7
|
+
import { Button } from "./primitives.js";
|
|
8
|
+
import { useLatest } from "./latest.js";
|
|
9
|
+
import { Portal } from "./portal.js";
|
|
10
|
+
import { useNishStrings } from "./provider.js";
|
|
11
|
+
import { CLOSE_BUTTON, MODAL_SIZE, SHEET_HEIGHT_CLASS } from "./overlay-classes.js";
|
|
12
|
+
import { isPhoneViewport, closeVetoes, warnDev, useMergedRef, useScrollable } from "./overlay-core.js";
|
|
13
|
+
import { useDialog, useSheetExit, useSheetSwipe, SheetHandle } from "./overlay-dialog.js";
|
|
14
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
15
|
+
function Modal({ open, ...props }) {
|
|
16
|
+
if (open === false) return null;
|
|
17
|
+
return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx(ModalPanel, { ...props }) });
|
|
18
|
+
}
|
|
19
|
+
function ModalPanel({
|
|
20
|
+
title,
|
|
21
|
+
onClose,
|
|
22
|
+
onOpenChange,
|
|
23
|
+
children,
|
|
24
|
+
size = "md",
|
|
25
|
+
footer,
|
|
26
|
+
subtitle,
|
|
27
|
+
placement = "center",
|
|
28
|
+
header,
|
|
29
|
+
hideClose = false,
|
|
30
|
+
"aria-label": ariaLabel,
|
|
31
|
+
"aria-labelledby": ariaLabelledBy,
|
|
32
|
+
sheetHeight = "auto",
|
|
33
|
+
dismissible = true,
|
|
34
|
+
closeOnBack = true,
|
|
35
|
+
canClose,
|
|
36
|
+
className,
|
|
37
|
+
role,
|
|
38
|
+
ref,
|
|
39
|
+
...rest
|
|
40
|
+
}) {
|
|
41
|
+
const strings = useNishStrings();
|
|
42
|
+
const titleId = useId();
|
|
43
|
+
const [panelRef, setPanelRef] = useMergedRef(ref);
|
|
44
|
+
const scrimRef = useRef(null);
|
|
45
|
+
const bodyRef = useRef(null);
|
|
46
|
+
const { closing, requestClose, closeNow, isClosing, allowClose } = useSheetExit(onClose, canClose, onOpenChange);
|
|
47
|
+
useDialog(panelRef, requestClose, closeOnBack);
|
|
48
|
+
const swipe = useSheetSwipe(panelRef, scrimRef, () => dismissible && !isClosing() && isPhoneViewport(), closeNow, allowClose);
|
|
49
|
+
const showTitle = header === void 0 && title !== void 0 && title !== null;
|
|
50
|
+
const named = showTitle || !!ariaLabel || !!ariaLabelledBy;
|
|
51
|
+
const scrollable = useScrollable(bodyRef);
|
|
52
|
+
const bodyLabel = typeof title === "string" ? title : ariaLabel;
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!named) warnDev("nish-ui: <Modal> has no accessible name. Pass `title`, `aria-label` or `aria-labelledby`.");
|
|
55
|
+
}, [named]);
|
|
56
|
+
return /* @__PURE__ */ jsx(
|
|
57
|
+
"div",
|
|
58
|
+
{
|
|
59
|
+
ref: scrimRef,
|
|
60
|
+
"data-nish-layer": "",
|
|
61
|
+
"data-state": closing ? "closed" : "open",
|
|
62
|
+
className: cn(
|
|
63
|
+
"fixed inset-0 z-[var(--nish-z-modal,50)] flex items-end justify-center bg-th-scrim/60 sm:p-4",
|
|
64
|
+
closing ? "pointer-events-none animate-[nish-fade-out_200ms_ease-in_forwards]" : "animate-[nish-fade-in_160ms_ease-out]",
|
|
65
|
+
placement === "top" ? "sm:items-start sm:pt-[12vh]" : "sm:items-center"
|
|
66
|
+
),
|
|
67
|
+
onMouseDown: (e) => {
|
|
68
|
+
if (e.target === e.currentTarget) requestClose();
|
|
69
|
+
},
|
|
70
|
+
children: /* @__PURE__ */ jsxs(
|
|
71
|
+
"div",
|
|
72
|
+
{
|
|
73
|
+
...rest,
|
|
74
|
+
ref: setPanelRef,
|
|
75
|
+
role: role ?? "dialog",
|
|
76
|
+
"aria-modal": "true",
|
|
77
|
+
"aria-labelledby": ariaLabelledBy ?? (showTitle && !ariaLabel ? titleId : void 0),
|
|
78
|
+
"aria-label": ariaLabel,
|
|
79
|
+
"data-state": closing ? "closed" : "open",
|
|
80
|
+
tabIndex: -1,
|
|
81
|
+
className: cn(
|
|
82
|
+
"flex w-full flex-col border border-th-line bg-th-surface shadow-modal outline-none",
|
|
83
|
+
"rounded-t-2xl pb-[env(safe-area-inset-bottom)]",
|
|
84
|
+
closing ? "max-sm:animate-[nish-sheet-out_200ms_ease-in_forwards] sm:animate-[nish-fade-out_200ms_ease-in_forwards]" : "animate-[nish-sheet-in_240ms_var(--nish-ease-out)] sm:animate-[nish-rise-in_200ms_var(--nish-ease-out)]",
|
|
85
|
+
"sm:max-h-[85dvh] sm:rounded-2xl sm:pb-0",
|
|
86
|
+
placement === "top" && "sm:max-h-[76dvh]",
|
|
87
|
+
SHEET_HEIGHT_CLASS[sheetHeight],
|
|
88
|
+
MODAL_SIZE[size],
|
|
89
|
+
className
|
|
90
|
+
),
|
|
91
|
+
children: [
|
|
92
|
+
dismissible ? /* @__PURE__ */ jsx(SheetHandle, { onClose: requestClose, swipe, className: "sm:hidden" }) : null,
|
|
93
|
+
header !== void 0 ? /* @__PURE__ */ jsx("div", { className: cn("shrink-0", dismissible && "max-sm:touch-pan-x"), ...dismissible ? swipe : {}, children: header }) : /* @__PURE__ */ jsxs(
|
|
94
|
+
"div",
|
|
95
|
+
{
|
|
96
|
+
className: cn("flex shrink-0 items-start justify-between gap-3 px-5 pb-2 pt-3 sm:pt-4", dismissible && "max-sm:touch-pan-x"),
|
|
97
|
+
...dismissible ? swipe : {},
|
|
98
|
+
children: [
|
|
99
|
+
/* @__PURE__ */ jsx("h2", { id: titleId, className: "min-w-0 line-clamp-2 break-words pt-1 text-base font-semibold leading-snug text-th-hi", children: title }),
|
|
100
|
+
hideClose ? null : /* @__PURE__ */ jsx("button", { type: "button", onClick: requestClose, "aria-label": strings.close, className: CLOSE_BUTTON, children: /* @__PURE__ */ jsx(XIcon, { size: 16 }) })
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
),
|
|
104
|
+
subtitle ? /* @__PURE__ */ jsx("div", { className: "shrink-0 border-b border-th-line px-5 pb-3 text-sm text-th-hi2", children: subtitle }) : null,
|
|
105
|
+
/* @__PURE__ */ jsx(
|
|
106
|
+
"div",
|
|
107
|
+
{
|
|
108
|
+
ref: bodyRef,
|
|
109
|
+
tabIndex: scrollable ? 0 : void 0,
|
|
110
|
+
role: scrollable && bodyLabel ? "region" : void 0,
|
|
111
|
+
"aria-label": scrollable ? bodyLabel : void 0,
|
|
112
|
+
className: cn("min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 pb-5 text-sm text-th-hi2", header !== void 0 && "pt-3"),
|
|
113
|
+
children
|
|
114
|
+
}
|
|
115
|
+
),
|
|
116
|
+
footer ? /* @__PURE__ */ jsx("div", { className: "flex shrink-0 flex-wrap items-center justify-end gap-2 border-t border-th-line px-5 py-3", children: footer }) : null
|
|
117
|
+
]
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
function ConfirmDialog({
|
|
124
|
+
title,
|
|
125
|
+
body,
|
|
126
|
+
confirmLabel,
|
|
127
|
+
cancelLabel,
|
|
128
|
+
tone = "danger",
|
|
129
|
+
pending = false,
|
|
130
|
+
onConfirm,
|
|
131
|
+
onCancel,
|
|
132
|
+
open,
|
|
133
|
+
onOpenChange
|
|
134
|
+
}) {
|
|
135
|
+
const strings = useNishStrings();
|
|
136
|
+
const bodyId = useId();
|
|
137
|
+
const pendingRef = useLatest(pending);
|
|
138
|
+
const onOpenChangeRef = useLatest(onOpenChange);
|
|
139
|
+
const onCancelRef = useLatest(onCancel);
|
|
140
|
+
const cancel = useCallback(() => {
|
|
141
|
+
if (pendingRef.current) return;
|
|
142
|
+
onCancelRef.current?.();
|
|
143
|
+
onOpenChangeRef.current?.(false);
|
|
144
|
+
}, [onCancelRef, onOpenChangeRef, pendingRef]);
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
closeVetoes.set(cancel, () => pendingRef.current);
|
|
147
|
+
return () => {
|
|
148
|
+
closeVetoes.delete(cancel);
|
|
149
|
+
};
|
|
150
|
+
}, [cancel, pendingRef]);
|
|
151
|
+
const danger = tone === "danger";
|
|
152
|
+
const hasBody = body !== void 0 && body !== null && body !== false;
|
|
153
|
+
return /* @__PURE__ */ jsx(
|
|
154
|
+
Modal,
|
|
155
|
+
{
|
|
156
|
+
open,
|
|
157
|
+
title,
|
|
158
|
+
onClose: cancel,
|
|
159
|
+
size: "md",
|
|
160
|
+
role: "alertdialog",
|
|
161
|
+
"aria-describedby": hasBody ? bodyId : void 0,
|
|
162
|
+
footer: /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
163
|
+
/* @__PURE__ */ jsx(Button, { onClick: cancel, disabled: pending, "data-autofocus": danger ? "" : void 0, children: cancelLabel ?? strings.cancel }),
|
|
164
|
+
/* @__PURE__ */ jsx(Button, { variant: danger ? "danger" : "primary", loading: pending, onClick: onConfirm, "data-autofocus": danger ? void 0 : "", children: confirmLabel })
|
|
165
|
+
] }),
|
|
166
|
+
children: hasBody ? /* @__PURE__ */ jsx("div", { id: bodyId, children: body }) : null
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
export {
|
|
171
|
+
ConfirmDialog,
|
|
172
|
+
Modal
|
|
173
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/more-sheet.tsx
|
|
4
|
+
import { cn } from "../cn.js";
|
|
5
|
+
import { FORCED_MARK } from "./display-classes.js";
|
|
6
|
+
import { Drawer } from "./drawer.js";
|
|
7
|
+
import { FOCUS, NavBadge, renderItem, visibleNavItems } from "./nav-items.js";
|
|
8
|
+
import { useNishStrings } from "./provider.js";
|
|
9
|
+
import { ThemeToggle } from "./theme-toggle.js";
|
|
10
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
function MoreSheet({ open, onClose, items, title, footer, renderLink }) {
|
|
12
|
+
const strings = useNishStrings();
|
|
13
|
+
if (!open) return null;
|
|
14
|
+
const shown = visibleNavItems(items);
|
|
15
|
+
const row = (active) => cn(
|
|
16
|
+
"flex min-h-[52px] w-full items-center gap-3 rounded-lg px-3 text-start transition-colors",
|
|
17
|
+
active ? cn("bg-th-accent-soft text-th-accent", FORCED_MARK) : "text-th-hi hover:bg-th-elevated",
|
|
18
|
+
FOCUS
|
|
19
|
+
);
|
|
20
|
+
return /* @__PURE__ */ jsx(
|
|
21
|
+
Drawer,
|
|
22
|
+
{
|
|
23
|
+
side: "bottom",
|
|
24
|
+
title: title ?? strings.more,
|
|
25
|
+
onClose,
|
|
26
|
+
bottomOffset: "var(--nish-bottom-nav-h, 0px)",
|
|
27
|
+
footer: footer ?? /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-3 px-5 py-3", children: [
|
|
28
|
+
/* @__PURE__ */ jsx("span", { className: "text-sm font-medium text-th-hi", children: strings.theme }),
|
|
29
|
+
/* @__PURE__ */ jsx(ThemeToggle, { phone: "segmented" })
|
|
30
|
+
] }),
|
|
31
|
+
children: /* @__PURE__ */ jsx("ul", { className: "m-0 list-none p-2", children: shown.map((item) => /* @__PURE__ */ jsx("li", { children: renderItem(
|
|
32
|
+
item,
|
|
33
|
+
row(!!item.active),
|
|
34
|
+
/* @__PURE__ */ jsxs(Fragment, { children: [
|
|
35
|
+
/* @__PURE__ */ jsxs("span", { className: "relative inline-flex size-6 shrink-0 items-center justify-center", children: [
|
|
36
|
+
item.icon,
|
|
37
|
+
/* @__PURE__ */ jsx(NavBadge, { value: item.badge })
|
|
38
|
+
] }),
|
|
39
|
+
/* @__PURE__ */ jsxs("span", { className: "min-w-0 flex-1", children: [
|
|
40
|
+
/* @__PURE__ */ jsx("span", { className: "block truncate text-sm font-medium", children: item.label }),
|
|
41
|
+
item.hint ? /* @__PURE__ */ jsx("span", { className: cn("block truncate text-xs", item.active ? "text-th-accent/80" : "text-th-hi3"), children: item.hint }) : null
|
|
42
|
+
] })
|
|
43
|
+
] }),
|
|
44
|
+
renderLink,
|
|
45
|
+
onClose
|
|
46
|
+
) }, item.key)) })
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
MoreSheet
|
|
52
|
+
};
|
|
@@ -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
|
+
};
|