nish-ui 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -8
- package/dist/chart.js +1 -1
- package/dist/elements-all.js +13 -13
- package/dist/elements-charts.js +1 -1
- package/dist/elements-data.js +1 -1
- package/dist/elements-forms.js +1 -1
- package/dist/elements.js +3 -3
- package/dist/esm/components/app-shell.js +145 -45
- package/dist/esm/components/breadcrumbs.js +1 -1
- package/dist/esm/components/calendar-classes.js +325 -0
- package/dist/esm/components/calendar.js +263 -0
- package/dist/esm/components/canvas/route.js +56 -20
- package/dist/esm/components/carousel-classes.js +27 -0
- package/dist/esm/components/carousel.js +317 -0
- package/dist/esm/components/command.js +2 -27
- package/dist/esm/components/context-menu.js +199 -0
- package/dist/esm/components/date-picker.js +208 -0
- package/dist/esm/components/display-classes.js +10 -13
- package/dist/esm/components/display.js +22 -19
- package/dist/esm/components/drawer.js +139 -0
- package/dist/esm/components/filters.js +1 -1
- package/dist/esm/components/flow.js +1 -1
- package/dist/esm/components/hover-card.js +221 -0
- package/dist/esm/components/icons.js +17 -0
- package/dist/esm/components/inbox.js +2 -1
- package/dist/esm/components/lazy-module.js +53 -0
- package/dist/esm/components/menu.js +344 -0
- package/dist/esm/components/modal.js +173 -0
- package/dist/esm/components/more-sheet.js +52 -0
- package/dist/esm/components/multi-select.js +420 -0
- package/dist/esm/components/nav-items.js +138 -0
- package/dist/esm/components/nav.js +13 -174
- package/dist/esm/components/navigate.js +33 -0
- package/dist/esm/components/overlay-core.js +221 -0
- package/dist/esm/components/overlay-dialog.js +255 -0
- package/dist/esm/components/overlay-position.js +163 -0
- package/dist/esm/components/overlay.js +7 -1441
- package/dist/esm/components/popover.js +92 -0
- package/dist/esm/components/segmented.js +2 -1
- package/dist/esm/components/split.js +1 -1
- package/dist/esm/components/strings.js +25 -0
- package/dist/esm/components/tooltip.js +177 -0
- package/dist/esm/components/tree-view-classes.js +57 -0
- package/dist/esm/components/tree-view.js +319 -0
- package/dist/esm/components/virtual-table.js +245 -0
- package/dist/esm/components/virtual.js +291 -0
- package/dist/esm/inbox.js +12 -0
- package/dist/esm/index.js +61 -2
- package/dist/esm/runtime.js +1 -1
- package/dist/esm/service-tokens.js +6 -0
- package/dist/esm/theme.js +2 -2
- package/dist/esm/virtual-range.js +73 -0
- package/dist/format.js +1 -1
- package/dist/inbox.d.ts +238 -0
- package/dist/index.d.ts +1030 -777
- package/dist/nish-components.css +2 -0
- package/dist/nish-components.min.css +1 -1
- package/dist/service-tokens.d.ts +60 -0
- package/dist/theme.d.ts +50 -0
- package/dist/theme.global.js +28 -3
- package/dist/theme.js +28 -3
- package/package.json +17 -1
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/overlay-dialog.tsx
|
|
4
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
|
+
import { cn } from "../cn.js";
|
|
6
|
+
import { useLatest } from "./latest.js";
|
|
7
|
+
import { useNishStrings } from "./provider.js";
|
|
8
|
+
import { SHEET_EXIT_MS } from "./overlay-classes.js";
|
|
9
|
+
import { ov, isTop, syncLayerAttr, pushInert, popInert, acquireGuard, releaseGuard, prefersReducedMotion, closeVetoes, focusables, lockScroll, queueHistoryPush, cancelHistoryPush } from "./overlay-core.js";
|
|
10
|
+
import { jsx } from "react/jsx-runtime";
|
|
11
|
+
function useDialog(panelRef, onClose, closeOnBackProp = false) {
|
|
12
|
+
const closeOnBack = closeOnBackProp && (typeof window === "undefined" || window.self === window.top);
|
|
13
|
+
const onCloseRef = useLatest(onClose);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
const s = ov();
|
|
16
|
+
const opener = document.activeElement;
|
|
17
|
+
const panel = panelRef.current;
|
|
18
|
+
const self = {};
|
|
19
|
+
s.layerStack.push(self);
|
|
20
|
+
s.dialogCount++;
|
|
21
|
+
syncLayerAttr();
|
|
22
|
+
const scrim = panel?.closest("[data-nish-layer]") ?? null;
|
|
23
|
+
if (scrim) pushInert(scrim);
|
|
24
|
+
const layerId = `nish-layer-${++s.layerSeq}`;
|
|
25
|
+
let pushed = false;
|
|
26
|
+
let rePushTimer = 0;
|
|
27
|
+
const pushEntry = () => {
|
|
28
|
+
const prev = history.state && typeof history.state === "object" ? history.state : {};
|
|
29
|
+
history.pushState({ ...prev, nishLayer: layerId }, "");
|
|
30
|
+
pushed = true;
|
|
31
|
+
};
|
|
32
|
+
const queuedPush = () => queueHistoryPush(pushEntry);
|
|
33
|
+
if (closeOnBack) acquireGuard();
|
|
34
|
+
const pushTimer = closeOnBack ? window.setTimeout(queuedPush, 0) : 0;
|
|
35
|
+
function onPop() {
|
|
36
|
+
if (!pushed) return;
|
|
37
|
+
if (history.state?.nishLayer === layerId) return;
|
|
38
|
+
pushed = false;
|
|
39
|
+
if (!isTop(self)) {
|
|
40
|
+
pushEntry();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
onCloseRef.current();
|
|
44
|
+
window.clearTimeout(rePushTimer);
|
|
45
|
+
rePushTimer = window.setTimeout(() => {
|
|
46
|
+
if (!pushed) queuedPush();
|
|
47
|
+
}, SHEET_EXIT_MS + 60);
|
|
48
|
+
}
|
|
49
|
+
if (closeOnBack) window.addEventListener("popstate", onPop);
|
|
50
|
+
const focusTimer = window.setTimeout(() => {
|
|
51
|
+
if (panel && !panel.contains(document.activeElement)) {
|
|
52
|
+
(panel.querySelector("[data-autofocus],[autofocus]") ?? panel).focus();
|
|
53
|
+
}
|
|
54
|
+
}, 0);
|
|
55
|
+
const unlock = lockScroll();
|
|
56
|
+
function onKey(e) {
|
|
57
|
+
if (!isTop(self)) return;
|
|
58
|
+
if (e.key === "Escape") {
|
|
59
|
+
e.stopPropagation();
|
|
60
|
+
onCloseRef.current();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (e.key !== "Tab" || !panel) return;
|
|
64
|
+
const nodes = focusables(panel);
|
|
65
|
+
if (nodes.length === 0) {
|
|
66
|
+
e.preventDefault();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const first = nodes[0];
|
|
70
|
+
const last = nodes[nodes.length - 1];
|
|
71
|
+
const active = document.activeElement;
|
|
72
|
+
if (!panel.contains(active)) {
|
|
73
|
+
e.preventDefault();
|
|
74
|
+
(e.shiftKey ? last : first).focus();
|
|
75
|
+
} else if (e.shiftKey && (active === first || active === panel)) {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
last.focus();
|
|
78
|
+
} else if (!e.shiftKey && active === last) {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
first.focus();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
document.addEventListener("keydown", onKey);
|
|
84
|
+
return () => {
|
|
85
|
+
window.clearTimeout(focusTimer);
|
|
86
|
+
window.clearTimeout(pushTimer);
|
|
87
|
+
window.clearTimeout(rePushTimer);
|
|
88
|
+
cancelHistoryPush(pushEntry);
|
|
89
|
+
window.removeEventListener("popstate", onPop);
|
|
90
|
+
const at = s.layerStack.indexOf(self);
|
|
91
|
+
if (at >= 0) s.layerStack.splice(at, 1);
|
|
92
|
+
s.dialogCount--;
|
|
93
|
+
syncLayerAttr();
|
|
94
|
+
if (scrim) popInert(scrim);
|
|
95
|
+
document.removeEventListener("keydown", onKey);
|
|
96
|
+
unlock();
|
|
97
|
+
opener?.focus?.();
|
|
98
|
+
if (pushed && history.state?.nishLayer === layerId) {
|
|
99
|
+
s.pendingBacks++;
|
|
100
|
+
history.back();
|
|
101
|
+
}
|
|
102
|
+
if (closeOnBack) releaseGuard();
|
|
103
|
+
};
|
|
104
|
+
}, [panelRef, closeOnBack, onCloseRef]);
|
|
105
|
+
}
|
|
106
|
+
function useSheetExit(onClose, canClose, onOpenChange) {
|
|
107
|
+
const [closing, setClosing] = useState(false);
|
|
108
|
+
const closingRef = useRef(false);
|
|
109
|
+
const timer = useRef(0);
|
|
110
|
+
const onCloseRef = useLatest(onClose);
|
|
111
|
+
const openChangeRef = useLatest(onOpenChange);
|
|
112
|
+
const canCloseRef = useLatest(canClose);
|
|
113
|
+
useEffect(() => () => window.clearTimeout(timer.current), []);
|
|
114
|
+
const fire = useCallback(() => {
|
|
115
|
+
onCloseRef.current?.();
|
|
116
|
+
openChangeRef.current?.(false);
|
|
117
|
+
}, [onCloseRef, openChangeRef]);
|
|
118
|
+
const requestClose = useCallback(() => {
|
|
119
|
+
if (closingRef.current) return;
|
|
120
|
+
const handler = onCloseRef.current;
|
|
121
|
+
if (handler && closeVetoes.get(handler)?.()) return;
|
|
122
|
+
if (canCloseRef.current && !canCloseRef.current()) return;
|
|
123
|
+
if (prefersReducedMotion()) {
|
|
124
|
+
fire();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
closingRef.current = true;
|
|
128
|
+
setClosing(true);
|
|
129
|
+
timer.current = window.setTimeout(() => {
|
|
130
|
+
fire();
|
|
131
|
+
closingRef.current = false;
|
|
132
|
+
setClosing(false);
|
|
133
|
+
}, SHEET_EXIT_MS);
|
|
134
|
+
}, [canCloseRef, fire, onCloseRef]);
|
|
135
|
+
const closeNow = useCallback(() => {
|
|
136
|
+
fire();
|
|
137
|
+
}, [fire]);
|
|
138
|
+
const isClosing = useCallback(() => closingRef.current, []);
|
|
139
|
+
const allowClose = useCallback(() => !canCloseRef.current || canCloseRef.current(), [canCloseRef]);
|
|
140
|
+
return { closing, requestClose, closeNow, isClosing, allowClose };
|
|
141
|
+
}
|
|
142
|
+
function useSheetSwipe(panelRef, scrimRef, enabled, onDismiss, allow = () => true) {
|
|
143
|
+
const drag = useRef(null);
|
|
144
|
+
const timer = useRef(0);
|
|
145
|
+
const enabledRef = useLatest(enabled);
|
|
146
|
+
const dismissRef = useLatest(onDismiss);
|
|
147
|
+
const allowRef = useLatest(allow);
|
|
148
|
+
useEffect(() => () => window.clearTimeout(timer.current), []);
|
|
149
|
+
const onPointerDown = useCallback(
|
|
150
|
+
(e) => {
|
|
151
|
+
if (drag.current || !enabledRef.current()) return;
|
|
152
|
+
if (e.pointerType === "mouse" && e.button !== 0) return;
|
|
153
|
+
const hit = e.target.closest('button,a,input,select,textarea,[role="button"]');
|
|
154
|
+
if (hit && hit !== e.currentTarget) return;
|
|
155
|
+
const panel = panelRef.current;
|
|
156
|
+
if (!panel) return;
|
|
157
|
+
window.clearTimeout(timer.current);
|
|
158
|
+
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
159
|
+
panel.style.transition = "none";
|
|
160
|
+
if (scrimRef.current) scrimRef.current.style.transition = "none";
|
|
161
|
+
drag.current = {
|
|
162
|
+
startY: e.clientY,
|
|
163
|
+
dy: 0,
|
|
164
|
+
lastY: e.clientY,
|
|
165
|
+
lastT: e.timeStamp,
|
|
166
|
+
v: 0
|
|
167
|
+
};
|
|
168
|
+
},
|
|
169
|
+
[enabledRef, panelRef, scrimRef]
|
|
170
|
+
);
|
|
171
|
+
const onPointerMove = useCallback(
|
|
172
|
+
(e) => {
|
|
173
|
+
const d = drag.current;
|
|
174
|
+
const panel = panelRef.current;
|
|
175
|
+
if (!d || !panel) return;
|
|
176
|
+
d.dy = e.clientY - d.startY;
|
|
177
|
+
const dt = e.timeStamp - d.lastT;
|
|
178
|
+
if (dt > 0) d.v = (e.clientY - d.lastY) / dt;
|
|
179
|
+
d.lastY = e.clientY;
|
|
180
|
+
d.lastT = e.timeStamp;
|
|
181
|
+
panel.style.transform = `translateY(${d.dy < 0 ? d.dy * 0.3 : d.dy}px)`;
|
|
182
|
+
if (scrimRef.current) {
|
|
183
|
+
scrimRef.current.style.opacity = String(Math.max(0, 1 - Math.max(0, d.dy) / Math.max(1, panel.offsetHeight)));
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
[panelRef, scrimRef]
|
|
187
|
+
);
|
|
188
|
+
const end = useCallback(
|
|
189
|
+
(e, cancelled) => {
|
|
190
|
+
const d = drag.current;
|
|
191
|
+
const panel = panelRef.current;
|
|
192
|
+
drag.current = null;
|
|
193
|
+
if (!d || !panel) return;
|
|
194
|
+
if (e.currentTarget.hasPointerCapture?.(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);
|
|
195
|
+
const scrim = scrimRef.current;
|
|
196
|
+
const velocity = e.timeStamp - d.lastT > 100 ? 0 : d.v;
|
|
197
|
+
const dismiss = !cancelled && (d.dy > panel.offsetHeight * 0.3 || velocity > 0.5) && allowRef.current();
|
|
198
|
+
const ms = SHEET_EXIT_MS - 20;
|
|
199
|
+
panel.style.transition = `transform ${ms}ms ease-out`;
|
|
200
|
+
if (scrim) scrim.style.transition = `opacity ${ms}ms ease-out`;
|
|
201
|
+
if (dismiss) {
|
|
202
|
+
panel.style.transform = "translateY(100%)";
|
|
203
|
+
if (scrim) scrim.style.opacity = "0";
|
|
204
|
+
timer.current = window.setTimeout(() => dismissRef.current(), ms);
|
|
205
|
+
} else {
|
|
206
|
+
panel.style.transform = "translateY(0)";
|
|
207
|
+
if (scrim) scrim.style.opacity = "1";
|
|
208
|
+
timer.current = window.setTimeout(() => {
|
|
209
|
+
panel.style.transform = "";
|
|
210
|
+
panel.style.transition = "";
|
|
211
|
+
if (scrim) {
|
|
212
|
+
scrim.style.opacity = "";
|
|
213
|
+
scrim.style.transition = "";
|
|
214
|
+
}
|
|
215
|
+
}, ms);
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
[allowRef, dismissRef, panelRef, scrimRef]
|
|
219
|
+
);
|
|
220
|
+
return {
|
|
221
|
+
onPointerDown,
|
|
222
|
+
onPointerMove,
|
|
223
|
+
onPointerUp: (e) => end(e, false),
|
|
224
|
+
onPointerCancel: (e) => end(e, true)
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
function SheetHandle({ onClose, swipe, className }) {
|
|
228
|
+
const strings = useNishStrings();
|
|
229
|
+
return /* @__PURE__ */ jsx(
|
|
230
|
+
"div",
|
|
231
|
+
{
|
|
232
|
+
role: "button",
|
|
233
|
+
tabIndex: 0,
|
|
234
|
+
"aria-label": strings.close,
|
|
235
|
+
...swipe,
|
|
236
|
+
onKeyDown: (e) => {
|
|
237
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
238
|
+
e.preventDefault();
|
|
239
|
+
onClose();
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
className: cn(
|
|
243
|
+
"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",
|
|
244
|
+
className
|
|
245
|
+
),
|
|
246
|
+
children: /* @__PURE__ */ jsx("span", { className: "h-1 w-10 rounded-full bg-th-line-strong forced-colors:bg-[ButtonText]", "aria-hidden": "true" })
|
|
247
|
+
}
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
export {
|
|
251
|
+
SheetHandle,
|
|
252
|
+
useDialog,
|
|
253
|
+
useSheetExit,
|
|
254
|
+
useSheetSwipe
|
|
255
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/overlay-position.ts
|
|
4
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
|
+
import { isRtl, useIsoLayoutEffect } from "./client.js";
|
|
6
|
+
import { useLatest } from "./latest.js";
|
|
7
|
+
import { ov, isTop } from "./overlay-core.js";
|
|
8
|
+
var GAP = 8;
|
|
9
|
+
var EDGE = 8;
|
|
10
|
+
var OPPOSITE = {
|
|
11
|
+
top: "bottom",
|
|
12
|
+
bottom: "top",
|
|
13
|
+
left: "right",
|
|
14
|
+
right: "left"
|
|
15
|
+
};
|
|
16
|
+
function viewport() {
|
|
17
|
+
return {
|
|
18
|
+
vw: window.innerWidth || document.documentElement.clientWidth || 0,
|
|
19
|
+
vh: window.innerHeight || document.documentElement.clientHeight || 0
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function clamp(v, size, limit) {
|
|
23
|
+
return Math.max(EDGE, Math.min(v, limit - size - EDGE));
|
|
24
|
+
}
|
|
25
|
+
function place(r, w, h, side, align, gap, rtl = false) {
|
|
26
|
+
const { vw, vh } = viewport();
|
|
27
|
+
const room = {
|
|
28
|
+
top: r.top,
|
|
29
|
+
bottom: vh - r.bottom,
|
|
30
|
+
left: r.left,
|
|
31
|
+
right: vw - r.right
|
|
32
|
+
};
|
|
33
|
+
const need = side === "top" || side === "bottom" ? h + gap + EDGE : w + gap + EDGE;
|
|
34
|
+
const chosen = room[side] < need && room[OPPOSITE[side]] > room[side] ? OPPOSITE[side] : side;
|
|
35
|
+
if (chosen === "top" || chosen === "bottom") {
|
|
36
|
+
const top2 = chosen === "top" ? r.top - gap - h : r.bottom + gap;
|
|
37
|
+
const edge = rtl && align !== "center" ? align === "start" ? "end" : "start" : align;
|
|
38
|
+
const left2 = edge === "start" ? r.left : edge === "end" ? r.right - w : r.left + r.width / 2 - w / 2;
|
|
39
|
+
return {
|
|
40
|
+
side: chosen,
|
|
41
|
+
top: clamp(top2, h, vh),
|
|
42
|
+
left: clamp(left2, w, vw),
|
|
43
|
+
maxHeight: Math.max(0, room[chosen] - gap - EDGE)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const left = chosen === "left" ? r.left - gap - w : r.right + gap;
|
|
47
|
+
const top = align === "start" ? r.top : align === "end" ? r.bottom - h : r.top + r.height / 2 - h / 2;
|
|
48
|
+
return {
|
|
49
|
+
side: chosen,
|
|
50
|
+
top: clamp(top, h, vh),
|
|
51
|
+
left: clamp(left, w, vw),
|
|
52
|
+
maxHeight: Math.max(0, vh - 2 * EDGE)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function useAnchoredPosition(anchor, floating, open, side, align, gap = GAP) {
|
|
56
|
+
const [pos, setPos] = useState(null);
|
|
57
|
+
useIsoLayoutEffect(() => {
|
|
58
|
+
if (!open) {
|
|
59
|
+
setPos(null);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const update = () => {
|
|
63
|
+
const a = anchor.current;
|
|
64
|
+
const f = floating.current;
|
|
65
|
+
if (!a || !f) return;
|
|
66
|
+
const naturalH = f.scrollHeight + (f.offsetHeight - f.clientHeight);
|
|
67
|
+
const next = place(a.getBoundingClientRect(), f.offsetWidth, Math.max(f.offsetHeight, naturalH), side, align, gap, isRtl(a));
|
|
68
|
+
setPos(
|
|
69
|
+
(prev) => prev && prev.top === next.top && prev.left === next.left && prev.side === next.side && prev.maxHeight === next.maxHeight ? prev : next
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
update();
|
|
73
|
+
window.addEventListener("resize", update);
|
|
74
|
+
window.addEventListener("scroll", update, true);
|
|
75
|
+
const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update);
|
|
76
|
+
if (floating.current) ro?.observe(floating.current);
|
|
77
|
+
if (anchor.current) ro?.observe(anchor.current);
|
|
78
|
+
return () => {
|
|
79
|
+
window.removeEventListener("resize", update);
|
|
80
|
+
window.removeEventListener("scroll", update, true);
|
|
81
|
+
ro?.disconnect();
|
|
82
|
+
};
|
|
83
|
+
}, [anchor, floating, open, side, align, gap]);
|
|
84
|
+
return open ? pos : null;
|
|
85
|
+
}
|
|
86
|
+
function floatingStyle(pos, extra) {
|
|
87
|
+
return {
|
|
88
|
+
position: "fixed",
|
|
89
|
+
top: pos?.top ?? 0,
|
|
90
|
+
left: pos?.left ?? 0,
|
|
91
|
+
visibility: pos ? void 0 : "hidden",
|
|
92
|
+
...extra
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function useDismiss(open, panel, anchor, close) {
|
|
96
|
+
const closeRef = useLatest(close);
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (!open) return;
|
|
99
|
+
const { layerStack } = ov();
|
|
100
|
+
const self = {};
|
|
101
|
+
layerStack.push(self);
|
|
102
|
+
const onKey = (e) => {
|
|
103
|
+
if (e.key === "Escape" && isTop(self)) {
|
|
104
|
+
e.stopPropagation();
|
|
105
|
+
closeRef.current("escape");
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const onDown = (e) => {
|
|
109
|
+
const t = e.target;
|
|
110
|
+
if (!t || panel.current?.contains(t) || anchor.current?.contains(t)) return;
|
|
111
|
+
if (!isTop(self)) return;
|
|
112
|
+
closeRef.current("outside");
|
|
113
|
+
};
|
|
114
|
+
document.addEventListener("keydown", onKey);
|
|
115
|
+
document.addEventListener("pointerdown", onDown, true);
|
|
116
|
+
return () => {
|
|
117
|
+
const at = layerStack.indexOf(self);
|
|
118
|
+
if (at >= 0) layerStack.splice(at, 1);
|
|
119
|
+
document.removeEventListener("keydown", onKey);
|
|
120
|
+
document.removeEventListener("pointerdown", onDown, true);
|
|
121
|
+
};
|
|
122
|
+
}, [open, panel, anchor, closeRef]);
|
|
123
|
+
}
|
|
124
|
+
function useReturnFocus(open, panel, anchor) {
|
|
125
|
+
const wasOpen = useRef(false);
|
|
126
|
+
const focusWasInside = useRef(false);
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!open) return;
|
|
129
|
+
const p = panel.current;
|
|
130
|
+
const track = () => {
|
|
131
|
+
focusWasInside.current = !!p && p.contains(document.activeElement);
|
|
132
|
+
};
|
|
133
|
+
p?.addEventListener("focusin", track);
|
|
134
|
+
p?.addEventListener("focusout", track);
|
|
135
|
+
return () => {
|
|
136
|
+
p?.removeEventListener("focusin", track);
|
|
137
|
+
p?.removeEventListener("focusout", track);
|
|
138
|
+
};
|
|
139
|
+
}, [open, panel]);
|
|
140
|
+
useIsoLayoutEffect(() => {
|
|
141
|
+
if (open) {
|
|
142
|
+
wasOpen.current = true;
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (!wasOpen.current) return;
|
|
146
|
+
wasOpen.current = false;
|
|
147
|
+
const active = document.activeElement;
|
|
148
|
+
if (focusWasInside.current || !active || active === document.body) anchor.current?.focus();
|
|
149
|
+
focusWasInside.current = false;
|
|
150
|
+
}, [open, anchor]);
|
|
151
|
+
}
|
|
152
|
+
export {
|
|
153
|
+
EDGE,
|
|
154
|
+
GAP,
|
|
155
|
+
OPPOSITE,
|
|
156
|
+
clamp,
|
|
157
|
+
floatingStyle,
|
|
158
|
+
place,
|
|
159
|
+
useAnchoredPosition,
|
|
160
|
+
useDismiss,
|
|
161
|
+
useReturnFocus,
|
|
162
|
+
viewport
|
|
163
|
+
};
|