@hamsterbase/ui 0.1.0 → 0.3.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/dist/components/Button/Button.d.ts +5 -3
- package/dist/components/Button/styles.d.ts +3 -3
- package/dist/components/Checkbox/Checkbox.d.ts +3 -3
- package/dist/components/Divider/Divider.d.ts +10 -6
- package/dist/components/Divider/styles.d.ts +8 -4
- package/dist/components/IconButton/IconButton.d.ts +7 -7
- package/dist/components/IconButton/styles.d.ts +3 -3
- package/dist/components/ResizeHandle/ResizeHandle.d.ts +7 -4
- package/dist/components/ResizeHandle/styles.d.ts +6 -4
- package/dist/components/Select/Select.d.ts +17 -0
- package/dist/components/Select/layout.d.ts +39 -0
- package/dist/components/Select/styles.d.ts +26 -0
- package/dist/components/Select/useScrollAffordance.d.ts +12 -0
- package/dist/components/Select/useSelectValue.d.ts +15 -0
- package/dist/components/Toast/Toast.d.ts +17 -0
- package/dist/components/Toast/ToastStore.d.ts +33 -0
- package/dist/components/Toast/Toaster.d.ts +7 -0
- package/dist/components/Toast/styles.d.ts +15 -0
- package/dist/components/Tooltip/Tooltip.d.ts +10 -0
- package/dist/components/Tooltip/coordinator.d.ts +79 -0
- package/dist/components/Tooltip/layout.d.ts +28 -0
- package/dist/components/Tooltip/styles.d.ts +6 -0
- package/dist/index.d.ts +13 -3
- package/dist/index.js +1265 -58
- package/dist/styles.css +776 -115
- package/dist/utils/css.d.ts +6 -0
- package/dist/utils/focus.d.ts +4 -0
- package/docs/Checkbox.md +2 -0
- package/docs/IconButton.md +1 -1
- package/docs/ResizeHandle.md +2 -1
- package/docs/Select.md +20 -0
- package/docs/Toast.md +60 -0
- package/docs/Tooltip.md +20 -0
- package/docs/index.md +3 -0
- package/package.json +10 -8
package/dist/index.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
+
"use client";
|
|
1
2
|
import { clsx } from "clsx/lite";
|
|
2
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
-
import {
|
|
4
|
-
import { Check, Minus } from "lucide-react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { cloneElement, useCallback, useEffect, useId, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
5
|
+
import { Check, ChevronDown, ChevronUp, CircleAlert, Info, Minus, X } from "lucide-react";
|
|
6
|
+
// src/utils/focus.ts
|
|
7
|
+
/** 元素自身获得键盘焦点时。 */
|
|
8
|
+
var focusRing = "focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-hub-focus-ring";
|
|
9
|
+
/** 前一个兄弟节点(peer,如隐藏的原生 input)获得键盘焦点时。 */
|
|
10
|
+
var peerFocusRing = "peer-focus-visible:outline-2 peer-focus-visible:outline-solid peer-focus-visible:outline-offset-2 peer-focus-visible:outline-hub-focus-ring";
|
|
5
11
|
// src/components/Button/styles.ts
|
|
6
12
|
var buttonStyles = {
|
|
7
|
-
|
|
13
|
+
Root: clsx("inline-flex items-center justify-center gap-1.5", "select-none whitespace-nowrap border rounded-full", "font-sans font-medium leading-4", "transition-colors duration-(--hub-duration-normal) motion-reduce:transition-none", focusRing, "disabled:cursor-not-allowed disabled:opacity-50"),
|
|
8
14
|
variants: {
|
|
9
|
-
primary: "border-transparent bg-hub-primary-solid text-hub-primary-contrast enabled:hover:bg-hub-primary-solid
|
|
15
|
+
primary: "border-transparent bg-hub-primary-solid text-hub-primary-contrast enabled:hover:bg-hub-primary-solid-hover",
|
|
10
16
|
secondary: "border-hub-border bg-hub-bg-panel text-hub-fg enabled:hover:bg-hub-bg-muted",
|
|
11
17
|
ghost: "border-transparent bg-transparent text-hub-fg enabled:hover:bg-hub-bg-muted",
|
|
12
|
-
danger: "border-transparent bg-hub-error-solid text-hub-error-contrast enabled:hover:bg-hub-error-solid
|
|
18
|
+
danger: "border-transparent bg-hub-error-solid text-hub-error-contrast enabled:hover:bg-hub-error-solid-hover"
|
|
13
19
|
},
|
|
14
20
|
sizes: {
|
|
15
21
|
sm: "h-6 px-2.5 text-hub-sm",
|
|
@@ -22,43 +28,47 @@ function Button({ variant = "primary", size = "md", type = "button", className,
|
|
|
22
28
|
return /* @__PURE__ */ jsx("button", {
|
|
23
29
|
...props,
|
|
24
30
|
type,
|
|
25
|
-
className: clsx(buttonStyles.
|
|
31
|
+
className: clsx(buttonStyles.Root, buttonStyles.variants[variant], buttonStyles.sizes[size], className)
|
|
26
32
|
});
|
|
27
33
|
}
|
|
28
34
|
// src/components/IconButton/styles.ts
|
|
29
35
|
var iconButtonStyles = {
|
|
30
|
-
|
|
36
|
+
Root: clsx("inline-grid flex-none select-none place-items-center rounded-full border-0", "transition-colors duration-(--hub-duration-fast) motion-reduce:transition-none", focusRing, "disabled:cursor-not-allowed disabled:opacity-50", "aria-pressed:bg-hub-bg-muted aria-pressed:text-hub-fg", "aria-pressed:enabled:hover:bg-hub-bg-emphasized aria-pressed:enabled:active:bg-hub-bg-emphasized"),
|
|
31
37
|
sizes: {
|
|
32
38
|
sm: "size-6 [&_svg]:size-3.5",
|
|
33
39
|
md: "size-7 [&_svg]:size-3.5",
|
|
34
40
|
lg: "size-8 [&_svg]:size-4"
|
|
35
41
|
},
|
|
36
42
|
variants: {
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
surface: clsx("bg-hub-bg-panel text-hub-fg shadow-hub-sm", "enabled:hover:bg-hub-bg-muted enabled:active:bg-hub-bg-emphasized"),
|
|
44
|
+
ghost: clsx("bg-transparent text-hub-fg-muted", "enabled:hover:bg-hub-bg-muted enabled:hover:text-hub-fg enabled:active:bg-hub-bg-muted")
|
|
39
45
|
}
|
|
40
46
|
};
|
|
41
47
|
// src/components/IconButton/IconButton.tsx
|
|
42
|
-
function IconButton({ className, size = "md", type = "button", variant = "
|
|
48
|
+
function IconButton({ className, size = "md", type = "button", variant = "surface", pressed, ...props }) {
|
|
43
49
|
return /* @__PURE__ */ jsx("button", {
|
|
44
50
|
...props,
|
|
45
51
|
type,
|
|
46
52
|
"aria-pressed": pressed,
|
|
47
|
-
className: clsx(iconButtonStyles.
|
|
53
|
+
className: clsx(iconButtonStyles.Root, iconButtonStyles.sizes[size], iconButtonStyles.variants[variant], className)
|
|
48
54
|
});
|
|
49
55
|
}
|
|
50
56
|
// src/components/Checkbox/styles.ts
|
|
51
57
|
var checkboxStyles = {
|
|
52
58
|
Root: "group/checkbox relative inline-flex size-3.5 flex-none align-middle",
|
|
53
|
-
Input: clsx("peer absolute inset-0 z-
|
|
54
|
-
Surface: clsx("pointer-events-none box-border grid size-full place-items-center rounded-
|
|
59
|
+
Input: clsx("peer absolute inset-0 z-(--hub-z-raised) m-0 size-full cursor-pointer opacity-0", "disabled:cursor-default"),
|
|
60
|
+
Surface: clsx("pointer-events-none box-border grid size-full place-items-center rounded-hub-sm border", "border-hub-border-strong bg-transparent text-hub-primary-contrast", "transition-colors duration-(--hub-duration-fast) ease-out motion-reduce:transition-none", "peer-checked:border-hub-primary-solid peer-checked:bg-hub-primary-solid", "peer-indeterminate:border-hub-primary-solid peer-indeterminate:bg-hub-primary-solid", peerFocusRing, "peer-disabled:opacity-50"),
|
|
55
61
|
Check: "hidden [grid-area:1/1] group-has-[:checked:not(:indeterminate)]/checkbox:block",
|
|
56
62
|
Mixed: "hidden [grid-area:1/1] group-has-[:indeterminate]/checkbox:block"
|
|
57
63
|
};
|
|
58
64
|
// src/components/Checkbox/Checkbox.tsx
|
|
59
|
-
|
|
65
|
+
function Checkbox({ indeterminate = false, className, ref, ...props }) {
|
|
60
66
|
const input = useRef(null);
|
|
61
|
-
|
|
67
|
+
const setInput = useCallback((node) => {
|
|
68
|
+
input.current = node;
|
|
69
|
+
if (typeof ref === "function") return ref(node);
|
|
70
|
+
if (ref) ref.current = node;
|
|
71
|
+
}, [ref]);
|
|
62
72
|
useLayoutEffect(() => {
|
|
63
73
|
if (input.current) input.current.indeterminate = indeterminate;
|
|
64
74
|
}, [indeterminate]);
|
|
@@ -66,7 +76,7 @@ var Checkbox = forwardRef(function Checkbox({ indeterminate = false, className,
|
|
|
66
76
|
className: clsx(checkboxStyles.Root, className),
|
|
67
77
|
children: [/* @__PURE__ */ jsx("input", {
|
|
68
78
|
...props,
|
|
69
|
-
ref:
|
|
79
|
+
ref: setInput,
|
|
70
80
|
type: "checkbox",
|
|
71
81
|
className: checkboxStyles.Input
|
|
72
82
|
}), /* @__PURE__ */ jsxs("span", {
|
|
@@ -83,68 +93,82 @@ var Checkbox = forwardRef(function Checkbox({ indeterminate = false, className,
|
|
|
83
93
|
})]
|
|
84
94
|
})]
|
|
85
95
|
});
|
|
86
|
-
}
|
|
96
|
+
}
|
|
87
97
|
// src/components/Divider/styles.ts
|
|
88
98
|
var dividerStyles = {
|
|
89
99
|
Root: "pointer-events-none block flex-none",
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
orientations: {
|
|
101
|
+
horizontal: "h-(--hub-hairline) w-full",
|
|
102
|
+
vertical: "w-(--hub-hairline) self-stretch"
|
|
103
|
+
},
|
|
104
|
+
tones: {
|
|
105
|
+
default: "bg-hub-border",
|
|
106
|
+
subtle: "bg-hub-border-subtle"
|
|
107
|
+
}
|
|
94
108
|
};
|
|
95
109
|
// src/components/Divider/Divider.tsx
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const geometry = edge ? {
|
|
110
|
+
/** 贴边:绝对定位到容器的一条边,inset 为两端内缩。 */
|
|
111
|
+
function edgeGeometry(edge, horizontal, inset, length) {
|
|
112
|
+
return horizontal ? {
|
|
100
113
|
[edge]: 0,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
width: length ?? "auto"
|
|
105
|
-
} : {
|
|
106
|
-
top: inset,
|
|
107
|
-
bottom: inset,
|
|
108
|
-
height: length
|
|
109
|
-
}
|
|
114
|
+
left: inset,
|
|
115
|
+
right: inset,
|
|
116
|
+
width: length ?? "auto"
|
|
110
117
|
} : {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
[edge]: 0,
|
|
119
|
+
top: inset,
|
|
120
|
+
bottom: inset,
|
|
121
|
+
height: length
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/** 文档流内:inset 为两端外边距;指定 length 时在交叉轴上居中。 */
|
|
125
|
+
function flowGeometry(horizontal, inset, length) {
|
|
126
|
+
const centered = length === void 0 ? {} : { alignSelf: "center" };
|
|
127
|
+
if (!horizontal) return {
|
|
128
|
+
marginBlock: inset,
|
|
129
|
+
height: length,
|
|
130
|
+
...centered
|
|
119
131
|
};
|
|
132
|
+
const fill = inset ? `calc(100% - ${inset * 2}px)` : void 0;
|
|
133
|
+
return {
|
|
134
|
+
marginInline: inset,
|
|
135
|
+
width: length ?? fill,
|
|
136
|
+
...centered
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function Divider({ orientation = "horizontal", tone = "default", edge, inset = 0, length, className, style, ...props }) {
|
|
140
|
+
const direction = edge ? edge === "left" || edge === "right" ? "vertical" : "horizontal" : orientation;
|
|
141
|
+
const horizontal = direction === "horizontal";
|
|
142
|
+
const geometry = edge ? edgeGeometry(edge, horizontal, inset, length) : flowGeometry(horizontal, inset, length);
|
|
120
143
|
return /* @__PURE__ */ jsx("span", {
|
|
121
144
|
...props,
|
|
122
|
-
ref,
|
|
123
145
|
"aria-hidden": "true",
|
|
124
146
|
"data-divider": "",
|
|
125
147
|
"data-orientation": direction,
|
|
126
148
|
"data-tone": tone,
|
|
127
149
|
"data-edge": edge,
|
|
128
|
-
className: clsx(dividerStyles.Root, dividerStyles[direction], dividerStyles[tone], edge && "absolute", className),
|
|
150
|
+
className: clsx(dividerStyles.Root, dividerStyles.orientations[direction], dividerStyles.tones[tone], edge && "absolute", className),
|
|
129
151
|
style: {
|
|
130
152
|
...geometry,
|
|
131
153
|
...style
|
|
132
154
|
}
|
|
133
155
|
});
|
|
134
|
-
}
|
|
156
|
+
}
|
|
135
157
|
// src/components/ResizeHandle/styles.ts
|
|
136
|
-
var line = "pointer-events-none absolute inset-y-0 left-[
|
|
158
|
+
var line = "pointer-events-none absolute inset-y-0 left-[calc((var(--hub-hit-area)_-_1px)_/_2)]";
|
|
137
159
|
var resizeHandleStyles = {
|
|
138
|
-
Root: clsx("group/resize absolute z-
|
|
139
|
-
|
|
140
|
-
|
|
160
|
+
Root: clsx("group/resize absolute z-(--hub-z-raised) w-(--hub-hit-area) touch-none select-none cursor-col-resize outline-none"),
|
|
161
|
+
edges: {
|
|
162
|
+
left: "left-[calc(var(--hub-hit-area)_/_-2)]",
|
|
163
|
+
right: "right-[calc(var(--hub-hit-area)_/_-2)]"
|
|
164
|
+
},
|
|
141
165
|
Line: line,
|
|
142
|
-
Feedback: clsx(line, "w-px origin-left scale-x-50 overflow-hidden bg-hub-border opacity-0", "[mask-image:linear-gradient(transparent,black_10%,black_90%,transparent)]", "transition-opacity duration-
|
|
166
|
+
Feedback: clsx(line, "w-px origin-left scale-x-50 overflow-hidden bg-hub-border opacity-0", "[mask-image:linear-gradient(transparent,black_10%,black_90%,transparent)]", "transition-opacity duration-(--hub-duration-slow) ease-in-out motion-reduce:transition-none", "group-hover/resize:opacity-100 group-data-[dragging=true]/resize:opacity-100", "group-focus-visible/resize:opacity-100"),
|
|
143
167
|
Gradient: clsx("absolute -top-1/2 left-0 h-[200%] w-full bg-hub-fg", "[mask-image:linear-gradient(transparent_5%,black_40%,black_60%,transparent_95%)]"),
|
|
144
|
-
DragShield: "fixed inset-0 z-
|
|
168
|
+
DragShield: "fixed inset-0 z-(--hub-z-overlay) cursor-col-resize"
|
|
145
169
|
};
|
|
146
170
|
// src/components/ResizeHandle/ResizeHandle.tsx
|
|
147
|
-
|
|
171
|
+
function ResizeHandle({ label, controls, value, min, max, defaultValue, step = 16, showDivider = true, inset = 0, edge = "right", onChange, className, style, ...props }) {
|
|
148
172
|
const sign = edge === "right" ? 1 : -1;
|
|
149
173
|
const drag = useRef(void 0);
|
|
150
174
|
const feedback = useRef(null);
|
|
@@ -156,7 +180,6 @@ var ResizeHandle = forwardRef(function ResizeHandle({ label, controls, value, mi
|
|
|
156
180
|
const resize = (width) => onChange(Math.max(min, Math.min(max, width)));
|
|
157
181
|
return /* @__PURE__ */ jsxs("div", {
|
|
158
182
|
...props,
|
|
159
|
-
ref,
|
|
160
183
|
role: "separator",
|
|
161
184
|
"aria-label": label,
|
|
162
185
|
"aria-controls": controls,
|
|
@@ -165,7 +188,7 @@ var ResizeHandle = forwardRef(function ResizeHandle({ label, controls, value, mi
|
|
|
165
188
|
"aria-valuemax": max,
|
|
166
189
|
"aria-valuenow": value,
|
|
167
190
|
tabIndex: 0,
|
|
168
|
-
className: clsx(resizeHandleStyles.Root, resizeHandleStyles[edge], className),
|
|
191
|
+
className: clsx(resizeHandleStyles.Root, resizeHandleStyles.edges[edge], className),
|
|
169
192
|
style: {
|
|
170
193
|
insetBlock: inset,
|
|
171
194
|
...style
|
|
@@ -176,7 +199,12 @@ var ResizeHandle = forwardRef(function ResizeHandle({ label, controls, value, mi
|
|
|
176
199
|
if (defaultValue !== void 0) resize(defaultValue);
|
|
177
200
|
},
|
|
178
201
|
onKeyDown: (event) => {
|
|
179
|
-
const width =
|
|
202
|
+
const width = {
|
|
203
|
+
Home: min,
|
|
204
|
+
End: max,
|
|
205
|
+
ArrowLeft: value - step * sign,
|
|
206
|
+
ArrowRight: value + step * sign
|
|
207
|
+
}[event.key];
|
|
180
208
|
if (width === void 0) return;
|
|
181
209
|
event.preventDefault();
|
|
182
210
|
event.stopPropagation();
|
|
@@ -235,5 +263,1184 @@ var ResizeHandle = forwardRef(function ResizeHandle({ label, controls, value, mi
|
|
|
235
263
|
}) : null
|
|
236
264
|
]
|
|
237
265
|
});
|
|
238
|
-
}
|
|
239
|
-
|
|
266
|
+
}
|
|
267
|
+
// src/components/Toast/styles.ts
|
|
268
|
+
var toastStyles = {
|
|
269
|
+
Viewport: clsx("fixed bottom-6 right-6 z-(--hub-z-toast)", "flex max-w-[calc(100vw_-_var(--hub-float-gutter)_*_2)] flex-col items-end gap-2"),
|
|
270
|
+
Root: clsx("animate-[hub-toast-in_var(--hub-duration-enter)_ease]", "data-closing:pointer-events-none data-closing:animate-[hub-toast-out_var(--hub-duration-exit)_ease_forwards]", "motion-reduce:animate-none motion-reduce:data-closing:animate-none"),
|
|
271
|
+
Card: clsx("box-border flex w-fit max-w-[min(var(--hub-float-width),calc(100vw_-_var(--hub-float-gutter)_*_2))] items-start gap-2", "rounded-hub-lg border-(length:--hub-hairline) border-solid border-hub-border bg-hub-bg-panel p-3 shadow-hub-md", "font-sans text-hub-md leading-4 text-hub-fg", "data-expanded:w-(--hub-float-width)"),
|
|
272
|
+
icons: {
|
|
273
|
+
info: "size-4 shrink-0 text-hub-fg-muted",
|
|
274
|
+
success: clsx("grid size-4 shrink-0 place-items-center rounded-full", "bg-hub-success-solid text-hub-success-contrast"),
|
|
275
|
+
error: "size-4 shrink-0 text-hub-error-solid"
|
|
276
|
+
},
|
|
277
|
+
Content: "min-w-0 flex-1 break-words",
|
|
278
|
+
Message: "font-medium",
|
|
279
|
+
Description: "mt-2 line-clamp-2 leading-5 text-hub-fg-muted",
|
|
280
|
+
Action: clsx("mt-3 block cursor-pointer border-0 bg-transparent p-0", "font-sans text-hub-md leading-5 text-hub-primary-solid", "enabled:hover:underline", focusRing, "disabled:cursor-not-allowed disabled:opacity-50"),
|
|
281
|
+
Close: "-m-1"
|
|
282
|
+
};
|
|
283
|
+
// src/utils/css.ts
|
|
284
|
+
/** CSS 变量的计算值(去除首尾空白)。 */
|
|
285
|
+
function cssValue(element, name) {
|
|
286
|
+
return getComputedStyle(element).getPropertyValue(name).trim();
|
|
287
|
+
}
|
|
288
|
+
/** 以 px 表示的 CSS 变量的数值。 */
|
|
289
|
+
function cssPx(element, name) {
|
|
290
|
+
return parseFloat(cssValue(element, name));
|
|
291
|
+
}
|
|
292
|
+
/** CSS 变量里的时长(ms 或 s),返回毫秒;读不到时为 0。 */
|
|
293
|
+
function cssMs(element, name) {
|
|
294
|
+
const value = cssValue(element, name);
|
|
295
|
+
return (value.endsWith("ms") ? parseFloat(value) : parseFloat(value) * 1e3) || 0;
|
|
296
|
+
}
|
|
297
|
+
// src/components/Toast/Toast.tsx
|
|
298
|
+
var reducedMotion = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
299
|
+
function Toast({ message, description, variant = "info", action, duration = variant === "error" ? 8e3 : 4e3, revision = 0, closeLabel, onDismiss, className, onMouseEnter, onMouseLeave, onFocus, onBlur, ...props }) {
|
|
300
|
+
const card = useRef(null);
|
|
301
|
+
const previousRevision = useRef(revision);
|
|
302
|
+
const dismiss = useRef(onDismiss);
|
|
303
|
+
dismiss.current = onDismiss;
|
|
304
|
+
const [hovered, setHovered] = useState(false);
|
|
305
|
+
const [focused, setFocused] = useState(false);
|
|
306
|
+
const [busy, setBusy] = useState(false);
|
|
307
|
+
const [closing, setClosing] = useState(false);
|
|
308
|
+
const paused = hovered || focused;
|
|
309
|
+
useEffect(() => {
|
|
310
|
+
if (previousRevision.current === revision) return;
|
|
311
|
+
previousRevision.current = revision;
|
|
312
|
+
setClosing(false);
|
|
313
|
+
const element = card.current;
|
|
314
|
+
if (!element || reducedMotion()) return;
|
|
315
|
+
const animation = element.animate([
|
|
316
|
+
{
|
|
317
|
+
transform: "scale(1)",
|
|
318
|
+
easing: "ease-out",
|
|
319
|
+
offset: 0
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
transform: "scale(1.05)",
|
|
323
|
+
easing: "linear",
|
|
324
|
+
offset: .125
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
transform: "scale(1.05)",
|
|
328
|
+
easing: "ease-in-out",
|
|
329
|
+
offset: .25
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
transform: "scale(1)",
|
|
333
|
+
offset: 1
|
|
334
|
+
}
|
|
335
|
+
], { duration: cssMs(element, "--hub-duration-emphasis") });
|
|
336
|
+
return () => animation.cancel();
|
|
337
|
+
}, [revision]);
|
|
338
|
+
useEffect(() => {
|
|
339
|
+
if (!duration || paused || busy) return;
|
|
340
|
+
const timer = setTimeout(() => setClosing(true), duration);
|
|
341
|
+
return () => clearTimeout(timer);
|
|
342
|
+
}, [
|
|
343
|
+
duration,
|
|
344
|
+
revision,
|
|
345
|
+
paused,
|
|
346
|
+
busy
|
|
347
|
+
]);
|
|
348
|
+
useEffect(() => {
|
|
349
|
+
if (!closing) return;
|
|
350
|
+
let cancelled = false;
|
|
351
|
+
const animations = card.current?.parentElement?.getAnimations() ?? [];
|
|
352
|
+
Promise.allSettled(animations.map((animation) => animation.finished)).then(() => {
|
|
353
|
+
if (!cancelled) dismiss.current();
|
|
354
|
+
});
|
|
355
|
+
return () => {
|
|
356
|
+
cancelled = true;
|
|
357
|
+
};
|
|
358
|
+
}, [closing]);
|
|
359
|
+
const runAction = async () => {
|
|
360
|
+
if (busy || !action) return;
|
|
361
|
+
setBusy(true);
|
|
362
|
+
try {
|
|
363
|
+
await action.onClick();
|
|
364
|
+
setClosing(true);
|
|
365
|
+
} finally {
|
|
366
|
+
setBusy(false);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
return /* @__PURE__ */ jsx("div", {
|
|
370
|
+
...props,
|
|
371
|
+
className: clsx(toastStyles.Root, className),
|
|
372
|
+
"data-closing": closing || void 0,
|
|
373
|
+
onMouseEnter: (event) => {
|
|
374
|
+
setHovered(true);
|
|
375
|
+
onMouseEnter?.(event);
|
|
376
|
+
},
|
|
377
|
+
onMouseLeave: (event) => {
|
|
378
|
+
setHovered(false);
|
|
379
|
+
onMouseLeave?.(event);
|
|
380
|
+
},
|
|
381
|
+
onFocus: (event) => {
|
|
382
|
+
setFocused(true);
|
|
383
|
+
onFocus?.(event);
|
|
384
|
+
},
|
|
385
|
+
onBlur: (event) => {
|
|
386
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
|
|
387
|
+
onBlur?.(event);
|
|
388
|
+
},
|
|
389
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
390
|
+
ref: card,
|
|
391
|
+
className: toastStyles.Card,
|
|
392
|
+
"data-variant": variant,
|
|
393
|
+
"data-expanded": Boolean(description || action) || void 0,
|
|
394
|
+
children: [
|
|
395
|
+
/* @__PURE__ */ jsx("span", {
|
|
396
|
+
className: toastStyles.icons[variant],
|
|
397
|
+
children: variant === "success" ? /* @__PURE__ */ jsx(Check, {
|
|
398
|
+
size: 11,
|
|
399
|
+
strokeWidth: 3,
|
|
400
|
+
"aria-hidden": true
|
|
401
|
+
}) : variant === "error" ? /* @__PURE__ */ jsx(CircleAlert, {
|
|
402
|
+
size: 16,
|
|
403
|
+
"aria-hidden": true
|
|
404
|
+
}) : /* @__PURE__ */ jsx(Info, {
|
|
405
|
+
size: 16,
|
|
406
|
+
"aria-hidden": true
|
|
407
|
+
})
|
|
408
|
+
}),
|
|
409
|
+
/* @__PURE__ */ jsxs("div", {
|
|
410
|
+
className: toastStyles.Content,
|
|
411
|
+
children: [
|
|
412
|
+
/* @__PURE__ */ jsx("div", {
|
|
413
|
+
role: variant === "error" ? "alert" : "status",
|
|
414
|
+
"aria-atomic": "true",
|
|
415
|
+
className: toastStyles.Message,
|
|
416
|
+
children: message
|
|
417
|
+
}),
|
|
418
|
+
description && /* @__PURE__ */ jsx("div", {
|
|
419
|
+
className: toastStyles.Description,
|
|
420
|
+
children: description
|
|
421
|
+
}),
|
|
422
|
+
action && /* @__PURE__ */ jsx("button", {
|
|
423
|
+
type: "button",
|
|
424
|
+
className: toastStyles.Action,
|
|
425
|
+
disabled: busy,
|
|
426
|
+
onClick: () => void runAction(),
|
|
427
|
+
children: action.label
|
|
428
|
+
})
|
|
429
|
+
]
|
|
430
|
+
}),
|
|
431
|
+
/* @__PURE__ */ jsx(IconButton, {
|
|
432
|
+
variant: "ghost",
|
|
433
|
+
size: "sm",
|
|
434
|
+
className: toastStyles.Close,
|
|
435
|
+
"aria-label": closeLabel,
|
|
436
|
+
disabled: busy,
|
|
437
|
+
onClick: () => setClosing(true),
|
|
438
|
+
children: /* @__PURE__ */ jsx(X, { "aria-hidden": true })
|
|
439
|
+
})
|
|
440
|
+
]
|
|
441
|
+
})
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
// src/components/Toast/Toaster.tsx
|
|
445
|
+
function Toaster({ store, closeLabel, className, ...props }) {
|
|
446
|
+
const items = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
|
|
447
|
+
if (items.length === 0) return null;
|
|
448
|
+
return /* @__PURE__ */ jsx("div", {
|
|
449
|
+
role: "region",
|
|
450
|
+
...props,
|
|
451
|
+
className: clsx(toastStyles.Viewport, className),
|
|
452
|
+
children: items.map(({ id, options: { onClose: _onClose, ...options }, revision }) => /* @__PURE__ */ jsx(Toast, {
|
|
453
|
+
...options,
|
|
454
|
+
revision,
|
|
455
|
+
closeLabel,
|
|
456
|
+
onDismiss: () => store.dismiss(id)
|
|
457
|
+
}, id))
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
// src/components/Toast/ToastStore.ts
|
|
461
|
+
var isRepeat = (a, b) => a.message === b.message && a.description === b.description && (a.variant ?? "info") === (b.variant ?? "info") && !a.action && !b.action;
|
|
462
|
+
/** Toast 的状态容器,不依赖 React;由应用创建并交给 Toaster 渲染。 */
|
|
463
|
+
var ToastStore = class {
|
|
464
|
+
items = [];
|
|
465
|
+
listeners = /* @__PURE__ */ new Set();
|
|
466
|
+
max;
|
|
467
|
+
nextId = 0;
|
|
468
|
+
constructor(options = {}) {
|
|
469
|
+
this.max = Math.max(1, options.max ?? 1);
|
|
470
|
+
}
|
|
471
|
+
subscribe = (listener) => {
|
|
472
|
+
this.listeners.add(listener);
|
|
473
|
+
return () => {
|
|
474
|
+
this.listeners.delete(listener);
|
|
475
|
+
};
|
|
476
|
+
};
|
|
477
|
+
getSnapshot = () => this.items;
|
|
478
|
+
show(options) {
|
|
479
|
+
const repeated = this.items.find((item) => isRepeat(item.options, options));
|
|
480
|
+
if (repeated) {
|
|
481
|
+
this.commit(this.items.map((item) => item === repeated ? {
|
|
482
|
+
...item,
|
|
483
|
+
options,
|
|
484
|
+
revision: item.revision + 1
|
|
485
|
+
} : item));
|
|
486
|
+
return repeated.id;
|
|
487
|
+
}
|
|
488
|
+
const id = `toast-${++this.nextId}`;
|
|
489
|
+
this.commit([...this.items, {
|
|
490
|
+
id,
|
|
491
|
+
options,
|
|
492
|
+
revision: 0
|
|
493
|
+
}].slice(-this.max));
|
|
494
|
+
return id;
|
|
495
|
+
}
|
|
496
|
+
/** 不传 id 时关闭全部。 */
|
|
497
|
+
dismiss(id) {
|
|
498
|
+
const next = id === void 0 ? [] : this.items.filter((item) => item.id !== id);
|
|
499
|
+
if (next.length !== this.items.length) this.commit(next);
|
|
500
|
+
}
|
|
501
|
+
commit(next) {
|
|
502
|
+
const removed = this.items.filter((item) => !next.some(({ id }) => id === item.id));
|
|
503
|
+
this.items = next;
|
|
504
|
+
for (const listener of [...this.listeners]) listener();
|
|
505
|
+
for (const item of removed) item.options.onClose?.();
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
// src/components/Select/layout.ts
|
|
509
|
+
function readMenuMetrics(panel) {
|
|
510
|
+
return {
|
|
511
|
+
gutter: cssPx(panel, "--hub-menu-gutter"),
|
|
512
|
+
maxHeight: cssPx(panel, "--hub-menu-max-height"),
|
|
513
|
+
affordance: cssPx(panel, "--hub-scroll-affordance")
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
/** 根据测量值计算菜单位置:优先让初始行与触发器对齐,贴近视口边缘时改为向下或向上展开。 */
|
|
517
|
+
function computeMenuLayout({ trigger, viewport, metrics, panelWidth, borderHeight, contentHeight, row }) {
|
|
518
|
+
const { gutter, maxHeight, affordance } = metrics;
|
|
519
|
+
const rowHeight = row?.height ?? trigger.height;
|
|
520
|
+
const rowTop = row?.top ?? 0;
|
|
521
|
+
const width = Math.min(Math.max(trigger.width, panelWidth), viewport.width - gutter * 2);
|
|
522
|
+
const left = Math.max(gutter, Math.min(trigger.left, viewport.width - width - gutter));
|
|
523
|
+
const center = trigger.top + trigger.height / 2;
|
|
524
|
+
const itemCenter = rowTop + rowHeight / 2;
|
|
525
|
+
const idealTop = center - itemCenter;
|
|
526
|
+
const height = Math.min(contentHeight, maxHeight, viewport.height - gutter * 2);
|
|
527
|
+
if (center - gutter >= affordance + rowHeight / 2 && viewport.height - gutter - center >= affordance + rowHeight / 2) {
|
|
528
|
+
let top = Math.max(gutter, idealTop);
|
|
529
|
+
const clipped = Math.min(height, contentHeight - Math.max(0, top - idealTop));
|
|
530
|
+
const available = Math.min(clipped, viewport.height - gutter - top);
|
|
531
|
+
const reserve = row?.hasNext ? affordance : contentHeight - (rowTop + rowHeight);
|
|
532
|
+
if (center - top > available - reserve - rowHeight / 2) top = center - available + reserve + rowHeight / 2;
|
|
533
|
+
return {
|
|
534
|
+
placement: "aligned",
|
|
535
|
+
top,
|
|
536
|
+
left,
|
|
537
|
+
width,
|
|
538
|
+
maxHeight: Math.max(rowHeight, available),
|
|
539
|
+
scrollTop: Math.max(0, itemCenter - (center - top))
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
const below = viewport.height - trigger.bottom - gutter;
|
|
543
|
+
const above = trigger.top - gutter;
|
|
544
|
+
const down = below >= height || below >= above;
|
|
545
|
+
const available = Math.min(height, down ? below : above);
|
|
546
|
+
const listHeight = Math.max(rowHeight, available);
|
|
547
|
+
const top = down ? trigger.bottom : trigger.top - Math.min(contentHeight, listHeight) - borderHeight;
|
|
548
|
+
return {
|
|
549
|
+
placement: down ? "below" : "above",
|
|
550
|
+
top,
|
|
551
|
+
left,
|
|
552
|
+
width,
|
|
553
|
+
maxHeight: listHeight,
|
|
554
|
+
scrollTop: Math.max(0, itemCenter - available / 2)
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
// src/components/Select/styles.ts
|
|
558
|
+
var selectStyles = {
|
|
559
|
+
Root: "inline-flex min-w-0 align-middle",
|
|
560
|
+
Trigger: clsx("inline-flex min-w-0 items-center gap-2 box-border cursor-default", "rounded-(--hub-radius-md) border-(length:--hub-hairline) border-solid border-hub-border-subtle", "bg-hub-bg-panel text-hub-fg font-sans font-normal", "pl-2.5 pr-2.5 outline-none hover:bg-hub-bg-muted aria-expanded:bg-hub-bg-muted", "disabled:opacity-50 disabled:pointer-events-none", focusRing),
|
|
561
|
+
Size: {
|
|
562
|
+
small: "h-(--hub-control-height-sm) text-hub-sm",
|
|
563
|
+
default: "h-(--hub-control-height-md) text-hub-md"
|
|
564
|
+
},
|
|
565
|
+
Font: {
|
|
566
|
+
small: "text-hub-sm",
|
|
567
|
+
default: "text-hub-md"
|
|
568
|
+
},
|
|
569
|
+
Row: {
|
|
570
|
+
small: "h-(--hub-control-height-sm)",
|
|
571
|
+
default: "h-(--hub-row-height-md)"
|
|
572
|
+
},
|
|
573
|
+
Label: "min-w-0 flex-1 truncate text-left",
|
|
574
|
+
Icon: "inline-flex shrink-0 items-center justify-center",
|
|
575
|
+
Chevron: "h-2 w-2.5 shrink-0 text-hub-fg-muted",
|
|
576
|
+
Check: "size-4 shrink-0 text-hub-fg-muted",
|
|
577
|
+
Popup: clsx("fixed inset-auto m-0 p-0 box-border w-max max-w-[calc(100vw-var(--hub-menu-gutter)*2)]", "overflow-hidden rounded-hub-lg border-(length:--hub-hairline) border-solid border-hub-border-subtle", "bg-hub-bg-panel text-hub-fg shadow-hub-md outline-none", "font-sans font-normal"),
|
|
578
|
+
List: "relative box-border overflow-y-auto overscroll-contain p-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
|
|
579
|
+
Option: clsx("relative flex box-border cursor-default items-center gap-2 px-2 rounded-(--hub-radius-md)", "whitespace-nowrap outline-none data-active:bg-hub-bg-muted aria-disabled:opacity-40"),
|
|
580
|
+
Scroll: clsx("absolute inset-x-0 z-(--hub-z-raised) flex h-(--hub-scroll-affordance) items-center justify-center", "cursor-default text-hub-fg-muted backdrop-blur-sm"),
|
|
581
|
+
ScrollTop: "top-0 bg-linear-to-b from-hub-bg-panel via-hub-bg-panel/90 to-hub-bg-panel/50",
|
|
582
|
+
ScrollBottom: "bottom-0 bg-linear-to-t from-hub-bg-panel via-hub-bg-panel/90 to-hub-bg-panel/50"
|
|
583
|
+
};
|
|
584
|
+
// src/components/Select/useScrollAffordance.ts
|
|
585
|
+
var atStart = (node) => node.scrollTop <= 1;
|
|
586
|
+
var atEnd = (node) => node.scrollTop + node.clientHeight >= node.scrollHeight - 1;
|
|
587
|
+
/** 列表两端的滚动提示:是否还有被裁剪的内容,以及悬停提示时的连续滚动。 */
|
|
588
|
+
function useScrollAffordance(list) {
|
|
589
|
+
const [edges, setEdges] = useState({
|
|
590
|
+
top: false,
|
|
591
|
+
bottom: false
|
|
592
|
+
});
|
|
593
|
+
const timer = useRef(void 0);
|
|
594
|
+
const update = () => {
|
|
595
|
+
const node = list.current;
|
|
596
|
+
if (!node) return;
|
|
597
|
+
const top = !atStart(node);
|
|
598
|
+
const bottom = !atEnd(node);
|
|
599
|
+
setEdges((previous) => previous.top === top && previous.bottom === bottom ? previous : {
|
|
600
|
+
top,
|
|
601
|
+
bottom
|
|
602
|
+
});
|
|
603
|
+
};
|
|
604
|
+
const stop = () => {
|
|
605
|
+
clearInterval(timer.current);
|
|
606
|
+
timer.current = void 0;
|
|
607
|
+
};
|
|
608
|
+
const start = (side) => {
|
|
609
|
+
stop();
|
|
610
|
+
const direction = side === "top" ? -1 : 1;
|
|
611
|
+
timer.current = setInterval(() => {
|
|
612
|
+
const node = list.current;
|
|
613
|
+
if (!node) {
|
|
614
|
+
stop();
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
node.scrollBy({ top: direction * 4 });
|
|
618
|
+
if (direction < 0 ? node.scrollTop <= 0 : atEnd(node)) stop();
|
|
619
|
+
}, 16);
|
|
620
|
+
};
|
|
621
|
+
useEffect(() => stop, []);
|
|
622
|
+
return {
|
|
623
|
+
edges,
|
|
624
|
+
update,
|
|
625
|
+
start,
|
|
626
|
+
stop
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
// src/components/Select/useSelectValue.ts
|
|
630
|
+
/** 受控 / 非受控的选中值;非受控时随所属表单重置恢复 defaultValue。 */
|
|
631
|
+
function useSelectValue({ value, defaultValue, onValueChange, trigger, form }) {
|
|
632
|
+
const [internal, setInternal] = useState(defaultValue ?? "");
|
|
633
|
+
const controlled = value !== void 0;
|
|
634
|
+
const current = value ?? internal;
|
|
635
|
+
const commit = (next) => {
|
|
636
|
+
if (!controlled) setInternal(next);
|
|
637
|
+
if (next !== current) onValueChange?.(next);
|
|
638
|
+
};
|
|
639
|
+
useEffect(() => {
|
|
640
|
+
const owner = trigger.current?.form;
|
|
641
|
+
if (!owner || controlled) return;
|
|
642
|
+
const timers = /* @__PURE__ */ new Set();
|
|
643
|
+
const reset = (event) => {
|
|
644
|
+
const timer = setTimeout(() => {
|
|
645
|
+
timers.delete(timer);
|
|
646
|
+
if (!event.defaultPrevented) setInternal(defaultValue ?? "");
|
|
647
|
+
});
|
|
648
|
+
timers.add(timer);
|
|
649
|
+
};
|
|
650
|
+
owner.addEventListener("reset", reset);
|
|
651
|
+
return () => {
|
|
652
|
+
owner.removeEventListener("reset", reset);
|
|
653
|
+
timers.forEach(clearTimeout);
|
|
654
|
+
};
|
|
655
|
+
}, [
|
|
656
|
+
controlled,
|
|
657
|
+
defaultValue,
|
|
658
|
+
form,
|
|
659
|
+
trigger
|
|
660
|
+
]);
|
|
661
|
+
return {
|
|
662
|
+
current,
|
|
663
|
+
commit
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
// src/components/Select/Select.tsx
|
|
667
|
+
/** Closed: only these keys open the menu; everything else (Escape, Tab, shortcuts) bubbles to the host. */
|
|
668
|
+
var OPEN_KEYS = /* @__PURE__ */ new Set([
|
|
669
|
+
"ArrowDown",
|
|
670
|
+
"ArrowUp",
|
|
671
|
+
"Enter",
|
|
672
|
+
" "
|
|
673
|
+
]);
|
|
674
|
+
/** Open: keys the menu handles and stops from reaching the host. */
|
|
675
|
+
var MENU_KEYS = /* @__PURE__ */ new Set([
|
|
676
|
+
"ArrowDown",
|
|
677
|
+
"ArrowUp",
|
|
678
|
+
"Home",
|
|
679
|
+
"End",
|
|
680
|
+
"Enter",
|
|
681
|
+
" ",
|
|
682
|
+
"Escape"
|
|
683
|
+
]);
|
|
684
|
+
var AFFORDANCES = [{
|
|
685
|
+
side: "top",
|
|
686
|
+
className: selectStyles.ScrollTop,
|
|
687
|
+
Icon: ChevronUp
|
|
688
|
+
}, {
|
|
689
|
+
side: "bottom",
|
|
690
|
+
className: selectStyles.ScrollBottom,
|
|
691
|
+
Icon: ChevronDown
|
|
692
|
+
}];
|
|
693
|
+
function OptionContent({ icon, label }) {
|
|
694
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [icon && /* @__PURE__ */ jsx("span", {
|
|
695
|
+
className: selectStyles.Icon,
|
|
696
|
+
children: icon
|
|
697
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
698
|
+
className: selectStyles.Label,
|
|
699
|
+
children: label
|
|
700
|
+
})] });
|
|
701
|
+
}
|
|
702
|
+
function Select({ options, value, defaultValue, onValueChange, size = "default", placeholder = "请选择", name, disabled, className, onKeyDown, onClick, ...props }) {
|
|
703
|
+
const trigger = useRef(null);
|
|
704
|
+
const popup = useRef(null);
|
|
705
|
+
const list = useRef(null);
|
|
706
|
+
const metrics = useRef(void 0);
|
|
707
|
+
const { current, commit } = useSelectValue({
|
|
708
|
+
value,
|
|
709
|
+
defaultValue,
|
|
710
|
+
onValueChange,
|
|
711
|
+
trigger,
|
|
712
|
+
form: props.form
|
|
713
|
+
});
|
|
714
|
+
const scroll = useScrollAffordance(list);
|
|
715
|
+
const [open, setOpen] = useState(false);
|
|
716
|
+
const [active, setActive] = useState(-1);
|
|
717
|
+
const id = useId();
|
|
718
|
+
const selected = options.findIndex((option) => option.value === current);
|
|
719
|
+
const selectedOption = options[selected];
|
|
720
|
+
const inactive = disabled || !options.length;
|
|
721
|
+
const enabled = options.map((option, index) => option.disabled ? -1 : index).filter((index) => index >= 0);
|
|
722
|
+
const content = options.map((option) => [
|
|
723
|
+
option.value,
|
|
724
|
+
option.label,
|
|
725
|
+
option.disabled ? 1 : 0
|
|
726
|
+
].join("\0")).join("");
|
|
727
|
+
const markClosed = () => {
|
|
728
|
+
scroll.stop();
|
|
729
|
+
setOpen(false);
|
|
730
|
+
};
|
|
731
|
+
const close = (restoreFocus = true) => {
|
|
732
|
+
markClosed();
|
|
733
|
+
if (popup.current?.matches(":popover-open")) popup.current.hidePopover();
|
|
734
|
+
if (restoreFocus) trigger.current?.focus({ preventScroll: true });
|
|
735
|
+
};
|
|
736
|
+
const choose = (index) => {
|
|
737
|
+
const option = options[index];
|
|
738
|
+
if (!option || option.disabled) return;
|
|
739
|
+
commit(option.value);
|
|
740
|
+
close();
|
|
741
|
+
};
|
|
742
|
+
/** Highlight a row and scroll it clear of the scroll affordances. */
|
|
743
|
+
const move = (index) => {
|
|
744
|
+
setActive(index);
|
|
745
|
+
const node = list.current;
|
|
746
|
+
const item = node?.children[index];
|
|
747
|
+
if (!node || !item) return;
|
|
748
|
+
const edge = metrics.current?.affordance ?? 0;
|
|
749
|
+
const top = item.offsetTop;
|
|
750
|
+
if (top < node.scrollTop + edge) node.scrollTop = Math.max(0, top - edge);
|
|
751
|
+
else if (top + item.offsetHeight > node.scrollTop + node.clientHeight - edge) node.scrollTop = top + item.offsetHeight - node.clientHeight + edge;
|
|
752
|
+
scroll.update();
|
|
753
|
+
};
|
|
754
|
+
const keyDown = (event) => {
|
|
755
|
+
const { key } = event;
|
|
756
|
+
if (!open) {
|
|
757
|
+
if (OPEN_KEYS.has(key)) {
|
|
758
|
+
event.preventDefault();
|
|
759
|
+
setOpen(true);
|
|
760
|
+
}
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
if (key === "Tab") {
|
|
764
|
+
close();
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
if (!MENU_KEYS.has(key)) return;
|
|
768
|
+
event.preventDefault();
|
|
769
|
+
event.stopPropagation();
|
|
770
|
+
if (key === "Escape") close();
|
|
771
|
+
else if (key === "Enter" || key === " ") choose(active);
|
|
772
|
+
else if (key === "Home") move(enabled[0] ?? -1);
|
|
773
|
+
else if (key === "End") move(enabled.at(-1) ?? -1);
|
|
774
|
+
else {
|
|
775
|
+
const next = enabled.indexOf(active) + (key === "ArrowDown" ? 1 : -1);
|
|
776
|
+
move(enabled[Math.max(0, Math.min(enabled.length - 1, next))] ?? -1);
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
const onTriggerClick = (event) => {
|
|
780
|
+
onClick?.(event);
|
|
781
|
+
if (event.defaultPrevented) return;
|
|
782
|
+
if (open) close();
|
|
783
|
+
else setOpen(true);
|
|
784
|
+
};
|
|
785
|
+
const onTriggerKeyDown = (event) => {
|
|
786
|
+
onKeyDown?.(event);
|
|
787
|
+
if (!event.defaultPrevented) keyDown(event);
|
|
788
|
+
};
|
|
789
|
+
const onPopupToggle = (event) => {
|
|
790
|
+
if (event.newState === "closed") markClosed();
|
|
791
|
+
};
|
|
792
|
+
useLayoutEffect(() => {
|
|
793
|
+
if (!open || inactive) return;
|
|
794
|
+
const panel = popup.current;
|
|
795
|
+
const viewport = list.current;
|
|
796
|
+
panel.showPopover();
|
|
797
|
+
panel.style.visibility = "hidden";
|
|
798
|
+
panel.style.width = "";
|
|
799
|
+
viewport.style.maxHeight = "";
|
|
800
|
+
viewport.scrollTop = 0;
|
|
801
|
+
const initial = selectedOption && !selectedOption.disabled ? selected : enabled[0] ?? -1;
|
|
802
|
+
setActive(initial);
|
|
803
|
+
metrics.current = readMenuMetrics(panel);
|
|
804
|
+
const item = viewport.children[Math.max(0, initial)];
|
|
805
|
+
const panelRect = panel.getBoundingClientRect();
|
|
806
|
+
const layout = computeMenuLayout({
|
|
807
|
+
trigger: trigger.current.getBoundingClientRect(),
|
|
808
|
+
viewport: {
|
|
809
|
+
width: window.innerWidth,
|
|
810
|
+
height: window.innerHeight
|
|
811
|
+
},
|
|
812
|
+
metrics: metrics.current,
|
|
813
|
+
panelWidth: panelRect.width,
|
|
814
|
+
borderHeight: panelRect.height - viewport.getBoundingClientRect().height,
|
|
815
|
+
contentHeight: viewport.scrollHeight,
|
|
816
|
+
row: item && {
|
|
817
|
+
top: item.offsetTop,
|
|
818
|
+
height: item.offsetHeight,
|
|
819
|
+
hasNext: !!item.nextElementSibling
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
panel.dataset.placement = layout.placement;
|
|
823
|
+
panel.style.top = `${layout.top}px`;
|
|
824
|
+
panel.style.left = `${layout.left}px`;
|
|
825
|
+
panel.style.width = `${layout.width}px`;
|
|
826
|
+
viewport.style.maxHeight = `${layout.maxHeight}px`;
|
|
827
|
+
viewport.scrollTop = layout.scrollTop;
|
|
828
|
+
panel.style.visibility = "";
|
|
829
|
+
panel.focus({ preventScroll: true });
|
|
830
|
+
scroll.update();
|
|
831
|
+
return scroll.stop;
|
|
832
|
+
}, [
|
|
833
|
+
open,
|
|
834
|
+
inactive,
|
|
835
|
+
content
|
|
836
|
+
]);
|
|
837
|
+
useEffect(() => {
|
|
838
|
+
if (!open || inactive) return;
|
|
839
|
+
const panel = popup.current;
|
|
840
|
+
const dismiss = () => close(false);
|
|
841
|
+
const outsideScroll = (event) => {
|
|
842
|
+
if (!panel.contains(event.target)) dismiss();
|
|
843
|
+
};
|
|
844
|
+
window.addEventListener("resize", dismiss);
|
|
845
|
+
window.addEventListener("scroll", outsideScroll, true);
|
|
846
|
+
return () => {
|
|
847
|
+
window.removeEventListener("resize", dismiss);
|
|
848
|
+
window.removeEventListener("scroll", outsideScroll, true);
|
|
849
|
+
};
|
|
850
|
+
}, [open, inactive]);
|
|
851
|
+
useEffect(() => {
|
|
852
|
+
if (inactive) close(false);
|
|
853
|
+
}, [inactive]);
|
|
854
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
855
|
+
className: selectStyles.Root,
|
|
856
|
+
children: [
|
|
857
|
+
name && /* @__PURE__ */ jsx("input", {
|
|
858
|
+
type: "hidden",
|
|
859
|
+
name,
|
|
860
|
+
form: props.form,
|
|
861
|
+
value: current,
|
|
862
|
+
disabled: inactive
|
|
863
|
+
}),
|
|
864
|
+
/* @__PURE__ */ jsxs("button", {
|
|
865
|
+
...props,
|
|
866
|
+
ref: trigger,
|
|
867
|
+
type: "button",
|
|
868
|
+
role: "combobox",
|
|
869
|
+
"aria-haspopup": "listbox",
|
|
870
|
+
"aria-expanded": open,
|
|
871
|
+
"aria-controls": id,
|
|
872
|
+
disabled: inactive,
|
|
873
|
+
className: clsx(selectStyles.Trigger, selectStyles.Size[size], className),
|
|
874
|
+
"data-size": size,
|
|
875
|
+
onClick: onTriggerClick,
|
|
876
|
+
onKeyDown: onTriggerKeyDown,
|
|
877
|
+
children: [/* @__PURE__ */ jsx(OptionContent, {
|
|
878
|
+
icon: selectedOption?.icon,
|
|
879
|
+
label: selectedOption?.label ?? placeholder
|
|
880
|
+
}), /* @__PURE__ */ jsx(ChevronDown, {
|
|
881
|
+
className: selectStyles.Chevron,
|
|
882
|
+
"aria-hidden": "true"
|
|
883
|
+
})]
|
|
884
|
+
}),
|
|
885
|
+
/* @__PURE__ */ jsxs("div", {
|
|
886
|
+
ref: popup,
|
|
887
|
+
popover: "auto",
|
|
888
|
+
id,
|
|
889
|
+
role: "listbox",
|
|
890
|
+
tabIndex: -1,
|
|
891
|
+
"aria-label": props["aria-label"],
|
|
892
|
+
"aria-labelledby": props["aria-labelledby"],
|
|
893
|
+
"aria-activedescendant": active >= 0 ? `${id}-${active}` : void 0,
|
|
894
|
+
className: clsx(selectStyles.Popup, selectStyles.Font[size]),
|
|
895
|
+
"data-size": size,
|
|
896
|
+
onKeyDown: keyDown,
|
|
897
|
+
onToggle: onPopupToggle,
|
|
898
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
899
|
+
ref: list,
|
|
900
|
+
className: selectStyles.List,
|
|
901
|
+
onScroll: scroll.update,
|
|
902
|
+
children: options.map((option, index) => /* @__PURE__ */ jsxs("div", {
|
|
903
|
+
id: `${id}-${index}`,
|
|
904
|
+
role: "option",
|
|
905
|
+
"aria-selected": current === option.value,
|
|
906
|
+
"aria-disabled": option.disabled || void 0,
|
|
907
|
+
"data-active": active === index || void 0,
|
|
908
|
+
"data-value": option.value,
|
|
909
|
+
className: clsx(selectStyles.Option, selectStyles.Row[size]),
|
|
910
|
+
onPointerMove: (event) => {
|
|
911
|
+
if (event.movementX || event.movementY) setActive(option.disabled ? -1 : index);
|
|
912
|
+
},
|
|
913
|
+
onClick: () => choose(index),
|
|
914
|
+
children: [/* @__PURE__ */ jsx(OptionContent, {
|
|
915
|
+
icon: option.icon,
|
|
916
|
+
label: option.label
|
|
917
|
+
}), current === option.value && /* @__PURE__ */ jsx(Check, {
|
|
918
|
+
className: selectStyles.Check,
|
|
919
|
+
"aria-hidden": "true"
|
|
920
|
+
})]
|
|
921
|
+
}, option.value))
|
|
922
|
+
}), AFFORDANCES.map(({ side, className: sideClass, Icon }) => scroll.edges[side] && /* @__PURE__ */ jsx("div", {
|
|
923
|
+
"aria-hidden": "true",
|
|
924
|
+
className: clsx(selectStyles.Scroll, sideClass),
|
|
925
|
+
onPointerEnter: () => scroll.start(side),
|
|
926
|
+
onPointerLeave: scroll.stop,
|
|
927
|
+
onPointerDown: (event) => event.preventDefault(),
|
|
928
|
+
children: /* @__PURE__ */ jsx(Icon, { className: selectStyles.Check })
|
|
929
|
+
}, side))]
|
|
930
|
+
})
|
|
931
|
+
]
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
// src/components/Tooltip/coordinator.ts
|
|
935
|
+
var MODIFIERS = [
|
|
936
|
+
"Control",
|
|
937
|
+
"Alt",
|
|
938
|
+
"Shift",
|
|
939
|
+
"Meta"
|
|
940
|
+
];
|
|
941
|
+
/** 显示文字(小写)到 KeyboardEvent.key(小写)的映射;其余标签按原文匹配。 */
|
|
942
|
+
var KEY_ALIASES = {
|
|
943
|
+
"⌘": "meta",
|
|
944
|
+
cmd: "meta",
|
|
945
|
+
command: "meta",
|
|
946
|
+
meta: "meta",
|
|
947
|
+
ctrl: "control",
|
|
948
|
+
control: "control",
|
|
949
|
+
"⌃": "control",
|
|
950
|
+
alt: "alt",
|
|
951
|
+
option: "alt",
|
|
952
|
+
"⌥": "alt",
|
|
953
|
+
shift: "shift",
|
|
954
|
+
"⇧": "shift",
|
|
955
|
+
esc: "escape",
|
|
956
|
+
escape: "escape",
|
|
957
|
+
enter: "enter",
|
|
958
|
+
return: "enter",
|
|
959
|
+
"↵": "enter",
|
|
960
|
+
space: " ",
|
|
961
|
+
spacebar: " ",
|
|
962
|
+
"␣": " ",
|
|
963
|
+
"↑": "arrowup",
|
|
964
|
+
"↓": "arrowdown",
|
|
965
|
+
"←": "arrowleft",
|
|
966
|
+
"→": "arrowright"
|
|
967
|
+
};
|
|
968
|
+
/** 输入法和死键产生的占位键名,不代表实际按键。 */
|
|
969
|
+
var PLACEHOLDER_KEYS = /* @__PURE__ */ new Set([
|
|
970
|
+
"process",
|
|
971
|
+
"dead",
|
|
972
|
+
"unidentified"
|
|
973
|
+
]);
|
|
974
|
+
function keyName(label) {
|
|
975
|
+
const lower = label.toLowerCase();
|
|
976
|
+
return KEY_ALIASES[lower.trim()] ?? lower;
|
|
977
|
+
}
|
|
978
|
+
/** 一个 Tooltip 实例在协调器中的状态:显示条件和自身的定时器。 */
|
|
979
|
+
var TooltipHandle = class {
|
|
980
|
+
coordinator;
|
|
981
|
+
host;
|
|
982
|
+
hover = false;
|
|
983
|
+
popupHover = false;
|
|
984
|
+
focus = false;
|
|
985
|
+
shortcut = false;
|
|
986
|
+
disabled = false;
|
|
987
|
+
openTimer;
|
|
988
|
+
closeTimer;
|
|
989
|
+
constructor(coordinator, host) {
|
|
990
|
+
this.coordinator = coordinator;
|
|
991
|
+
this.host = host;
|
|
992
|
+
}
|
|
993
|
+
get wanted() {
|
|
994
|
+
return !this.disabled && (this.hover || this.popupHover || this.focus);
|
|
995
|
+
}
|
|
996
|
+
delay(name) {
|
|
997
|
+
const trigger = this.host.trigger();
|
|
998
|
+
return trigger ? cssMs(trigger, name) : 0;
|
|
999
|
+
}
|
|
1000
|
+
clearClose() {
|
|
1001
|
+
window.clearTimeout(this.closeTimer);
|
|
1002
|
+
this.closeTimer = void 0;
|
|
1003
|
+
}
|
|
1004
|
+
enter() {
|
|
1005
|
+
this.hover = true;
|
|
1006
|
+
this.coordinator.request(this, false);
|
|
1007
|
+
}
|
|
1008
|
+
leave() {
|
|
1009
|
+
this.hover = false;
|
|
1010
|
+
this.coordinator.release(this, true);
|
|
1011
|
+
}
|
|
1012
|
+
popupEnter() {
|
|
1013
|
+
this.popupHover = true;
|
|
1014
|
+
this.coordinator.request(this, false);
|
|
1015
|
+
}
|
|
1016
|
+
popupLeave() {
|
|
1017
|
+
this.popupHover = false;
|
|
1018
|
+
this.coordinator.release(this, true);
|
|
1019
|
+
}
|
|
1020
|
+
focusIn() {
|
|
1021
|
+
this.focus = true;
|
|
1022
|
+
this.coordinator.request(this, true);
|
|
1023
|
+
}
|
|
1024
|
+
focusOut() {
|
|
1025
|
+
this.focus = false;
|
|
1026
|
+
this.coordinator.release(this, false);
|
|
1027
|
+
}
|
|
1028
|
+
/** 关闭并清除显示条件;之后需要新的进入或聚焦动作才会再次显示。 */
|
|
1029
|
+
dismiss() {
|
|
1030
|
+
this.coordinator.dismiss(this);
|
|
1031
|
+
}
|
|
1032
|
+
setDisabled(disabled) {
|
|
1033
|
+
this.disabled = disabled;
|
|
1034
|
+
if (disabled) this.dismiss();
|
|
1035
|
+
}
|
|
1036
|
+
setShortcut(shortcut) {
|
|
1037
|
+
if (this.shortcut === shortcut) return;
|
|
1038
|
+
this.shortcut = shortcut;
|
|
1039
|
+
this.coordinator.countShortcut(shortcut ? 1 : -1);
|
|
1040
|
+
}
|
|
1041
|
+
isPressed(label) {
|
|
1042
|
+
return this.coordinator.isPressed(label);
|
|
1043
|
+
}
|
|
1044
|
+
dispose() {
|
|
1045
|
+
this.coordinator.unregister(this);
|
|
1046
|
+
}
|
|
1047
|
+
};
|
|
1048
|
+
/** 同一文档内所有 Tooltip 共享的开关、免等待和按键状态。 */
|
|
1049
|
+
var TooltipCoordinator = class {
|
|
1050
|
+
document;
|
|
1051
|
+
onEmpty;
|
|
1052
|
+
handles = /* @__PURE__ */ new Set();
|
|
1053
|
+
visible = null;
|
|
1054
|
+
pending = null;
|
|
1055
|
+
/** 免等待截止时间(performance.now 时间戳)。 */
|
|
1056
|
+
skipUntil = 0;
|
|
1057
|
+
shortcuts = 0;
|
|
1058
|
+
modifiers = /* @__PURE__ */ new Set();
|
|
1059
|
+
/** 按下的普通键:event.code 到 keydown 时的逻辑键,释放时按 code 找回。 */
|
|
1060
|
+
keys = /* @__PURE__ */ new Map();
|
|
1061
|
+
composing = false;
|
|
1062
|
+
constructor(document, onEmpty) {
|
|
1063
|
+
this.document = document;
|
|
1064
|
+
this.onEmpty = onEmpty;
|
|
1065
|
+
}
|
|
1066
|
+
register(host) {
|
|
1067
|
+
const handle = new TooltipHandle(this, host);
|
|
1068
|
+
if (!this.handles.size) this.listen(true);
|
|
1069
|
+
this.handles.add(handle);
|
|
1070
|
+
return handle;
|
|
1071
|
+
}
|
|
1072
|
+
unregister(handle) {
|
|
1073
|
+
if (!this.handles.delete(handle)) return;
|
|
1074
|
+
this.dismiss(handle);
|
|
1075
|
+
handle.setShortcut(false);
|
|
1076
|
+
if (this.handles.size) return;
|
|
1077
|
+
this.listen(false);
|
|
1078
|
+
this.skipUntil = 0;
|
|
1079
|
+
this.onEmpty();
|
|
1080
|
+
}
|
|
1081
|
+
/** 请求显示:已有提示显示或处于免等待时间内立即切换,否则从头等待。 */
|
|
1082
|
+
request(handle, immediate) {
|
|
1083
|
+
if (!handle.wanted) return;
|
|
1084
|
+
if (this.visible === handle) {
|
|
1085
|
+
handle.clearClose();
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
if (immediate || this.visible || performance.now() < this.skipUntil) {
|
|
1089
|
+
this.show(handle);
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
if (this.pending === handle) return;
|
|
1093
|
+
this.cancelPending();
|
|
1094
|
+
this.pending = handle;
|
|
1095
|
+
handle.openTimer = window.setTimeout(() => {
|
|
1096
|
+
if (this.pending !== handle) return;
|
|
1097
|
+
this.pending = null;
|
|
1098
|
+
if (this.handles.has(handle) && handle.wanted) this.show(handle);
|
|
1099
|
+
}, handle.delay("--hub-delay-open"));
|
|
1100
|
+
}
|
|
1101
|
+
/** 某个显示条件消失:全部消失后才关闭;鼠标离开时留出缓冲,便于移到提示内容上。 */
|
|
1102
|
+
release(handle, buffered) {
|
|
1103
|
+
if (handle.wanted) return;
|
|
1104
|
+
if (this.pending === handle) this.cancelPending();
|
|
1105
|
+
if (this.visible !== handle) return;
|
|
1106
|
+
handle.clearClose();
|
|
1107
|
+
if (!buffered) {
|
|
1108
|
+
this.hide(handle);
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
handle.closeTimer = window.setTimeout(() => {
|
|
1112
|
+
if (!handle.wanted) this.hide(handle);
|
|
1113
|
+
}, handle.delay("--hub-duration-fast"));
|
|
1114
|
+
}
|
|
1115
|
+
dismiss(handle) {
|
|
1116
|
+
handle.hover = handle.popupHover = handle.focus = false;
|
|
1117
|
+
if (this.pending === handle) this.cancelPending();
|
|
1118
|
+
this.hide(handle);
|
|
1119
|
+
}
|
|
1120
|
+
countShortcut(change) {
|
|
1121
|
+
this.shortcuts += change;
|
|
1122
|
+
if (!this.shortcuts) this.clearKeys();
|
|
1123
|
+
}
|
|
1124
|
+
isPressed(label) {
|
|
1125
|
+
const name = keyName(label);
|
|
1126
|
+
if (this.modifiers.has(name)) return true;
|
|
1127
|
+
for (const key of this.keys.values()) if (key === name) return true;
|
|
1128
|
+
return false;
|
|
1129
|
+
}
|
|
1130
|
+
show(handle) {
|
|
1131
|
+
this.cancelPending();
|
|
1132
|
+
const previous = this.visible;
|
|
1133
|
+
if (previous === handle) return;
|
|
1134
|
+
this.visible = handle;
|
|
1135
|
+
if (previous) {
|
|
1136
|
+
previous.clearClose();
|
|
1137
|
+
previous.host.onOpenChange(false);
|
|
1138
|
+
}
|
|
1139
|
+
handle.host.onOpenChange(true);
|
|
1140
|
+
}
|
|
1141
|
+
hide(handle) {
|
|
1142
|
+
handle.clearClose();
|
|
1143
|
+
if (this.visible !== handle) return;
|
|
1144
|
+
this.visible = null;
|
|
1145
|
+
this.skipUntil = performance.now() + handle.delay("--hub-delay-skip");
|
|
1146
|
+
handle.host.onOpenChange(false);
|
|
1147
|
+
}
|
|
1148
|
+
cancelPending() {
|
|
1149
|
+
if (!this.pending) return;
|
|
1150
|
+
window.clearTimeout(this.pending.openTimer);
|
|
1151
|
+
this.pending = null;
|
|
1152
|
+
}
|
|
1153
|
+
dismissAll() {
|
|
1154
|
+
if (this.pending) this.dismiss(this.pending);
|
|
1155
|
+
if (this.visible) this.dismiss(this.visible);
|
|
1156
|
+
}
|
|
1157
|
+
keyState() {
|
|
1158
|
+
return [
|
|
1159
|
+
...this.modifiers,
|
|
1160
|
+
"",
|
|
1161
|
+
...this.keys.values()
|
|
1162
|
+
].join("\0");
|
|
1163
|
+
}
|
|
1164
|
+
clearKeys() {
|
|
1165
|
+
if (!this.modifiers.size && !this.keys.size) return;
|
|
1166
|
+
this.modifiers.clear();
|
|
1167
|
+
this.keys.clear();
|
|
1168
|
+
this.visible?.host.onKeysChange();
|
|
1169
|
+
}
|
|
1170
|
+
onKey = (event) => {
|
|
1171
|
+
if (!this.shortcuts) return;
|
|
1172
|
+
const before = this.keyState();
|
|
1173
|
+
const down = event.type === "keydown";
|
|
1174
|
+
const key = event.key.toLowerCase();
|
|
1175
|
+
const modifiers = /* @__PURE__ */ new Set();
|
|
1176
|
+
for (const name of MODIFIERS) if (event.getModifierState(name)) modifiers.add(name.toLowerCase());
|
|
1177
|
+
const isModifier = MODIFIERS.some((name) => name.toLowerCase() === key);
|
|
1178
|
+
if (down && isModifier) modifiers.add(key);
|
|
1179
|
+
this.modifiers = modifiers;
|
|
1180
|
+
const id = event.code || key;
|
|
1181
|
+
if (!down) {
|
|
1182
|
+
this.keys.delete(id);
|
|
1183
|
+
if (key === "meta") this.keys.clear();
|
|
1184
|
+
} else if (!isModifier && !this.composing && !event.isComposing && !PLACEHOLDER_KEYS.has(key)) this.keys.set(id, key);
|
|
1185
|
+
if (this.keyState() !== before) this.visible?.host.onKeysChange();
|
|
1186
|
+
};
|
|
1187
|
+
onComposition = (event) => {
|
|
1188
|
+
this.composing = event.type === "compositionstart";
|
|
1189
|
+
if (!this.composing || !this.keys.size) return;
|
|
1190
|
+
this.keys.clear();
|
|
1191
|
+
this.visible?.host.onKeysChange();
|
|
1192
|
+
};
|
|
1193
|
+
/** 触发器随滚动移动时关闭;由键盘焦点保持的提示改为跟随,否则 Tab 到视口外的触发器会立刻被关掉。 */
|
|
1194
|
+
onScroll = (event) => {
|
|
1195
|
+
const target = event.target;
|
|
1196
|
+
for (const handle of /* @__PURE__ */ new Set([this.pending, this.visible])) {
|
|
1197
|
+
const trigger = handle?.host.trigger();
|
|
1198
|
+
if (!handle || !trigger || !target.contains(trigger)) continue;
|
|
1199
|
+
if (handle !== this.visible || !handle.focus || !handle.host.reposition()) this.dismiss(handle);
|
|
1200
|
+
}
|
|
1201
|
+
};
|
|
1202
|
+
onResize = () => this.dismissAll();
|
|
1203
|
+
/** 窗口失焦或页面隐藏:之后收不到按键释放和鼠标离开,全部状态归零。 */
|
|
1204
|
+
onInactive = (event) => {
|
|
1205
|
+
if (event.type === "visibilitychange" && !this.document.hidden) return;
|
|
1206
|
+
if (event.type === "blur" && event.target !== this.document.defaultView) return;
|
|
1207
|
+
this.dismissAll();
|
|
1208
|
+
this.skipUntil = 0;
|
|
1209
|
+
this.composing = false;
|
|
1210
|
+
this.clearKeys();
|
|
1211
|
+
};
|
|
1212
|
+
listen(on) {
|
|
1213
|
+
const view = this.document.defaultView;
|
|
1214
|
+
const toggle = (target, type, listener) => {
|
|
1215
|
+
if (on) target?.addEventListener(type, listener, true);
|
|
1216
|
+
else target?.removeEventListener(type, listener, true);
|
|
1217
|
+
};
|
|
1218
|
+
toggle(this.document, "keydown", this.onKey);
|
|
1219
|
+
toggle(this.document, "keyup", this.onKey);
|
|
1220
|
+
toggle(this.document, "compositionstart", this.onComposition);
|
|
1221
|
+
toggle(this.document, "compositionend", this.onComposition);
|
|
1222
|
+
toggle(this.document, "visibilitychange", this.onInactive);
|
|
1223
|
+
toggle(view, "blur", this.onInactive);
|
|
1224
|
+
toggle(view, "resize", this.onResize);
|
|
1225
|
+
toggle(view, "scroll", this.onScroll);
|
|
1226
|
+
if (!on) this.clearKeys();
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
var coordinators = /* @__PURE__ */ new WeakMap();
|
|
1230
|
+
/** 在触发器所在文档的协调器中注册一个实例;协调器按需创建,最后一个实例注销后移除。 */
|
|
1231
|
+
function registerTooltip(document, host) {
|
|
1232
|
+
let coordinator = coordinators.get(document);
|
|
1233
|
+
if (!coordinator) {
|
|
1234
|
+
coordinator = new TooltipCoordinator(document, () => coordinators.delete(document));
|
|
1235
|
+
coordinators.set(document, coordinator);
|
|
1236
|
+
}
|
|
1237
|
+
return coordinator.register(host);
|
|
1238
|
+
}
|
|
1239
|
+
// src/components/Tooltip/layout.ts
|
|
1240
|
+
var OPPOSITE = {
|
|
1241
|
+
top: "bottom",
|
|
1242
|
+
bottom: "top",
|
|
1243
|
+
left: "right",
|
|
1244
|
+
right: "left"
|
|
1245
|
+
};
|
|
1246
|
+
/** 优先使用 side 和 align;主方向放不下且反方向更宽裕时翻转,最后把位置约束在视口内。 */
|
|
1247
|
+
function computeTooltipLayout({ trigger, panel, margin, viewport, side, align, rtl }) {
|
|
1248
|
+
const width = panel.width + margin * 2;
|
|
1249
|
+
const height = panel.height + margin * 2;
|
|
1250
|
+
const space = {
|
|
1251
|
+
top: trigger.top,
|
|
1252
|
+
bottom: viewport.height - trigger.bottom,
|
|
1253
|
+
left: trigger.left,
|
|
1254
|
+
right: viewport.width - trigger.right
|
|
1255
|
+
};
|
|
1256
|
+
const vertical = side === "top" || side === "bottom";
|
|
1257
|
+
const opposite = OPPOSITE[side];
|
|
1258
|
+
const placed = space[side] < (vertical ? height : width) && space[opposite] > space[side] ? opposite : side;
|
|
1259
|
+
const main = placed === "top" ? trigger.top - height : placed === "bottom" ? trigger.bottom : placed === "left" ? trigger.left - width : trigger.right;
|
|
1260
|
+
const start = vertical ? trigger.left : trigger.top;
|
|
1261
|
+
const length = vertical ? trigger.width : trigger.height;
|
|
1262
|
+
const size = vertical ? width : height;
|
|
1263
|
+
const edge = vertical && rtl && align !== "center" ? align === "start" ? "end" : "start" : align;
|
|
1264
|
+
const cross = edge === "start" ? start - margin : edge === "end" ? start + length - size + margin : start + (length - size) / 2;
|
|
1265
|
+
const clamp = (value, max) => Math.round(Math.max(0, Math.min(value, max)));
|
|
1266
|
+
return {
|
|
1267
|
+
side: placed,
|
|
1268
|
+
top: clamp(vertical ? main : cross, viewport.height - height),
|
|
1269
|
+
left: clamp(vertical ? cross : main, viewport.width - width)
|
|
1270
|
+
};
|
|
1271
|
+
}
|
|
1272
|
+
// src/components/Tooltip/styles.ts
|
|
1273
|
+
var tooltipStyles = {
|
|
1274
|
+
Popup: clsx("fixed inset-auto m-1.5 box-border w-max border-0 px-2 py-1.25", "max-w-[min(var(--hub-float-width),calc(100vw-var(--hub-float-gutter)))]", "max-h-[calc(100vh-var(--hub-float-gutter))] overflow-x-hidden overflow-y-auto overscroll-contain", "open:inline-flex items-center-safe gap-2", "rounded-(--hub-radius-md) bg-hub-bg-panel text-hub-fg shadow-hub-sm", "font-sans font-normal text-hub-sm leading-4 text-left", "transition-opacity duration-(--hub-duration-fast) starting:opacity-0 motion-reduce:transition-none"),
|
|
1275
|
+
Content: "min-w-0 whitespace-normal wrap-anywhere",
|
|
1276
|
+
Keys: "-mr-0.5 inline-flex shrink-0 items-center gap-0.5",
|
|
1277
|
+
Kbd: clsx("inline-flex h-4.5 min-w-4.5 box-border items-center justify-center px-0.5", "rounded-hub-sm border-(length:--hub-hairline) border-solid border-hub-border", "bg-transparent text-hub-fg-muted font-sans text-hub-sm leading-none", "transition-colors duration-(--hub-duration-fast) motion-reduce:transition-none", "data-pressed:bg-hub-bg-emphasized")
|
|
1278
|
+
};
|
|
1279
|
+
// src/components/Tooltip/Tooltip.tsx
|
|
1280
|
+
/** 先执行宿主原有的事件处理,再执行 Tooltip 的。 */
|
|
1281
|
+
function chain(host, own) {
|
|
1282
|
+
return (event) => {
|
|
1283
|
+
host?.(event);
|
|
1284
|
+
own(event);
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
/** 触发器是否仍有可见部分:既要在视口内,也不能被祖先滚动容器完全裁剪。 */
|
|
1288
|
+
function isVisible(node, rect, viewport) {
|
|
1289
|
+
let top = Math.max(rect.top, 0);
|
|
1290
|
+
let left = Math.max(rect.left, 0);
|
|
1291
|
+
let bottom = Math.min(rect.bottom, viewport.height);
|
|
1292
|
+
let right = Math.min(rect.right, viewport.width);
|
|
1293
|
+
for (let ancestor = node; ancestor && ancestor !== node.ownerDocument.documentElement; ancestor = ancestor.parentElement) {
|
|
1294
|
+
if (ancestor !== node) {
|
|
1295
|
+
const { overflowX, overflowY } = getComputedStyle(ancestor);
|
|
1296
|
+
if (overflowX !== "visible" || overflowY !== "visible") {
|
|
1297
|
+
const clip = ancestor.getBoundingClientRect();
|
|
1298
|
+
if (overflowY !== "visible") {
|
|
1299
|
+
top = Math.max(top, clip.top);
|
|
1300
|
+
bottom = Math.min(bottom, clip.bottom);
|
|
1301
|
+
}
|
|
1302
|
+
if (overflowX !== "visible") {
|
|
1303
|
+
left = Math.max(left, clip.left);
|
|
1304
|
+
right = Math.min(right, clip.right);
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
if (ancestor.matches(":popover-open, :modal, :fullscreen")) break;
|
|
1309
|
+
}
|
|
1310
|
+
return bottom > top && right > left;
|
|
1311
|
+
}
|
|
1312
|
+
function Tooltip({ children, content, shortcut, side = "bottom", align = "center", disabled = false }) {
|
|
1313
|
+
const trigger = useRef(null);
|
|
1314
|
+
const popup = useRef(null);
|
|
1315
|
+
const handle = useRef(null);
|
|
1316
|
+
const pressing = useRef(false);
|
|
1317
|
+
const place = useRef(() => false);
|
|
1318
|
+
const [open, setOpen] = useState(false);
|
|
1319
|
+
const [, setKeysVersion] = useState(0);
|
|
1320
|
+
const id = useId();
|
|
1321
|
+
const child = children;
|
|
1322
|
+
const host = child.props;
|
|
1323
|
+
const keys = shortcut?.length ? shortcut : void 0;
|
|
1324
|
+
const hostRef = host.ref;
|
|
1325
|
+
const ref = useCallback((node) => {
|
|
1326
|
+
trigger.current = node;
|
|
1327
|
+
if (typeof hostRef === "function") {
|
|
1328
|
+
const cleanup = hostRef(node);
|
|
1329
|
+
if (typeof cleanup === "function") return () => {
|
|
1330
|
+
trigger.current = null;
|
|
1331
|
+
cleanup();
|
|
1332
|
+
};
|
|
1333
|
+
} else if (hostRef) hostRef.current = node;
|
|
1334
|
+
}, [hostRef]);
|
|
1335
|
+
useLayoutEffect(() => {
|
|
1336
|
+
place.current = () => {
|
|
1337
|
+
const node = trigger.current;
|
|
1338
|
+
const panel = popup.current;
|
|
1339
|
+
if (!node || !panel) return false;
|
|
1340
|
+
const root = node.ownerDocument.documentElement;
|
|
1341
|
+
const viewport = {
|
|
1342
|
+
width: root.clientWidth,
|
|
1343
|
+
height: root.clientHeight
|
|
1344
|
+
};
|
|
1345
|
+
const rect = node.getBoundingClientRect();
|
|
1346
|
+
const style = getComputedStyle(panel);
|
|
1347
|
+
const layout = computeTooltipLayout({
|
|
1348
|
+
trigger: rect,
|
|
1349
|
+
panel: panel.getBoundingClientRect(),
|
|
1350
|
+
margin: parseFloat(style.marginTop) || 0,
|
|
1351
|
+
viewport,
|
|
1352
|
+
side,
|
|
1353
|
+
align,
|
|
1354
|
+
rtl: style.direction === "rtl"
|
|
1355
|
+
});
|
|
1356
|
+
panel.dataset.side = layout.side;
|
|
1357
|
+
panel.style.top = `${layout.top}px`;
|
|
1358
|
+
panel.style.left = `${layout.left}px`;
|
|
1359
|
+
return isVisible(node, rect, viewport);
|
|
1360
|
+
};
|
|
1361
|
+
});
|
|
1362
|
+
useLayoutEffect(() => {
|
|
1363
|
+
const node = trigger.current;
|
|
1364
|
+
if (!node) return;
|
|
1365
|
+
const registered = registerTooltip(node.ownerDocument, {
|
|
1366
|
+
trigger: () => trigger.current,
|
|
1367
|
+
onOpenChange: setOpen,
|
|
1368
|
+
onKeysChange: () => setKeysVersion((version) => version + 1),
|
|
1369
|
+
reposition: () => place.current()
|
|
1370
|
+
});
|
|
1371
|
+
handle.current = registered;
|
|
1372
|
+
return () => {
|
|
1373
|
+
handle.current = null;
|
|
1374
|
+
registered.dispose();
|
|
1375
|
+
};
|
|
1376
|
+
}, []);
|
|
1377
|
+
useLayoutEffect(() => {
|
|
1378
|
+
handle.current?.setShortcut(!!keys);
|
|
1379
|
+
}, [!!keys]);
|
|
1380
|
+
useLayoutEffect(() => {
|
|
1381
|
+
handle.current?.setDisabled(disabled);
|
|
1382
|
+
}, [disabled]);
|
|
1383
|
+
useLayoutEffect(() => {
|
|
1384
|
+
const panel = popup.current;
|
|
1385
|
+
if (!open || !panel) return;
|
|
1386
|
+
panel.showPopover();
|
|
1387
|
+
place.current();
|
|
1388
|
+
const observer = new ResizeObserver(() => place.current());
|
|
1389
|
+
observer.observe(panel);
|
|
1390
|
+
return () => {
|
|
1391
|
+
observer.disconnect();
|
|
1392
|
+
if (panel.matches(":popover-open")) panel.hidePopover();
|
|
1393
|
+
};
|
|
1394
|
+
}, [open]);
|
|
1395
|
+
useLayoutEffect(() => {
|
|
1396
|
+
if (open) place.current();
|
|
1397
|
+
}, [side, align]);
|
|
1398
|
+
const describedBy = disabled ? host["aria-describedby"] : [.../* @__PURE__ */ new Set([...host["aria-describedby"]?.split(/\s+/).filter(Boolean) ?? [], id])].join(" ");
|
|
1399
|
+
const release = () => {
|
|
1400
|
+
pressing.current = false;
|
|
1401
|
+
};
|
|
1402
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [cloneElement(child, {
|
|
1403
|
+
ref,
|
|
1404
|
+
"aria-describedby": describedBy,
|
|
1405
|
+
onPointerEnter: chain(host.onPointerEnter, (event) => {
|
|
1406
|
+
if (event.pointerType !== "touch") handle.current?.enter();
|
|
1407
|
+
}),
|
|
1408
|
+
onPointerLeave: chain(host.onPointerLeave, () => {
|
|
1409
|
+
release();
|
|
1410
|
+
handle.current?.leave();
|
|
1411
|
+
}),
|
|
1412
|
+
onPointerDown: chain(host.onPointerDown, () => {
|
|
1413
|
+
pressing.current = true;
|
|
1414
|
+
}),
|
|
1415
|
+
onPointerUp: chain(host.onPointerUp, release),
|
|
1416
|
+
onPointerCancel: chain(host.onPointerCancel, release),
|
|
1417
|
+
onFocus: chain(host.onFocus, (event) => {
|
|
1418
|
+
if (!pressing.current && event.target.matches(":focus-visible")) handle.current?.focusIn();
|
|
1419
|
+
}),
|
|
1420
|
+
onBlur: chain(host.onBlur, (event) => {
|
|
1421
|
+
if (!event.currentTarget.contains(event.relatedTarget)) handle.current?.focusOut();
|
|
1422
|
+
}),
|
|
1423
|
+
onClick: chain(host.onClick, () => handle.current?.dismiss())
|
|
1424
|
+
}), !disabled && /* @__PURE__ */ jsxs("span", {
|
|
1425
|
+
ref: popup,
|
|
1426
|
+
id,
|
|
1427
|
+
role: "tooltip",
|
|
1428
|
+
popover: "manual",
|
|
1429
|
+
tabIndex: -1,
|
|
1430
|
+
className: tooltipStyles.Popup,
|
|
1431
|
+
onPointerEnter: () => handle.current?.popupEnter(),
|
|
1432
|
+
onPointerLeave: () => handle.current?.popupLeave(),
|
|
1433
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
1434
|
+
className: tooltipStyles.Content,
|
|
1435
|
+
children: content
|
|
1436
|
+
}), keys && /* @__PURE__ */ jsx("span", {
|
|
1437
|
+
className: tooltipStyles.Keys,
|
|
1438
|
+
children: keys.map((key, index) => /* @__PURE__ */ jsx("kbd", {
|
|
1439
|
+
className: tooltipStyles.Kbd,
|
|
1440
|
+
"data-pressed": open && handle.current?.isPressed(key) || void 0,
|
|
1441
|
+
children: key
|
|
1442
|
+
}, index))
|
|
1443
|
+
})]
|
|
1444
|
+
})] });
|
|
1445
|
+
}
|
|
1446
|
+
export { Button, Checkbox, Divider, IconButton, ResizeHandle, Select, Toast, ToastStore, Toaster, Tooltip };
|