nish-ui 1.9.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/README.md +218 -58
  2. package/dist/base-tailwind.css +1757 -0
  3. package/dist/base.css +1590 -1445
  4. package/dist/chart.d.ts +4 -1
  5. package/dist/chart.js +122 -49
  6. package/dist/elements-all.d.ts +8 -0
  7. package/dist/elements-all.js +178 -0
  8. package/dist/elements-charts.d.ts +71 -0
  9. package/dist/elements-charts.js +56 -57
  10. package/dist/elements-data.d.ts +71 -0
  11. package/dist/elements-data.js +5 -6
  12. package/dist/elements-forms.d.ts +71 -0
  13. package/dist/elements-forms.js +24 -25
  14. package/dist/elements.d.ts +71 -0
  15. package/dist/elements.js +4 -5
  16. package/dist/esm/chart-values.js +58 -0
  17. package/dist/esm/chart.js +10 -0
  18. package/dist/esm/cn.js +19 -0
  19. package/dist/esm/combobox-filter.js +13 -0
  20. package/dist/esm/components/accordion.js +166 -0
  21. package/dist/esm/components/app-shell.js +198 -0
  22. package/dist/esm/components/avatar-classes.js +28 -0
  23. package/dist/esm/components/avatar.js +113 -0
  24. package/dist/esm/components/breadcrumbs.js +154 -0
  25. package/dist/esm/components/canvas/follow-caption.js +32 -0
  26. package/dist/esm/components/canvas/follow.js +119 -0
  27. package/dist/esm/components/canvas/icons.js +70 -0
  28. package/dist/esm/components/canvas/index.js +10 -0
  29. package/dist/esm/components/canvas/layout.js +258 -0
  30. package/dist/esm/components/canvas/legend.js +93 -0
  31. package/dist/esm/components/canvas/minimap.js +127 -0
  32. package/dist/esm/components/canvas/node.js +87 -0
  33. package/dist/esm/components/canvas/route.js +262 -0
  34. package/dist/esm/components/canvas/system-canvas.js +837 -0
  35. package/dist/esm/components/canvas/types.js +10 -0
  36. package/dist/esm/components/canvas/view.js +47 -0
  37. package/dist/esm/components/chart-hooks.js +19 -0
  38. package/dist/esm/components/charts.js +467 -0
  39. package/dist/esm/components/client.js +24 -0
  40. package/dist/esm/components/combobox.js +343 -0
  41. package/dist/esm/components/command.js +403 -0
  42. package/dist/esm/components/data-classes.js +159 -0
  43. package/dist/esm/components/data.js +517 -0
  44. package/dist/esm/components/display-classes.js +130 -0
  45. package/dist/esm/components/display.js +755 -0
  46. package/dist/esm/components/error-boundary.js +64 -0
  47. package/dist/esm/components/feedback.js +136 -0
  48. package/dist/esm/components/file-dropzone.js +184 -0
  49. package/dist/esm/components/filters.js +108 -0
  50. package/dist/esm/components/flow.js +746 -0
  51. package/dist/esm/components/forms-classes.js +43 -0
  52. package/dist/esm/components/forms.js +581 -0
  53. package/dist/esm/components/hooks-classes.js +9 -0
  54. package/dist/esm/components/hooks.js +236 -0
  55. package/dist/esm/components/icons.js +96 -0
  56. package/dist/esm/components/inbox-classes.js +29 -0
  57. package/dist/esm/components/inbox.js +257 -0
  58. package/dist/esm/components/latest.js +13 -0
  59. package/dist/esm/components/layout-vars.js +96 -0
  60. package/dist/esm/components/layout.js +272 -0
  61. package/dist/esm/components/media.js +143 -0
  62. package/dist/esm/components/nav.js +614 -0
  63. package/dist/esm/components/overlay-classes.js +66 -0
  64. package/dist/esm/components/overlay.js +1447 -0
  65. package/dist/esm/components/pagination-classes.js +29 -0
  66. package/dist/esm/components/pagination.js +154 -0
  67. package/dist/esm/components/portal.js +16 -0
  68. package/dist/esm/components/primitives-classes.js +198 -0
  69. package/dist/esm/components/primitives.js +268 -0
  70. package/dist/esm/components/progress.js +77 -0
  71. package/dist/esm/components/provider.js +32 -0
  72. package/dist/esm/components/radio-group.js +186 -0
  73. package/dist/esm/components/resource-classes.js +133 -0
  74. package/dist/esm/components/resource.js +209 -0
  75. package/dist/esm/components/segmented.js +178 -0
  76. package/dist/esm/components/separator.js +38 -0
  77. package/dist/esm/components/service-tokens.js +221 -0
  78. package/dist/esm/components/settings.js +301 -0
  79. package/dist/esm/components/slider.js +262 -0
  80. package/dist/esm/components/split-classes.js +8 -0
  81. package/dist/esm/components/split.js +411 -0
  82. package/dist/esm/components/strings.js +119 -0
  83. package/dist/esm/components/table.js +144 -0
  84. package/dist/esm/components/tabs-classes.js +21 -0
  85. package/dist/esm/components/tabs.js +201 -0
  86. package/dist/esm/components/tag-input.js +257 -0
  87. package/dist/esm/components/theme-toggle.js +177 -0
  88. package/dist/esm/components/toast.js +361 -0
  89. package/dist/esm/components/url-state.js +118 -0
  90. package/dist/esm/components/visually-hidden.js +20 -0
  91. package/dist/esm/format.js +320 -0
  92. package/dist/esm/inbox-client.js +2 -0
  93. package/dist/esm/inbox-types.js +2 -0
  94. package/dist/esm/index.js +320 -0
  95. package/dist/esm/runtime.js +37 -0
  96. package/dist/esm/server/inbox/client.js +297 -0
  97. package/dist/esm/server/inbox/types.js +24 -0
  98. package/dist/esm/sparkline-path.js +59 -0
  99. package/dist/esm/split-layout.js +419 -0
  100. package/dist/esm/system-canvas.js +2 -0
  101. package/dist/esm/theme.js +804 -0
  102. package/dist/esm/tokens.mjs +425 -0
  103. package/dist/esm/url.js +20 -0
  104. package/dist/files.json +5 -1
  105. package/dist/fonts/LICENSE-Inter.txt +93 -0
  106. package/dist/format.d.ts +32 -18
  107. package/dist/format.js +227 -82
  108. package/dist/inbox-client.js +3 -1
  109. package/dist/index.d.ts +1686 -667
  110. package/dist/nish-components.css +4649 -4506
  111. package/dist/nish-components.min.css +1 -0
  112. package/dist/nish.css +1603 -1450
  113. package/dist/nish.min.css +1 -0
  114. package/dist/node/eslint.js +29 -14
  115. package/dist/node/th-check.js +29 -14
  116. package/dist/node/vite.d.ts +12 -0
  117. package/dist/node/vite.js +12 -2
  118. package/dist/server/hono.d.ts +15 -6
  119. package/dist/server/hono.js +30 -25
  120. package/dist/server/index.d.ts +3 -10
  121. package/dist/server/index.js +71 -38
  122. package/dist/{spa-DuVB5O7S.d.ts → spa-BLlMc0eI.d.ts} +38 -5
  123. package/dist/system-canvas-BLuWd69H.d.ts +183 -0
  124. package/dist/system-canvas.d.ts +2 -168
  125. package/dist/tailwind-core.css +114 -0
  126. package/dist/tailwind.css +3 -7
  127. package/dist/theme-init.js +23 -9
  128. package/dist/theme-init.sha256 +1 -0
  129. package/dist/theme.d.ts +243 -4
  130. package/dist/theme.global.js +356 -89
  131. package/dist/theme.js +337 -67
  132. package/dist/tokens.css +14 -6
  133. package/dist/tokens.dtcg.json +1513 -0
  134. package/dist/tokens.js +29 -14
  135. package/dist/tokens.json +512 -0
  136. package/package.json +51 -13
  137. package/dist/chart.js.map +0 -1
  138. package/dist/elements-charts.js.map +0 -1
  139. package/dist/elements-data.js.map +0 -1
  140. package/dist/elements-forms.js.map +0 -1
  141. package/dist/elements.js.map +0 -1
  142. package/dist/format.js.map +0 -1
  143. package/dist/inbox-client.js.map +0 -1
  144. package/dist/index.js +0 -10260
  145. package/dist/index.js.map +0 -1
  146. package/dist/server/hono.js.map +0 -1
  147. package/dist/server/index.js.map +0 -1
  148. package/dist/split-layout.js.map +0 -1
  149. package/dist/system-canvas.js +0 -2204
  150. package/dist/system-canvas.js.map +0 -1
  151. package/dist/theme.global.js.map +0 -1
  152. package/dist/theme.js.map +0 -1
@@ -0,0 +1,1447 @@
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 FOCUSABLE_HOST_TAGS = /* @__PURE__ */ new Set(["button", "a", "input", "select", "textarea", "summary"]);
838
+ function Tooltip({ tip, children, className, side = "top", as = "span", focusable, variant = "default", ref, ...rest }) {
839
+ const [triggerRef, setTriggerRef] = useMergedRef(ref);
840
+ const [open, setOpen] = useState(false);
841
+ const id = useId();
842
+ const graceTimer = useRef(0);
843
+ const only = isValidElement(children) ? children : null;
844
+ const staticInner = only !== null && typeof only.type === "string" && FOCUSABLE_HOST_TAGS.has(only.type);
845
+ const [measuredInner, setMeasuredInner] = useState(false);
846
+ useIsoLayoutEffect(() => {
847
+ const el = triggerRef.current;
848
+ const found = only !== null && !!el && el.querySelector(FOCUSABLE) !== null;
849
+ setMeasuredInner(found);
850
+ });
851
+ const inner = only !== null && (staticInner || measuredInner);
852
+ useEffect(() => {
853
+ const el = triggerRef.current;
854
+ if (!el) return;
855
+ const touch = touchTracker();
856
+ const show = () => {
857
+ window.clearTimeout(graceTimer.current);
858
+ setOpen(true);
859
+ };
860
+ const hide = () => {
861
+ window.clearTimeout(graceTimer.current);
862
+ setOpen(false);
863
+ };
864
+ const hideSoon = () => {
865
+ window.clearTimeout(graceTimer.current);
866
+ graceTimer.current = window.setTimeout(() => setOpen(false), TIP_GRACE_MS);
867
+ };
868
+ const onMouseEnter = () => {
869
+ if (!touch.recent()) show();
870
+ };
871
+ const onFocusIn = (e) => {
872
+ if (e.target === el || e.target instanceof Element && keyboardFocused(e.target)) show();
873
+ };
874
+ const onPointerDown = (e) => {
875
+ if (e.target !== el) hide();
876
+ };
877
+ el.addEventListener("pointerenter", touch.onPointer);
878
+ el.addEventListener("pointerdown", touch.onPointer);
879
+ el.addEventListener("pointerdown", onPointerDown);
880
+ el.addEventListener("mouseenter", onMouseEnter);
881
+ el.addEventListener("mouseleave", hideSoon);
882
+ el.addEventListener("focusin", onFocusIn);
883
+ el.addEventListener("focusout", hide);
884
+ return () => {
885
+ window.clearTimeout(graceTimer.current);
886
+ el.removeEventListener("pointerenter", touch.onPointer);
887
+ el.removeEventListener("pointerdown", touch.onPointer);
888
+ el.removeEventListener("pointerdown", onPointerDown);
889
+ el.removeEventListener("mouseenter", onMouseEnter);
890
+ el.removeEventListener("mouseleave", hideSoon);
891
+ el.removeEventListener("focusin", onFocusIn);
892
+ el.removeEventListener("focusout", hide);
893
+ };
894
+ }, [triggerRef]);
895
+ useEffect(() => {
896
+ if (!open) return;
897
+ const onKey = (e) => {
898
+ if (e.key !== "Escape") return;
899
+ e.stopPropagation();
900
+ setOpen(false);
901
+ };
902
+ document.addEventListener("keydown", onKey, true);
903
+ return () => document.removeEventListener("keydown", onKey, true);
904
+ }, [open]);
905
+ const onTipPointer = useCallback((over) => {
906
+ window.clearTimeout(graceTimer.current);
907
+ if (!over) graceTimer.current = window.setTimeout(() => setOpen(false), TIP_GRACE_MS);
908
+ }, []);
909
+ const wrapperFocusable = focusable ?? !inner;
910
+ const a11y = {
911
+ tabIndex: wrapperFocusable ? 0 : void 0,
912
+ "aria-describedby": open && !inner ? id : void 0,
913
+ "data-state": open ? "open" : "closed"
914
+ };
915
+ const hint = variant === "hint" && "cursor-help underline decoration-th-hi3 decoration-dotted underline-offset-2";
916
+ let content = children;
917
+ if (inner && only) {
918
+ const existing = only.props["aria-describedby"];
919
+ const describedby = open ? [typeof existing === "string" ? existing : "", id].filter(Boolean).join(" ") : existing;
920
+ content = cloneElement(only, {
921
+ "aria-describedby": describedby || void 0
922
+ });
923
+ }
924
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
925
+ as === "tr" ? /* @__PURE__ */ jsx("tr", { ...rest, ref: setTriggerRef, className: cn(hint, className), ...a11y, children: content }) : /* @__PURE__ */ jsx(
926
+ "span",
927
+ {
928
+ ...rest,
929
+ ref: setTriggerRef,
930
+ className: cn(hint ? "inline" : "inline-flex items-center", hint, className),
931
+ ...a11y,
932
+ children: content
933
+ }
934
+ ),
935
+ open ? /* @__PURE__ */ jsx(
936
+ FloatingTip,
937
+ {
938
+ anchor: triggerRef,
939
+ id,
940
+ tip,
941
+ side,
942
+ onPointerEnter: () => onTipPointer(true),
943
+ onPointerLeave: () => onTipPointer(false)
944
+ }
945
+ ) : null
946
+ ] });
947
+ }
948
+ function FloatingTip({
949
+ anchor,
950
+ id,
951
+ tip,
952
+ side = "top",
953
+ onPointerEnter,
954
+ onPointerLeave
955
+ }) {
956
+ const config = useNishConfig();
957
+ const tipRef = useRef(null);
958
+ const pos = useAnchoredPosition(anchor, tipRef, true, side, "center");
959
+ return createPortal(
960
+ /* @__PURE__ */ jsx(
961
+ "span",
962
+ {
963
+ ref: tipRef,
964
+ id,
965
+ role: "tooltip",
966
+ "data-side": pos?.side,
967
+ "data-state": "open",
968
+ onPointerEnter,
969
+ onPointerLeave,
970
+ style: floatingStyle(pos, {
971
+ maxWidth: Math.min(TIP_MAX_WIDTH, (window.innerWidth || TIP_MAX_WIDTH) - 2 * EDGE),
972
+ zIndex: "var(--nish-z-tooltip, 100)"
973
+ }),
974
+ 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",
975
+ children: tip
976
+ }
977
+ ),
978
+ layerHost(anchor.current, config.portalContainer)
979
+ );
980
+ }
981
+ function useDismiss(open, panel, anchor, close) {
982
+ const closeRef = useLatest(close);
983
+ useEffect(() => {
984
+ if (!open) return;
985
+ const { layerStack } = ov();
986
+ const self = {};
987
+ layerStack.push(self);
988
+ const onKey = (e) => {
989
+ if (e.key === "Escape" && isTop(self)) {
990
+ e.stopPropagation();
991
+ closeRef.current("escape");
992
+ }
993
+ };
994
+ const onDown = (e) => {
995
+ const t = e.target;
996
+ if (!t || panel.current?.contains(t) || anchor.current?.contains(t)) return;
997
+ if (!isTop(self)) return;
998
+ closeRef.current("outside");
999
+ };
1000
+ document.addEventListener("keydown", onKey);
1001
+ document.addEventListener("pointerdown", onDown, true);
1002
+ return () => {
1003
+ const at = layerStack.indexOf(self);
1004
+ if (at >= 0) layerStack.splice(at, 1);
1005
+ document.removeEventListener("keydown", onKey);
1006
+ document.removeEventListener("pointerdown", onDown, true);
1007
+ };
1008
+ }, [open, panel, anchor, closeRef]);
1009
+ }
1010
+ function useReturnFocus(open, panel, anchor) {
1011
+ const wasOpen = useRef(false);
1012
+ const focusWasInside = useRef(false);
1013
+ useEffect(() => {
1014
+ if (!open) return;
1015
+ const p = panel.current;
1016
+ const track = () => {
1017
+ focusWasInside.current = !!p && p.contains(document.activeElement);
1018
+ };
1019
+ p?.addEventListener("focusin", track);
1020
+ p?.addEventListener("focusout", track);
1021
+ return () => {
1022
+ p?.removeEventListener("focusin", track);
1023
+ p?.removeEventListener("focusout", track);
1024
+ };
1025
+ }, [open, panel]);
1026
+ useIsoLayoutEffect(() => {
1027
+ if (open) {
1028
+ wasOpen.current = true;
1029
+ return;
1030
+ }
1031
+ if (!wasOpen.current) return;
1032
+ wasOpen.current = false;
1033
+ const active = document.activeElement;
1034
+ if (focusWasInside.current || !active || active === document.body) anchor.current?.focus();
1035
+ focusWasInside.current = false;
1036
+ }, [open, anchor]);
1037
+ }
1038
+ function Popover({
1039
+ open,
1040
+ onOpenChange,
1041
+ dismissOnOutside = true,
1042
+ anchor,
1043
+ side = "bottom",
1044
+ align = "start",
1045
+ width,
1046
+ "aria-label": ariaLabel,
1047
+ className,
1048
+ children,
1049
+ ref,
1050
+ style,
1051
+ onKeyDown: userKeyDown,
1052
+ ...rest
1053
+ }) {
1054
+ const config = useNishConfig();
1055
+ const isClient = useIsClient();
1056
+ const [panelRef, setPanelRef] = useMergedRef(ref);
1057
+ const pos = useAnchoredPosition(anchor, panelRef, open, side, align);
1058
+ const onOpenChangeRef = useLatest(onOpenChange);
1059
+ const outsideRef = useLatest(dismissOnOutside);
1060
+ useDismiss(open, panelRef, anchor, (reason) => {
1061
+ if (reason === "outside" && !outsideRef.current) return;
1062
+ onOpenChangeRef.current(false, reason);
1063
+ });
1064
+ useReturnFocus(open, panelRef, anchor);
1065
+ useEffect(() => {
1066
+ if (!open) return;
1067
+ const t = window.setTimeout(() => {
1068
+ const p = panelRef.current;
1069
+ if (p && !p.contains(document.activeElement)) (p.querySelector("[data-autofocus],[autofocus]") ?? p).focus();
1070
+ }, 0);
1071
+ return () => window.clearTimeout(t);
1072
+ }, [open, panelRef]);
1073
+ if (!open || !isClient) return null;
1074
+ function onKeyDown(e) {
1075
+ userKeyDown?.(e);
1076
+ if (e.key !== "Tab" || !panelRef.current) return;
1077
+ const nodes = focusables(panelRef.current);
1078
+ const active = document.activeElement;
1079
+ const atStart = nodes.length === 0 || active === nodes[0] || active === panelRef.current;
1080
+ const atEnd = nodes.length === 0 || active === nodes[nodes.length - 1];
1081
+ if (e.shiftKey && atStart || !e.shiftKey && atEnd) {
1082
+ e.preventDefault();
1083
+ onOpenChangeRef.current(false, "tab");
1084
+ }
1085
+ }
1086
+ return createPortal(
1087
+ /* @__PURE__ */ jsx(
1088
+ "div",
1089
+ {
1090
+ ...rest,
1091
+ ref: setPanelRef,
1092
+ role: "dialog",
1093
+ "aria-label": ariaLabel,
1094
+ tabIndex: -1,
1095
+ "data-side": pos?.side,
1096
+ "data-state": "open",
1097
+ onKeyDown,
1098
+ style: floatingStyle(pos, {
1099
+ ...style,
1100
+ width,
1101
+ maxHeight: pos?.maxHeight,
1102
+ zIndex: "var(--nish-z-dropdown, 40)"
1103
+ }),
1104
+ className: cn(
1105
+ "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",
1106
+ "animate-[nish-fade-in_120ms_ease-out]",
1107
+ className
1108
+ ),
1109
+ children
1110
+ }
1111
+ ),
1112
+ layerHost(anchor.current, config.portalContainer)
1113
+ );
1114
+ }
1115
+ var MenuContext = /* @__PURE__ */ createContext(null);
1116
+ var ITEM_SELECTOR = '[role="menuitem"]:not([aria-disabled="true"])';
1117
+ function menuItems(menu) {
1118
+ return menu ? Array.from(menu.querySelectorAll(ITEM_SELECTOR)) : [];
1119
+ }
1120
+ function usePhoneViewport(enabled) {
1121
+ const phone = useMediaQuery(PHONE_QUERY);
1122
+ return enabled && phone;
1123
+ }
1124
+ function Cloned({ element, ...props }) {
1125
+ return cloneElement(element, props);
1126
+ }
1127
+ function DropdownMenu({
1128
+ trigger,
1129
+ align = "start",
1130
+ side = "bottom",
1131
+ children,
1132
+ open: openProp,
1133
+ onOpenChange,
1134
+ defaultOpen = false,
1135
+ "aria-label": ariaLabel,
1136
+ sheetBelow = false,
1137
+ sheetTitle,
1138
+ className,
1139
+ ref,
1140
+ style,
1141
+ onKeyDown: userKeyDown,
1142
+ ...rest
1143
+ }) {
1144
+ const strings = useNishStrings();
1145
+ const config = useNishConfig();
1146
+ const [openState, setOpenState] = useState(defaultOpen);
1147
+ const open = openProp ?? openState;
1148
+ const triggerRef = useRef(null);
1149
+ const [triggerNode, setTriggerNode] = useState(null);
1150
+ const triggerPropRef = trigger.props.ref;
1151
+ const attachTrigger = useCallback(
1152
+ (node) => {
1153
+ triggerRef.current = node;
1154
+ setTriggerNode(node);
1155
+ setRef(triggerPropRef, node);
1156
+ },
1157
+ [triggerPropRef]
1158
+ );
1159
+ const shown = open && triggerNode != null;
1160
+ const [menuRef, setMenuRef] = useMergedRef(ref);
1161
+ const menuId = useId();
1162
+ const triggerId = useId();
1163
+ const initial = useRef("first");
1164
+ const asSheet = usePhoneViewport(sheetBelow === "sm");
1165
+ const pos = useAnchoredPosition(triggerRef, menuRef, shown && !asSheet, side, align, 4);
1166
+ const onOpenChangeRef = useLatest(onOpenChange);
1167
+ const setOpen = useCallback(
1168
+ (next, reason) => {
1169
+ if (openProp === void 0) setOpenState(next);
1170
+ onOpenChangeRef.current?.(next, reason);
1171
+ },
1172
+ [onOpenChangeRef, openProp]
1173
+ );
1174
+ const close = useCallback(() => setOpen(false), [setOpen]);
1175
+ const dismiss = useCallback((reason) => setOpen(false, reason), [setOpen]);
1176
+ useDismiss(shown && !asSheet, menuRef, triggerRef, dismiss);
1177
+ useReturnFocus(shown, menuRef, triggerRef);
1178
+ useEffect(() => {
1179
+ if (!shown) return;
1180
+ const menu = menuRef.current;
1181
+ if (!menu) return;
1182
+ const focusInitial = () => {
1183
+ const items = menuItems(menu);
1184
+ const target = initial.current === "last" ? items[items.length - 1] : initial.current === "menu" ? menu : items[0];
1185
+ (target ?? menu).focus();
1186
+ };
1187
+ const t = window.setTimeout(focusInitial, 0);
1188
+ const mo = new MutationObserver(() => {
1189
+ if (!menu.contains(document.activeElement) && (document.activeElement === document.body || !document.activeElement)) {
1190
+ initial.current = "first";
1191
+ focusInitial();
1192
+ }
1193
+ });
1194
+ mo.observe(menu, { childList: true, subtree: true });
1195
+ return () => {
1196
+ window.clearTimeout(t);
1197
+ mo.disconnect();
1198
+ };
1199
+ }, [shown, asSheet, menuRef]);
1200
+ function openWith(where) {
1201
+ initial.current = where;
1202
+ setOpen(true);
1203
+ }
1204
+ function onMenuKeyDown(e) {
1205
+ userKeyDown?.(e);
1206
+ const items = menuItems(menuRef.current);
1207
+ const i = items.indexOf(document.activeElement);
1208
+ const go = (n) => {
1209
+ e.preventDefault();
1210
+ items[(n + items.length) % items.length]?.focus();
1211
+ };
1212
+ switch (e.key) {
1213
+ case "ArrowDown":
1214
+ return go(i + 1);
1215
+ case "ArrowUp":
1216
+ return go(i < 0 ? items.length - 1 : i - 1);
1217
+ case "Home":
1218
+ return go(0);
1219
+ case "End":
1220
+ return go(items.length - 1);
1221
+ case "Tab":
1222
+ e.preventDefault();
1223
+ dismiss("tab");
1224
+ return;
1225
+ default:
1226
+ if (e.key.length === 1 && /\S/.test(e.key) && !e.ctrlKey && !e.metaKey && !e.altKey) {
1227
+ const k = e.key.toLowerCase();
1228
+ const order = [...items.slice(i + 1), ...items.slice(0, i + 1)];
1229
+ const hit = order.find((el) => (el.textContent ?? "").trim().toLowerCase().startsWith(k));
1230
+ if (hit) {
1231
+ e.preventDefault();
1232
+ hit.focus();
1233
+ }
1234
+ }
1235
+ }
1236
+ }
1237
+ const tp = trigger.props;
1238
+ const triggerEl = /* @__PURE__ */ jsx(
1239
+ Cloned,
1240
+ {
1241
+ element: trigger,
1242
+ ref: attachTrigger,
1243
+ id: tp.id ?? triggerId,
1244
+ "aria-haspopup": "menu",
1245
+ "aria-expanded": open,
1246
+ "aria-controls": open ? menuId : void 0,
1247
+ onClick: (e) => {
1248
+ tp.onClick?.(e);
1249
+ if (e.defaultPrevented) return;
1250
+ if (open) close();
1251
+ else openWith(e.detail === 0 ? "first" : "menu");
1252
+ },
1253
+ onKeyDown: (e) => {
1254
+ tp.onKeyDown?.(e);
1255
+ if (e.defaultPrevented) return;
1256
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
1257
+ e.preventDefault();
1258
+ openWith(e.key === "ArrowDown" ? "first" : "last");
1259
+ }
1260
+ }
1261
+ }
1262
+ );
1263
+ const menuProps = {
1264
+ ...rest,
1265
+ ref: setMenuRef,
1266
+ id: menuId,
1267
+ role: "menu",
1268
+ "aria-label": ariaLabel,
1269
+ "aria-labelledby": ariaLabel ? void 0 : tp.id ?? triggerId,
1270
+ "aria-orientation": "vertical",
1271
+ "data-state": "open",
1272
+ tabIndex: -1,
1273
+ onKeyDown: onMenuKeyDown
1274
+ };
1275
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1276
+ triggerEl,
1277
+ 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(
1278
+ "div",
1279
+ {
1280
+ ...menuProps,
1281
+ style,
1282
+ className: cn(
1283
+ "-mx-5 -my-4 py-2 outline-none [&_[role=menuitem]]:min-h-12 [&_[role=menuitem]]:px-5 [&_[role=menuitem]]:text-sm",
1284
+ className
1285
+ ),
1286
+ children
1287
+ }
1288
+ ) }) }) : shown ? createPortal(
1289
+ /* @__PURE__ */ jsx(MenuContext.Provider, { value: { close }, children: /* @__PURE__ */ jsx(
1290
+ "div",
1291
+ {
1292
+ ...menuProps,
1293
+ "data-side": pos?.side,
1294
+ style: floatingStyle(pos, {
1295
+ ...style,
1296
+ maxHeight: pos ? Math.min(pos.maxHeight, 384) : void 0,
1297
+ zIndex: "var(--nish-z-dropdown, 40)"
1298
+ }),
1299
+ className: cn(
1300
+ "max-h-[min(24rem,calc(100dvh-1rem))] min-w-44 max-w-[calc(100vw-1rem)] overflow-y-auto overscroll-contain",
1301
+ "rounded-lg border border-th-line bg-th-elevated py-1 shadow-pop outline-none animate-[nish-fade-in_120ms_ease-out]",
1302
+ className
1303
+ ),
1304
+ children
1305
+ }
1306
+ ) }),
1307
+ layerHost(triggerNode, config.portalContainer)
1308
+ ) : null
1309
+ ] });
1310
+ }
1311
+ function MenuItem({
1312
+ icon,
1313
+ trailing,
1314
+ danger = false,
1315
+ active = false,
1316
+ disabled = false,
1317
+ onSelect,
1318
+ className,
1319
+ children,
1320
+ ref,
1321
+ onClick,
1322
+ onMouseMove,
1323
+ ...rest
1324
+ }) {
1325
+ const ctx = useContext(MenuContext);
1326
+ function select() {
1327
+ if (disabled) return;
1328
+ const ev = new Event("nish-menu-select", { cancelable: true });
1329
+ onSelect?.(ev);
1330
+ if (!ev.defaultPrevented) ctx?.close();
1331
+ }
1332
+ return /* @__PURE__ */ jsxs(
1333
+ "button",
1334
+ {
1335
+ ...rest,
1336
+ ref,
1337
+ type: "button",
1338
+ role: "menuitem",
1339
+ tabIndex: -1,
1340
+ "aria-disabled": disabled || void 0,
1341
+ "aria-current": active || void 0,
1342
+ "data-disabled": disabled ? "" : void 0,
1343
+ onClick: (e) => {
1344
+ onClick?.(e);
1345
+ if (!e.defaultPrevented) select();
1346
+ },
1347
+ onMouseMove: (e) => {
1348
+ onMouseMove?.(e);
1349
+ if (!disabled && document.activeElement !== e.currentTarget) e.currentTarget.focus();
1350
+ },
1351
+ className: menuItemClass({
1352
+ active,
1353
+ danger,
1354
+ disabled,
1355
+ className: cn(!disabled && (danger ? "focus:bg-th-loss-soft" : "focus:bg-th-overlay focus:text-th-hi"), className)
1356
+ }),
1357
+ children: [
1358
+ icon ? /* @__PURE__ */ jsx("span", { className: cn("inline-flex shrink-0", danger || disabled ? "" : "text-th-hi3"), children: icon }) : null,
1359
+ /* @__PURE__ */ jsx("span", { className: "min-w-0 flex-1 truncate", children }),
1360
+ trailing ? /* @__PURE__ */ jsx("span", { className: cn("ms-auto inline-flex shrink-0 items-center", disabled ? "" : "text-th-hi2"), children: trailing }) : null
1361
+ ]
1362
+ }
1363
+ );
1364
+ }
1365
+ function MenuLabel({
1366
+ children,
1367
+ className,
1368
+ ref,
1369
+ ...rest
1370
+ }) {
1371
+ return /* @__PURE__ */ jsx(
1372
+ "div",
1373
+ {
1374
+ ...rest,
1375
+ ref,
1376
+ role: "presentation",
1377
+ className: cn("px-3 pb-1 pt-2 text-2xs font-semibold uppercase tracking-wider text-th-hi2", className),
1378
+ children
1379
+ }
1380
+ );
1381
+ }
1382
+ function MenuSeparator({
1383
+ className,
1384
+ ref,
1385
+ ...rest
1386
+ }) {
1387
+ return /* @__PURE__ */ jsx("div", { ...rest, ref, role: "separator", className: cn("my-1 h-px bg-th-line forced-colors:bg-[CanvasText]", className) });
1388
+ }
1389
+ function MenuSubHeader({ onBack, children, className, ref, onKeyDown, ...rest }) {
1390
+ const strings = useNishStrings();
1391
+ return /* @__PURE__ */ jsxs(
1392
+ "button",
1393
+ {
1394
+ ...rest,
1395
+ ref,
1396
+ type: "button",
1397
+ role: "menuitem",
1398
+ tabIndex: -1,
1399
+ onClick: onBack,
1400
+ onKeyDown: (e) => {
1401
+ onKeyDown?.(e);
1402
+ if (e.key === (isRtl(e.currentTarget) ? "ArrowRight" : "ArrowLeft")) {
1403
+ e.preventDefault();
1404
+ onBack();
1405
+ }
1406
+ },
1407
+ 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),
1408
+ children: [
1409
+ /* @__PURE__ */ jsx(
1410
+ "svg",
1411
+ {
1412
+ width: 14,
1413
+ height: 14,
1414
+ viewBox: "0 0 24 24",
1415
+ fill: "none",
1416
+ stroke: "currentColor",
1417
+ strokeWidth: 2,
1418
+ strokeLinecap: "round",
1419
+ strokeLinejoin: "round",
1420
+ "aria-hidden": "true",
1421
+ className: "shrink-0 text-th-hi3 rtl:-scale-x-100",
1422
+ children: /* @__PURE__ */ jsx("path", { d: "m15 18-6-6 6-6" })
1423
+ }
1424
+ ),
1425
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: strings.back }),
1426
+ /* @__PURE__ */ jsx("span", { className: "min-w-0 flex-1 truncate", children })
1427
+ ]
1428
+ }
1429
+ );
1430
+ }
1431
+ export {
1432
+ ConfirmDialog,
1433
+ Drawer,
1434
+ DropdownMenu,
1435
+ FloatingTip,
1436
+ MenuItem,
1437
+ MenuLabel,
1438
+ MenuSeparator,
1439
+ MenuSubHeader,
1440
+ Modal,
1441
+ Popover,
1442
+ Tooltip,
1443
+ keyboardFocused,
1444
+ menuItemClass,
1445
+ touchTracker,
1446
+ whenHistorySettled
1447
+ };