nish-ui 1.9.0 → 2.0.1
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 +218 -58
- 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 +71 -0
- package/dist/elements-charts.js +56 -57
- package/dist/elements-data.d.ts +71 -0
- package/dist/elements-data.js +5 -6
- 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 +1461 -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 +1686 -667
- package/dist/nish-components.css +4649 -4506
- 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 +243 -4
- package/dist/theme.global.js +356 -89
- package/dist/theme.js +337 -67
- 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,1461 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/overlay.tsx
|
|
4
|
+
import {
|
|
5
|
+
cloneElement,
|
|
6
|
+
createContext,
|
|
7
|
+
isValidElement,
|
|
8
|
+
useCallback,
|
|
9
|
+
useContext,
|
|
10
|
+
useEffect,
|
|
11
|
+
useId,
|
|
12
|
+
useRef,
|
|
13
|
+
useState
|
|
14
|
+
} from "react";
|
|
15
|
+
import { createPortal } from "react-dom";
|
|
16
|
+
import { cn } from "../cn.js";
|
|
17
|
+
import { shared } from "../runtime.js";
|
|
18
|
+
import { isRtl, useIsClient, useIsoLayoutEffect } from "./client.js";
|
|
19
|
+
import { useMediaQuery } from "./hooks.js";
|
|
20
|
+
import { XIcon } from "./icons.js";
|
|
21
|
+
import { Button, IconButton } from "./primitives.js";
|
|
22
|
+
import { useLatest } from "./latest.js";
|
|
23
|
+
import { Portal } from "./portal.js";
|
|
24
|
+
import { useNishConfig, useNishStrings } from "./provider.js";
|
|
25
|
+
import {
|
|
26
|
+
CLOSE_BUTTON,
|
|
27
|
+
DRAWER_SHEET_HEIGHT,
|
|
28
|
+
DRAWER_WIDTH,
|
|
29
|
+
MENU_ROW,
|
|
30
|
+
MODAL_SIZE,
|
|
31
|
+
PHONE_QUERY,
|
|
32
|
+
SHEET_EXIT_MS,
|
|
33
|
+
SHEET_HEIGHT_CLASS,
|
|
34
|
+
keyboardFocused,
|
|
35
|
+
menuItemClass,
|
|
36
|
+
touchTracker
|
|
37
|
+
} from "./overlay-classes.js";
|
|
38
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
39
|
+
function createState() {
|
|
40
|
+
const s = {
|
|
41
|
+
layerStack: [],
|
|
42
|
+
dialogCount: 0,
|
|
43
|
+
dialogPortals: [],
|
|
44
|
+
inerted: /* @__PURE__ */ new Set(),
|
|
45
|
+
lockCount: 0,
|
|
46
|
+
savedOverflow: null,
|
|
47
|
+
pendingBacks: 0,
|
|
48
|
+
queuedPushes: [],
|
|
49
|
+
layerSeq: 0,
|
|
50
|
+
backDialogs: 0,
|
|
51
|
+
guardOn: false,
|
|
52
|
+
guard: () => {
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
s.guard = (e) => {
|
|
56
|
+
if (s.pendingBacks <= 0) return;
|
|
57
|
+
s.pendingBacks--;
|
|
58
|
+
e.stopImmediatePropagation();
|
|
59
|
+
if (s.pendingBacks === 0) {
|
|
60
|
+
for (const push of s.queuedPushes.splice(0)) push();
|
|
61
|
+
removeGuardIfIdle(s);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
return s;
|
|
65
|
+
}
|
|
66
|
+
var cachedState;
|
|
67
|
+
function ov() {
|
|
68
|
+
return cachedState ??= shared("overlay", createState);
|
|
69
|
+
}
|
|
70
|
+
function removeGuardIfIdle(s) {
|
|
71
|
+
if (s.guardOn && s.backDialogs === 0 && s.pendingBacks === 0) {
|
|
72
|
+
window.removeEventListener("popstate", s.guard, true);
|
|
73
|
+
s.guardOn = false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function acquireGuard() {
|
|
77
|
+
const s = ov();
|
|
78
|
+
s.backDialogs++;
|
|
79
|
+
if (!s.guardOn) {
|
|
80
|
+
window.addEventListener("popstate", s.guard, true);
|
|
81
|
+
s.guardOn = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function releaseGuard() {
|
|
85
|
+
const s = ov();
|
|
86
|
+
s.backDialogs--;
|
|
87
|
+
removeGuardIfIdle(s);
|
|
88
|
+
}
|
|
89
|
+
var isTop = (self) => {
|
|
90
|
+
const stack = ov().layerStack;
|
|
91
|
+
return stack[stack.length - 1] === self;
|
|
92
|
+
};
|
|
93
|
+
function syncLayerAttr() {
|
|
94
|
+
if (typeof document === "undefined") return;
|
|
95
|
+
const html = document.documentElement;
|
|
96
|
+
if (ov().dialogCount > 0) html.setAttribute("data-nish-layer-open", "");
|
|
97
|
+
else html.removeAttribute("data-nish-layer-open");
|
|
98
|
+
}
|
|
99
|
+
function syncInert() {
|
|
100
|
+
if (typeof document === "undefined") return;
|
|
101
|
+
const { dialogPortals, inerted } = ov();
|
|
102
|
+
for (let i = dialogPortals.length - 1; i >= 0; i--) if (!dialogPortals[i].isConnected) dialogPortals.splice(i, 1);
|
|
103
|
+
const top = dialogPortals[dialogPortals.length - 1];
|
|
104
|
+
if (!top) {
|
|
105
|
+
inerted.forEach((el) => {
|
|
106
|
+
el.inert = false;
|
|
107
|
+
el.removeAttribute("inert");
|
|
108
|
+
});
|
|
109
|
+
inerted.clear();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
for (const el of Array.from(document.body.children)) {
|
|
113
|
+
const skip = el === top || el.contains(top) || el.hasAttribute("data-nish-keep-active");
|
|
114
|
+
if (skip) {
|
|
115
|
+
if (inerted.has(el)) {
|
|
116
|
+
el.inert = false;
|
|
117
|
+
el.removeAttribute("inert");
|
|
118
|
+
inerted.delete(el);
|
|
119
|
+
}
|
|
120
|
+
} else if (!el.inert) {
|
|
121
|
+
el.inert = true;
|
|
122
|
+
el.setAttribute("inert", "");
|
|
123
|
+
inerted.add(el);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function pushInert(scrim) {
|
|
128
|
+
const { dialogPortals } = ov();
|
|
129
|
+
if (!dialogPortals.includes(scrim)) dialogPortals.push(scrim);
|
|
130
|
+
syncInert();
|
|
131
|
+
}
|
|
132
|
+
function popInert(scrim) {
|
|
133
|
+
const { dialogPortals } = ov();
|
|
134
|
+
const i = dialogPortals.indexOf(scrim);
|
|
135
|
+
if (i >= 0) dialogPortals.splice(i, 1);
|
|
136
|
+
syncInert();
|
|
137
|
+
}
|
|
138
|
+
function isPhoneViewport() {
|
|
139
|
+
return typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia(PHONE_QUERY).matches;
|
|
140
|
+
}
|
|
141
|
+
function prefersReducedMotion() {
|
|
142
|
+
return typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
143
|
+
}
|
|
144
|
+
var closeVetoes = /* @__PURE__ */ new WeakMap();
|
|
145
|
+
var FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
|
|
146
|
+
function focusables(root) {
|
|
147
|
+
return Array.from(root.querySelectorAll(FOCUSABLE)).filter((n) => n.offsetParent !== null);
|
|
148
|
+
}
|
|
149
|
+
function lockScroll() {
|
|
150
|
+
const s = ov();
|
|
151
|
+
if (s.lockCount++ === 0) {
|
|
152
|
+
const html = document.documentElement;
|
|
153
|
+
s.savedOverflow = {
|
|
154
|
+
html: html.style.overflow,
|
|
155
|
+
body: document.body.style.overflow
|
|
156
|
+
};
|
|
157
|
+
html.style.overflow = "hidden";
|
|
158
|
+
document.body.style.overflow = "hidden";
|
|
159
|
+
}
|
|
160
|
+
let released = false;
|
|
161
|
+
return () => {
|
|
162
|
+
if (released) return;
|
|
163
|
+
released = true;
|
|
164
|
+
if (--s.lockCount === 0 && s.savedOverflow) {
|
|
165
|
+
document.documentElement.style.overflow = s.savedOverflow.html;
|
|
166
|
+
document.body.style.overflow = s.savedOverflow.body;
|
|
167
|
+
s.savedOverflow = null;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function layerHost(anchor, container) {
|
|
172
|
+
return anchor?.closest("[data-nish-layer]") ?? container ?? document.body;
|
|
173
|
+
}
|
|
174
|
+
function queueHistoryPush(push) {
|
|
175
|
+
const s = ov();
|
|
176
|
+
if (s.pendingBacks > 0) s.queuedPushes.push(push);
|
|
177
|
+
else push();
|
|
178
|
+
}
|
|
179
|
+
function whenHistorySettled(cb) {
|
|
180
|
+
queueHistoryPush(cb);
|
|
181
|
+
}
|
|
182
|
+
function cancelHistoryPush(push) {
|
|
183
|
+
const q = ov().queuedPushes;
|
|
184
|
+
const i = q.indexOf(push);
|
|
185
|
+
if (i >= 0) q.splice(i, 1);
|
|
186
|
+
}
|
|
187
|
+
var warnedDev = /* @__PURE__ */ new Set();
|
|
188
|
+
function warnDev(message) {
|
|
189
|
+
if (warnedDev.has(message)) return;
|
|
190
|
+
try {
|
|
191
|
+
if (process.env.NODE_ENV !== "production") {
|
|
192
|
+
warnedDev.add(message);
|
|
193
|
+
console.warn(message);
|
|
194
|
+
}
|
|
195
|
+
} catch {
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function setRef(ref, value) {
|
|
199
|
+
if (typeof ref === "function") ref(value);
|
|
200
|
+
else if (ref) ref.current = value;
|
|
201
|
+
}
|
|
202
|
+
function useMergedRef(external) {
|
|
203
|
+
const internalRef = useRef(null);
|
|
204
|
+
const callbackRef = useCallback(
|
|
205
|
+
(node) => {
|
|
206
|
+
internalRef.current = node;
|
|
207
|
+
setRef(external, node);
|
|
208
|
+
},
|
|
209
|
+
[external]
|
|
210
|
+
);
|
|
211
|
+
return [internalRef, callbackRef];
|
|
212
|
+
}
|
|
213
|
+
function useScrollable(ref) {
|
|
214
|
+
const [scrollable, setScrollable] = useState(false);
|
|
215
|
+
useEffect(() => {
|
|
216
|
+
const el = ref.current;
|
|
217
|
+
if (!el) return;
|
|
218
|
+
const check = () => setScrollable(el.scrollHeight > el.clientHeight + 1);
|
|
219
|
+
check();
|
|
220
|
+
const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(check);
|
|
221
|
+
ro?.observe(el);
|
|
222
|
+
const mo = typeof MutationObserver === "undefined" ? null : new MutationObserver(check);
|
|
223
|
+
mo?.observe(el, { childList: true, subtree: true, characterData: true });
|
|
224
|
+
return () => {
|
|
225
|
+
ro?.disconnect();
|
|
226
|
+
mo?.disconnect();
|
|
227
|
+
};
|
|
228
|
+
}, [ref]);
|
|
229
|
+
return scrollable;
|
|
230
|
+
}
|
|
231
|
+
function useDialog(panelRef, onClose, closeOnBackProp = false) {
|
|
232
|
+
const closeOnBack = closeOnBackProp && (typeof window === "undefined" || window.self === window.top);
|
|
233
|
+
const onCloseRef = useLatest(onClose);
|
|
234
|
+
useEffect(() => {
|
|
235
|
+
const s = ov();
|
|
236
|
+
const opener = document.activeElement;
|
|
237
|
+
const panel = panelRef.current;
|
|
238
|
+
const self = {};
|
|
239
|
+
s.layerStack.push(self);
|
|
240
|
+
s.dialogCount++;
|
|
241
|
+
syncLayerAttr();
|
|
242
|
+
const scrim = panel?.closest("[data-nish-layer]") ?? null;
|
|
243
|
+
if (scrim) pushInert(scrim);
|
|
244
|
+
const layerId = `nish-layer-${++s.layerSeq}`;
|
|
245
|
+
let pushed = false;
|
|
246
|
+
let rePushTimer = 0;
|
|
247
|
+
const pushEntry = () => {
|
|
248
|
+
const prev = history.state && typeof history.state === "object" ? history.state : {};
|
|
249
|
+
history.pushState({ ...prev, nishLayer: layerId }, "");
|
|
250
|
+
pushed = true;
|
|
251
|
+
};
|
|
252
|
+
const queuedPush = () => queueHistoryPush(pushEntry);
|
|
253
|
+
if (closeOnBack) acquireGuard();
|
|
254
|
+
const pushTimer = closeOnBack ? window.setTimeout(queuedPush, 0) : 0;
|
|
255
|
+
function onPop() {
|
|
256
|
+
if (!pushed) return;
|
|
257
|
+
if (history.state?.nishLayer === layerId) return;
|
|
258
|
+
pushed = false;
|
|
259
|
+
if (!isTop(self)) {
|
|
260
|
+
pushEntry();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
onCloseRef.current();
|
|
264
|
+
window.clearTimeout(rePushTimer);
|
|
265
|
+
rePushTimer = window.setTimeout(() => {
|
|
266
|
+
if (!pushed) queuedPush();
|
|
267
|
+
}, SHEET_EXIT_MS + 60);
|
|
268
|
+
}
|
|
269
|
+
if (closeOnBack) window.addEventListener("popstate", onPop);
|
|
270
|
+
const focusTimer = window.setTimeout(() => {
|
|
271
|
+
if (panel && !panel.contains(document.activeElement)) {
|
|
272
|
+
(panel.querySelector("[data-autofocus],[autofocus]") ?? panel).focus();
|
|
273
|
+
}
|
|
274
|
+
}, 0);
|
|
275
|
+
const unlock = lockScroll();
|
|
276
|
+
function onKey(e) {
|
|
277
|
+
if (!isTop(self)) return;
|
|
278
|
+
if (e.key === "Escape") {
|
|
279
|
+
e.stopPropagation();
|
|
280
|
+
onCloseRef.current();
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (e.key !== "Tab" || !panel) return;
|
|
284
|
+
const nodes = focusables(panel);
|
|
285
|
+
if (nodes.length === 0) {
|
|
286
|
+
e.preventDefault();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const first = nodes[0];
|
|
290
|
+
const last = nodes[nodes.length - 1];
|
|
291
|
+
const active = document.activeElement;
|
|
292
|
+
if (!panel.contains(active)) {
|
|
293
|
+
e.preventDefault();
|
|
294
|
+
(e.shiftKey ? last : first).focus();
|
|
295
|
+
} else if (e.shiftKey && (active === first || active === panel)) {
|
|
296
|
+
e.preventDefault();
|
|
297
|
+
last.focus();
|
|
298
|
+
} else if (!e.shiftKey && active === last) {
|
|
299
|
+
e.preventDefault();
|
|
300
|
+
first.focus();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
document.addEventListener("keydown", onKey);
|
|
304
|
+
return () => {
|
|
305
|
+
window.clearTimeout(focusTimer);
|
|
306
|
+
window.clearTimeout(pushTimer);
|
|
307
|
+
window.clearTimeout(rePushTimer);
|
|
308
|
+
cancelHistoryPush(pushEntry);
|
|
309
|
+
window.removeEventListener("popstate", onPop);
|
|
310
|
+
const at = s.layerStack.indexOf(self);
|
|
311
|
+
if (at >= 0) s.layerStack.splice(at, 1);
|
|
312
|
+
s.dialogCount--;
|
|
313
|
+
syncLayerAttr();
|
|
314
|
+
if (scrim) popInert(scrim);
|
|
315
|
+
document.removeEventListener("keydown", onKey);
|
|
316
|
+
unlock();
|
|
317
|
+
opener?.focus?.();
|
|
318
|
+
if (pushed && history.state?.nishLayer === layerId) {
|
|
319
|
+
s.pendingBacks++;
|
|
320
|
+
history.back();
|
|
321
|
+
}
|
|
322
|
+
if (closeOnBack) releaseGuard();
|
|
323
|
+
};
|
|
324
|
+
}, [panelRef, closeOnBack, onCloseRef]);
|
|
325
|
+
}
|
|
326
|
+
function useSheetExit(onClose, canClose, onOpenChange) {
|
|
327
|
+
const [closing, setClosing] = useState(false);
|
|
328
|
+
const closingRef = useRef(false);
|
|
329
|
+
const timer = useRef(0);
|
|
330
|
+
const onCloseRef = useLatest(onClose);
|
|
331
|
+
const openChangeRef = useLatest(onOpenChange);
|
|
332
|
+
const canCloseRef = useLatest(canClose);
|
|
333
|
+
useEffect(() => () => window.clearTimeout(timer.current), []);
|
|
334
|
+
const fire = useCallback(() => {
|
|
335
|
+
onCloseRef.current?.();
|
|
336
|
+
openChangeRef.current?.(false);
|
|
337
|
+
}, [onCloseRef, openChangeRef]);
|
|
338
|
+
const requestClose = useCallback(() => {
|
|
339
|
+
if (closingRef.current) return;
|
|
340
|
+
const handler = onCloseRef.current;
|
|
341
|
+
if (handler && closeVetoes.get(handler)?.()) return;
|
|
342
|
+
if (canCloseRef.current && !canCloseRef.current()) return;
|
|
343
|
+
if (prefersReducedMotion()) {
|
|
344
|
+
fire();
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
closingRef.current = true;
|
|
348
|
+
setClosing(true);
|
|
349
|
+
timer.current = window.setTimeout(() => {
|
|
350
|
+
fire();
|
|
351
|
+
closingRef.current = false;
|
|
352
|
+
setClosing(false);
|
|
353
|
+
}, SHEET_EXIT_MS);
|
|
354
|
+
}, [canCloseRef, fire, onCloseRef]);
|
|
355
|
+
const closeNow = useCallback(() => {
|
|
356
|
+
fire();
|
|
357
|
+
}, [fire]);
|
|
358
|
+
const isClosing = useCallback(() => closingRef.current, []);
|
|
359
|
+
const allowClose = useCallback(() => !canCloseRef.current || canCloseRef.current(), [canCloseRef]);
|
|
360
|
+
return { closing, requestClose, closeNow, isClosing, allowClose };
|
|
361
|
+
}
|
|
362
|
+
function useSheetSwipe(panelRef, scrimRef, enabled, onDismiss, allow = () => true) {
|
|
363
|
+
const drag = useRef(null);
|
|
364
|
+
const timer = useRef(0);
|
|
365
|
+
const enabledRef = useLatest(enabled);
|
|
366
|
+
const dismissRef = useLatest(onDismiss);
|
|
367
|
+
const allowRef = useLatest(allow);
|
|
368
|
+
useEffect(() => () => window.clearTimeout(timer.current), []);
|
|
369
|
+
const onPointerDown = useCallback(
|
|
370
|
+
(e) => {
|
|
371
|
+
if (drag.current || !enabledRef.current()) return;
|
|
372
|
+
if (e.pointerType === "mouse" && e.button !== 0) return;
|
|
373
|
+
const hit = e.target.closest('button,a,input,select,textarea,[role="button"]');
|
|
374
|
+
if (hit && hit !== e.currentTarget) return;
|
|
375
|
+
const panel = panelRef.current;
|
|
376
|
+
if (!panel) return;
|
|
377
|
+
window.clearTimeout(timer.current);
|
|
378
|
+
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
379
|
+
panel.style.transition = "none";
|
|
380
|
+
if (scrimRef.current) scrimRef.current.style.transition = "none";
|
|
381
|
+
drag.current = {
|
|
382
|
+
startY: e.clientY,
|
|
383
|
+
dy: 0,
|
|
384
|
+
lastY: e.clientY,
|
|
385
|
+
lastT: e.timeStamp,
|
|
386
|
+
v: 0
|
|
387
|
+
};
|
|
388
|
+
},
|
|
389
|
+
[enabledRef, panelRef, scrimRef]
|
|
390
|
+
);
|
|
391
|
+
const onPointerMove = useCallback(
|
|
392
|
+
(e) => {
|
|
393
|
+
const d = drag.current;
|
|
394
|
+
const panel = panelRef.current;
|
|
395
|
+
if (!d || !panel) return;
|
|
396
|
+
d.dy = e.clientY - d.startY;
|
|
397
|
+
const dt = e.timeStamp - d.lastT;
|
|
398
|
+
if (dt > 0) d.v = (e.clientY - d.lastY) / dt;
|
|
399
|
+
d.lastY = e.clientY;
|
|
400
|
+
d.lastT = e.timeStamp;
|
|
401
|
+
panel.style.transform = `translateY(${d.dy < 0 ? d.dy * 0.3 : d.dy}px)`;
|
|
402
|
+
if (scrimRef.current) {
|
|
403
|
+
scrimRef.current.style.opacity = String(Math.max(0, 1 - Math.max(0, d.dy) / Math.max(1, panel.offsetHeight)));
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
[panelRef, scrimRef]
|
|
407
|
+
);
|
|
408
|
+
const end = useCallback(
|
|
409
|
+
(e, cancelled) => {
|
|
410
|
+
const d = drag.current;
|
|
411
|
+
const panel = panelRef.current;
|
|
412
|
+
drag.current = null;
|
|
413
|
+
if (!d || !panel) return;
|
|
414
|
+
if (e.currentTarget.hasPointerCapture?.(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);
|
|
415
|
+
const scrim = scrimRef.current;
|
|
416
|
+
const velocity = e.timeStamp - d.lastT > 100 ? 0 : d.v;
|
|
417
|
+
const dismiss = !cancelled && (d.dy > panel.offsetHeight * 0.3 || velocity > 0.5) && allowRef.current();
|
|
418
|
+
const ms = SHEET_EXIT_MS - 20;
|
|
419
|
+
panel.style.transition = `transform ${ms}ms ease-out`;
|
|
420
|
+
if (scrim) scrim.style.transition = `opacity ${ms}ms ease-out`;
|
|
421
|
+
if (dismiss) {
|
|
422
|
+
panel.style.transform = "translateY(100%)";
|
|
423
|
+
if (scrim) scrim.style.opacity = "0";
|
|
424
|
+
timer.current = window.setTimeout(() => dismissRef.current(), ms);
|
|
425
|
+
} else {
|
|
426
|
+
panel.style.transform = "translateY(0)";
|
|
427
|
+
if (scrim) scrim.style.opacity = "1";
|
|
428
|
+
timer.current = window.setTimeout(() => {
|
|
429
|
+
panel.style.transform = "";
|
|
430
|
+
panel.style.transition = "";
|
|
431
|
+
if (scrim) {
|
|
432
|
+
scrim.style.opacity = "";
|
|
433
|
+
scrim.style.transition = "";
|
|
434
|
+
}
|
|
435
|
+
}, ms);
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
[allowRef, dismissRef, panelRef, scrimRef]
|
|
439
|
+
);
|
|
440
|
+
return {
|
|
441
|
+
onPointerDown,
|
|
442
|
+
onPointerMove,
|
|
443
|
+
onPointerUp: (e) => end(e, false),
|
|
444
|
+
onPointerCancel: (e) => end(e, true)
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
function SheetHandle({ onClose, swipe, className }) {
|
|
448
|
+
const strings = useNishStrings();
|
|
449
|
+
return /* @__PURE__ */ jsx(
|
|
450
|
+
"div",
|
|
451
|
+
{
|
|
452
|
+
role: "button",
|
|
453
|
+
tabIndex: 0,
|
|
454
|
+
"aria-label": strings.close,
|
|
455
|
+
...swipe,
|
|
456
|
+
onKeyDown: (e) => {
|
|
457
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
458
|
+
e.preventDefault();
|
|
459
|
+
onClose();
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
className: cn(
|
|
463
|
+
"flex h-5 min-h-11 w-full shrink-0 cursor-grab touch-none items-center justify-center rounded-t-2xl outline-none focus-visible:bg-th-overlay forced-colors:focus-visible:outline-solid forced-colors:focus-visible:outline-2",
|
|
464
|
+
className
|
|
465
|
+
),
|
|
466
|
+
children: /* @__PURE__ */ jsx("span", { className: "h-1 w-10 rounded-full bg-th-line-strong forced-colors:bg-[ButtonText]", "aria-hidden": "true" })
|
|
467
|
+
}
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
function Modal({ open, ...props }) {
|
|
471
|
+
if (open === false) return null;
|
|
472
|
+
return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx(ModalPanel, { ...props }) });
|
|
473
|
+
}
|
|
474
|
+
function ModalPanel({
|
|
475
|
+
title,
|
|
476
|
+
onClose,
|
|
477
|
+
onOpenChange,
|
|
478
|
+
children,
|
|
479
|
+
size = "md",
|
|
480
|
+
footer,
|
|
481
|
+
subtitle,
|
|
482
|
+
placement = "center",
|
|
483
|
+
header,
|
|
484
|
+
hideClose = false,
|
|
485
|
+
"aria-label": ariaLabel,
|
|
486
|
+
"aria-labelledby": ariaLabelledBy,
|
|
487
|
+
sheetHeight = "auto",
|
|
488
|
+
dismissible = true,
|
|
489
|
+
closeOnBack = true,
|
|
490
|
+
canClose,
|
|
491
|
+
className,
|
|
492
|
+
role,
|
|
493
|
+
ref,
|
|
494
|
+
...rest
|
|
495
|
+
}) {
|
|
496
|
+
const strings = useNishStrings();
|
|
497
|
+
const titleId = useId();
|
|
498
|
+
const [panelRef, setPanelRef] = useMergedRef(ref);
|
|
499
|
+
const scrimRef = useRef(null);
|
|
500
|
+
const bodyRef = useRef(null);
|
|
501
|
+
const { closing, requestClose, closeNow, isClosing, allowClose } = useSheetExit(onClose, canClose, onOpenChange);
|
|
502
|
+
useDialog(panelRef, requestClose, closeOnBack);
|
|
503
|
+
const swipe = useSheetSwipe(panelRef, scrimRef, () => dismissible && !isClosing() && isPhoneViewport(), closeNow, allowClose);
|
|
504
|
+
const showTitle = header === void 0 && title !== void 0 && title !== null;
|
|
505
|
+
const named = showTitle || !!ariaLabel || !!ariaLabelledBy;
|
|
506
|
+
const scrollable = useScrollable(bodyRef);
|
|
507
|
+
const bodyLabel = typeof title === "string" ? title : ariaLabel;
|
|
508
|
+
useEffect(() => {
|
|
509
|
+
if (!named) warnDev("nish-ui: <Modal> has no accessible name. Pass `title`, `aria-label` or `aria-labelledby`.");
|
|
510
|
+
}, [named]);
|
|
511
|
+
return /* @__PURE__ */ jsx(
|
|
512
|
+
"div",
|
|
513
|
+
{
|
|
514
|
+
ref: scrimRef,
|
|
515
|
+
"data-nish-layer": "",
|
|
516
|
+
"data-state": closing ? "closed" : "open",
|
|
517
|
+
className: cn(
|
|
518
|
+
"fixed inset-0 z-[var(--nish-z-modal,50)] flex items-end justify-center bg-th-scrim/60 sm:p-4",
|
|
519
|
+
closing ? "pointer-events-none animate-[nish-fade-out_200ms_ease-in_forwards]" : "animate-[nish-fade-in_160ms_ease-out]",
|
|
520
|
+
placement === "top" ? "sm:items-start sm:pt-[12vh]" : "sm:items-center"
|
|
521
|
+
),
|
|
522
|
+
onMouseDown: (e) => {
|
|
523
|
+
if (e.target === e.currentTarget) requestClose();
|
|
524
|
+
},
|
|
525
|
+
children: /* @__PURE__ */ jsxs(
|
|
526
|
+
"div",
|
|
527
|
+
{
|
|
528
|
+
...rest,
|
|
529
|
+
ref: setPanelRef,
|
|
530
|
+
role: role ?? "dialog",
|
|
531
|
+
"aria-modal": "true",
|
|
532
|
+
"aria-labelledby": ariaLabelledBy ?? (showTitle && !ariaLabel ? titleId : void 0),
|
|
533
|
+
"aria-label": ariaLabel,
|
|
534
|
+
"data-state": closing ? "closed" : "open",
|
|
535
|
+
tabIndex: -1,
|
|
536
|
+
className: cn(
|
|
537
|
+
"flex w-full flex-col border border-th-line bg-th-surface shadow-modal outline-none",
|
|
538
|
+
"rounded-t-2xl pb-[env(safe-area-inset-bottom)]",
|
|
539
|
+
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)]",
|
|
540
|
+
"sm:max-h-[85dvh] sm:rounded-2xl sm:pb-0",
|
|
541
|
+
placement === "top" && "sm:max-h-[76dvh]",
|
|
542
|
+
SHEET_HEIGHT_CLASS[sheetHeight],
|
|
543
|
+
MODAL_SIZE[size],
|
|
544
|
+
className
|
|
545
|
+
),
|
|
546
|
+
children: [
|
|
547
|
+
dismissible ? /* @__PURE__ */ jsx(SheetHandle, { onClose: requestClose, swipe, className: "sm:hidden" }) : null,
|
|
548
|
+
header !== void 0 ? /* @__PURE__ */ jsx("div", { className: cn("shrink-0", dismissible && "max-sm:touch-pan-x"), ...dismissible ? swipe : {}, children: header }) : /* @__PURE__ */ jsxs(
|
|
549
|
+
"div",
|
|
550
|
+
{
|
|
551
|
+
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"),
|
|
552
|
+
...dismissible ? swipe : {},
|
|
553
|
+
children: [
|
|
554
|
+
/* @__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 }),
|
|
555
|
+
hideClose ? null : /* @__PURE__ */ jsx("button", { type: "button", onClick: requestClose, "aria-label": strings.close, className: CLOSE_BUTTON, children: /* @__PURE__ */ jsx(XIcon, { size: 16 }) })
|
|
556
|
+
]
|
|
557
|
+
}
|
|
558
|
+
),
|
|
559
|
+
subtitle ? /* @__PURE__ */ jsx("div", { className: "shrink-0 border-b border-th-line px-5 pb-3 text-sm text-th-hi2", children: subtitle }) : null,
|
|
560
|
+
/* @__PURE__ */ jsx(
|
|
561
|
+
"div",
|
|
562
|
+
{
|
|
563
|
+
ref: bodyRef,
|
|
564
|
+
tabIndex: scrollable ? 0 : void 0,
|
|
565
|
+
role: scrollable && bodyLabel ? "region" : void 0,
|
|
566
|
+
"aria-label": scrollable ? bodyLabel : void 0,
|
|
567
|
+
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"),
|
|
568
|
+
children
|
|
569
|
+
}
|
|
570
|
+
),
|
|
571
|
+
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
|
|
572
|
+
]
|
|
573
|
+
}
|
|
574
|
+
)
|
|
575
|
+
}
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
function Drawer({ open, ...props }) {
|
|
579
|
+
if (open === false) return null;
|
|
580
|
+
return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx(DrawerPanel, { ...props }) });
|
|
581
|
+
}
|
|
582
|
+
function DrawerPanel({
|
|
583
|
+
title,
|
|
584
|
+
subtitle,
|
|
585
|
+
onClose,
|
|
586
|
+
onOpenChange,
|
|
587
|
+
side = "right",
|
|
588
|
+
size = "md",
|
|
589
|
+
actions,
|
|
590
|
+
footer,
|
|
591
|
+
bottomOffset,
|
|
592
|
+
dismissible = true,
|
|
593
|
+
closeOnBack = true,
|
|
594
|
+
canClose,
|
|
595
|
+
"aria-label": ariaLabel,
|
|
596
|
+
"aria-labelledby": ariaLabelledBy,
|
|
597
|
+
children,
|
|
598
|
+
className,
|
|
599
|
+
ref,
|
|
600
|
+
...rest
|
|
601
|
+
}) {
|
|
602
|
+
const strings = useNishStrings();
|
|
603
|
+
const titleId = useId();
|
|
604
|
+
const [panelRef, setPanelRef] = useMergedRef(ref);
|
|
605
|
+
const scrimRef = useRef(null);
|
|
606
|
+
const bodyRef = useRef(null);
|
|
607
|
+
const { closing, requestClose, closeNow, isClosing, allowClose } = useSheetExit(onClose, canClose, onOpenChange);
|
|
608
|
+
useDialog(panelRef, requestClose, closeOnBack);
|
|
609
|
+
const bottom = side === "bottom";
|
|
610
|
+
const swipe = useSheetSwipe(panelRef, scrimRef, () => bottom && dismissible && !isClosing(), closeNow, allowClose);
|
|
611
|
+
const swipeHeader = bottom && dismissible;
|
|
612
|
+
const hasTitle = title !== void 0 && title !== null;
|
|
613
|
+
const named = hasTitle || !!ariaLabel || !!ariaLabelledBy;
|
|
614
|
+
const scrollable = useScrollable(bodyRef);
|
|
615
|
+
const bodyLabel = typeof title === "string" ? title : ariaLabel;
|
|
616
|
+
useEffect(() => {
|
|
617
|
+
if (!named) warnDev("nish-ui: <Drawer> has no accessible name. Pass `title`, `aria-label` or `aria-labelledby`.");
|
|
618
|
+
}, [named]);
|
|
619
|
+
return /* @__PURE__ */ jsx(
|
|
620
|
+
"div",
|
|
621
|
+
{
|
|
622
|
+
ref: scrimRef,
|
|
623
|
+
"data-nish-layer": "",
|
|
624
|
+
"data-state": closing ? "closed" : "open",
|
|
625
|
+
className: cn(
|
|
626
|
+
"fixed inset-0 z-[var(--nish-z-modal,50)] bg-th-scrim/50",
|
|
627
|
+
closing ? "pointer-events-none animate-[nish-fade-out_200ms_ease-in_forwards]" : "animate-[nish-fade-in_160ms_ease-out]"
|
|
628
|
+
),
|
|
629
|
+
style: bottom && bottomOffset ? { bottom: bottomOffset } : void 0,
|
|
630
|
+
onMouseDown: (e) => {
|
|
631
|
+
if (e.target === e.currentTarget) requestClose();
|
|
632
|
+
},
|
|
633
|
+
children: /* @__PURE__ */ jsxs(
|
|
634
|
+
"div",
|
|
635
|
+
{
|
|
636
|
+
...rest,
|
|
637
|
+
ref: setPanelRef,
|
|
638
|
+
role: "dialog",
|
|
639
|
+
"aria-modal": "true",
|
|
640
|
+
"aria-labelledby": ariaLabelledBy ?? (ariaLabel || !hasTitle ? void 0 : titleId),
|
|
641
|
+
"aria-label": ariaLabel,
|
|
642
|
+
"data-state": closing ? "closed" : "open",
|
|
643
|
+
tabIndex: -1,
|
|
644
|
+
className: cn(
|
|
645
|
+
"absolute flex flex-col border-th-line bg-th-surface shadow-modal outline-none",
|
|
646
|
+
bottom ? cn(
|
|
647
|
+
"inset-x-0 bottom-0 rounded-t-2xl border-t",
|
|
648
|
+
closing ? "animate-[nish-sheet-out_200ms_ease-in_forwards]" : "animate-[nish-sheet-in_240ms_var(--nish-ease-out)]",
|
|
649
|
+
bottomOffset ? "" : "pb-[env(safe-area-inset-bottom)]",
|
|
650
|
+
DRAWER_SHEET_HEIGHT[size]
|
|
651
|
+
) : cn(
|
|
652
|
+
"inset-y-0 w-full pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]",
|
|
653
|
+
DRAWER_WIDTH[size],
|
|
654
|
+
side === "right" ? cn(
|
|
655
|
+
"right-0 border-l pr-[env(safe-area-inset-right)]",
|
|
656
|
+
closing ? "translate-x-full transition-transform duration-200 ease-in" : "animate-[nish-slide-in-right_240ms_var(--nish-ease-out)]"
|
|
657
|
+
) : cn(
|
|
658
|
+
"left-0 border-r pl-[env(safe-area-inset-left)]",
|
|
659
|
+
closing ? "-translate-x-full transition-transform duration-200 ease-in" : "animate-[nish-slide-in-left_240ms_var(--nish-ease-out)]"
|
|
660
|
+
)
|
|
661
|
+
),
|
|
662
|
+
className
|
|
663
|
+
),
|
|
664
|
+
children: [
|
|
665
|
+
swipeHeader ? /* @__PURE__ */ jsx(SheetHandle, { onClose: requestClose, swipe }) : bottom ? /* @__PURE__ */ jsx("div", { className: "h-3 shrink-0", "aria-hidden": "true" }) : null,
|
|
666
|
+
/* @__PURE__ */ jsxs(
|
|
667
|
+
"div",
|
|
668
|
+
{
|
|
669
|
+
className: cn("flex shrink-0 items-start justify-between gap-3 border-b border-th-line px-5 py-3", swipeHeader && "touch-pan-x"),
|
|
670
|
+
...swipeHeader ? swipe : {},
|
|
671
|
+
children: [
|
|
672
|
+
/* @__PURE__ */ jsxs("div", { className: "min-w-0 pt-1", children: [
|
|
673
|
+
hasTitle ? /* @__PURE__ */ jsx("h2", { id: titleId, className: "line-clamp-2 break-words text-base font-semibold leading-snug text-th-hi", children: title }) : null,
|
|
674
|
+
subtitle ? /* @__PURE__ */ jsx("div", { className: "mt-0.5 text-xs text-th-hi3", children: subtitle }) : null
|
|
675
|
+
] }),
|
|
676
|
+
/* @__PURE__ */ jsxs("div", { className: "-me-1.5 flex shrink-0 items-center gap-1", children: [
|
|
677
|
+
actions,
|
|
678
|
+
/* @__PURE__ */ jsx(IconButton, { label: strings.close, onClick: requestClose, children: /* @__PURE__ */ jsx(XIcon, { size: 16 }) })
|
|
679
|
+
] })
|
|
680
|
+
]
|
|
681
|
+
}
|
|
682
|
+
),
|
|
683
|
+
/* @__PURE__ */ jsx(
|
|
684
|
+
"div",
|
|
685
|
+
{
|
|
686
|
+
ref: bodyRef,
|
|
687
|
+
tabIndex: scrollable ? 0 : void 0,
|
|
688
|
+
role: scrollable && bodyLabel ? "region" : void 0,
|
|
689
|
+
"aria-label": scrollable ? bodyLabel : void 0,
|
|
690
|
+
className: "min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 py-4 text-sm text-th-hi2",
|
|
691
|
+
children
|
|
692
|
+
}
|
|
693
|
+
),
|
|
694
|
+
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
|
|
695
|
+
]
|
|
696
|
+
}
|
|
697
|
+
)
|
|
698
|
+
}
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
function ConfirmDialog({
|
|
702
|
+
title,
|
|
703
|
+
body,
|
|
704
|
+
confirmLabel,
|
|
705
|
+
cancelLabel,
|
|
706
|
+
tone = "danger",
|
|
707
|
+
pending = false,
|
|
708
|
+
onConfirm,
|
|
709
|
+
onCancel,
|
|
710
|
+
open,
|
|
711
|
+
onOpenChange
|
|
712
|
+
}) {
|
|
713
|
+
const strings = useNishStrings();
|
|
714
|
+
const bodyId = useId();
|
|
715
|
+
const pendingRef = useLatest(pending);
|
|
716
|
+
const onOpenChangeRef = useLatest(onOpenChange);
|
|
717
|
+
const onCancelRef = useLatest(onCancel);
|
|
718
|
+
const cancel = useCallback(() => {
|
|
719
|
+
if (pendingRef.current) return;
|
|
720
|
+
onCancelRef.current?.();
|
|
721
|
+
onOpenChangeRef.current?.(false);
|
|
722
|
+
}, [onCancelRef, onOpenChangeRef, pendingRef]);
|
|
723
|
+
useEffect(() => {
|
|
724
|
+
closeVetoes.set(cancel, () => pendingRef.current);
|
|
725
|
+
return () => {
|
|
726
|
+
closeVetoes.delete(cancel);
|
|
727
|
+
};
|
|
728
|
+
}, [cancel, pendingRef]);
|
|
729
|
+
const danger = tone === "danger";
|
|
730
|
+
const hasBody = body !== void 0 && body !== null && body !== false;
|
|
731
|
+
return /* @__PURE__ */ jsx(
|
|
732
|
+
Modal,
|
|
733
|
+
{
|
|
734
|
+
open,
|
|
735
|
+
title,
|
|
736
|
+
onClose: cancel,
|
|
737
|
+
size: "md",
|
|
738
|
+
role: "alertdialog",
|
|
739
|
+
"aria-describedby": hasBody ? bodyId : void 0,
|
|
740
|
+
footer: /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
741
|
+
/* @__PURE__ */ jsx(Button, { onClick: cancel, disabled: pending, "data-autofocus": danger ? "" : void 0, children: cancelLabel ?? strings.cancel }),
|
|
742
|
+
/* @__PURE__ */ jsx(Button, { variant: danger ? "danger" : "primary", loading: pending, onClick: onConfirm, "data-autofocus": danger ? void 0 : "", children: confirmLabel })
|
|
743
|
+
] }),
|
|
744
|
+
children: hasBody ? /* @__PURE__ */ jsx("div", { id: bodyId, children: body }) : null
|
|
745
|
+
}
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
var GAP = 8;
|
|
749
|
+
var EDGE = 8;
|
|
750
|
+
var OPPOSITE = {
|
|
751
|
+
top: "bottom",
|
|
752
|
+
bottom: "top",
|
|
753
|
+
left: "right",
|
|
754
|
+
right: "left"
|
|
755
|
+
};
|
|
756
|
+
function viewport() {
|
|
757
|
+
return {
|
|
758
|
+
vw: window.innerWidth || document.documentElement.clientWidth || 0,
|
|
759
|
+
vh: window.innerHeight || document.documentElement.clientHeight || 0
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
function clamp(v, size, limit) {
|
|
763
|
+
return Math.max(EDGE, Math.min(v, limit - size - EDGE));
|
|
764
|
+
}
|
|
765
|
+
function place(r, w, h, side, align, gap, rtl = false) {
|
|
766
|
+
const { vw, vh } = viewport();
|
|
767
|
+
const room = {
|
|
768
|
+
top: r.top,
|
|
769
|
+
bottom: vh - r.bottom,
|
|
770
|
+
left: r.left,
|
|
771
|
+
right: vw - r.right
|
|
772
|
+
};
|
|
773
|
+
const need = side === "top" || side === "bottom" ? h + gap + EDGE : w + gap + EDGE;
|
|
774
|
+
const chosen = room[side] < need && room[OPPOSITE[side]] > room[side] ? OPPOSITE[side] : side;
|
|
775
|
+
if (chosen === "top" || chosen === "bottom") {
|
|
776
|
+
const top2 = chosen === "top" ? r.top - gap - h : r.bottom + gap;
|
|
777
|
+
const edge = rtl && align !== "center" ? align === "start" ? "end" : "start" : align;
|
|
778
|
+
const left2 = edge === "start" ? r.left : edge === "end" ? r.right - w : r.left + r.width / 2 - w / 2;
|
|
779
|
+
return {
|
|
780
|
+
side: chosen,
|
|
781
|
+
top: clamp(top2, h, vh),
|
|
782
|
+
left: clamp(left2, w, vw),
|
|
783
|
+
maxHeight: Math.max(0, room[chosen] - gap - EDGE)
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
const left = chosen === "left" ? r.left - gap - w : r.right + gap;
|
|
787
|
+
const top = align === "start" ? r.top : align === "end" ? r.bottom - h : r.top + r.height / 2 - h / 2;
|
|
788
|
+
return {
|
|
789
|
+
side: chosen,
|
|
790
|
+
top: clamp(top, h, vh),
|
|
791
|
+
left: clamp(left, w, vw),
|
|
792
|
+
maxHeight: Math.max(0, vh - 2 * EDGE)
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
function useAnchoredPosition(anchor, floating, open, side, align, gap = GAP) {
|
|
796
|
+
const [pos, setPos] = useState(null);
|
|
797
|
+
useIsoLayoutEffect(() => {
|
|
798
|
+
if (!open) {
|
|
799
|
+
setPos(null);
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
const update = () => {
|
|
803
|
+
const a = anchor.current;
|
|
804
|
+
const f = floating.current;
|
|
805
|
+
if (!a || !f) return;
|
|
806
|
+
const naturalH = f.scrollHeight + (f.offsetHeight - f.clientHeight);
|
|
807
|
+
const next = place(a.getBoundingClientRect(), f.offsetWidth, Math.max(f.offsetHeight, naturalH), side, align, gap, isRtl(a));
|
|
808
|
+
setPos(
|
|
809
|
+
(prev) => prev && prev.top === next.top && prev.left === next.left && prev.side === next.side && prev.maxHeight === next.maxHeight ? prev : next
|
|
810
|
+
);
|
|
811
|
+
};
|
|
812
|
+
update();
|
|
813
|
+
window.addEventListener("resize", update);
|
|
814
|
+
window.addEventListener("scroll", update, true);
|
|
815
|
+
const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update);
|
|
816
|
+
if (floating.current) ro?.observe(floating.current);
|
|
817
|
+
if (anchor.current) ro?.observe(anchor.current);
|
|
818
|
+
return () => {
|
|
819
|
+
window.removeEventListener("resize", update);
|
|
820
|
+
window.removeEventListener("scroll", update, true);
|
|
821
|
+
ro?.disconnect();
|
|
822
|
+
};
|
|
823
|
+
}, [anchor, floating, open, side, align, gap]);
|
|
824
|
+
return open ? pos : null;
|
|
825
|
+
}
|
|
826
|
+
function floatingStyle(pos, extra) {
|
|
827
|
+
return {
|
|
828
|
+
position: "fixed",
|
|
829
|
+
top: pos?.top ?? 0,
|
|
830
|
+
left: pos?.left ?? 0,
|
|
831
|
+
visibility: pos ? void 0 : "hidden",
|
|
832
|
+
...extra
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
var TIP_MAX_WIDTH = 320;
|
|
836
|
+
var TIP_GRACE_MS = 100;
|
|
837
|
+
var openTip = () => shared("tooltip", () => ({ close: null }));
|
|
838
|
+
var FOCUSABLE_HOST_TAGS = /* @__PURE__ */ new Set(["button", "a", "input", "select", "textarea", "summary"]);
|
|
839
|
+
function Tooltip({ tip, children, className, side = "top", as = "span", focusable, variant = "default", ref, ...rest }) {
|
|
840
|
+
const [triggerRef, setTriggerRef] = useMergedRef(ref);
|
|
841
|
+
const [open, setOpen] = useState(false);
|
|
842
|
+
const id = useId();
|
|
843
|
+
const graceTimer = useRef(0);
|
|
844
|
+
const only = isValidElement(children) ? children : null;
|
|
845
|
+
const staticInner = only !== null && typeof only.type === "string" && FOCUSABLE_HOST_TAGS.has(only.type);
|
|
846
|
+
const [measuredInner, setMeasuredInner] = useState(false);
|
|
847
|
+
useIsoLayoutEffect(() => {
|
|
848
|
+
const el = triggerRef.current;
|
|
849
|
+
const found = only !== null && !!el && el.querySelector(FOCUSABLE) !== null;
|
|
850
|
+
setMeasuredInner(found);
|
|
851
|
+
});
|
|
852
|
+
const inner = only !== null && (staticInner || measuredInner);
|
|
853
|
+
useEffect(() => {
|
|
854
|
+
const el = triggerRef.current;
|
|
855
|
+
if (!el) return;
|
|
856
|
+
const touch = touchTracker();
|
|
857
|
+
const show = () => {
|
|
858
|
+
window.clearTimeout(graceTimer.current);
|
|
859
|
+
setOpen(true);
|
|
860
|
+
};
|
|
861
|
+
const hide = () => {
|
|
862
|
+
window.clearTimeout(graceTimer.current);
|
|
863
|
+
setOpen(false);
|
|
864
|
+
};
|
|
865
|
+
const hideSoon = () => {
|
|
866
|
+
window.clearTimeout(graceTimer.current);
|
|
867
|
+
graceTimer.current = window.setTimeout(() => setOpen(false), TIP_GRACE_MS);
|
|
868
|
+
};
|
|
869
|
+
const onMouseEnter = () => {
|
|
870
|
+
if (!touch.recent()) show();
|
|
871
|
+
};
|
|
872
|
+
const onFocusIn = (e) => {
|
|
873
|
+
if (e.target === el || e.target instanceof Element && keyboardFocused(e.target)) show();
|
|
874
|
+
};
|
|
875
|
+
const onPointerDown = (e) => {
|
|
876
|
+
if (e.target !== el) hide();
|
|
877
|
+
};
|
|
878
|
+
el.addEventListener("pointerenter", touch.onPointer);
|
|
879
|
+
el.addEventListener("pointerdown", touch.onPointer);
|
|
880
|
+
el.addEventListener("pointerdown", onPointerDown);
|
|
881
|
+
el.addEventListener("mouseenter", onMouseEnter);
|
|
882
|
+
el.addEventListener("mouseleave", hideSoon);
|
|
883
|
+
el.addEventListener("focusin", onFocusIn);
|
|
884
|
+
el.addEventListener("focusout", hide);
|
|
885
|
+
return () => {
|
|
886
|
+
window.clearTimeout(graceTimer.current);
|
|
887
|
+
el.removeEventListener("pointerenter", touch.onPointer);
|
|
888
|
+
el.removeEventListener("pointerdown", touch.onPointer);
|
|
889
|
+
el.removeEventListener("pointerdown", onPointerDown);
|
|
890
|
+
el.removeEventListener("mouseenter", onMouseEnter);
|
|
891
|
+
el.removeEventListener("mouseleave", hideSoon);
|
|
892
|
+
el.removeEventListener("focusin", onFocusIn);
|
|
893
|
+
el.removeEventListener("focusout", hide);
|
|
894
|
+
};
|
|
895
|
+
}, [triggerRef]);
|
|
896
|
+
useEffect(() => {
|
|
897
|
+
if (!open) return;
|
|
898
|
+
const slot = openTip();
|
|
899
|
+
const close = () => {
|
|
900
|
+
window.clearTimeout(graceTimer.current);
|
|
901
|
+
setOpen(false);
|
|
902
|
+
};
|
|
903
|
+
if (slot.close && slot.close !== close) slot.close();
|
|
904
|
+
slot.close = close;
|
|
905
|
+
return () => {
|
|
906
|
+
if (slot.close === close) slot.close = null;
|
|
907
|
+
};
|
|
908
|
+
}, [open]);
|
|
909
|
+
useEffect(() => {
|
|
910
|
+
if (!open) return;
|
|
911
|
+
const onKey = (e) => {
|
|
912
|
+
if (e.key !== "Escape") return;
|
|
913
|
+
e.stopPropagation();
|
|
914
|
+
setOpen(false);
|
|
915
|
+
};
|
|
916
|
+
document.addEventListener("keydown", onKey, true);
|
|
917
|
+
return () => document.removeEventListener("keydown", onKey, true);
|
|
918
|
+
}, [open]);
|
|
919
|
+
const onTipPointer = useCallback((over) => {
|
|
920
|
+
window.clearTimeout(graceTimer.current);
|
|
921
|
+
if (!over) graceTimer.current = window.setTimeout(() => setOpen(false), TIP_GRACE_MS);
|
|
922
|
+
}, []);
|
|
923
|
+
const wrapperFocusable = focusable ?? !inner;
|
|
924
|
+
const a11y = {
|
|
925
|
+
tabIndex: wrapperFocusable ? 0 : void 0,
|
|
926
|
+
"aria-describedby": open && !inner ? id : void 0,
|
|
927
|
+
"data-state": open ? "open" : "closed"
|
|
928
|
+
};
|
|
929
|
+
const hint = variant === "hint" && "cursor-help underline decoration-th-hi3 decoration-dotted underline-offset-2";
|
|
930
|
+
let content = children;
|
|
931
|
+
if (inner && only) {
|
|
932
|
+
const existing = only.props["aria-describedby"];
|
|
933
|
+
const describedby = open ? [typeof existing === "string" ? existing : "", id].filter(Boolean).join(" ") : existing;
|
|
934
|
+
content = cloneElement(only, {
|
|
935
|
+
"aria-describedby": describedby || void 0
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
939
|
+
as === "tr" ? /* @__PURE__ */ jsx("tr", { ...rest, ref: setTriggerRef, className: cn(hint, className), ...a11y, children: content }) : /* @__PURE__ */ jsx(
|
|
940
|
+
"span",
|
|
941
|
+
{
|
|
942
|
+
...rest,
|
|
943
|
+
ref: setTriggerRef,
|
|
944
|
+
className: cn(hint ? "inline" : "inline-flex items-center", hint, className),
|
|
945
|
+
...a11y,
|
|
946
|
+
children: content
|
|
947
|
+
}
|
|
948
|
+
),
|
|
949
|
+
open ? /* @__PURE__ */ jsx(
|
|
950
|
+
FloatingTip,
|
|
951
|
+
{
|
|
952
|
+
anchor: triggerRef,
|
|
953
|
+
id,
|
|
954
|
+
tip,
|
|
955
|
+
side,
|
|
956
|
+
onPointerEnter: () => onTipPointer(true),
|
|
957
|
+
onPointerLeave: () => onTipPointer(false)
|
|
958
|
+
}
|
|
959
|
+
) : null
|
|
960
|
+
] });
|
|
961
|
+
}
|
|
962
|
+
function FloatingTip({
|
|
963
|
+
anchor,
|
|
964
|
+
id,
|
|
965
|
+
tip,
|
|
966
|
+
side = "top",
|
|
967
|
+
onPointerEnter,
|
|
968
|
+
onPointerLeave
|
|
969
|
+
}) {
|
|
970
|
+
const config = useNishConfig();
|
|
971
|
+
const tipRef = useRef(null);
|
|
972
|
+
const pos = useAnchoredPosition(anchor, tipRef, true, side, "center");
|
|
973
|
+
return createPortal(
|
|
974
|
+
/* @__PURE__ */ jsx(
|
|
975
|
+
"span",
|
|
976
|
+
{
|
|
977
|
+
ref: tipRef,
|
|
978
|
+
id,
|
|
979
|
+
role: "tooltip",
|
|
980
|
+
"data-side": pos?.side,
|
|
981
|
+
"data-state": "open",
|
|
982
|
+
onPointerEnter,
|
|
983
|
+
onPointerLeave,
|
|
984
|
+
style: floatingStyle(pos, {
|
|
985
|
+
maxWidth: Math.min(TIP_MAX_WIDTH, (window.innerWidth || TIP_MAX_WIDTH) - 2 * EDGE),
|
|
986
|
+
zIndex: "var(--nish-z-tooltip, 100)"
|
|
987
|
+
}),
|
|
988
|
+
className: "block w-max rounded-lg border border-th-line-strong bg-th-elevated px-3 py-2 text-xs font-normal normal-case leading-snug tracking-normal text-th-hi shadow-pop",
|
|
989
|
+
children: tip
|
|
990
|
+
}
|
|
991
|
+
),
|
|
992
|
+
layerHost(anchor.current, config.portalContainer)
|
|
993
|
+
);
|
|
994
|
+
}
|
|
995
|
+
function useDismiss(open, panel, anchor, close) {
|
|
996
|
+
const closeRef = useLatest(close);
|
|
997
|
+
useEffect(() => {
|
|
998
|
+
if (!open) return;
|
|
999
|
+
const { layerStack } = ov();
|
|
1000
|
+
const self = {};
|
|
1001
|
+
layerStack.push(self);
|
|
1002
|
+
const onKey = (e) => {
|
|
1003
|
+
if (e.key === "Escape" && isTop(self)) {
|
|
1004
|
+
e.stopPropagation();
|
|
1005
|
+
closeRef.current("escape");
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
const onDown = (e) => {
|
|
1009
|
+
const t = e.target;
|
|
1010
|
+
if (!t || panel.current?.contains(t) || anchor.current?.contains(t)) return;
|
|
1011
|
+
if (!isTop(self)) return;
|
|
1012
|
+
closeRef.current("outside");
|
|
1013
|
+
};
|
|
1014
|
+
document.addEventListener("keydown", onKey);
|
|
1015
|
+
document.addEventListener("pointerdown", onDown, true);
|
|
1016
|
+
return () => {
|
|
1017
|
+
const at = layerStack.indexOf(self);
|
|
1018
|
+
if (at >= 0) layerStack.splice(at, 1);
|
|
1019
|
+
document.removeEventListener("keydown", onKey);
|
|
1020
|
+
document.removeEventListener("pointerdown", onDown, true);
|
|
1021
|
+
};
|
|
1022
|
+
}, [open, panel, anchor, closeRef]);
|
|
1023
|
+
}
|
|
1024
|
+
function useReturnFocus(open, panel, anchor) {
|
|
1025
|
+
const wasOpen = useRef(false);
|
|
1026
|
+
const focusWasInside = useRef(false);
|
|
1027
|
+
useEffect(() => {
|
|
1028
|
+
if (!open) return;
|
|
1029
|
+
const p = panel.current;
|
|
1030
|
+
const track = () => {
|
|
1031
|
+
focusWasInside.current = !!p && p.contains(document.activeElement);
|
|
1032
|
+
};
|
|
1033
|
+
p?.addEventListener("focusin", track);
|
|
1034
|
+
p?.addEventListener("focusout", track);
|
|
1035
|
+
return () => {
|
|
1036
|
+
p?.removeEventListener("focusin", track);
|
|
1037
|
+
p?.removeEventListener("focusout", track);
|
|
1038
|
+
};
|
|
1039
|
+
}, [open, panel]);
|
|
1040
|
+
useIsoLayoutEffect(() => {
|
|
1041
|
+
if (open) {
|
|
1042
|
+
wasOpen.current = true;
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
if (!wasOpen.current) return;
|
|
1046
|
+
wasOpen.current = false;
|
|
1047
|
+
const active = document.activeElement;
|
|
1048
|
+
if (focusWasInside.current || !active || active === document.body) anchor.current?.focus();
|
|
1049
|
+
focusWasInside.current = false;
|
|
1050
|
+
}, [open, anchor]);
|
|
1051
|
+
}
|
|
1052
|
+
function Popover({
|
|
1053
|
+
open,
|
|
1054
|
+
onOpenChange,
|
|
1055
|
+
dismissOnOutside = true,
|
|
1056
|
+
anchor,
|
|
1057
|
+
side = "bottom",
|
|
1058
|
+
align = "start",
|
|
1059
|
+
width,
|
|
1060
|
+
"aria-label": ariaLabel,
|
|
1061
|
+
className,
|
|
1062
|
+
children,
|
|
1063
|
+
ref,
|
|
1064
|
+
style,
|
|
1065
|
+
onKeyDown: userKeyDown,
|
|
1066
|
+
...rest
|
|
1067
|
+
}) {
|
|
1068
|
+
const config = useNishConfig();
|
|
1069
|
+
const isClient = useIsClient();
|
|
1070
|
+
const [panelRef, setPanelRef] = useMergedRef(ref);
|
|
1071
|
+
const pos = useAnchoredPosition(anchor, panelRef, open, side, align);
|
|
1072
|
+
const onOpenChangeRef = useLatest(onOpenChange);
|
|
1073
|
+
const outsideRef = useLatest(dismissOnOutside);
|
|
1074
|
+
useDismiss(open, panelRef, anchor, (reason) => {
|
|
1075
|
+
if (reason === "outside" && !outsideRef.current) return;
|
|
1076
|
+
onOpenChangeRef.current(false, reason);
|
|
1077
|
+
});
|
|
1078
|
+
useReturnFocus(open, panelRef, anchor);
|
|
1079
|
+
useEffect(() => {
|
|
1080
|
+
if (!open) return;
|
|
1081
|
+
const t = window.setTimeout(() => {
|
|
1082
|
+
const p = panelRef.current;
|
|
1083
|
+
if (p && !p.contains(document.activeElement)) (p.querySelector("[data-autofocus],[autofocus]") ?? p).focus();
|
|
1084
|
+
}, 0);
|
|
1085
|
+
return () => window.clearTimeout(t);
|
|
1086
|
+
}, [open, panelRef]);
|
|
1087
|
+
if (!open || !isClient) return null;
|
|
1088
|
+
function onKeyDown(e) {
|
|
1089
|
+
userKeyDown?.(e);
|
|
1090
|
+
if (e.key !== "Tab" || !panelRef.current) return;
|
|
1091
|
+
const nodes = focusables(panelRef.current);
|
|
1092
|
+
const active = document.activeElement;
|
|
1093
|
+
const atStart = nodes.length === 0 || active === nodes[0] || active === panelRef.current;
|
|
1094
|
+
const atEnd = nodes.length === 0 || active === nodes[nodes.length - 1];
|
|
1095
|
+
if (e.shiftKey && atStart || !e.shiftKey && atEnd) {
|
|
1096
|
+
e.preventDefault();
|
|
1097
|
+
onOpenChangeRef.current(false, "tab");
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
return createPortal(
|
|
1101
|
+
/* @__PURE__ */ jsx(
|
|
1102
|
+
"div",
|
|
1103
|
+
{
|
|
1104
|
+
...rest,
|
|
1105
|
+
ref: setPanelRef,
|
|
1106
|
+
role: "dialog",
|
|
1107
|
+
"aria-label": ariaLabel,
|
|
1108
|
+
tabIndex: -1,
|
|
1109
|
+
"data-side": pos?.side,
|
|
1110
|
+
"data-state": "open",
|
|
1111
|
+
onKeyDown,
|
|
1112
|
+
style: floatingStyle(pos, {
|
|
1113
|
+
...style,
|
|
1114
|
+
width,
|
|
1115
|
+
maxHeight: pos?.maxHeight,
|
|
1116
|
+
zIndex: "var(--nish-z-dropdown, 40)"
|
|
1117
|
+
}),
|
|
1118
|
+
className: cn(
|
|
1119
|
+
"max-w-[calc(100vw-1rem)] overflow-y-auto overscroll-contain rounded-lg border border-th-line bg-th-elevated p-3 text-sm text-th-hi2 shadow-pop outline-none",
|
|
1120
|
+
"animate-[nish-fade-in_120ms_ease-out]",
|
|
1121
|
+
className
|
|
1122
|
+
),
|
|
1123
|
+
children
|
|
1124
|
+
}
|
|
1125
|
+
),
|
|
1126
|
+
layerHost(anchor.current, config.portalContainer)
|
|
1127
|
+
);
|
|
1128
|
+
}
|
|
1129
|
+
var MenuContext = /* @__PURE__ */ createContext(null);
|
|
1130
|
+
var ITEM_SELECTOR = '[role="menuitem"]:not([aria-disabled="true"])';
|
|
1131
|
+
function menuItems(menu) {
|
|
1132
|
+
return menu ? Array.from(menu.querySelectorAll(ITEM_SELECTOR)) : [];
|
|
1133
|
+
}
|
|
1134
|
+
function usePhoneViewport(enabled) {
|
|
1135
|
+
const phone = useMediaQuery(PHONE_QUERY);
|
|
1136
|
+
return enabled && phone;
|
|
1137
|
+
}
|
|
1138
|
+
function Cloned({ element, ...props }) {
|
|
1139
|
+
return cloneElement(element, props);
|
|
1140
|
+
}
|
|
1141
|
+
function DropdownMenu({
|
|
1142
|
+
trigger,
|
|
1143
|
+
align = "start",
|
|
1144
|
+
side = "bottom",
|
|
1145
|
+
children,
|
|
1146
|
+
open: openProp,
|
|
1147
|
+
onOpenChange,
|
|
1148
|
+
defaultOpen = false,
|
|
1149
|
+
"aria-label": ariaLabel,
|
|
1150
|
+
sheetBelow = false,
|
|
1151
|
+
sheetTitle,
|
|
1152
|
+
className,
|
|
1153
|
+
ref,
|
|
1154
|
+
style,
|
|
1155
|
+
onKeyDown: userKeyDown,
|
|
1156
|
+
...rest
|
|
1157
|
+
}) {
|
|
1158
|
+
const strings = useNishStrings();
|
|
1159
|
+
const config = useNishConfig();
|
|
1160
|
+
const [openState, setOpenState] = useState(defaultOpen);
|
|
1161
|
+
const open = openProp ?? openState;
|
|
1162
|
+
const triggerRef = useRef(null);
|
|
1163
|
+
const [triggerNode, setTriggerNode] = useState(null);
|
|
1164
|
+
const triggerPropRef = trigger.props.ref;
|
|
1165
|
+
const attachTrigger = useCallback(
|
|
1166
|
+
(node) => {
|
|
1167
|
+
triggerRef.current = node;
|
|
1168
|
+
setTriggerNode(node);
|
|
1169
|
+
setRef(triggerPropRef, node);
|
|
1170
|
+
},
|
|
1171
|
+
[triggerPropRef]
|
|
1172
|
+
);
|
|
1173
|
+
const shown = open && triggerNode != null;
|
|
1174
|
+
const [menuRef, setMenuRef] = useMergedRef(ref);
|
|
1175
|
+
const menuId = useId();
|
|
1176
|
+
const triggerId = useId();
|
|
1177
|
+
const initial = useRef("first");
|
|
1178
|
+
const asSheet = usePhoneViewport(sheetBelow === "sm");
|
|
1179
|
+
const pos = useAnchoredPosition(triggerRef, menuRef, shown && !asSheet, side, align, 4);
|
|
1180
|
+
const onOpenChangeRef = useLatest(onOpenChange);
|
|
1181
|
+
const setOpen = useCallback(
|
|
1182
|
+
(next, reason) => {
|
|
1183
|
+
if (openProp === void 0) setOpenState(next);
|
|
1184
|
+
onOpenChangeRef.current?.(next, reason);
|
|
1185
|
+
},
|
|
1186
|
+
[onOpenChangeRef, openProp]
|
|
1187
|
+
);
|
|
1188
|
+
const close = useCallback(() => setOpen(false), [setOpen]);
|
|
1189
|
+
const dismiss = useCallback((reason) => setOpen(false, reason), [setOpen]);
|
|
1190
|
+
useDismiss(shown && !asSheet, menuRef, triggerRef, dismiss);
|
|
1191
|
+
useReturnFocus(shown, menuRef, triggerRef);
|
|
1192
|
+
useEffect(() => {
|
|
1193
|
+
if (!shown) return;
|
|
1194
|
+
const menu = menuRef.current;
|
|
1195
|
+
if (!menu) return;
|
|
1196
|
+
const focusInitial = () => {
|
|
1197
|
+
const items = menuItems(menu);
|
|
1198
|
+
const target = initial.current === "last" ? items[items.length - 1] : initial.current === "menu" ? menu : items[0];
|
|
1199
|
+
(target ?? menu).focus();
|
|
1200
|
+
};
|
|
1201
|
+
const t = window.setTimeout(focusInitial, 0);
|
|
1202
|
+
const mo = new MutationObserver(() => {
|
|
1203
|
+
if (!menu.contains(document.activeElement) && (document.activeElement === document.body || !document.activeElement)) {
|
|
1204
|
+
initial.current = "first";
|
|
1205
|
+
focusInitial();
|
|
1206
|
+
}
|
|
1207
|
+
});
|
|
1208
|
+
mo.observe(menu, { childList: true, subtree: true });
|
|
1209
|
+
return () => {
|
|
1210
|
+
window.clearTimeout(t);
|
|
1211
|
+
mo.disconnect();
|
|
1212
|
+
};
|
|
1213
|
+
}, [shown, asSheet, menuRef]);
|
|
1214
|
+
function openWith(where) {
|
|
1215
|
+
initial.current = where;
|
|
1216
|
+
setOpen(true);
|
|
1217
|
+
}
|
|
1218
|
+
function onMenuKeyDown(e) {
|
|
1219
|
+
userKeyDown?.(e);
|
|
1220
|
+
const items = menuItems(menuRef.current);
|
|
1221
|
+
const i = items.indexOf(document.activeElement);
|
|
1222
|
+
const go = (n) => {
|
|
1223
|
+
e.preventDefault();
|
|
1224
|
+
items[(n + items.length) % items.length]?.focus();
|
|
1225
|
+
};
|
|
1226
|
+
switch (e.key) {
|
|
1227
|
+
case "ArrowDown":
|
|
1228
|
+
return go(i + 1);
|
|
1229
|
+
case "ArrowUp":
|
|
1230
|
+
return go(i < 0 ? items.length - 1 : i - 1);
|
|
1231
|
+
case "Home":
|
|
1232
|
+
return go(0);
|
|
1233
|
+
case "End":
|
|
1234
|
+
return go(items.length - 1);
|
|
1235
|
+
case "Tab":
|
|
1236
|
+
e.preventDefault();
|
|
1237
|
+
dismiss("tab");
|
|
1238
|
+
return;
|
|
1239
|
+
default:
|
|
1240
|
+
if (e.key.length === 1 && /\S/.test(e.key) && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
1241
|
+
const k = e.key.toLowerCase();
|
|
1242
|
+
const order = [...items.slice(i + 1), ...items.slice(0, i + 1)];
|
|
1243
|
+
const hit = order.find((el) => (el.textContent ?? "").trim().toLowerCase().startsWith(k));
|
|
1244
|
+
if (hit) {
|
|
1245
|
+
e.preventDefault();
|
|
1246
|
+
hit.focus();
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
const tp = trigger.props;
|
|
1252
|
+
const triggerEl = /* @__PURE__ */ jsx(
|
|
1253
|
+
Cloned,
|
|
1254
|
+
{
|
|
1255
|
+
element: trigger,
|
|
1256
|
+
ref: attachTrigger,
|
|
1257
|
+
id: tp.id ?? triggerId,
|
|
1258
|
+
"aria-haspopup": "menu",
|
|
1259
|
+
"aria-expanded": open,
|
|
1260
|
+
"aria-controls": open ? menuId : void 0,
|
|
1261
|
+
onClick: (e) => {
|
|
1262
|
+
tp.onClick?.(e);
|
|
1263
|
+
if (e.defaultPrevented) return;
|
|
1264
|
+
if (open) close();
|
|
1265
|
+
else openWith(e.detail === 0 ? "first" : "menu");
|
|
1266
|
+
},
|
|
1267
|
+
onKeyDown: (e) => {
|
|
1268
|
+
tp.onKeyDown?.(e);
|
|
1269
|
+
if (e.defaultPrevented) return;
|
|
1270
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
1271
|
+
e.preventDefault();
|
|
1272
|
+
openWith(e.key === "ArrowDown" ? "first" : "last");
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
);
|
|
1277
|
+
const menuProps = {
|
|
1278
|
+
...rest,
|
|
1279
|
+
ref: setMenuRef,
|
|
1280
|
+
id: menuId,
|
|
1281
|
+
role: "menu",
|
|
1282
|
+
"aria-label": ariaLabel,
|
|
1283
|
+
"aria-labelledby": ariaLabel ? void 0 : tp.id ?? triggerId,
|
|
1284
|
+
"aria-orientation": "vertical",
|
|
1285
|
+
"data-state": "open",
|
|
1286
|
+
tabIndex: -1,
|
|
1287
|
+
onKeyDown: onMenuKeyDown
|
|
1288
|
+
};
|
|
1289
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1290
|
+
triggerEl,
|
|
1291
|
+
open && asSheet ? /* @__PURE__ */ jsx(MenuContext.Provider, { value: { close }, children: /* @__PURE__ */ jsx(Drawer, { side: "bottom", size: "md", title: sheetTitle ?? strings.menu, onClose: close, children: /* @__PURE__ */ jsx(
|
|
1292
|
+
"div",
|
|
1293
|
+
{
|
|
1294
|
+
...menuProps,
|
|
1295
|
+
style,
|
|
1296
|
+
className: cn(
|
|
1297
|
+
"-mx-5 -my-4 py-2 outline-none [&_[role=menuitem]]:min-h-12 [&_[role=menuitem]]:px-5 [&_[role=menuitem]]:text-sm",
|
|
1298
|
+
className
|
|
1299
|
+
),
|
|
1300
|
+
children
|
|
1301
|
+
}
|
|
1302
|
+
) }) }) : shown ? createPortal(
|
|
1303
|
+
/* @__PURE__ */ jsx(MenuContext.Provider, { value: { close }, children: /* @__PURE__ */ jsx(
|
|
1304
|
+
"div",
|
|
1305
|
+
{
|
|
1306
|
+
...menuProps,
|
|
1307
|
+
"data-side": pos?.side,
|
|
1308
|
+
style: floatingStyle(pos, {
|
|
1309
|
+
...style,
|
|
1310
|
+
maxHeight: pos ? Math.min(pos.maxHeight, 384) : void 0,
|
|
1311
|
+
zIndex: "var(--nish-z-dropdown, 40)"
|
|
1312
|
+
}),
|
|
1313
|
+
className: cn(
|
|
1314
|
+
"max-h-[min(24rem,calc(100dvh-1rem))] min-w-44 max-w-[calc(100vw-1rem)] overflow-y-auto overscroll-contain",
|
|
1315
|
+
"rounded-lg border border-th-line bg-th-elevated py-1 shadow-pop outline-none animate-[nish-fade-in_120ms_ease-out]",
|
|
1316
|
+
className
|
|
1317
|
+
),
|
|
1318
|
+
children
|
|
1319
|
+
}
|
|
1320
|
+
) }),
|
|
1321
|
+
layerHost(triggerNode, config.portalContainer)
|
|
1322
|
+
) : null
|
|
1323
|
+
] });
|
|
1324
|
+
}
|
|
1325
|
+
function MenuItem({
|
|
1326
|
+
icon,
|
|
1327
|
+
trailing,
|
|
1328
|
+
danger = false,
|
|
1329
|
+
active = false,
|
|
1330
|
+
disabled = false,
|
|
1331
|
+
onSelect,
|
|
1332
|
+
className,
|
|
1333
|
+
children,
|
|
1334
|
+
ref,
|
|
1335
|
+
onClick,
|
|
1336
|
+
onMouseMove,
|
|
1337
|
+
...rest
|
|
1338
|
+
}) {
|
|
1339
|
+
const ctx = useContext(MenuContext);
|
|
1340
|
+
function select() {
|
|
1341
|
+
if (disabled) return;
|
|
1342
|
+
const ev = new Event("nish-menu-select", { cancelable: true });
|
|
1343
|
+
onSelect?.(ev);
|
|
1344
|
+
if (!ev.defaultPrevented) ctx?.close();
|
|
1345
|
+
}
|
|
1346
|
+
return /* @__PURE__ */ jsxs(
|
|
1347
|
+
"button",
|
|
1348
|
+
{
|
|
1349
|
+
...rest,
|
|
1350
|
+
ref,
|
|
1351
|
+
type: "button",
|
|
1352
|
+
role: "menuitem",
|
|
1353
|
+
tabIndex: -1,
|
|
1354
|
+
"aria-disabled": disabled || void 0,
|
|
1355
|
+
"aria-current": active || void 0,
|
|
1356
|
+
"data-disabled": disabled ? "" : void 0,
|
|
1357
|
+
onClick: (e) => {
|
|
1358
|
+
onClick?.(e);
|
|
1359
|
+
if (!e.defaultPrevented) select();
|
|
1360
|
+
},
|
|
1361
|
+
onMouseMove: (e) => {
|
|
1362
|
+
onMouseMove?.(e);
|
|
1363
|
+
if (!disabled && document.activeElement !== e.currentTarget) e.currentTarget.focus();
|
|
1364
|
+
},
|
|
1365
|
+
className: menuItemClass({
|
|
1366
|
+
active,
|
|
1367
|
+
danger,
|
|
1368
|
+
disabled,
|
|
1369
|
+
className: cn(!disabled && (danger ? "focus:bg-th-loss-soft" : "focus:bg-th-overlay focus:text-th-hi"), className)
|
|
1370
|
+
}),
|
|
1371
|
+
children: [
|
|
1372
|
+
icon ? /* @__PURE__ */ jsx("span", { className: cn("inline-flex shrink-0", danger || disabled ? "" : "text-th-hi3"), children: icon }) : null,
|
|
1373
|
+
/* @__PURE__ */ jsx("span", { className: "min-w-0 flex-1 truncate", children }),
|
|
1374
|
+
trailing ? /* @__PURE__ */ jsx("span", { className: cn("ms-auto inline-flex shrink-0 items-center", disabled ? "" : "text-th-hi2"), children: trailing }) : null
|
|
1375
|
+
]
|
|
1376
|
+
}
|
|
1377
|
+
);
|
|
1378
|
+
}
|
|
1379
|
+
function MenuLabel({
|
|
1380
|
+
children,
|
|
1381
|
+
className,
|
|
1382
|
+
ref,
|
|
1383
|
+
...rest
|
|
1384
|
+
}) {
|
|
1385
|
+
return /* @__PURE__ */ jsx(
|
|
1386
|
+
"div",
|
|
1387
|
+
{
|
|
1388
|
+
...rest,
|
|
1389
|
+
ref,
|
|
1390
|
+
role: "presentation",
|
|
1391
|
+
className: cn("px-3 pb-1 pt-2 text-2xs font-semibold uppercase tracking-wider text-th-hi2", className),
|
|
1392
|
+
children
|
|
1393
|
+
}
|
|
1394
|
+
);
|
|
1395
|
+
}
|
|
1396
|
+
function MenuSeparator({
|
|
1397
|
+
className,
|
|
1398
|
+
ref,
|
|
1399
|
+
...rest
|
|
1400
|
+
}) {
|
|
1401
|
+
return /* @__PURE__ */ jsx("div", { ...rest, ref, role: "separator", className: cn("my-1 h-px bg-th-line forced-colors:bg-[CanvasText]", className) });
|
|
1402
|
+
}
|
|
1403
|
+
function MenuSubHeader({ onBack, children, className, ref, onKeyDown, ...rest }) {
|
|
1404
|
+
const strings = useNishStrings();
|
|
1405
|
+
return /* @__PURE__ */ jsxs(
|
|
1406
|
+
"button",
|
|
1407
|
+
{
|
|
1408
|
+
...rest,
|
|
1409
|
+
ref,
|
|
1410
|
+
type: "button",
|
|
1411
|
+
role: "menuitem",
|
|
1412
|
+
tabIndex: -1,
|
|
1413
|
+
onClick: onBack,
|
|
1414
|
+
onKeyDown: (e) => {
|
|
1415
|
+
onKeyDown?.(e);
|
|
1416
|
+
if (e.key === (isRtl(e.currentTarget) ? "ArrowRight" : "ArrowLeft")) {
|
|
1417
|
+
e.preventDefault();
|
|
1418
|
+
onBack();
|
|
1419
|
+
}
|
|
1420
|
+
},
|
|
1421
|
+
className: cn(MENU_ROW, "mb-1 border-b border-th-line pb-2 font-semibold text-th-hi hover:bg-th-overlay focus:bg-th-overlay", className),
|
|
1422
|
+
children: [
|
|
1423
|
+
/* @__PURE__ */ jsx(
|
|
1424
|
+
"svg",
|
|
1425
|
+
{
|
|
1426
|
+
width: 14,
|
|
1427
|
+
height: 14,
|
|
1428
|
+
viewBox: "0 0 24 24",
|
|
1429
|
+
fill: "none",
|
|
1430
|
+
stroke: "currentColor",
|
|
1431
|
+
strokeWidth: 2,
|
|
1432
|
+
strokeLinecap: "round",
|
|
1433
|
+
strokeLinejoin: "round",
|
|
1434
|
+
"aria-hidden": "true",
|
|
1435
|
+
className: "shrink-0 text-th-hi3 rtl:-scale-x-100",
|
|
1436
|
+
children: /* @__PURE__ */ jsx("path", { d: "m15 18-6-6 6-6" })
|
|
1437
|
+
}
|
|
1438
|
+
),
|
|
1439
|
+
/* @__PURE__ */ jsx("span", { className: "sr-only", children: strings.back }),
|
|
1440
|
+
/* @__PURE__ */ jsx("span", { className: "min-w-0 flex-1 truncate", children })
|
|
1441
|
+
]
|
|
1442
|
+
}
|
|
1443
|
+
);
|
|
1444
|
+
}
|
|
1445
|
+
export {
|
|
1446
|
+
ConfirmDialog,
|
|
1447
|
+
Drawer,
|
|
1448
|
+
DropdownMenu,
|
|
1449
|
+
FloatingTip,
|
|
1450
|
+
MenuItem,
|
|
1451
|
+
MenuLabel,
|
|
1452
|
+
MenuSeparator,
|
|
1453
|
+
MenuSubHeader,
|
|
1454
|
+
Modal,
|
|
1455
|
+
Popover,
|
|
1456
|
+
Tooltip,
|
|
1457
|
+
keyboardFocused,
|
|
1458
|
+
menuItemClass,
|
|
1459
|
+
touchTracker,
|
|
1460
|
+
whenHistorySettled
|
|
1461
|
+
};
|