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.
Files changed (62) hide show
  1. package/README.md +18 -8
  2. package/dist/chart.js +1 -1
  3. package/dist/elements-all.js +13 -13
  4. package/dist/elements-charts.js +1 -1
  5. package/dist/elements-data.js +1 -1
  6. package/dist/elements-forms.js +1 -1
  7. package/dist/elements.js +3 -3
  8. package/dist/esm/components/app-shell.js +145 -45
  9. package/dist/esm/components/breadcrumbs.js +1 -1
  10. package/dist/esm/components/calendar-classes.js +325 -0
  11. package/dist/esm/components/calendar.js +263 -0
  12. package/dist/esm/components/canvas/route.js +56 -20
  13. package/dist/esm/components/carousel-classes.js +27 -0
  14. package/dist/esm/components/carousel.js +317 -0
  15. package/dist/esm/components/command.js +2 -27
  16. package/dist/esm/components/context-menu.js +199 -0
  17. package/dist/esm/components/date-picker.js +208 -0
  18. package/dist/esm/components/display-classes.js +10 -13
  19. package/dist/esm/components/display.js +22 -19
  20. package/dist/esm/components/drawer.js +139 -0
  21. package/dist/esm/components/filters.js +1 -1
  22. package/dist/esm/components/flow.js +1 -1
  23. package/dist/esm/components/hover-card.js +221 -0
  24. package/dist/esm/components/icons.js +17 -0
  25. package/dist/esm/components/inbox.js +2 -1
  26. package/dist/esm/components/lazy-module.js +53 -0
  27. package/dist/esm/components/menu.js +344 -0
  28. package/dist/esm/components/modal.js +173 -0
  29. package/dist/esm/components/more-sheet.js +52 -0
  30. package/dist/esm/components/multi-select.js +420 -0
  31. package/dist/esm/components/nav-items.js +138 -0
  32. package/dist/esm/components/nav.js +13 -174
  33. package/dist/esm/components/navigate.js +33 -0
  34. package/dist/esm/components/overlay-core.js +221 -0
  35. package/dist/esm/components/overlay-dialog.js +255 -0
  36. package/dist/esm/components/overlay-position.js +163 -0
  37. package/dist/esm/components/overlay.js +7 -1441
  38. package/dist/esm/components/popover.js +92 -0
  39. package/dist/esm/components/segmented.js +2 -1
  40. package/dist/esm/components/split.js +1 -1
  41. package/dist/esm/components/strings.js +25 -0
  42. package/dist/esm/components/tooltip.js +177 -0
  43. package/dist/esm/components/tree-view-classes.js +57 -0
  44. package/dist/esm/components/tree-view.js +319 -0
  45. package/dist/esm/components/virtual-table.js +245 -0
  46. package/dist/esm/components/virtual.js +291 -0
  47. package/dist/esm/inbox.js +12 -0
  48. package/dist/esm/index.js +61 -2
  49. package/dist/esm/runtime.js +1 -1
  50. package/dist/esm/service-tokens.js +6 -0
  51. package/dist/esm/theme.js +2 -2
  52. package/dist/esm/virtual-range.js +73 -0
  53. package/dist/format.js +1 -1
  54. package/dist/inbox.d.ts +238 -0
  55. package/dist/index.d.ts +1030 -777
  56. package/dist/nish-components.css +2 -0
  57. package/dist/nish-components.min.css +1 -1
  58. package/dist/service-tokens.d.ts +60 -0
  59. package/dist/theme.d.ts +50 -0
  60. package/dist/theme.global.js +28 -3
  61. package/dist/theme.js +28 -3
  62. package/package.json +17 -1
@@ -0,0 +1,208 @@
1
+ "use client";
2
+
3
+ // src/components/date-picker.tsx
4
+ import { useEffect, useId, useRef, useState } from "react";
5
+ import { cn } from "../cn.js";
6
+ import { getDefaultLocale } from "../format.js";
7
+ import { Calendar } from "./calendar.js";
8
+ import {
9
+ datePlaceholder,
10
+ formatDateField,
11
+ fullDate,
12
+ inRange,
13
+ parseDateField,
14
+ parseIso,
15
+ resolveLocale
16
+ } from "./calendar-classes.js";
17
+ import { setRef } from "./forms-classes.js";
18
+ import { Popover } from "./popover.js";
19
+ import { CalendarIcon } from "./icons.js";
20
+ import { fieldClass } from "./primitives-classes.js";
21
+ import { useNishConfig, useNishStrings } from "./provider.js";
22
+ import { jsx, jsxs } from "react/jsx-runtime";
23
+ function DatePicker({
24
+ ref,
25
+ value: valueProp,
26
+ defaultValue = null,
27
+ onValueChange,
28
+ min: minProp,
29
+ max: maxProp,
30
+ isDateDisabled,
31
+ locale,
32
+ weekStartsOn,
33
+ captionLayout,
34
+ placeholder,
35
+ "aria-label": ariaLabel,
36
+ "aria-labelledby": ariaLabelledBy,
37
+ "aria-describedby": ariaDescribedBy,
38
+ "aria-invalid": ariaInvalid,
39
+ invalid,
40
+ id,
41
+ name,
42
+ form,
43
+ required,
44
+ disabled = false,
45
+ size = "md",
46
+ calendarLabel,
47
+ className,
48
+ ...rest
49
+ }) {
50
+ const strings = useNishStrings();
51
+ const config = useNishConfig();
52
+ const loc = resolveLocale(locale ?? config.locale ?? getDefaultLocale());
53
+ const autoId = useId().replace(/[^a-zA-Z0-9_-]/g, "");
54
+ const inputId = id ?? `nish-dp-${autoId}`;
55
+ const min = parseIso(minProp) ? minProp : null;
56
+ const max = parseIso(maxProp) ? maxProp : null;
57
+ const controlled = valueProp !== void 0;
58
+ const [own, setOwn] = useState(parseIso(defaultValue) ? defaultValue : null);
59
+ const value = controlled ? parseIso(valueProp) ? valueProp : null : own;
60
+ const [open, setOpen] = useState(false);
61
+ const [text, setText] = useState("");
62
+ const [dirty, setDirty] = useState(false);
63
+ const [badText, setBadText] = useState(false);
64
+ const inputRef = useRef(null);
65
+ const buttonRef = useRef(null);
66
+ const shown = dirty ? text : value ? formatDateField(value, loc) : "";
67
+ const isInvalid = invalid === true || ariaInvalid === true || ariaInvalid === "true" || badText;
68
+ useEffect(() => {
69
+ inputRef.current?.setCustomValidity(badText ? strings.invalidDate : "");
70
+ }, [badText, strings.invalidDate]);
71
+ useEffect(() => {
72
+ const f = inputRef.current?.form;
73
+ if (!f || controlled) return;
74
+ const onReset = () => {
75
+ setOwn(parseIso(defaultValue) ? defaultValue : null);
76
+ setDirty(false);
77
+ setBadText(false);
78
+ };
79
+ f.addEventListener("reset", onReset);
80
+ return () => f.removeEventListener("reset", onReset);
81
+ }, [name, form, controlled, defaultValue]);
82
+ const commit = (v) => {
83
+ if (!controlled) setOwn(v);
84
+ onValueChange?.(v);
85
+ };
86
+ const commitText = () => {
87
+ if (!dirty) return;
88
+ if (text.trim() === "") {
89
+ if (value !== null) commit(null);
90
+ setDirty(false);
91
+ setBadText(false);
92
+ return;
93
+ }
94
+ const iso = parseDateField(text, loc);
95
+ if (iso && inRange(iso, min, max) && isDateDisabled?.(iso) !== true) {
96
+ if (iso !== value) commit(iso);
97
+ setDirty(false);
98
+ setBadText(false);
99
+ return;
100
+ }
101
+ setBadText(true);
102
+ };
103
+ const pickFromCalendar = (iso) => {
104
+ commit(iso);
105
+ setDirty(false);
106
+ setBadText(false);
107
+ setOpen(false);
108
+ };
109
+ const onKeyDown = (e) => {
110
+ if (disabled) return;
111
+ if (e.key === "Enter") {
112
+ if (dirty) {
113
+ e.preventDefault();
114
+ commitText();
115
+ }
116
+ } else if (e.key === "ArrowDown" && e.altKey) {
117
+ e.preventDefault();
118
+ setOpen(true);
119
+ } else if (e.key === "Escape" && dirty) {
120
+ e.preventDefault();
121
+ e.stopPropagation();
122
+ setDirty(false);
123
+ setBadText(false);
124
+ }
125
+ };
126
+ const buttonLabel = calendarLabel ?? (value ? strings.changeDate(fullDate(value, loc)) : strings.chooseDate);
127
+ return /* @__PURE__ */ jsxs(
128
+ "div",
129
+ {
130
+ ...rest,
131
+ "data-state": open ? "open" : "closed",
132
+ "data-disabled": disabled ? "" : void 0,
133
+ className: cn("relative w-full min-w-0", className),
134
+ children: [
135
+ /* @__PURE__ */ jsx(
136
+ "input",
137
+ {
138
+ ref: (node) => {
139
+ inputRef.current = node;
140
+ setRef(ref, node);
141
+ },
142
+ id: inputId,
143
+ type: "text",
144
+ inputMode: "numeric",
145
+ autoComplete: "off",
146
+ spellCheck: false,
147
+ value: shown,
148
+ placeholder: placeholder ?? datePlaceholder(loc, {
149
+ day: strings.dayToken,
150
+ month: strings.monthToken,
151
+ year: strings.yearToken
152
+ }),
153
+ disabled,
154
+ required,
155
+ "aria-label": ariaLabel,
156
+ "aria-labelledby": ariaLabelledBy,
157
+ "aria-describedby": ariaDescribedBy,
158
+ "aria-invalid": isInvalid || void 0,
159
+ "aria-required": required || void 0,
160
+ onChange: (e) => {
161
+ setText(e.target.value);
162
+ setDirty(true);
163
+ },
164
+ onBlur: commitText,
165
+ onKeyDown,
166
+ className: fieldClass({
167
+ size,
168
+ invalid: isInvalid,
169
+ className: "pe-11 tabular-nums"
170
+ })
171
+ }
172
+ ),
173
+ name != null ? /* @__PURE__ */ jsx("input", { type: "hidden", name, value: value ?? "", form, disabled }) : null,
174
+ /* @__PURE__ */ jsx(
175
+ "button",
176
+ {
177
+ ref: buttonRef,
178
+ type: "button",
179
+ "aria-label": buttonLabel,
180
+ "aria-haspopup": "dialog",
181
+ "aria-expanded": open,
182
+ disabled,
183
+ onClick: () => setOpen((o) => !o),
184
+ className: "absolute inset-y-0 end-0 flex w-11 items-center justify-center text-th-hi3 hover:text-th-hi disabled:pointer-events-none disabled:opacity-50",
185
+ children: /* @__PURE__ */ jsx("span", { className: "flex size-8 items-center justify-center rounded-md hover:bg-th-elevated", children: /* @__PURE__ */ jsx(CalendarIcon, { size: 16 }) })
186
+ }
187
+ ),
188
+ /* @__PURE__ */ jsx(Popover, { open, onOpenChange: (o) => setOpen(o), anchor: buttonRef, align: "end", "aria-label": strings.chooseDate, className: "p-3", children: /* @__PURE__ */ jsx(
189
+ Calendar,
190
+ {
191
+ value,
192
+ onValueChange: pickFromCalendar,
193
+ min: minProp,
194
+ max: maxProp,
195
+ isDateDisabled,
196
+ locale: loc,
197
+ weekStartsOn,
198
+ captionLayout,
199
+ autoFocus: true
200
+ }
201
+ ) })
202
+ ]
203
+ }
204
+ );
205
+ }
206
+ export {
207
+ DatePicker
208
+ };
@@ -103,27 +103,24 @@ var LEGEND_FILL = {
103
103
  loss: "bg-th-loss-fill",
104
104
  info: "bg-th-info-fill"
105
105
  };
106
- var displayInternals = {
107
- FILL,
108
- CALLOUT_TONE,
106
+ var FORCED_MARK = "forced-colors:forced-color-adjust-none forced-colors:bg-[Highlight] forced-colors:text-[HighlightText]";
107
+ export {
109
108
  CALLOUT_CLAMP,
109
+ CALLOUT_TONE,
110
110
  DOT_SIZE,
111
- HALO,
112
- STAT_VALUE_SIZE,
113
- STAT_SKELETON,
114
- STAT_TONE,
111
+ FILL,
112
+ FORCED_MARK,
115
113
  GRID_BASE,
116
114
  GRID_MD,
115
+ HALO,
116
+ LEGEND_FILL,
117
117
  METER_FILL,
118
118
  METER_THICK,
119
119
  METER_THICK_V,
120
120
  SPARK_TONE,
121
- LEGEND_FILL
122
- };
123
- var FORCED_MARK = "forced-colors:forced-color-adjust-none forced-colors:bg-[Highlight] forced-colors:text-[HighlightText]";
124
- export {
125
- FORCED_MARK,
126
- displayInternals,
121
+ STAT_SKELETON,
122
+ STAT_TONE,
123
+ STAT_VALUE_SIZE,
127
124
  signTone,
128
125
  toneFor,
129
126
  warnDev
@@ -13,33 +13,36 @@ import { cn } from "../cn.js";
13
13
  import { sparklineGeometry } from "../sparkline-path.js";
14
14
  import { safeUrl } from "../url.js";
15
15
  import { mergeRefs } from "./data-classes.js";
16
- import { displayInternals, FORCED_MARK, toneFor, warnDev } from "./display-classes.js";
17
- import { Skeleton } from "./feedback.js";
18
- import { AlertTriangleIcon, CheckCircleIcon, ChevronDownIcon, InfoIcon, XIcon } from "./icons.js";
19
- import { useHeightVar, useStuck } from "./layout-vars.js";
20
- import { DropdownMenu, Tooltip } from "./overlay.js";
21
- import { Badge, Button, Card, IconButton } from "./primitives.js";
22
- import { useNishStrings } from "./provider.js";
23
- import { ThemeToggle } from "./theme-toggle.js";
24
- import { signTone, toneFor as toneFor2 } from "./display-classes.js";
25
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
26
- var {
27
- FILL,
28
- CALLOUT_TONE,
16
+ import {
29
17
  CALLOUT_CLAMP,
18
+ CALLOUT_TONE,
30
19
  DOT_SIZE,
31
- HALO,
32
- STAT_VALUE_SIZE,
33
- STAT_SKELETON,
34
- STAT_TONE,
20
+ FILL,
21
+ FORCED_MARK,
35
22
  GRID_BASE,
36
23
  GRID_MD,
24
+ HALO,
25
+ LEGEND_FILL,
37
26
  METER_FILL,
38
27
  METER_THICK,
39
28
  METER_THICK_V,
40
29
  SPARK_TONE,
41
- LEGEND_FILL
42
- } = displayInternals;
30
+ STAT_SKELETON,
31
+ STAT_TONE,
32
+ STAT_VALUE_SIZE,
33
+ toneFor,
34
+ warnDev
35
+ } from "./display-classes.js";
36
+ import { Skeleton } from "./feedback.js";
37
+ import { AlertTriangleIcon, CheckCircleIcon, ChevronDownIcon, InfoIcon, XIcon } from "./icons.js";
38
+ import { useHeightVar, useStuck } from "./layout-vars.js";
39
+ import { DropdownMenu } from "./menu.js";
40
+ import { Tooltip } from "./tooltip.js";
41
+ import { Badge, Button, Card, IconButton } from "./primitives.js";
42
+ import { useNishStrings } from "./provider.js";
43
+ import { ThemeToggle } from "./theme-toggle.js";
44
+ import { signTone, toneFor as toneFor2 } from "./display-classes.js";
45
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
43
46
  function Svg({ size = 16, children, ...rest }) {
44
47
  return /* @__PURE__ */ jsx(
45
48
  "svg",
@@ -0,0 +1,139 @@
1
+ "use client";
2
+
3
+ // src/components/drawer.tsx
4
+ import { useEffect, useId, useRef } from "react";
5
+ import { cn } from "../cn.js";
6
+ import { XIcon } from "./icons.js";
7
+ import { IconButton } from "./primitives.js";
8
+ import { Portal } from "./portal.js";
9
+ import { useNishStrings } from "./provider.js";
10
+ import { DRAWER_SHEET_HEIGHT, DRAWER_WIDTH } from "./overlay-classes.js";
11
+ import { warnDev, useMergedRef, useScrollable } from "./overlay-core.js";
12
+ import { useDialog, useSheetExit, useSheetSwipe, SheetHandle } from "./overlay-dialog.js";
13
+ import { jsx, jsxs } from "react/jsx-runtime";
14
+ function Drawer({ open, ...props }) {
15
+ if (open === false) return null;
16
+ return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx(DrawerPanel, { ...props }) });
17
+ }
18
+ function DrawerPanel({
19
+ title,
20
+ subtitle,
21
+ onClose,
22
+ onOpenChange,
23
+ side = "right",
24
+ size = "md",
25
+ actions,
26
+ footer,
27
+ bottomOffset,
28
+ dismissible = true,
29
+ closeOnBack = true,
30
+ canClose,
31
+ "aria-label": ariaLabel,
32
+ "aria-labelledby": ariaLabelledBy,
33
+ children,
34
+ className,
35
+ ref,
36
+ ...rest
37
+ }) {
38
+ const strings = useNishStrings();
39
+ const titleId = useId();
40
+ const [panelRef, setPanelRef] = useMergedRef(ref);
41
+ const scrimRef = useRef(null);
42
+ const bodyRef = useRef(null);
43
+ const { closing, requestClose, closeNow, isClosing, allowClose } = useSheetExit(onClose, canClose, onOpenChange);
44
+ useDialog(panelRef, requestClose, closeOnBack);
45
+ const bottom = side === "bottom";
46
+ const swipe = useSheetSwipe(panelRef, scrimRef, () => bottom && dismissible && !isClosing(), closeNow, allowClose);
47
+ const swipeHeader = bottom && dismissible;
48
+ const hasTitle = title !== void 0 && title !== null;
49
+ const named = hasTitle || !!ariaLabel || !!ariaLabelledBy;
50
+ const scrollable = useScrollable(bodyRef);
51
+ const bodyLabel = typeof title === "string" ? title : ariaLabel;
52
+ useEffect(() => {
53
+ if (!named) warnDev("nish-ui: <Drawer> has no accessible name. Pass `title`, `aria-label` or `aria-labelledby`.");
54
+ }, [named]);
55
+ return /* @__PURE__ */ jsx(
56
+ "div",
57
+ {
58
+ ref: scrimRef,
59
+ "data-nish-layer": "",
60
+ "data-state": closing ? "closed" : "open",
61
+ className: cn(
62
+ "fixed inset-0 z-[var(--nish-z-modal,50)] bg-th-scrim/50",
63
+ closing ? "pointer-events-none animate-[nish-fade-out_200ms_ease-in_forwards]" : "animate-[nish-fade-in_160ms_ease-out]"
64
+ ),
65
+ style: bottom && bottomOffset ? { bottom: bottomOffset } : void 0,
66
+ onMouseDown: (e) => {
67
+ if (e.target === e.currentTarget) requestClose();
68
+ },
69
+ children: /* @__PURE__ */ jsxs(
70
+ "div",
71
+ {
72
+ ...rest,
73
+ ref: setPanelRef,
74
+ role: "dialog",
75
+ "aria-modal": "true",
76
+ "aria-labelledby": ariaLabelledBy ?? (ariaLabel || !hasTitle ? void 0 : titleId),
77
+ "aria-label": ariaLabel,
78
+ "data-state": closing ? "closed" : "open",
79
+ tabIndex: -1,
80
+ className: cn(
81
+ "absolute flex flex-col border-th-line bg-th-surface shadow-modal outline-none",
82
+ bottom ? cn(
83
+ "inset-x-0 bottom-0 rounded-t-2xl border-t",
84
+ closing ? "animate-[nish-sheet-out_200ms_ease-in_forwards]" : "animate-[nish-sheet-in_240ms_var(--nish-ease-out)]",
85
+ bottomOffset ? "" : "pb-[env(safe-area-inset-bottom)]",
86
+ DRAWER_SHEET_HEIGHT[size]
87
+ ) : cn(
88
+ "inset-y-0 w-full pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]",
89
+ DRAWER_WIDTH[size],
90
+ side === "right" ? cn(
91
+ "right-0 border-l pr-[env(safe-area-inset-right)]",
92
+ closing ? "translate-x-full transition-transform duration-200 ease-in" : "animate-[nish-slide-in-right_240ms_var(--nish-ease-out)]"
93
+ ) : cn(
94
+ "left-0 border-r pl-[env(safe-area-inset-left)]",
95
+ closing ? "-translate-x-full transition-transform duration-200 ease-in" : "animate-[nish-slide-in-left_240ms_var(--nish-ease-out)]"
96
+ )
97
+ ),
98
+ className
99
+ ),
100
+ children: [
101
+ swipeHeader ? /* @__PURE__ */ jsx(SheetHandle, { onClose: requestClose, swipe }) : bottom ? /* @__PURE__ */ jsx("div", { className: "h-3 shrink-0", "aria-hidden": "true" }) : null,
102
+ /* @__PURE__ */ jsxs(
103
+ "div",
104
+ {
105
+ className: cn("flex shrink-0 items-start justify-between gap-3 border-b border-th-line px-5 py-3", swipeHeader && "touch-pan-x"),
106
+ ...swipeHeader ? swipe : {},
107
+ children: [
108
+ /* @__PURE__ */ jsxs("div", { className: "min-w-0 pt-1", children: [
109
+ hasTitle ? /* @__PURE__ */ jsx("h2", { id: titleId, className: "line-clamp-2 break-words text-base font-semibold leading-snug text-th-hi", children: title }) : null,
110
+ subtitle ? /* @__PURE__ */ jsx("div", { className: "mt-0.5 text-xs text-th-hi3", children: subtitle }) : null
111
+ ] }),
112
+ /* @__PURE__ */ jsxs("div", { className: "-me-1.5 flex shrink-0 items-center gap-1", children: [
113
+ actions,
114
+ /* @__PURE__ */ jsx(IconButton, { label: strings.close, onClick: requestClose, children: /* @__PURE__ */ jsx(XIcon, { size: 16 }) })
115
+ ] })
116
+ ]
117
+ }
118
+ ),
119
+ /* @__PURE__ */ jsx(
120
+ "div",
121
+ {
122
+ ref: bodyRef,
123
+ tabIndex: scrollable ? 0 : void 0,
124
+ role: scrollable && bodyLabel ? "region" : void 0,
125
+ "aria-label": scrollable ? bodyLabel : void 0,
126
+ className: "min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 py-4 text-sm text-th-hi2",
127
+ children
128
+ }
129
+ ),
130
+ 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
131
+ ]
132
+ }
133
+ )
134
+ }
135
+ );
136
+ }
137
+ export {
138
+ Drawer
139
+ };
@@ -5,7 +5,7 @@ import { useState } from "react";
5
5
  import { cn } from "../cn.js";
6
6
  import { SearchInput } from "./forms.js";
7
7
  import { NARROW_QUERY, useMediaQuery } from "./hooks.js";
8
- import { Drawer } from "./overlay.js";
8
+ import { Drawer } from "./drawer.js";
9
9
  import { Badge, Button } from "./primitives.js";
10
10
  import { useNishStrings } from "./provider.js";
11
11
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
@@ -12,7 +12,7 @@ import { cn } from "../cn.js";
12
12
  import { ago } from "../format.js";
13
13
  import { useIsoLayoutEffect } from "./client.js";
14
14
  import { useMediaQuery, useNow } from "./hooks.js";
15
- import { Tooltip } from "./overlay.js";
15
+ import { Tooltip } from "./tooltip.js";
16
16
  import { Badge } from "./primitives.js";
17
17
  import { useNishStrings } from "./provider.js";
18
18
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
@@ -0,0 +1,221 @@
1
+ "use client";
2
+
3
+ // src/components/hover-card.tsx
4
+ import {
5
+ Children,
6
+ cloneElement,
7
+ isValidElement,
8
+ useCallback,
9
+ useEffect,
10
+ useId,
11
+ useMemo,
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 { mergeRefs, warnOnce } from "./data-classes.js";
20
+ import { useLatest } from "./latest.js";
21
+ import { place } from "./overlay-position.js";
22
+ import { keyboardFocused, touchTracker } from "./overlay-classes.js";
23
+ import { useNishConfig } from "./provider.js";
24
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
25
+ var openCard = () => shared("hoverCard", () => ({ close: null }));
26
+ function useCardPosition(anchor, card, open, side, align) {
27
+ const [pos, setPos] = useState(null);
28
+ useIsoLayoutEffect(() => {
29
+ if (!open) {
30
+ setPos(null);
31
+ return;
32
+ }
33
+ const update = () => {
34
+ const a = anchor.current;
35
+ const f = card.current;
36
+ if (!a || !f) return;
37
+ const naturalH = f.scrollHeight + (f.offsetHeight - f.clientHeight);
38
+ const next = place(a.getBoundingClientRect(), f.offsetWidth, Math.max(f.offsetHeight, naturalH), side, align, 8, isRtl(a));
39
+ setPos(
40
+ (prev) => prev && prev.top === next.top && prev.left === next.left && prev.side === next.side && prev.maxHeight === next.maxHeight ? prev : next
41
+ );
42
+ };
43
+ update();
44
+ window.addEventListener("resize", update);
45
+ window.addEventListener("scroll", update, true);
46
+ const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update);
47
+ if (card.current) ro?.observe(card.current);
48
+ if (anchor.current) ro?.observe(anchor.current);
49
+ return () => {
50
+ window.removeEventListener("resize", update);
51
+ window.removeEventListener("scroll", update, true);
52
+ ro?.disconnect();
53
+ };
54
+ }, [anchor, card, open, side, align]);
55
+ return open ? pos : null;
56
+ }
57
+ function HoverCard({
58
+ children,
59
+ content,
60
+ side = "bottom",
61
+ align = "center",
62
+ openDelay = 500,
63
+ closeDelay = 300,
64
+ open: openProp,
65
+ defaultOpen,
66
+ onOpenChange,
67
+ width = 320,
68
+ className,
69
+ ref,
70
+ style,
71
+ onPointerEnter,
72
+ onPointerLeave,
73
+ ...rest
74
+ }) {
75
+ const [openState, setOpenState] = useState(defaultOpen ?? false);
76
+ const open = openProp ?? openState;
77
+ const config = useNishConfig();
78
+ const isClient = useIsClient();
79
+ const autoId = useId();
80
+ const id = rest.id ?? autoId;
81
+ const wrapperRef = useRef(null);
82
+ const cardRef = useRef(null);
83
+ const timer = useRef(0);
84
+ const latest = useLatest({ open, onOpenChange, controlled: openProp !== void 0 });
85
+ const child = Children.count(children) === 1 && isValidElement(children) ? children : null;
86
+ const valid = child !== null;
87
+ useEffect(() => {
88
+ if (!valid) warnOnce("hovercard-child", "HoverCard: children must be one element, such as a link.");
89
+ }, [valid]);
90
+ const apply = useCallback(
91
+ (next) => {
92
+ const cur = latest.current;
93
+ if (next === cur.open) return;
94
+ if (!cur.controlled) setOpenState(next);
95
+ cur.onOpenChange?.(next);
96
+ },
97
+ [latest]
98
+ );
99
+ const show = useCallback(() => {
100
+ window.clearTimeout(timer.current);
101
+ timer.current = window.setTimeout(() => apply(true), openDelay);
102
+ }, [apply, openDelay]);
103
+ const hide = useCallback(() => {
104
+ window.clearTimeout(timer.current);
105
+ timer.current = window.setTimeout(() => apply(false), closeDelay);
106
+ }, [apply, closeDelay]);
107
+ const stay = useCallback(() => window.clearTimeout(timer.current), []);
108
+ const closeNow = useCallback(() => {
109
+ window.clearTimeout(timer.current);
110
+ apply(false);
111
+ }, [apply]);
112
+ useEffect(() => {
113
+ const el = wrapperRef.current;
114
+ if (!el) return;
115
+ const touch = touchTracker();
116
+ const onPointerEnterTrigger = (e) => touch.onPointer(e);
117
+ const onPointerDown = (e) => {
118
+ touch.onPointer(e);
119
+ if (e.pointerType === "touch") closeNow();
120
+ };
121
+ const onMouseEnter = () => {
122
+ if (!touch.recent()) show();
123
+ };
124
+ const onMouseLeave = (e) => {
125
+ if (e.relatedTarget instanceof Node && cardRef.current?.contains(e.relatedTarget)) return;
126
+ const active = document.activeElement;
127
+ if (active && el.contains(active) && keyboardFocused(active)) return;
128
+ hide();
129
+ };
130
+ const onFocusIn = (e) => {
131
+ if (e.target instanceof Element && keyboardFocused(e.target)) show();
132
+ };
133
+ el.addEventListener("pointerenter", onPointerEnterTrigger);
134
+ el.addEventListener("pointerdown", onPointerDown);
135
+ el.addEventListener("mouseenter", onMouseEnter);
136
+ el.addEventListener("mouseleave", onMouseLeave);
137
+ el.addEventListener("focusin", onFocusIn);
138
+ el.addEventListener("focusout", hide);
139
+ return () => {
140
+ window.clearTimeout(timer.current);
141
+ el.removeEventListener("pointerenter", onPointerEnterTrigger);
142
+ el.removeEventListener("pointerdown", onPointerDown);
143
+ el.removeEventListener("mouseenter", onMouseEnter);
144
+ el.removeEventListener("mouseleave", onMouseLeave);
145
+ el.removeEventListener("focusin", onFocusIn);
146
+ el.removeEventListener("focusout", hide);
147
+ };
148
+ }, [valid, show, hide, closeNow]);
149
+ useEffect(() => {
150
+ if (!open) return;
151
+ const onKey = (e) => {
152
+ if (e.key !== "Escape") return;
153
+ e.stopPropagation();
154
+ closeNow();
155
+ };
156
+ document.addEventListener("keydown", onKey, true);
157
+ return () => document.removeEventListener("keydown", onKey, true);
158
+ }, [open, closeNow]);
159
+ useEffect(() => {
160
+ if (!open) return;
161
+ const slot = openCard();
162
+ if (slot.close && slot.close !== closeNow) slot.close();
163
+ slot.close = closeNow;
164
+ return () => {
165
+ if (slot.close === closeNow) slot.close = null;
166
+ };
167
+ }, [open, closeNow]);
168
+ const [host, setHost] = useState(null);
169
+ useIsoLayoutEffect(() => {
170
+ setHost(open ? wrapperRef.current?.closest("[data-nish-layer]") ?? config.portalContainer ?? document.body : null);
171
+ }, [open, config.portalContainer]);
172
+ const cardRefs = useMemo(() => mergeRefs(cardRef, ref), [ref]);
173
+ const pos = useCardPosition(wrapperRef, cardRef, open && isClient && valid && host !== null, side, align);
174
+ if (!child) return /* @__PURE__ */ jsx(Fragment, { children });
175
+ const existing = child.props["aria-describedby"];
176
+ const describedby = open ? [typeof existing === "string" ? existing : "", id].filter(Boolean).join(" ") : existing;
177
+ const trigger = cloneElement(child, { "aria-describedby": describedby || void 0 });
178
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
179
+ /* @__PURE__ */ jsx("span", { ref: wrapperRef, className: "inline-flex", "data-state": open ? "open" : "closed", children: trigger }),
180
+ open && isClient && host ? createPortal(
181
+ /* @__PURE__ */ jsx(
182
+ "div",
183
+ {
184
+ ...rest,
185
+ ref: cardRefs,
186
+ id,
187
+ "data-side": pos?.side,
188
+ "data-state": "open",
189
+ onPointerEnter: (e) => {
190
+ stay();
191
+ onPointerEnter?.(e);
192
+ },
193
+ onPointerLeave: (e) => {
194
+ hide();
195
+ onPointerLeave?.(e);
196
+ },
197
+ style: {
198
+ ...style,
199
+ position: "fixed",
200
+ top: pos?.top ?? 0,
201
+ left: pos?.left ?? 0,
202
+ visibility: pos ? void 0 : "hidden",
203
+ width,
204
+ maxWidth: "calc(100vw - 1rem)",
205
+ maxHeight: pos?.maxHeight,
206
+ zIndex: "var(--nish-z-tooltip, 100)"
207
+ },
208
+ className: cn(
209
+ "overflow-y-auto overscroll-contain rounded-lg border border-th-line bg-th-elevated p-3 text-sm text-th-hi2 shadow-pop animate-[nish-fade-in_120ms_ease-out] motion-reduce:animate-none",
210
+ className
211
+ ),
212
+ children: content
213
+ }
214
+ ),
215
+ host
216
+ ) : null
217
+ ] });
218
+ }
219
+ export {
220
+ HoverCard
221
+ };