nish-ui 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -8
- package/dist/chart.js +1 -1
- package/dist/elements-all.js +13 -13
- package/dist/elements-charts.js +1 -1
- package/dist/elements-data.js +1 -1
- package/dist/elements-forms.js +1 -1
- package/dist/elements.js +3 -3
- package/dist/esm/components/app-shell.js +145 -45
- package/dist/esm/components/breadcrumbs.js +1 -1
- package/dist/esm/components/calendar-classes.js +325 -0
- package/dist/esm/components/calendar.js +263 -0
- package/dist/esm/components/canvas/route.js +56 -20
- package/dist/esm/components/carousel-classes.js +27 -0
- package/dist/esm/components/carousel.js +317 -0
- package/dist/esm/components/command.js +2 -27
- package/dist/esm/components/context-menu.js +199 -0
- package/dist/esm/components/date-picker.js +208 -0
- package/dist/esm/components/display-classes.js +10 -13
- package/dist/esm/components/display.js +22 -19
- package/dist/esm/components/drawer.js +139 -0
- package/dist/esm/components/filters.js +1 -1
- package/dist/esm/components/flow.js +1 -1
- package/dist/esm/components/hooks.js +17 -6
- package/dist/esm/components/hover-card.js +221 -0
- package/dist/esm/components/icons.js +17 -0
- package/dist/esm/components/inbox.js +2 -1
- package/dist/esm/components/lazy-module.js +53 -0
- package/dist/esm/components/menu.js +344 -0
- package/dist/esm/components/modal.js +173 -0
- package/dist/esm/components/more-sheet.js +52 -0
- package/dist/esm/components/multi-select.js +420 -0
- package/dist/esm/components/nav-items.js +138 -0
- package/dist/esm/components/nav.js +13 -174
- package/dist/esm/components/navigate.js +33 -0
- package/dist/esm/components/overlay-core.js +221 -0
- package/dist/esm/components/overlay-dialog.js +255 -0
- package/dist/esm/components/overlay-position.js +163 -0
- package/dist/esm/components/overlay.js +7 -1441
- package/dist/esm/components/popover.js +92 -0
- package/dist/esm/components/segmented.js +2 -1
- package/dist/esm/components/split.js +1 -1
- package/dist/esm/components/strings.js +25 -0
- package/dist/esm/components/tooltip.js +177 -0
- package/dist/esm/components/tree-view-classes.js +57 -0
- package/dist/esm/components/tree-view.js +319 -0
- package/dist/esm/components/virtual-table.js +245 -0
- package/dist/esm/components/virtual.js +291 -0
- package/dist/esm/inbox.js +12 -0
- package/dist/esm/index.js +61 -2
- package/dist/esm/runtime.js +1 -1
- package/dist/esm/service-tokens.js +6 -0
- package/dist/esm/theme.js +2 -2
- package/dist/esm/virtual-range.js +73 -0
- package/dist/format.js +1 -1
- package/dist/inbox.d.ts +238 -0
- package/dist/index.d.ts +1032 -778
- package/dist/nish-components.css +2 -0
- package/dist/nish-components.min.css +1 -1
- package/dist/service-tokens.d.ts +60 -0
- package/dist/theme.d.ts +50 -0
- package/dist/theme.global.js +28 -3
- package/dist/theme.js +28 -3
- package/package.json +17 -1
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/carousel.tsx
|
|
4
|
+
import {
|
|
5
|
+
Children,
|
|
6
|
+
useCallback,
|
|
7
|
+
useEffect,
|
|
8
|
+
useRef,
|
|
9
|
+
useState
|
|
10
|
+
} from "react";
|
|
11
|
+
import { cn } from "../cn.js";
|
|
12
|
+
import { isRtl, useIsoLayoutEffect } from "./client.js";
|
|
13
|
+
import { CAROUSEL_MAX_DOTS, CAROUSEL_MIN_INTERVAL, nearestSlide, stepIndex } from "./carousel-classes.js";
|
|
14
|
+
import { useMediaQuery } from "./hooks.js";
|
|
15
|
+
import { ChevronLeftIcon, ChevronRightIcon, PauseIcon, PlayIcon } from "./icons.js";
|
|
16
|
+
import { IconButton } from "./primitives.js";
|
|
17
|
+
import { useNishStrings } from "./provider.js";
|
|
18
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
19
|
+
var SETTLE_MS = 700;
|
|
20
|
+
function Carousel({
|
|
21
|
+
"aria-label": ariaLabel,
|
|
22
|
+
children,
|
|
23
|
+
index: indexProp,
|
|
24
|
+
defaultIndex = 0,
|
|
25
|
+
onIndexChange,
|
|
26
|
+
slideWidth = "100%",
|
|
27
|
+
loop = false,
|
|
28
|
+
autoPlay,
|
|
29
|
+
indicators,
|
|
30
|
+
slideLabel,
|
|
31
|
+
previousLabel,
|
|
32
|
+
nextLabel,
|
|
33
|
+
className,
|
|
34
|
+
ref,
|
|
35
|
+
onPointerEnter,
|
|
36
|
+
onPointerLeave,
|
|
37
|
+
onFocus,
|
|
38
|
+
onBlur,
|
|
39
|
+
...rest
|
|
40
|
+
}) {
|
|
41
|
+
const strings = useNishStrings();
|
|
42
|
+
const reduced = useMediaQuery("(prefers-reduced-motion: reduce)");
|
|
43
|
+
const slides = Children.toArray(children);
|
|
44
|
+
const count = slides.length;
|
|
45
|
+
const label = slideLabel ?? strings.slideLabel;
|
|
46
|
+
const prevName = previousLabel ?? strings.previousSlide;
|
|
47
|
+
const nextName = nextLabel ?? strings.nextSlide;
|
|
48
|
+
const [ownIndex, setOwnIndex] = useState(defaultIndex);
|
|
49
|
+
const index = count === 0 ? 0 : Math.min(Math.max(indexProp ?? ownIndex, 0), count - 1);
|
|
50
|
+
const canPrev = loop || index > 0;
|
|
51
|
+
const canNext = loop || index < count - 1;
|
|
52
|
+
const coarse = useMediaQuery("(pointer: coarse)");
|
|
53
|
+
const [slotWidth, setSlotWidth] = useState(0);
|
|
54
|
+
const dotsFit = slotWidth === 0 || count * (coarse ? 44 : 32) <= slotWidth;
|
|
55
|
+
const showDots = indicators ?? (count <= CAROUSEL_MAX_DOTS && dotsFit);
|
|
56
|
+
const [announce, setAnnounce] = useState("");
|
|
57
|
+
const trackRef = useRef(null);
|
|
58
|
+
const indexRef = useRef(index);
|
|
59
|
+
const viewRef = useRef(0);
|
|
60
|
+
const targetRef = useRef(null);
|
|
61
|
+
const frameRef = useRef(0);
|
|
62
|
+
const slotCleanup = useRef(null);
|
|
63
|
+
const latest = useRef({ onIndexChange, reduced, label, count });
|
|
64
|
+
useIsoLayoutEffect(() => {
|
|
65
|
+
latest.current = { onIndexChange, reduced, label, count };
|
|
66
|
+
indexRef.current = index;
|
|
67
|
+
});
|
|
68
|
+
const [hovered, setHovered] = useState(false);
|
|
69
|
+
const [focusedIn, setFocusedIn] = useState(false);
|
|
70
|
+
const [hidden, setHidden] = useState(false);
|
|
71
|
+
const [playState, setPlayState] = useState("auto");
|
|
72
|
+
const playing = playState === "on" || playState === "auto" && !reduced;
|
|
73
|
+
const paused = hovered || focusedIn || hidden;
|
|
74
|
+
const interval = Math.max(autoPlay ?? 0, CAROUSEL_MIN_INTERVAL);
|
|
75
|
+
const rotating = !!autoPlay && count > 1 && playing && !paused;
|
|
76
|
+
const setCurrent = useCallback((i) => {
|
|
77
|
+
if (i === indexRef.current) return;
|
|
78
|
+
indexRef.current = i;
|
|
79
|
+
setOwnIndex(i);
|
|
80
|
+
latest.current.onIndexChange?.(i);
|
|
81
|
+
}, []);
|
|
82
|
+
const scrollToSlide = useCallback((i, behavior) => {
|
|
83
|
+
viewRef.current = i;
|
|
84
|
+
targetRef.current = { index: i, at: Date.now() };
|
|
85
|
+
const el = trackRef.current?.children[i];
|
|
86
|
+
el?.scrollIntoView({ behavior, block: "nearest", inline: "start" });
|
|
87
|
+
}, []);
|
|
88
|
+
const goTo = useCallback(
|
|
89
|
+
(i) => {
|
|
90
|
+
setCurrent(i);
|
|
91
|
+
setAnnounce(latest.current.label(i + 1, latest.current.count));
|
|
92
|
+
scrollToSlide(i, latest.current.reduced ? "auto" : "smooth");
|
|
93
|
+
},
|
|
94
|
+
[setCurrent, scrollToSlide]
|
|
95
|
+
);
|
|
96
|
+
const goToRef = useRef(goTo);
|
|
97
|
+
useIsoLayoutEffect(() => {
|
|
98
|
+
goToRef.current = goTo;
|
|
99
|
+
});
|
|
100
|
+
const mounted = useRef(false);
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
const first = !mounted.current;
|
|
103
|
+
mounted.current = true;
|
|
104
|
+
if (count === 0 || index === viewRef.current) return;
|
|
105
|
+
scrollToSlide(index, first || latest.current.reduced ? "auto" : "smooth");
|
|
106
|
+
}, [index, count, scrollToSlide]);
|
|
107
|
+
const measure = useCallback(() => {
|
|
108
|
+
const track = trackRef.current;
|
|
109
|
+
if (!track) return;
|
|
110
|
+
const rtl = isRtl(track);
|
|
111
|
+
const edge = (el) => {
|
|
112
|
+
const r = el.getBoundingClientRect();
|
|
113
|
+
return rtl ? r.right : r.left;
|
|
114
|
+
};
|
|
115
|
+
const n = nearestSlide(Array.from(track.children, edge), edge(track));
|
|
116
|
+
if (n < 0) return;
|
|
117
|
+
const target = targetRef.current;
|
|
118
|
+
if (target) {
|
|
119
|
+
if (n === target.index) targetRef.current = null;
|
|
120
|
+
else if (Date.now() - target.at < SETTLE_MS) return;
|
|
121
|
+
else targetRef.current = null;
|
|
122
|
+
}
|
|
123
|
+
viewRef.current = n;
|
|
124
|
+
setCurrent(n);
|
|
125
|
+
}, [setCurrent]);
|
|
126
|
+
const onScroll = () => {
|
|
127
|
+
if (frameRef.current) return;
|
|
128
|
+
frameRef.current = requestAnimationFrame(() => {
|
|
129
|
+
frameRef.current = 0;
|
|
130
|
+
measure();
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
useEffect(
|
|
134
|
+
() => () => {
|
|
135
|
+
if (frameRef.current) cancelAnimationFrame(frameRef.current);
|
|
136
|
+
},
|
|
137
|
+
[]
|
|
138
|
+
);
|
|
139
|
+
const setFromFocus = useCallback(
|
|
140
|
+
(i) => {
|
|
141
|
+
viewRef.current = i;
|
|
142
|
+
targetRef.current = { index: i, at: Date.now() };
|
|
143
|
+
setCurrent(i);
|
|
144
|
+
},
|
|
145
|
+
[setCurrent]
|
|
146
|
+
);
|
|
147
|
+
const onTrackKey = (e) => {
|
|
148
|
+
if (e.target !== e.currentTarget || count < 2) return;
|
|
149
|
+
const rtl = isRtl(e.currentTarget);
|
|
150
|
+
let to = null;
|
|
151
|
+
if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
|
|
152
|
+
const forward = e.key === "ArrowRight" !== rtl;
|
|
153
|
+
if (forward ? canNext : canPrev) to = stepIndex(index, forward ? 1 : -1, count, loop);
|
|
154
|
+
} else if (e.key === "Home") to = 0;
|
|
155
|
+
else if (e.key === "End") to = count - 1;
|
|
156
|
+
else return;
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
if (to !== null) goTo(to);
|
|
159
|
+
};
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
const sync = () => setHidden(document.visibilityState === "hidden");
|
|
162
|
+
sync();
|
|
163
|
+
document.addEventListener("visibilitychange", sync);
|
|
164
|
+
return () => document.removeEventListener("visibilitychange", sync);
|
|
165
|
+
}, []);
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
if (!rotating) return;
|
|
168
|
+
const id = setTimeout(() => goToRef.current(index === count - 1 ? 0 : index + 1), interval);
|
|
169
|
+
return () => clearTimeout(id);
|
|
170
|
+
}, [rotating, index, count, interval]);
|
|
171
|
+
const empty = count === 0;
|
|
172
|
+
const tooFast = autoPlay !== void 0 && autoPlay > 0 && autoPlay < CAROUSEL_MIN_INTERVAL;
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
if (process.env.NODE_ENV !== "production" && empty) console.warn("Carousel: no slides");
|
|
175
|
+
}, [empty]);
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
if (process.env.NODE_ENV !== "production" && tooFast) {
|
|
178
|
+
console.warn(`Carousel: autoPlay must be at least ${CAROUSEL_MIN_INTERVAL} ms; using ${CAROUSEL_MIN_INTERVAL}.`);
|
|
179
|
+
}
|
|
180
|
+
}, [tooFast]);
|
|
181
|
+
const handlePointerEnter = (e) => {
|
|
182
|
+
setHovered(true);
|
|
183
|
+
onPointerEnter?.(e);
|
|
184
|
+
};
|
|
185
|
+
const handlePointerLeave = (e) => {
|
|
186
|
+
setHovered(false);
|
|
187
|
+
onPointerLeave?.(e);
|
|
188
|
+
};
|
|
189
|
+
const handleFocus = (e) => {
|
|
190
|
+
setFocusedIn(true);
|
|
191
|
+
onFocus?.(e);
|
|
192
|
+
};
|
|
193
|
+
const handleBlur = (e) => {
|
|
194
|
+
if (!e.currentTarget.contains(e.relatedTarget)) setFocusedIn(false);
|
|
195
|
+
onBlur?.(e);
|
|
196
|
+
};
|
|
197
|
+
const slotRef = useCallback((el) => {
|
|
198
|
+
slotCleanup.current?.();
|
|
199
|
+
slotCleanup.current = null;
|
|
200
|
+
if (!el || typeof ResizeObserver === "undefined") return;
|
|
201
|
+
const ro = new ResizeObserver(() => setSlotWidth(el.clientWidth));
|
|
202
|
+
ro.observe(el);
|
|
203
|
+
setSlotWidth(el.clientWidth);
|
|
204
|
+
slotCleanup.current = () => ro.disconnect();
|
|
205
|
+
}, []);
|
|
206
|
+
const togglePlay = () => setPlayState(playing ? "off" : "on");
|
|
207
|
+
const arrowClass = "aria-disabled:cursor-not-allowed aria-disabled:opacity-40";
|
|
208
|
+
return /* @__PURE__ */ jsxs(
|
|
209
|
+
"section",
|
|
210
|
+
{
|
|
211
|
+
...rest,
|
|
212
|
+
ref,
|
|
213
|
+
"aria-roledescription": strings.carouselRole,
|
|
214
|
+
"aria-label": ariaLabel,
|
|
215
|
+
"data-state": rotating ? "active" : "inactive",
|
|
216
|
+
onPointerEnter: handlePointerEnter,
|
|
217
|
+
onPointerLeave: handlePointerLeave,
|
|
218
|
+
onFocus: handleFocus,
|
|
219
|
+
onBlur: handleBlur,
|
|
220
|
+
className: cn("flex w-full min-w-0 flex-col gap-2", className),
|
|
221
|
+
children: [
|
|
222
|
+
/* @__PURE__ */ jsx("div", { "aria-live": rotating ? "off" : "polite", "aria-atomic": "true", className: "sr-only", children: announce }),
|
|
223
|
+
/* @__PURE__ */ jsxs("div", { className: "relative min-w-0", children: [
|
|
224
|
+
autoPlay && count > 1 ? /* @__PURE__ */ jsx(
|
|
225
|
+
IconButton,
|
|
226
|
+
{
|
|
227
|
+
label: playing ? strings.stopRotation : strings.startRotation,
|
|
228
|
+
size: "sm",
|
|
229
|
+
variant: "subtle",
|
|
230
|
+
onClick: togglePlay,
|
|
231
|
+
className: "absolute end-2 top-2 z-[1] rounded-full shadow-pop",
|
|
232
|
+
children: playing ? /* @__PURE__ */ jsx(PauseIcon, {}) : /* @__PURE__ */ jsx(PlayIcon, {})
|
|
233
|
+
}
|
|
234
|
+
) : null,
|
|
235
|
+
/* @__PURE__ */ jsx(
|
|
236
|
+
"div",
|
|
237
|
+
{
|
|
238
|
+
ref: trackRef,
|
|
239
|
+
tabIndex: 0,
|
|
240
|
+
role: "group",
|
|
241
|
+
"aria-label": ariaLabel,
|
|
242
|
+
onKeyDown: onTrackKey,
|
|
243
|
+
onScroll,
|
|
244
|
+
className: "flex w-full min-w-0 snap-x snap-mandatory gap-3 overflow-x-auto overscroll-x-contain rounded-lg [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
|
|
245
|
+
children: slides.map((child, i) => /* @__PURE__ */ jsx(
|
|
246
|
+
"div",
|
|
247
|
+
{
|
|
248
|
+
role: "group",
|
|
249
|
+
"aria-roledescription": strings.slideRole,
|
|
250
|
+
"aria-label": label(i + 1, count),
|
|
251
|
+
"data-index": i,
|
|
252
|
+
"data-state": i === index ? "active" : "inactive",
|
|
253
|
+
onFocus: () => setFromFocus(i),
|
|
254
|
+
className: "min-w-0 shrink-0 snap-start",
|
|
255
|
+
style: { width: slideWidth },
|
|
256
|
+
children: child
|
|
257
|
+
},
|
|
258
|
+
i
|
|
259
|
+
))
|
|
260
|
+
}
|
|
261
|
+
)
|
|
262
|
+
] }),
|
|
263
|
+
count > 1 ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
264
|
+
/* @__PURE__ */ jsx(
|
|
265
|
+
IconButton,
|
|
266
|
+
{
|
|
267
|
+
label: prevName,
|
|
268
|
+
size: "sm",
|
|
269
|
+
"aria-disabled": !canPrev || void 0,
|
|
270
|
+
onClick: () => canPrev && goTo(stepIndex(index, -1, count, loop)),
|
|
271
|
+
className: arrowClass,
|
|
272
|
+
children: /* @__PURE__ */ jsx(ChevronLeftIcon, { className: "rtl:-scale-x-100" })
|
|
273
|
+
}
|
|
274
|
+
),
|
|
275
|
+
/* @__PURE__ */ jsx("div", { ref: slotRef, className: "flex min-w-0 flex-1 justify-center", children: showDots ? /* @__PURE__ */ jsx("div", { role: "group", "aria-label": strings.slidePicker, className: "flex", children: slides.map((_, i) => /* @__PURE__ */ jsx(
|
|
276
|
+
"button",
|
|
277
|
+
{
|
|
278
|
+
type: "button",
|
|
279
|
+
"aria-label": strings.goToSlide(i + 1),
|
|
280
|
+
"aria-current": i === index ? "true" : void 0,
|
|
281
|
+
onClick: () => goTo(i),
|
|
282
|
+
className: "flex size-8 items-center justify-center rounded-full min-w-0 pointer-coarse:size-11",
|
|
283
|
+
children: /* @__PURE__ */ jsx(
|
|
284
|
+
"span",
|
|
285
|
+
{
|
|
286
|
+
className: cn(
|
|
287
|
+
"size-2 rounded-full transition-colors",
|
|
288
|
+
i === index ? "bg-th-accent forced-colors:bg-[Highlight]" : "bg-th-line-strong forced-colors:bg-[ButtonText]"
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
)
|
|
292
|
+
},
|
|
293
|
+
i
|
|
294
|
+
)) }) : /* @__PURE__ */ jsxs("span", { className: "text-xs tabular-nums text-th-hi3", "aria-hidden": "true", children: [
|
|
295
|
+
index + 1,
|
|
296
|
+
" / ",
|
|
297
|
+
count
|
|
298
|
+
] }) }),
|
|
299
|
+
/* @__PURE__ */ jsx(
|
|
300
|
+
IconButton,
|
|
301
|
+
{
|
|
302
|
+
label: nextName,
|
|
303
|
+
size: "sm",
|
|
304
|
+
"aria-disabled": !canNext || void 0,
|
|
305
|
+
onClick: () => canNext && goTo(stepIndex(index, 1, count, loop)),
|
|
306
|
+
className: arrowClass,
|
|
307
|
+
children: /* @__PURE__ */ jsx(ChevronRightIcon, { className: "rtl:-scale-x-100" })
|
|
308
|
+
}
|
|
309
|
+
)
|
|
310
|
+
] }) : null
|
|
311
|
+
]
|
|
312
|
+
}
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
export {
|
|
316
|
+
Carousel
|
|
317
|
+
};
|
|
@@ -11,7 +11,8 @@ import {
|
|
|
11
11
|
import { cn } from "../cn.js";
|
|
12
12
|
import { safeUrl } from "../url.js";
|
|
13
13
|
import { XIcon } from "./icons.js";
|
|
14
|
-
import { Modal
|
|
14
|
+
import { Modal } from "./modal.js";
|
|
15
|
+
import { navigateFromLayer, warnUnsafe } from "./navigate.js";
|
|
15
16
|
import { useLatest } from "./latest.js";
|
|
16
17
|
import { useNishStrings } from "./provider.js";
|
|
17
18
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -49,32 +50,6 @@ function matchText(text, q) {
|
|
|
49
50
|
}
|
|
50
51
|
return { tier: 3, idx };
|
|
51
52
|
}
|
|
52
|
-
function navigateFromLayer(rawHref) {
|
|
53
|
-
const href = safeUrl(rawHref);
|
|
54
|
-
if (href === null) {
|
|
55
|
-
warnUnsafe(rawHref);
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const inLayer = !!history.state?.nishLayer;
|
|
59
|
-
if (!inLayer) {
|
|
60
|
-
window.location.assign(href);
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
let done = false;
|
|
64
|
-
const go = () => {
|
|
65
|
-
if (done) return;
|
|
66
|
-
done = true;
|
|
67
|
-
window.location.assign(href);
|
|
68
|
-
};
|
|
69
|
-
window.setTimeout(() => whenHistorySettled(go), 0);
|
|
70
|
-
window.setTimeout(go, 400);
|
|
71
|
-
}
|
|
72
|
-
function warnUnsafe(href) {
|
|
73
|
-
try {
|
|
74
|
-
if (process.env.NODE_ENV !== "production") console.warn(`CommandPalette: ignored an unsafe href (${href.slice(0, 40)}).`);
|
|
75
|
-
} catch {
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
53
|
function rankItems(items, query) {
|
|
79
54
|
const q = query.trim().toLowerCase();
|
|
80
55
|
if (!q) return items.slice(0, MAX_RESULTS).map(({ item }) => ({ item, marks: [] }));
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/context-menu.tsx
|
|
4
|
+
import { useRef, useState } from "react";
|
|
5
|
+
import { cn } from "../cn.js";
|
|
6
|
+
import { isRtl, useIsoLayoutEffect } from "./client.js";
|
|
7
|
+
import { mergeRefs } from "./data-classes.js";
|
|
8
|
+
import { DropdownMenu } from "./menu.js";
|
|
9
|
+
import { useNishStrings } from "./provider.js";
|
|
10
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
var FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
|
|
12
|
+
var NATIVE_MENU_TARGETS = 'input, textarea, select, [contenteditable]:not([contenteditable="false"])';
|
|
13
|
+
var MOVE_CANCELS_PRESS = 10;
|
|
14
|
+
var SWALLOW_CLICK_MS = 800;
|
|
15
|
+
var SWALLOW_NATIVE_MENU_MS = 1e3;
|
|
16
|
+
function ContextMenu({
|
|
17
|
+
children,
|
|
18
|
+
menu,
|
|
19
|
+
open: openProp,
|
|
20
|
+
onOpenChange,
|
|
21
|
+
disabled,
|
|
22
|
+
menuLabel,
|
|
23
|
+
sheetBelow = "sm",
|
|
24
|
+
sheetTitle,
|
|
25
|
+
longPressMs = 500,
|
|
26
|
+
menuClassName,
|
|
27
|
+
className,
|
|
28
|
+
ref,
|
|
29
|
+
onContextMenu,
|
|
30
|
+
onKeyDown,
|
|
31
|
+
onPointerDown,
|
|
32
|
+
onPointerMove,
|
|
33
|
+
onPointerUp,
|
|
34
|
+
onPointerCancel,
|
|
35
|
+
onClickCapture,
|
|
36
|
+
...rest
|
|
37
|
+
}) {
|
|
38
|
+
const t = useNishStrings();
|
|
39
|
+
const [openState, setOpenState] = useState(false);
|
|
40
|
+
const [point, setPoint] = useState(null);
|
|
41
|
+
const [target, setTarget] = useState(null);
|
|
42
|
+
const rootRef = useRef(null);
|
|
43
|
+
const returnTo = useRef(null);
|
|
44
|
+
const lastOpen = useRef(0);
|
|
45
|
+
const suppressClickUntil = useRef(0);
|
|
46
|
+
const pressTimer = useRef(void 0);
|
|
47
|
+
const pressStart = useRef(null);
|
|
48
|
+
const wasOpen = useRef(false);
|
|
49
|
+
const open = openProp ?? openState;
|
|
50
|
+
const setOpen = (next, reason) => {
|
|
51
|
+
if (openProp === void 0) setOpenState(next);
|
|
52
|
+
onOpenChange?.(next, reason);
|
|
53
|
+
};
|
|
54
|
+
const cancelPress = () => {
|
|
55
|
+
window.clearTimeout(pressTimer.current);
|
|
56
|
+
pressTimer.current = void 0;
|
|
57
|
+
pressStart.current = null;
|
|
58
|
+
};
|
|
59
|
+
const inside = (e) => e.currentTarget.contains(e.target);
|
|
60
|
+
const openAt = (x, y, el, swallowNative) => {
|
|
61
|
+
if (disabled) return false;
|
|
62
|
+
const content2 = typeof menu === "function" ? menu(el) : menu;
|
|
63
|
+
if (content2 == null || content2 === false) return false;
|
|
64
|
+
const root = rootRef.current;
|
|
65
|
+
const active = document.activeElement;
|
|
66
|
+
const focusable = active && root?.contains(active) ? active : el.closest(FOCUSABLE);
|
|
67
|
+
returnTo.current = focusable && root?.contains(focusable) ? focusable : null;
|
|
68
|
+
setPoint({ x, y });
|
|
69
|
+
setTarget(el);
|
|
70
|
+
if (swallowNative) lastOpen.current = Date.now();
|
|
71
|
+
setOpen(true);
|
|
72
|
+
return true;
|
|
73
|
+
};
|
|
74
|
+
const handleContextMenu = (e) => {
|
|
75
|
+
onContextMenu?.(e);
|
|
76
|
+
if (disabled || e.defaultPrevented || !inside(e)) return;
|
|
77
|
+
const el = e.target;
|
|
78
|
+
if (e.shiftKey || el.closest(NATIVE_MENU_TARGETS)) return;
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
if (Date.now() - lastOpen.current < SWALLOW_NATIVE_MENU_MS) return;
|
|
81
|
+
openAt(e.clientX, e.clientY, el, false);
|
|
82
|
+
};
|
|
83
|
+
const handleKeyDown = (e) => {
|
|
84
|
+
onKeyDown?.(e);
|
|
85
|
+
if (disabled || e.defaultPrevented || !inside(e)) return;
|
|
86
|
+
if (!(e.key === "F10" && e.shiftKey || e.key === "ContextMenu")) return;
|
|
87
|
+
e.preventDefault();
|
|
88
|
+
const root = e.currentTarget;
|
|
89
|
+
const from = document.activeElement ?? root;
|
|
90
|
+
const r = from.getBoundingClientRect();
|
|
91
|
+
openAt(isRtl(root) ? r.right : r.left, r.bottom, from, true);
|
|
92
|
+
};
|
|
93
|
+
const handlePointerDown = (e) => {
|
|
94
|
+
onPointerDown?.(e);
|
|
95
|
+
if (disabled || !inside(e) || e.pointerType === "mouse" || e.button !== 0) return;
|
|
96
|
+
cancelPress();
|
|
97
|
+
const start = { x: e.clientX, y: e.clientY };
|
|
98
|
+
const el = e.target;
|
|
99
|
+
pressStart.current = start;
|
|
100
|
+
pressTimer.current = window.setTimeout(() => {
|
|
101
|
+
pressTimer.current = void 0;
|
|
102
|
+
pressStart.current = null;
|
|
103
|
+
if (openAt(start.x, start.y, el, true)) suppressClickUntil.current = Date.now() + SWALLOW_CLICK_MS;
|
|
104
|
+
}, longPressMs);
|
|
105
|
+
};
|
|
106
|
+
const handlePointerMove = (e) => {
|
|
107
|
+
onPointerMove?.(e);
|
|
108
|
+
const start = pressStart.current;
|
|
109
|
+
if (start && Math.hypot(e.clientX - start.x, e.clientY - start.y) > MOVE_CANCELS_PRESS) cancelPress();
|
|
110
|
+
};
|
|
111
|
+
const handlePointerUp = (e) => {
|
|
112
|
+
onPointerUp?.(e);
|
|
113
|
+
cancelPress();
|
|
114
|
+
};
|
|
115
|
+
const handlePointerCancel = (e) => {
|
|
116
|
+
onPointerCancel?.(e);
|
|
117
|
+
cancelPress();
|
|
118
|
+
};
|
|
119
|
+
const handleClickCapture = (e) => {
|
|
120
|
+
onClickCapture?.(e);
|
|
121
|
+
if (Date.now() < suppressClickUntil.current && inside(e)) {
|
|
122
|
+
e.preventDefault();
|
|
123
|
+
e.stopPropagation();
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
useIsoLayoutEffect(() => () => window.clearTimeout(pressTimer.current), []);
|
|
127
|
+
useIsoLayoutEffect(() => {
|
|
128
|
+
const root = rootRef.current;
|
|
129
|
+
if (!open || point !== null || !root) return;
|
|
130
|
+
const r = root.getBoundingClientRect();
|
|
131
|
+
setPoint({ x: isRtl(root) ? r.right - 8 : r.left + 8, y: r.top + 8 });
|
|
132
|
+
}, [open, point]);
|
|
133
|
+
useIsoLayoutEffect(() => {
|
|
134
|
+
if (open) {
|
|
135
|
+
wasOpen.current = true;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (!wasOpen.current) return;
|
|
139
|
+
wasOpen.current = false;
|
|
140
|
+
setPoint(null);
|
|
141
|
+
setTarget(null);
|
|
142
|
+
const active = document.activeElement;
|
|
143
|
+
if (!active || active === document.body || active.hasAttribute("data-nish-context-anchor")) returnTo.current?.focus({ preventScroll: true });
|
|
144
|
+
}, [open]);
|
|
145
|
+
const content = typeof menu === "function" ? target ? menu(target) : null : menu;
|
|
146
|
+
const shown = open && content != null && content !== false;
|
|
147
|
+
return /* @__PURE__ */ jsxs(
|
|
148
|
+
"div",
|
|
149
|
+
{
|
|
150
|
+
...rest,
|
|
151
|
+
ref: mergeRefs(ref, rootRef),
|
|
152
|
+
"data-state": open ? "open" : "closed",
|
|
153
|
+
className: cn("[-webkit-touch-callout:none]", className),
|
|
154
|
+
onContextMenu: handleContextMenu,
|
|
155
|
+
onKeyDown: handleKeyDown,
|
|
156
|
+
onPointerDown: handlePointerDown,
|
|
157
|
+
onPointerMove: handlePointerMove,
|
|
158
|
+
onPointerUp: handlePointerUp,
|
|
159
|
+
onPointerCancel: handlePointerCancel,
|
|
160
|
+
onClickCapture: handleClickCapture,
|
|
161
|
+
children: [
|
|
162
|
+
children,
|
|
163
|
+
/* @__PURE__ */ jsx(
|
|
164
|
+
DropdownMenu,
|
|
165
|
+
{
|
|
166
|
+
trigger: /* @__PURE__ */ jsx(
|
|
167
|
+
"span",
|
|
168
|
+
{
|
|
169
|
+
"aria-hidden": "true",
|
|
170
|
+
"data-nish-context-anchor": "",
|
|
171
|
+
style: {
|
|
172
|
+
position: "fixed",
|
|
173
|
+
left: point?.x ?? 0,
|
|
174
|
+
top: point?.y ?? 0,
|
|
175
|
+
width: 0,
|
|
176
|
+
height: 0,
|
|
177
|
+
pointerEvents: "none"
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
),
|
|
181
|
+
open: shown && point !== null,
|
|
182
|
+
onOpenChange: (next, reason) => setOpen(next, reason),
|
|
183
|
+
"aria-label": menuLabel ?? t.contextMenu,
|
|
184
|
+
sheetBelow,
|
|
185
|
+
sheetTitle,
|
|
186
|
+
className: menuClassName,
|
|
187
|
+
side: "bottom",
|
|
188
|
+
align: "start",
|
|
189
|
+
children: content
|
|
190
|
+
},
|
|
191
|
+
point ? `${point.x},${point.y}` : "none"
|
|
192
|
+
)
|
|
193
|
+
]
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
export {
|
|
198
|
+
ContextMenu
|
|
199
|
+
};
|