@hamsterbase/ui 0.0.0-stage → 0.2.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 +9 -0
- package/dist/components/Button/styles.d.ts +14 -0
- package/dist/components/Checkbox/Checkbox.d.ts +5 -0
- package/dist/components/Checkbox/styles.d.ts +7 -0
- package/dist/components/Divider/Divider.d.ts +13 -0
- package/dist/components/Divider/styles.d.ts +11 -0
- package/dist/components/IconButton/IconButton.d.ts +11 -0
- package/dist/components/IconButton/styles.d.ts +12 -0
- package/dist/components/ResizeHandle/ResizeHandle.d.ts +17 -0
- package/dist/components/ResizeHandle/styles.d.ts +11 -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/index.d.ts +16 -0
- package/dist/index.js +499 -0
- package/dist/styles.css +967 -0
- package/dist/utils/focus.d.ts +4 -0
- package/docs/Button.md +6 -0
- package/docs/Checkbox.md +7 -0
- package/docs/Divider.md +11 -0
- package/docs/IconButton.md +7 -0
- package/docs/ResizeHandle.md +19 -0
- package/docs/Toast.md +60 -0
- package/docs/index.md +19 -0
- package/package.json +50 -4
- package/README.md +0 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { clsx } from "clsx/lite";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
5
|
+
import { Check, 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";
|
|
11
|
+
// src/components/Button/styles.ts
|
|
12
|
+
var buttonStyles = {
|
|
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"),
|
|
14
|
+
variants: {
|
|
15
|
+
primary: "border-transparent bg-hub-primary-solid text-hub-primary-contrast enabled:hover:bg-hub-primary-solid-hover",
|
|
16
|
+
secondary: "border-hub-border bg-hub-bg-panel text-hub-fg enabled:hover:bg-hub-bg-muted",
|
|
17
|
+
ghost: "border-transparent bg-transparent text-hub-fg enabled:hover:bg-hub-bg-muted",
|
|
18
|
+
danger: "border-transparent bg-hub-error-solid text-hub-error-contrast enabled:hover:bg-hub-error-solid-hover"
|
|
19
|
+
},
|
|
20
|
+
sizes: {
|
|
21
|
+
sm: "h-6 px-2.5 text-hub-sm",
|
|
22
|
+
md: "h-7 px-3 text-hub-md",
|
|
23
|
+
lg: "h-8 px-4 text-hub-md"
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
// src/components/Button/Button.tsx
|
|
27
|
+
function Button({ variant = "primary", size = "md", type = "button", className, ...props }) {
|
|
28
|
+
return /* @__PURE__ */ jsx("button", {
|
|
29
|
+
...props,
|
|
30
|
+
type,
|
|
31
|
+
className: clsx(buttonStyles.Root, buttonStyles.variants[variant], buttonStyles.sizes[size], className)
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
// src/components/IconButton/styles.ts
|
|
35
|
+
var iconButtonStyles = {
|
|
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"),
|
|
37
|
+
sizes: {
|
|
38
|
+
sm: "size-6 [&_svg]:size-3.5",
|
|
39
|
+
md: "size-7 [&_svg]:size-3.5",
|
|
40
|
+
lg: "size-8 [&_svg]:size-4"
|
|
41
|
+
},
|
|
42
|
+
variants: {
|
|
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")
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
// src/components/IconButton/IconButton.tsx
|
|
48
|
+
function IconButton({ className, size = "md", type = "button", variant = "surface", pressed, ...props }) {
|
|
49
|
+
return /* @__PURE__ */ jsx("button", {
|
|
50
|
+
...props,
|
|
51
|
+
type,
|
|
52
|
+
"aria-pressed": pressed,
|
|
53
|
+
className: clsx(iconButtonStyles.Root, iconButtonStyles.sizes[size], iconButtonStyles.variants[variant], className)
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
// src/components/Checkbox/styles.ts
|
|
57
|
+
var checkboxStyles = {
|
|
58
|
+
Root: "group/checkbox relative inline-flex size-3.5 flex-none align-middle",
|
|
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"),
|
|
61
|
+
Check: "hidden [grid-area:1/1] group-has-[:checked:not(:indeterminate)]/checkbox:block",
|
|
62
|
+
Mixed: "hidden [grid-area:1/1] group-has-[:indeterminate]/checkbox:block"
|
|
63
|
+
};
|
|
64
|
+
// src/components/Checkbox/Checkbox.tsx
|
|
65
|
+
function Checkbox({ indeterminate = false, className, ref, ...props }) {
|
|
66
|
+
const input = useRef(null);
|
|
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]);
|
|
72
|
+
useLayoutEffect(() => {
|
|
73
|
+
if (input.current) input.current.indeterminate = indeterminate;
|
|
74
|
+
}, [indeterminate]);
|
|
75
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
76
|
+
className: clsx(checkboxStyles.Root, className),
|
|
77
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
78
|
+
...props,
|
|
79
|
+
ref: setInput,
|
|
80
|
+
type: "checkbox",
|
|
81
|
+
className: checkboxStyles.Input
|
|
82
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
83
|
+
className: checkboxStyles.Surface,
|
|
84
|
+
"aria-hidden": "true",
|
|
85
|
+
children: [/* @__PURE__ */ jsx(Check, {
|
|
86
|
+
className: checkboxStyles.Check,
|
|
87
|
+
size: 12,
|
|
88
|
+
strokeWidth: 2.5
|
|
89
|
+
}), /* @__PURE__ */ jsx(Minus, {
|
|
90
|
+
className: checkboxStyles.Mixed,
|
|
91
|
+
size: 12,
|
|
92
|
+
strokeWidth: 2.5
|
|
93
|
+
})]
|
|
94
|
+
})]
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// src/components/Divider/styles.ts
|
|
98
|
+
var dividerStyles = {
|
|
99
|
+
Root: "pointer-events-none block flex-none",
|
|
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
|
+
}
|
|
108
|
+
};
|
|
109
|
+
// src/components/Divider/Divider.tsx
|
|
110
|
+
/** 贴边:绝对定位到容器的一条边,inset 为两端内缩。 */
|
|
111
|
+
function edgeGeometry(edge, horizontal, inset, length) {
|
|
112
|
+
return horizontal ? {
|
|
113
|
+
[edge]: 0,
|
|
114
|
+
left: inset,
|
|
115
|
+
right: inset,
|
|
116
|
+
width: length ?? "auto"
|
|
117
|
+
} : {
|
|
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
|
|
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);
|
|
143
|
+
return /* @__PURE__ */ jsx("span", {
|
|
144
|
+
...props,
|
|
145
|
+
"aria-hidden": "true",
|
|
146
|
+
"data-divider": "",
|
|
147
|
+
"data-orientation": direction,
|
|
148
|
+
"data-tone": tone,
|
|
149
|
+
"data-edge": edge,
|
|
150
|
+
className: clsx(dividerStyles.Root, dividerStyles.orientations[direction], dividerStyles.tones[tone], edge && "absolute", className),
|
|
151
|
+
style: {
|
|
152
|
+
...geometry,
|
|
153
|
+
...style
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
// src/components/ResizeHandle/styles.ts
|
|
158
|
+
var line = "pointer-events-none absolute inset-y-0 left-[calc((var(--hub-hit-area)_-_1px)_/_2)]";
|
|
159
|
+
var resizeHandleStyles = {
|
|
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
|
+
},
|
|
165
|
+
Line: line,
|
|
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"),
|
|
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%)]"),
|
|
168
|
+
DragShield: "fixed inset-0 z-(--hub-z-overlay) cursor-col-resize"
|
|
169
|
+
};
|
|
170
|
+
// src/components/ResizeHandle/ResizeHandle.tsx
|
|
171
|
+
function ResizeHandle({ label, controls, value, min, max, defaultValue, step = 16, showDivider = true, inset = 0, edge = "right", onChange, className, style, ...props }) {
|
|
172
|
+
const sign = edge === "right" ? 1 : -1;
|
|
173
|
+
const drag = useRef(void 0);
|
|
174
|
+
const feedback = useRef(null);
|
|
175
|
+
const trackPointer = (event) => {
|
|
176
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
177
|
+
if (feedback.current) feedback.current.style.transform = `translateY(${event.clientY - rect.top - rect.height / 2}px)`;
|
|
178
|
+
};
|
|
179
|
+
const [dragging, setDragging] = useState(false);
|
|
180
|
+
const resize = (width) => onChange(Math.max(min, Math.min(max, width)));
|
|
181
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
182
|
+
...props,
|
|
183
|
+
role: "separator",
|
|
184
|
+
"aria-label": label,
|
|
185
|
+
"aria-controls": controls,
|
|
186
|
+
"aria-orientation": "vertical",
|
|
187
|
+
"aria-valuemin": min,
|
|
188
|
+
"aria-valuemax": max,
|
|
189
|
+
"aria-valuenow": value,
|
|
190
|
+
tabIndex: 0,
|
|
191
|
+
className: clsx(resizeHandleStyles.Root, resizeHandleStyles.edges[edge], className),
|
|
192
|
+
style: {
|
|
193
|
+
insetBlock: inset,
|
|
194
|
+
...style
|
|
195
|
+
},
|
|
196
|
+
"data-resize-handle": true,
|
|
197
|
+
"data-dragging": dragging,
|
|
198
|
+
onDoubleClick: () => {
|
|
199
|
+
if (defaultValue !== void 0) resize(defaultValue);
|
|
200
|
+
},
|
|
201
|
+
onKeyDown: (event) => {
|
|
202
|
+
const width = {
|
|
203
|
+
Home: min,
|
|
204
|
+
End: max,
|
|
205
|
+
ArrowLeft: value - step * sign,
|
|
206
|
+
ArrowRight: value + step * sign
|
|
207
|
+
}[event.key];
|
|
208
|
+
if (width === void 0) return;
|
|
209
|
+
event.preventDefault();
|
|
210
|
+
event.stopPropagation();
|
|
211
|
+
resize(width);
|
|
212
|
+
},
|
|
213
|
+
onPointerEnter: trackPointer,
|
|
214
|
+
onPointerDown: (event) => {
|
|
215
|
+
if (event.button !== 0 || !event.isPrimary || drag.current) return;
|
|
216
|
+
event.preventDefault();
|
|
217
|
+
trackPointer(event);
|
|
218
|
+
event.currentTarget.focus();
|
|
219
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
220
|
+
drag.current = {
|
|
221
|
+
pointerId: event.pointerId,
|
|
222
|
+
x: event.clientX,
|
|
223
|
+
width: value
|
|
224
|
+
};
|
|
225
|
+
setDragging(true);
|
|
226
|
+
},
|
|
227
|
+
onPointerMove: (event) => {
|
|
228
|
+
trackPointer(event);
|
|
229
|
+
const current = drag.current;
|
|
230
|
+
if (current?.pointerId !== event.pointerId) return;
|
|
231
|
+
resize(current.width + (event.clientX - current.x) * sign);
|
|
232
|
+
},
|
|
233
|
+
onPointerUp: (event) => {
|
|
234
|
+
if (drag.current?.pointerId !== event.pointerId) return;
|
|
235
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
|
|
236
|
+
drag.current = void 0;
|
|
237
|
+
setDragging(false);
|
|
238
|
+
},
|
|
239
|
+
onLostPointerCapture: () => {
|
|
240
|
+
drag.current = void 0;
|
|
241
|
+
setDragging(false);
|
|
242
|
+
},
|
|
243
|
+
onPointerCancel: () => {
|
|
244
|
+
drag.current = void 0;
|
|
245
|
+
setDragging(false);
|
|
246
|
+
},
|
|
247
|
+
children: [
|
|
248
|
+
showDivider && /* @__PURE__ */ jsx(Divider, {
|
|
249
|
+
orientation: "vertical",
|
|
250
|
+
className: resizeHandleStyles.Line
|
|
251
|
+
}),
|
|
252
|
+
/* @__PURE__ */ jsx("span", {
|
|
253
|
+
"aria-hidden": true,
|
|
254
|
+
className: resizeHandleStyles.Feedback,
|
|
255
|
+
children: /* @__PURE__ */ jsx("span", {
|
|
256
|
+
ref: feedback,
|
|
257
|
+
className: resizeHandleStyles.Gradient
|
|
258
|
+
})
|
|
259
|
+
}),
|
|
260
|
+
dragging ? /* @__PURE__ */ jsx("div", {
|
|
261
|
+
"aria-hidden": true,
|
|
262
|
+
className: resizeHandleStyles.DragShield
|
|
263
|
+
}) : null
|
|
264
|
+
]
|
|
265
|
+
});
|
|
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/components/Toast/Toast.tsx
|
|
284
|
+
/** 读取 CSS 变量里的时长(ms 或 s),返回毫秒;动效时长只在样式中定义。 */
|
|
285
|
+
function cssDuration(element, name) {
|
|
286
|
+
const value = getComputedStyle(element).getPropertyValue(name).trim();
|
|
287
|
+
return (value.endsWith("ms") ? parseFloat(value) : parseFloat(value) * 1e3) || 0;
|
|
288
|
+
}
|
|
289
|
+
var reducedMotion = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
290
|
+
function Toast({ message, description, variant = "info", action, duration = variant === "error" ? 8e3 : 4e3, revision = 0, closeLabel, onDismiss, className, onMouseEnter, onMouseLeave, onFocus, onBlur, ...props }) {
|
|
291
|
+
const card = useRef(null);
|
|
292
|
+
const previousRevision = useRef(revision);
|
|
293
|
+
const dismiss = useRef(onDismiss);
|
|
294
|
+
dismiss.current = onDismiss;
|
|
295
|
+
const [hovered, setHovered] = useState(false);
|
|
296
|
+
const [focused, setFocused] = useState(false);
|
|
297
|
+
const [busy, setBusy] = useState(false);
|
|
298
|
+
const [closing, setClosing] = useState(false);
|
|
299
|
+
const paused = hovered || focused;
|
|
300
|
+
useEffect(() => {
|
|
301
|
+
if (previousRevision.current === revision) return;
|
|
302
|
+
previousRevision.current = revision;
|
|
303
|
+
setClosing(false);
|
|
304
|
+
const element = card.current;
|
|
305
|
+
if (!element || reducedMotion()) return;
|
|
306
|
+
const animation = element.animate([
|
|
307
|
+
{
|
|
308
|
+
transform: "scale(1)",
|
|
309
|
+
easing: "ease-out",
|
|
310
|
+
offset: 0
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
transform: "scale(1.05)",
|
|
314
|
+
easing: "linear",
|
|
315
|
+
offset: .125
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
transform: "scale(1.05)",
|
|
319
|
+
easing: "ease-in-out",
|
|
320
|
+
offset: .25
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
transform: "scale(1)",
|
|
324
|
+
offset: 1
|
|
325
|
+
}
|
|
326
|
+
], { duration: cssDuration(element, "--hub-duration-emphasis") });
|
|
327
|
+
return () => animation.cancel();
|
|
328
|
+
}, [revision]);
|
|
329
|
+
useEffect(() => {
|
|
330
|
+
if (!duration || paused || busy) return;
|
|
331
|
+
const timer = setTimeout(() => setClosing(true), duration);
|
|
332
|
+
return () => clearTimeout(timer);
|
|
333
|
+
}, [
|
|
334
|
+
duration,
|
|
335
|
+
revision,
|
|
336
|
+
paused,
|
|
337
|
+
busy
|
|
338
|
+
]);
|
|
339
|
+
useEffect(() => {
|
|
340
|
+
if (!closing) return;
|
|
341
|
+
let cancelled = false;
|
|
342
|
+
const animations = card.current?.parentElement?.getAnimations() ?? [];
|
|
343
|
+
Promise.allSettled(animations.map((animation) => animation.finished)).then(() => {
|
|
344
|
+
if (!cancelled) dismiss.current();
|
|
345
|
+
});
|
|
346
|
+
return () => {
|
|
347
|
+
cancelled = true;
|
|
348
|
+
};
|
|
349
|
+
}, [closing]);
|
|
350
|
+
const runAction = async () => {
|
|
351
|
+
if (busy || !action) return;
|
|
352
|
+
setBusy(true);
|
|
353
|
+
try {
|
|
354
|
+
await action.onClick();
|
|
355
|
+
setClosing(true);
|
|
356
|
+
} finally {
|
|
357
|
+
setBusy(false);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
return /* @__PURE__ */ jsx("div", {
|
|
361
|
+
...props,
|
|
362
|
+
className: clsx(toastStyles.Root, className),
|
|
363
|
+
"data-closing": closing || void 0,
|
|
364
|
+
onMouseEnter: (event) => {
|
|
365
|
+
setHovered(true);
|
|
366
|
+
onMouseEnter?.(event);
|
|
367
|
+
},
|
|
368
|
+
onMouseLeave: (event) => {
|
|
369
|
+
setHovered(false);
|
|
370
|
+
onMouseLeave?.(event);
|
|
371
|
+
},
|
|
372
|
+
onFocus: (event) => {
|
|
373
|
+
setFocused(true);
|
|
374
|
+
onFocus?.(event);
|
|
375
|
+
},
|
|
376
|
+
onBlur: (event) => {
|
|
377
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
|
|
378
|
+
onBlur?.(event);
|
|
379
|
+
},
|
|
380
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
381
|
+
ref: card,
|
|
382
|
+
className: toastStyles.Card,
|
|
383
|
+
"data-variant": variant,
|
|
384
|
+
"data-expanded": Boolean(description || action) || void 0,
|
|
385
|
+
children: [
|
|
386
|
+
/* @__PURE__ */ jsx("span", {
|
|
387
|
+
className: toastStyles.icons[variant],
|
|
388
|
+
children: variant === "success" ? /* @__PURE__ */ jsx(Check, {
|
|
389
|
+
size: 11,
|
|
390
|
+
strokeWidth: 3,
|
|
391
|
+
"aria-hidden": true
|
|
392
|
+
}) : variant === "error" ? /* @__PURE__ */ jsx(CircleAlert, {
|
|
393
|
+
size: 16,
|
|
394
|
+
"aria-hidden": true
|
|
395
|
+
}) : /* @__PURE__ */ jsx(Info, {
|
|
396
|
+
size: 16,
|
|
397
|
+
"aria-hidden": true
|
|
398
|
+
})
|
|
399
|
+
}),
|
|
400
|
+
/* @__PURE__ */ jsxs("div", {
|
|
401
|
+
className: toastStyles.Content,
|
|
402
|
+
children: [
|
|
403
|
+
/* @__PURE__ */ jsx("div", {
|
|
404
|
+
role: variant === "error" ? "alert" : "status",
|
|
405
|
+
"aria-atomic": "true",
|
|
406
|
+
className: toastStyles.Message,
|
|
407
|
+
children: message
|
|
408
|
+
}),
|
|
409
|
+
description && /* @__PURE__ */ jsx("div", {
|
|
410
|
+
className: toastStyles.Description,
|
|
411
|
+
children: description
|
|
412
|
+
}),
|
|
413
|
+
action && /* @__PURE__ */ jsx("button", {
|
|
414
|
+
type: "button",
|
|
415
|
+
className: toastStyles.Action,
|
|
416
|
+
disabled: busy,
|
|
417
|
+
onClick: () => void runAction(),
|
|
418
|
+
children: action.label
|
|
419
|
+
})
|
|
420
|
+
]
|
|
421
|
+
}),
|
|
422
|
+
/* @__PURE__ */ jsx(IconButton, {
|
|
423
|
+
variant: "ghost",
|
|
424
|
+
size: "sm",
|
|
425
|
+
className: toastStyles.Close,
|
|
426
|
+
"aria-label": closeLabel,
|
|
427
|
+
disabled: busy,
|
|
428
|
+
onClick: () => setClosing(true),
|
|
429
|
+
children: /* @__PURE__ */ jsx(X, { "aria-hidden": true })
|
|
430
|
+
})
|
|
431
|
+
]
|
|
432
|
+
})
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
// src/components/Toast/Toaster.tsx
|
|
436
|
+
function Toaster({ store, closeLabel, className, ...props }) {
|
|
437
|
+
const items = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
|
|
438
|
+
if (items.length === 0) return null;
|
|
439
|
+
return /* @__PURE__ */ jsx("div", {
|
|
440
|
+
role: "region",
|
|
441
|
+
...props,
|
|
442
|
+
className: clsx(toastStyles.Viewport, className),
|
|
443
|
+
children: items.map(({ id, options: { onClose: _onClose, ...options }, revision }) => /* @__PURE__ */ jsx(Toast, {
|
|
444
|
+
...options,
|
|
445
|
+
revision,
|
|
446
|
+
closeLabel,
|
|
447
|
+
onDismiss: () => store.dismiss(id)
|
|
448
|
+
}, id))
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
// src/components/Toast/ToastStore.ts
|
|
452
|
+
var isRepeat = (a, b) => a.message === b.message && a.description === b.description && (a.variant ?? "info") === (b.variant ?? "info") && !a.action && !b.action;
|
|
453
|
+
/** Toast 的状态容器,不依赖 React;由应用创建并交给 Toaster 渲染。 */
|
|
454
|
+
var ToastStore = class {
|
|
455
|
+
items = [];
|
|
456
|
+
listeners = /* @__PURE__ */ new Set();
|
|
457
|
+
max;
|
|
458
|
+
nextId = 0;
|
|
459
|
+
constructor(options = {}) {
|
|
460
|
+
this.max = Math.max(1, options.max ?? 1);
|
|
461
|
+
}
|
|
462
|
+
subscribe = (listener) => {
|
|
463
|
+
this.listeners.add(listener);
|
|
464
|
+
return () => {
|
|
465
|
+
this.listeners.delete(listener);
|
|
466
|
+
};
|
|
467
|
+
};
|
|
468
|
+
getSnapshot = () => this.items;
|
|
469
|
+
show(options) {
|
|
470
|
+
const repeated = this.items.find((item) => isRepeat(item.options, options));
|
|
471
|
+
if (repeated) {
|
|
472
|
+
this.commit(this.items.map((item) => item === repeated ? {
|
|
473
|
+
...item,
|
|
474
|
+
options,
|
|
475
|
+
revision: item.revision + 1
|
|
476
|
+
} : item));
|
|
477
|
+
return repeated.id;
|
|
478
|
+
}
|
|
479
|
+
const id = `toast-${++this.nextId}`;
|
|
480
|
+
this.commit([...this.items, {
|
|
481
|
+
id,
|
|
482
|
+
options,
|
|
483
|
+
revision: 0
|
|
484
|
+
}].slice(-this.max));
|
|
485
|
+
return id;
|
|
486
|
+
}
|
|
487
|
+
/** 不传 id 时关闭全部。 */
|
|
488
|
+
dismiss(id) {
|
|
489
|
+
const next = id === void 0 ? [] : this.items.filter((item) => item.id !== id);
|
|
490
|
+
if (next.length !== this.items.length) this.commit(next);
|
|
491
|
+
}
|
|
492
|
+
commit(next) {
|
|
493
|
+
const removed = this.items.filter((item) => !next.some(({ id }) => id === item.id));
|
|
494
|
+
this.items = next;
|
|
495
|
+
for (const listener of [...this.listeners]) listener();
|
|
496
|
+
for (const item of removed) item.options.onClose?.();
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
export { Button, Checkbox, Divider, IconButton, ResizeHandle, Toast, ToastStore, Toaster };
|