@zyncat/ui 0.8.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/LICENSE +21 -0
- package/README.md +133 -0
- package/dist/alert.css +132 -0
- package/dist/alert.d.ts +46 -0
- package/dist/alert.js +80 -0
- package/dist/alert.js.map +1 -0
- package/dist/avatar-group.d.ts +24 -0
- package/dist/avatar-group.js +23 -0
- package/dist/avatar-group.js.map +1 -0
- package/dist/avatar.css +200 -0
- package/dist/avatar.d.ts +35 -0
- package/dist/avatar.js +67 -0
- package/dist/avatar.js.map +1 -0
- package/dist/badge.css +182 -0
- package/dist/badge.d.ts +34 -0
- package/dist/badge.js +4 -0
- package/dist/badge.js.map +1 -0
- package/dist/button.css +269 -0
- package/dist/button.d.ts +44 -0
- package/dist/button.js +4 -0
- package/dist/button.js.map +1 -0
- package/dist/checkbox.css +177 -0
- package/dist/checkbox.d.ts +35 -0
- package/dist/checkbox.js +5 -0
- package/dist/checkbox.js.map +1 -0
- package/dist/chunk-2ERBBFZS.js +12 -0
- package/dist/chunk-2ERBBFZS.js.map +1 -0
- package/dist/chunk-2HORSLXJ.js +191 -0
- package/dist/chunk-2HORSLXJ.js.map +1 -0
- package/dist/chunk-36WVAQKL.js +40 -0
- package/dist/chunk-36WVAQKL.js.map +1 -0
- package/dist/chunk-3SBCBYB7.js +575 -0
- package/dist/chunk-3SBCBYB7.js.map +1 -0
- package/dist/chunk-4R2GQO43.js +125 -0
- package/dist/chunk-4R2GQO43.js.map +1 -0
- package/dist/chunk-64UU4LGU.js +258 -0
- package/dist/chunk-64UU4LGU.js.map +1 -0
- package/dist/chunk-7SZCJ7WI.js +35 -0
- package/dist/chunk-7SZCJ7WI.js.map +1 -0
- package/dist/chunk-BIDVNLJO.js +38 -0
- package/dist/chunk-BIDVNLJO.js.map +1 -0
- package/dist/chunk-CONSWNIV.js +17 -0
- package/dist/chunk-CONSWNIV.js.map +1 -0
- package/dist/chunk-EQ32F2QJ.js +75 -0
- package/dist/chunk-EQ32F2QJ.js.map +1 -0
- package/dist/chunk-GY7JOPUQ.js +111 -0
- package/dist/chunk-GY7JOPUQ.js.map +1 -0
- package/dist/chunk-I4XQ4PV2.js +30 -0
- package/dist/chunk-I4XQ4PV2.js.map +1 -0
- package/dist/chunk-JSKXZMLH.js +67 -0
- package/dist/chunk-JSKXZMLH.js.map +1 -0
- package/dist/chunk-L3Q3VDIW.js +295 -0
- package/dist/chunk-L3Q3VDIW.js.map +1 -0
- package/dist/chunk-LDC3RIYA.js +62 -0
- package/dist/chunk-LDC3RIYA.js.map +1 -0
- package/dist/chunk-MKIQFK7S.js +43 -0
- package/dist/chunk-MKIQFK7S.js.map +1 -0
- package/dist/chunk-MMRIQNEN.js +20 -0
- package/dist/chunk-MMRIQNEN.js.map +1 -0
- package/dist/chunk-MXOPCFWR.js +59 -0
- package/dist/chunk-MXOPCFWR.js.map +1 -0
- package/dist/chunk-NEF5XAHG.js +17 -0
- package/dist/chunk-NEF5XAHG.js.map +1 -0
- package/dist/chunk-NNXJCLTV.js +22 -0
- package/dist/chunk-NNXJCLTV.js.map +1 -0
- package/dist/chunk-O4N2OBTR.js +94 -0
- package/dist/chunk-O4N2OBTR.js.map +1 -0
- package/dist/chunk-PMECHR3A.js +38 -0
- package/dist/chunk-PMECHR3A.js.map +1 -0
- package/dist/chunk-QDK66S4G.js +44 -0
- package/dist/chunk-QDK66S4G.js.map +1 -0
- package/dist/chunk-QGNC7LH5.js +8 -0
- package/dist/chunk-QGNC7LH5.js.map +1 -0
- package/dist/chunk-QMSJZCK3.js +398 -0
- package/dist/chunk-QMSJZCK3.js.map +1 -0
- package/dist/chunk-S5ZTZANS.js +57 -0
- package/dist/chunk-S5ZTZANS.js.map +1 -0
- package/dist/chunk-S7B34VGN.js +14 -0
- package/dist/chunk-S7B34VGN.js.map +1 -0
- package/dist/chunk-TCUCYFT7.js +25 -0
- package/dist/chunk-TCUCYFT7.js.map +1 -0
- package/dist/chunk-TYHR4VNM.js +193 -0
- package/dist/chunk-TYHR4VNM.js.map +1 -0
- package/dist/chunk-U5QQZC3U.js +56 -0
- package/dist/chunk-U5QQZC3U.js.map +1 -0
- package/dist/chunk-UE63OOVG.js +13 -0
- package/dist/chunk-UE63OOVG.js.map +1 -0
- package/dist/chunk-VTJTGVZZ.js +22 -0
- package/dist/chunk-VTJTGVZZ.js.map +1 -0
- package/dist/chunk-W3SHPKSB.js +49 -0
- package/dist/chunk-W3SHPKSB.js.map +1 -0
- package/dist/chunk-XZAEIQDR.js +188 -0
- package/dist/chunk-XZAEIQDR.js.map +1 -0
- package/dist/chunk-YGGCQNNU.js +92 -0
- package/dist/chunk-YGGCQNNU.js.map +1 -0
- package/dist/collapse.css +88 -0
- package/dist/collapse.d.ts +46 -0
- package/dist/collapse.js +5 -0
- package/dist/collapse.js.map +1 -0
- package/dist/count-badge.d.ts +13 -0
- package/dist/count-badge.js +13 -0
- package/dist/count-badge.js.map +1 -0
- package/dist/date-field.d.ts +25 -0
- package/dist/date-field.js +87 -0
- package/dist/date-field.js.map +1 -0
- package/dist/date-picker.css +595 -0
- package/dist/date-range-field.d.ts +33 -0
- package/dist/date-range-field.js +404 -0
- package/dist/date-range-field.js.map +1 -0
- package/dist/datetime-field.d.ts +29 -0
- package/dist/datetime-field.js +155 -0
- package/dist/datetime-field.js.map +1 -0
- package/dist/dialog.css +180 -0
- package/dist/dialog.d.ts +42 -0
- package/dist/dialog.js +128 -0
- package/dist/dialog.js.map +1 -0
- package/dist/dom-props-C0jrllBw.d.ts +5 -0
- package/dist/field-chrome-CMPOQSLs.d.ts +28 -0
- package/dist/field-shell-Cuda-feL.d.ts +24 -0
- package/dist/glide.d.ts +15 -0
- package/dist/glide.js +5 -0
- package/dist/glide.js.map +1 -0
- package/dist/icon.css +33 -0
- package/dist/input.css +333 -0
- package/dist/mcp.js +359 -0
- package/dist/motion-devtools.css +265 -0
- package/dist/motion-devtools.d.ts +51 -0
- package/dist/motion-devtools.js +354 -0
- package/dist/motion-devtools.js.map +1 -0
- package/dist/motion-scale-Canq4stZ.d.ts +18 -0
- package/dist/motion-tokens.d.ts +28 -0
- package/dist/motion-tokens.js +3 -0
- package/dist/motion-tokens.js.map +1 -0
- package/dist/multi-select.d.ts +44 -0
- package/dist/multi-select.js +109 -0
- package/dist/multi-select.js.map +1 -0
- package/dist/number-field.d.ts +36 -0
- package/dist/number-field.js +112 -0
- package/dist/number-field.js.map +1 -0
- package/dist/odometer.css +62 -0
- package/dist/otp-field.d.ts +31 -0
- package/dist/otp-field.js +98 -0
- package/dist/otp-field.js.map +1 -0
- package/dist/overlay.css +23 -0
- package/dist/pagination.css +43 -0
- package/dist/pagination.d.ts +29 -0
- package/dist/pagination.js +107 -0
- package/dist/pagination.js.map +1 -0
- package/dist/popover.css +69 -0
- package/dist/popover.d.ts +35 -0
- package/dist/popover.js +15 -0
- package/dist/popover.js.map +1 -0
- package/dist/radio-group.css +300 -0
- package/dist/radio-group.d.ts +52 -0
- package/dist/radio-group.js +115 -0
- package/dist/radio-group.js.map +1 -0
- package/dist/select.css +416 -0
- package/dist/select.d.ts +49 -0
- package/dist/select.js +99 -0
- package/dist/select.js.map +1 -0
- package/dist/sheet.css +26 -0
- package/dist/sheet.d.ts +31 -0
- package/dist/sheet.js +14 -0
- package/dist/sheet.js.map +1 -0
- package/dist/status-badge.d.ts +19 -0
- package/dist/status-badge.js +72 -0
- package/dist/status-badge.js.map +1 -0
- package/dist/table.css +348 -0
- package/dist/table.d.ts +75 -0
- package/dist/table.js +262 -0
- package/dist/table.js.map +1 -0
- package/dist/tabs.css +208 -0
- package/dist/tabs.d.ts +71 -0
- package/dist/tabs.js +181 -0
- package/dist/tabs.js.map +1 -0
- package/dist/tag.css +248 -0
- package/dist/tag.d.ts +52 -0
- package/dist/tag.js +72 -0
- package/dist/tag.js.map +1 -0
- package/dist/text-field.d.ts +28 -0
- package/dist/text-field.js +69 -0
- package/dist/text-field.js.map +1 -0
- package/dist/textarea.css +219 -0
- package/dist/textarea.d.ts +38 -0
- package/dist/textarea.js +144 -0
- package/dist/textarea.js.map +1 -0
- package/dist/time-field.d.ts +28 -0
- package/dist/time-field.js +66 -0
- package/dist/time-field.js.map +1 -0
- package/dist/timing-7JA_zQHP.d.ts +27 -0
- package/dist/toast-store.d.ts +86 -0
- package/dist/toast-store.js +3 -0
- package/dist/toast-store.js.map +1 -0
- package/dist/toast.css +257 -0
- package/dist/toast.d.ts +12 -0
- package/dist/toast.js +310 -0
- package/dist/toast.js.map +1 -0
- package/dist/toggle-tag.d.ts +34 -0
- package/dist/toggle-tag.js +57 -0
- package/dist/toggle-tag.js.map +1 -0
- package/dist/toggle.css +152 -0
- package/dist/toggle.d.ts +31 -0
- package/dist/toggle.js +75 -0
- package/dist/toggle.js.map +1 -0
- package/dist/tooltip.css +86 -0
- package/dist/tooltip.d.ts +30 -0
- package/dist/tooltip.js +268 -0
- package/dist/tooltip.js.map +1 -0
- package/dist/types-SGMShK7R.d.ts +22 -0
- package/llms.txt +196 -0
- package/next.d.mts +7 -0
- package/next.mjs +59 -0
- package/package.json +257 -0
- package/src/components/composites/alert/Alert.tsx +141 -0
- package/src/components/composites/alert/alert.css +132 -0
- package/src/components/composites/date-picker/DateField.tsx +80 -0
- package/src/components/composites/date-picker/DateRangeField.tsx +110 -0
- package/src/components/composites/date-picker/DateTimeField.tsx +162 -0
- package/src/components/composites/date-picker/TimeField.tsx +78 -0
- package/src/components/composites/date-picker/calendar-panel.tsx +216 -0
- package/src/components/composites/date-picker/date-picker.css +595 -0
- package/src/components/composites/date-picker/date-utils.ts +56 -0
- package/src/components/composites/date-picker/field-shell.tsx +85 -0
- package/src/components/composites/date-picker/range-panel.tsx +363 -0
- package/src/components/composites/date-picker/time-core.tsx +290 -0
- package/src/components/composites/date-picker/use-day-focus.ts +35 -0
- package/src/components/composites/dialog/Dialog.tsx +198 -0
- package/src/components/composites/dialog/dialog.css +180 -0
- package/src/components/composites/pagination/Pagination.tsx +131 -0
- package/src/components/composites/pagination/pagination.css +43 -0
- package/src/components/composites/popover/Popover.tsx +141 -0
- package/src/components/composites/popover/popover.css +69 -0
- package/src/components/composites/select/MultiSelect.tsx +133 -0
- package/src/components/composites/select/Select.tsx +122 -0
- package/src/components/composites/select/core/index.ts +4 -0
- package/src/components/composites/select/core/menu.tsx +100 -0
- package/src/components/composites/select/core/panel.tsx +154 -0
- package/src/components/composites/select/core/trigger.tsx +74 -0
- package/src/components/composites/select/core/types.ts +39 -0
- package/src/components/composites/select/core/use-listbox.ts +182 -0
- package/src/components/composites/select/select.css +416 -0
- package/src/components/composites/sheet/Sheet.tsx +135 -0
- package/src/components/composites/sheet/sheet.css +26 -0
- package/src/components/composites/sheet/use-sheet-drag.ts +126 -0
- package/src/components/composites/table/Table.tsx +377 -0
- package/src/components/composites/table/table.css +348 -0
- package/src/components/composites/tabs/Tabs.tsx +278 -0
- package/src/components/composites/tabs/tabs.css +208 -0
- package/src/components/composites/toast/Toast.tsx +380 -0
- package/src/components/composites/toast/toast-store.ts +295 -0
- package/src/components/composites/toast/toast.css +257 -0
- package/src/components/composites/tooltip/Tooltip.tsx +110 -0
- package/src/components/composites/tooltip/tooltip-host.tsx +184 -0
- package/src/components/composites/tooltip/tooltip-store.ts +81 -0
- package/src/components/composites/tooltip/tooltip.css +86 -0
- package/src/components/dev/MotionDevtools.tsx +200 -0
- package/src/components/dev/motion-devtools.css +265 -0
- package/src/components/dev/slowmo-engine.ts +230 -0
- package/src/components/internal/glass/glass.css +100 -0
- package/src/components/internal/glass/glint.ts +9 -0
- package/src/components/internal/hooks/use-controllable.ts +21 -0
- package/src/components/internal/hooks/use-scroll-edges.ts +46 -0
- package/src/components/internal/icon/Icon.tsx +66 -0
- package/src/components/internal/icon/IconSlot.tsx +10 -0
- package/src/components/internal/icon/icon.css +33 -0
- package/src/components/internal/overlay/focus.ts +68 -0
- package/src/components/internal/overlay/layer.tsx +149 -0
- package/src/components/internal/overlay/modal.tsx +141 -0
- package/src/components/internal/overlay/overlay.css +23 -0
- package/src/components/internal/overlay/position.ts +103 -0
- package/src/components/internal/utils/cx.ts +3 -0
- package/src/components/internal/utils/token-px.ts +8 -0
- package/src/components/primitives/avatar/Avatar.tsx +120 -0
- package/src/components/primitives/avatar/AvatarGroup.tsx +56 -0
- package/src/components/primitives/avatar/avatar.css +200 -0
- package/src/components/primitives/badge/Badge.tsx +75 -0
- package/src/components/primitives/badge/CountBadge.tsx +20 -0
- package/src/components/primitives/badge/StatusBadge.tsx +111 -0
- package/src/components/primitives/badge/badge.css +182 -0
- package/src/components/primitives/badge/odometer.css +62 -0
- package/src/components/primitives/badge/odometer.tsx +36 -0
- package/src/components/primitives/button/Button.tsx +102 -0
- package/src/components/primitives/button/button.css +269 -0
- package/src/components/primitives/checkbox/Checkbox.tsx +76 -0
- package/src/components/primitives/checkbox/check-glyph.tsx +28 -0
- package/src/components/primitives/checkbox/checkbox.css +177 -0
- package/src/components/primitives/collapse/Collapse.tsx +86 -0
- package/src/components/primitives/collapse/collapse.css +88 -0
- package/src/components/primitives/collapse/collapse.typecheck.tsx +29 -0
- package/src/components/primitives/input/NumberField.tsx +139 -0
- package/src/components/primitives/input/OtpField.tsx +134 -0
- package/src/components/primitives/input/TextField.tsx +110 -0
- package/src/components/primitives/input/field-chrome.tsx +83 -0
- package/src/components/primitives/input/input.css +333 -0
- package/src/components/primitives/radio-group/RadioGroup.tsx +183 -0
- package/src/components/primitives/radio-group/radio-group.css +300 -0
- package/src/components/primitives/tag/Tag.tsx +143 -0
- package/src/components/primitives/tag/ToggleTag.tsx +97 -0
- package/src/components/primitives/tag/tag.css +248 -0
- package/src/components/primitives/textarea/Textarea.tsx +190 -0
- package/src/components/primitives/textarea/textarea.css +219 -0
- package/src/components/primitives/toggle/Toggle.tsx +105 -0
- package/src/components/primitives/toggle/toggle.css +152 -0
- package/src/css.d.ts +1 -0
- package/src/dom-props.ts +1 -0
- package/src/engine/animate.ts +141 -0
- package/src/engine/drag.ts +53 -0
- package/src/engine/ease.ts +155 -0
- package/src/engine/flip.ts +59 -0
- package/src/engine/index.ts +13 -0
- package/src/mcp/server.ts +420 -0
- package/src/motion/element.tsx +53 -0
- package/src/motion/flip.tsx +87 -0
- package/src/motion/glide.tsx +47 -0
- package/src/motion/motion-timing.ts +33 -0
- package/src/motion/presence-context.ts +18 -0
- package/src/motion/presence.tsx +91 -0
- package/src/motion/timing.ts +49 -0
- package/src/motion/use-motion.ts +64 -0
- package/src/styles.css +26 -0
- package/src/tokens/avatar.css +20 -0
- package/src/tokens/color.css +58 -0
- package/src/tokens/elevation.css +22 -0
- package/src/tokens/fonts.css +3 -0
- package/src/tokens/glass.css +20 -0
- package/src/tokens/icons.css +7 -0
- package/src/tokens/layers.css +6 -0
- package/src/tokens/motion-scale.ts +17 -0
- package/src/tokens/motion-tokens.ts +92 -0
- package/src/tokens/motion.css +49 -0
- package/src/tokens/radius.css +11 -0
- package/src/tokens/semantic.css +65 -0
- package/src/tokens/spacing.css +26 -0
- package/src/tokens/typography.css +52 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
export type ToastTone = 'default' | 'success' | 'danger' | 'warning' | 'info' | 'loading' | 'custom';
|
|
2
|
+
|
|
3
|
+
export interface ToastAction {
|
|
4
|
+
label: string;
|
|
5
|
+
onClick: () => void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ToastOptions {
|
|
9
|
+
id?: string | number;
|
|
10
|
+
duration?: number;
|
|
11
|
+
description?: string | null;
|
|
12
|
+
action?: ToastAction | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ToastRecord {
|
|
16
|
+
id: string;
|
|
17
|
+
tone: ToastTone;
|
|
18
|
+
message: string | null;
|
|
19
|
+
description: string | null;
|
|
20
|
+
action: ToastAction | null;
|
|
21
|
+
duration: number;
|
|
22
|
+
remaining: number;
|
|
23
|
+
count: number;
|
|
24
|
+
timerKey: number;
|
|
25
|
+
node: unknown;
|
|
26
|
+
dismissible: boolean;
|
|
27
|
+
expiresAt?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ToastSnapshot {
|
|
31
|
+
toasts: ToastRecord[];
|
|
32
|
+
paused: boolean;
|
|
33
|
+
expanded: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type ToastPosition = 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
|
|
37
|
+
|
|
38
|
+
export interface ToasterConfig {
|
|
39
|
+
position: ToastPosition;
|
|
40
|
+
duration: number;
|
|
41
|
+
visibleToasts: number;
|
|
42
|
+
gap: number;
|
|
43
|
+
offset: number;
|
|
44
|
+
expand: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const DEFAULT_TOASTER_CONFIG: ToasterConfig = {
|
|
48
|
+
position: 'bottom-right',
|
|
49
|
+
duration: 0,
|
|
50
|
+
visibleToasts: 3,
|
|
51
|
+
gap: 0,
|
|
52
|
+
offset: 0,
|
|
53
|
+
expand: false,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const DURATION: Record<string, number> = {
|
|
57
|
+
default: 5000,
|
|
58
|
+
success: 5000,
|
|
59
|
+
info: 5000,
|
|
60
|
+
warning: 8000,
|
|
61
|
+
danger: 8000,
|
|
62
|
+
loading: Infinity,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
let uid = 0;
|
|
66
|
+
const timers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
67
|
+
|
|
68
|
+
interface ToastStore {
|
|
69
|
+
toasts: ToastRecord[];
|
|
70
|
+
paused: boolean;
|
|
71
|
+
expanded: boolean;
|
|
72
|
+
mounted: boolean;
|
|
73
|
+
config: ToasterConfig;
|
|
74
|
+
snap: ToastSnapshot;
|
|
75
|
+
listeners: Set<() => void>;
|
|
76
|
+
subscribe(l: () => void): () => void;
|
|
77
|
+
get(): ToastSnapshot;
|
|
78
|
+
emit(): void;
|
|
79
|
+
add(t: ToastRecord): string;
|
|
80
|
+
update(id: string, patch: Partial<ToastRecord>): string;
|
|
81
|
+
dismiss(id?: string | null): void;
|
|
82
|
+
schedule(t: ToastRecord): void;
|
|
83
|
+
pause(): void;
|
|
84
|
+
resume(): void;
|
|
85
|
+
setExpanded(v: boolean): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const store: ToastStore = {
|
|
89
|
+
toasts: [],
|
|
90
|
+
paused: false,
|
|
91
|
+
expanded: false,
|
|
92
|
+
mounted: false,
|
|
93
|
+
config: { ...DEFAULT_TOASTER_CONFIG },
|
|
94
|
+
snap: { toasts: [], paused: false, expanded: false },
|
|
95
|
+
listeners: new Set(),
|
|
96
|
+
subscribe(l) {
|
|
97
|
+
store.listeners.add(l);
|
|
98
|
+
return () => store.listeners.delete(l);
|
|
99
|
+
},
|
|
100
|
+
get: () => store.snap,
|
|
101
|
+
emit() {
|
|
102
|
+
store.snap = { toasts: store.toasts.slice(), paused: store.paused, expanded: store.expanded };
|
|
103
|
+
store.listeners.forEach((l) => l());
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
add(t) {
|
|
107
|
+
const twin = store.toasts.find(
|
|
108
|
+
(x) => !x.node && !t.node && x.tone === t.tone && x.message === t.message && x.description === t.description,
|
|
109
|
+
);
|
|
110
|
+
if (twin) return store.update(twin.id, { count: twin.count + 1 });
|
|
111
|
+
store.toasts.push(t);
|
|
112
|
+
store.schedule(t);
|
|
113
|
+
store.emit();
|
|
114
|
+
return t.id;
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
update(id, patch) {
|
|
118
|
+
const t = store.toasts.find((x) => x.id === id);
|
|
119
|
+
if (!t) return id;
|
|
120
|
+
Object.assign(t, patch);
|
|
121
|
+
if ('tone' in patch || 'duration' in patch || 'count' in patch) {
|
|
122
|
+
t.remaining = t.duration;
|
|
123
|
+
t.timerKey++;
|
|
124
|
+
store.schedule(t);
|
|
125
|
+
}
|
|
126
|
+
store.emit();
|
|
127
|
+
return id;
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
dismiss(id) {
|
|
131
|
+
const gone = id == null ? store.toasts : store.toasts.filter((t) => t.id === id);
|
|
132
|
+
gone.forEach((t) => {
|
|
133
|
+
clearTimeout(timers.get(t.id));
|
|
134
|
+
timers.delete(t.id);
|
|
135
|
+
});
|
|
136
|
+
store.toasts = id == null ? [] : store.toasts.filter((t) => t.id !== id);
|
|
137
|
+
store.emit();
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
schedule(t) {
|
|
141
|
+
clearTimeout(timers.get(t.id));
|
|
142
|
+
timers.delete(t.id);
|
|
143
|
+
if (!isFinite(t.duration)) return;
|
|
144
|
+
if (store.paused) return;
|
|
145
|
+
t.expiresAt = performance.now() + t.remaining;
|
|
146
|
+
timers.set(
|
|
147
|
+
t.id,
|
|
148
|
+
setTimeout(() => store.dismiss(t.id), t.remaining),
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
pause() {
|
|
153
|
+
if (store.paused) return;
|
|
154
|
+
store.paused = true;
|
|
155
|
+
store.toasts.forEach((t) => {
|
|
156
|
+
if (timers.has(t.id)) {
|
|
157
|
+
clearTimeout(timers.get(t.id));
|
|
158
|
+
timers.delete(t.id);
|
|
159
|
+
t.remaining = Math.max(0, (t.expiresAt ?? performance.now()) - performance.now());
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
store.emit();
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
resume() {
|
|
166
|
+
if (!store.paused) return;
|
|
167
|
+
store.paused = false;
|
|
168
|
+
store.toasts.forEach((t) => store.schedule(t));
|
|
169
|
+
store.emit();
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
setExpanded(v) {
|
|
173
|
+
if (v === store.expanded) return;
|
|
174
|
+
store.expanded = v;
|
|
175
|
+
store.emit();
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
let warned = false;
|
|
180
|
+
let warnScheduled = false;
|
|
181
|
+
|
|
182
|
+
function warnIfDetached(): void {
|
|
183
|
+
if (store.mounted || warned || warnScheduled || typeof window === 'undefined') return;
|
|
184
|
+
warnScheduled = true;
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
warnScheduled = false;
|
|
187
|
+
if (store.mounted || warned) return;
|
|
188
|
+
warned = true;
|
|
189
|
+
console.warn(
|
|
190
|
+
'zyncat-ui: a toast was triggered but no <Toaster /> is mounted, so nothing will render. Mount <Toaster /> once near your app root.',
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function resolveDuration(tone: ToastTone, opt?: number): number {
|
|
196
|
+
if (opt != null) return opt;
|
|
197
|
+
const toneDefault = DURATION[tone];
|
|
198
|
+
if (toneDefault === Infinity || store.config.duration <= 0) return toneDefault;
|
|
199
|
+
return store.config.duration;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function make(tone: ToastTone, message: string | null, opts: ToastOptions = {}): string {
|
|
203
|
+
warnIfDetached();
|
|
204
|
+
const id = opts.id != null ? String(opts.id) : 'toast-' + ++uid;
|
|
205
|
+
const duration = resolveDuration(tone, opts.duration);
|
|
206
|
+
if (store.toasts.some((t) => t.id === id)) {
|
|
207
|
+
return store.update(id, {
|
|
208
|
+
tone,
|
|
209
|
+
message,
|
|
210
|
+
duration,
|
|
211
|
+
description: opts.description != null ? opts.description : null,
|
|
212
|
+
action: opts.action != null ? opts.action : null,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return store.add({
|
|
216
|
+
id,
|
|
217
|
+
tone,
|
|
218
|
+
message,
|
|
219
|
+
description: opts.description != null ? opts.description : null,
|
|
220
|
+
action: opts.action != null ? opts.action : null,
|
|
221
|
+
duration,
|
|
222
|
+
remaining: duration,
|
|
223
|
+
count: 1,
|
|
224
|
+
timerKey: 0,
|
|
225
|
+
node: null,
|
|
226
|
+
dismissible: true,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
type PromiseMsg<V> = string | ((v: V) => string);
|
|
231
|
+
export interface ToastPromiseMsgs<T> {
|
|
232
|
+
loading?: string;
|
|
233
|
+
success?: PromiseMsg<T>;
|
|
234
|
+
error?: PromiseMsg<unknown>;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface ToastApi {
|
|
238
|
+
(message: string, opts?: ToastOptions): string;
|
|
239
|
+
success(m: string, o?: ToastOptions): string;
|
|
240
|
+
error(m: string, o?: ToastOptions): string;
|
|
241
|
+
warning(m: string, o?: ToastOptions): string;
|
|
242
|
+
info(m: string, o?: ToastOptions): string;
|
|
243
|
+
loading(m: string, o?: ToastOptions): string;
|
|
244
|
+
dismiss(id?: string | null): void;
|
|
245
|
+
update(id: string, patch?: Partial<ToastRecord>): string;
|
|
246
|
+
promise<T>(promise: Promise<T>, msgs?: ToastPromiseMsgs<T>): Promise<T>;
|
|
247
|
+
custom(node: unknown, opts?: { id?: string | number; duration?: number; dismissible?: boolean }): string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const toast = ((message: string, opts?: ToastOptions) => make('default', message, opts)) as ToastApi;
|
|
251
|
+
toast.success = (m, o) => make('success', m, o);
|
|
252
|
+
toast.error = (m, o) => make('danger', m, o);
|
|
253
|
+
toast.warning = (m, o) => make('warning', m, o);
|
|
254
|
+
toast.info = (m, o) => make('info', m, o);
|
|
255
|
+
toast.loading = (m, o) => make('loading', m, o);
|
|
256
|
+
toast.dismiss = (id) => store.dismiss(id);
|
|
257
|
+
|
|
258
|
+
toast.update = (id, patch = {}) => {
|
|
259
|
+
if (patch.tone && patch.duration == null) patch = { ...patch, duration: DURATION[patch.tone] };
|
|
260
|
+
return store.update(id, patch);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
toast.promise = function <T>(promise: Promise<T>, msgs: ToastPromiseMsgs<T> = {}): Promise<T> {
|
|
264
|
+
const id = make('loading', msgs.loading != null ? msgs.loading : 'Working...');
|
|
265
|
+
const text = <V>(m: PromiseMsg<V> | undefined, fallback: string, v: V): string =>
|
|
266
|
+
typeof m === 'function' ? (m as (x: V) => string)(v) : m != null ? m : fallback;
|
|
267
|
+
promise.then(
|
|
268
|
+
(v) => toast.update(id, { tone: 'success', message: text(msgs.success, 'Done', v) }),
|
|
269
|
+
(e) => toast.update(id, { tone: 'danger', message: text(msgs.error, 'Something went wrong', e) }),
|
|
270
|
+
);
|
|
271
|
+
return promise;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
toast.custom = (node, opts = {}) => {
|
|
275
|
+
warnIfDetached();
|
|
276
|
+
const id = opts.id != null ? String(opts.id) : 'toast-' + ++uid;
|
|
277
|
+
const duration = opts.duration != null ? opts.duration : Infinity;
|
|
278
|
+
if (store.toasts.some((t) => t.id === id)) return store.update(id, { node });
|
|
279
|
+
return store.add({
|
|
280
|
+
id,
|
|
281
|
+
tone: 'custom',
|
|
282
|
+
node,
|
|
283
|
+
message: null,
|
|
284
|
+
description: null,
|
|
285
|
+
action: null,
|
|
286
|
+
duration,
|
|
287
|
+
remaining: duration,
|
|
288
|
+
count: 1,
|
|
289
|
+
timerKey: 0,
|
|
290
|
+
dismissible: opts.dismissible === true,
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
export { toast };
|
|
295
|
+
export const UIToast = store;
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/* toast.css - the glass notification card, tinted per tone (a draining timer ring). */
|
|
2
|
+
|
|
3
|
+
.toast-viewport {
|
|
4
|
+
position: fixed;
|
|
5
|
+
width: min(22.5rem, calc(100vw - 2 * var(--toast-offset, var(--space-6))));
|
|
6
|
+
z-index: var(--layer-toast);
|
|
7
|
+
margin: 0;
|
|
8
|
+
padding: 0;
|
|
9
|
+
list-style: none;
|
|
10
|
+
pointer-events: none; /* only the cards themselves are live */
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/* Anchored by [data-position]; --toast-offset (set by <Toaster offset>) overrides the edge inset. */
|
|
14
|
+
.toast-viewport[data-position^='top'] {
|
|
15
|
+
top: var(--toast-offset, var(--space-6));
|
|
16
|
+
}
|
|
17
|
+
.toast-viewport[data-position^='bottom'] {
|
|
18
|
+
bottom: var(--toast-offset, var(--space-6));
|
|
19
|
+
}
|
|
20
|
+
.toast-viewport[data-position$='right'] {
|
|
21
|
+
right: var(--toast-offset, var(--space-6));
|
|
22
|
+
}
|
|
23
|
+
.toast-viewport[data-position$='left'] {
|
|
24
|
+
left: var(--toast-offset, var(--space-6));
|
|
25
|
+
}
|
|
26
|
+
.toast-viewport[data-position$='center'] {
|
|
27
|
+
left: 50%;
|
|
28
|
+
transform: translateX(-50%);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* The card - a glass consumer (.glass is set in JSX). */
|
|
32
|
+
.toast {
|
|
33
|
+
/* tone hooks - set by [data-tone] below */
|
|
34
|
+
--toast-hue: var(--text-subtle);
|
|
35
|
+
--glass-fg: var(--text-strong);
|
|
36
|
+
--glass-tint: color-mix(in oklab, var(--glass-tint-neutral) 75%, transparent);
|
|
37
|
+
--glass-sheen-rest: 0.45;
|
|
38
|
+
|
|
39
|
+
position: absolute;
|
|
40
|
+
left: 0;
|
|
41
|
+
width: 100%;
|
|
42
|
+
box-sizing: border-box;
|
|
43
|
+
overflow: hidden; /* custom content stays in the pane */
|
|
44
|
+
pointer-events: auto;
|
|
45
|
+
border-radius: var(--radius-lg);
|
|
46
|
+
|
|
47
|
+
/* Motion owns transform - keep only the color/shadow transitions (never smooth Motion's writes) */
|
|
48
|
+
transition:
|
|
49
|
+
background-color var(--duration-base) var(--ease-standard),
|
|
50
|
+
box-shadow var(--duration-base) var(--ease-standard);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* Cards pin to the stack's edge; Motion offsets each one away from it (up from bottom, down from top). */
|
|
54
|
+
.toast-viewport[data-position^='bottom'] .toast {
|
|
55
|
+
bottom: 0;
|
|
56
|
+
transform-origin: center bottom;
|
|
57
|
+
}
|
|
58
|
+
.toast-viewport[data-position^='top'] .toast {
|
|
59
|
+
top: 0;
|
|
60
|
+
transform-origin: center top;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* rich panes for genuine status, diluted to stay glass; neutral otherwise */
|
|
64
|
+
.toast[data-tone='success'] {
|
|
65
|
+
--toast-hue: var(--success);
|
|
66
|
+
--glass-fg: var(--success-text);
|
|
67
|
+
--glass-tint: color-mix(in oklab, var(--glass-tint-success) 70%, transparent);
|
|
68
|
+
}
|
|
69
|
+
.toast[data-tone='danger'] {
|
|
70
|
+
--toast-hue: var(--danger);
|
|
71
|
+
--glass-fg: var(--danger-text);
|
|
72
|
+
--glass-tint: color-mix(in oklab, var(--glass-tint-danger) 70%, transparent);
|
|
73
|
+
}
|
|
74
|
+
.toast[data-tone='warning'] {
|
|
75
|
+
--toast-hue: var(--warning);
|
|
76
|
+
--glass-fg: var(--warning-text);
|
|
77
|
+
--glass-tint: color-mix(in oklab, var(--glass-tint-warning) 70%, transparent);
|
|
78
|
+
}
|
|
79
|
+
.toast[data-tone='info'] {
|
|
80
|
+
--toast-hue: var(--info);
|
|
81
|
+
--glass-fg: var(--info-text);
|
|
82
|
+
--glass-tint: color-mix(in oklab, var(--glass-tint-info) 70%, transparent);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* receding cards fall quiet: tone drains out, sheen dims, content fades */
|
|
86
|
+
.toast[data-behind] {
|
|
87
|
+
--glass-tint: color-mix(in oklab, var(--glass-tint-neutral) 75%, transparent);
|
|
88
|
+
--glass-sheen-rest: 0.3;
|
|
89
|
+
}
|
|
90
|
+
.toast[data-behind] .toast__inner,
|
|
91
|
+
.toast[data-behind] .toast__custom {
|
|
92
|
+
opacity: 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.toast__inner {
|
|
96
|
+
display: flex;
|
|
97
|
+
align-items: center;
|
|
98
|
+
gap: var(--space-3);
|
|
99
|
+
padding: var(--space-3) var(--space-4);
|
|
100
|
+
transition: var(--transition-opacity);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* the dial - glyph centered in the timer ring; one instrument, not two */
|
|
104
|
+
.toast__icon {
|
|
105
|
+
position: relative;
|
|
106
|
+
flex: none;
|
|
107
|
+
display: grid;
|
|
108
|
+
place-items: center;
|
|
109
|
+
width: calc(var(--icon-md) + 2 * var(--space-2));
|
|
110
|
+
height: calc(var(--icon-md) + 2 * var(--space-2));
|
|
111
|
+
color: var(--toast-hue);
|
|
112
|
+
}
|
|
113
|
+
.toast__icon-glyph {
|
|
114
|
+
display: grid;
|
|
115
|
+
place-items: center;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/* the ring - a draining arc; linear by design, animation-duration set at runtime to the toast's lifetime */
|
|
119
|
+
.toast__ring {
|
|
120
|
+
position: absolute;
|
|
121
|
+
inset: 0;
|
|
122
|
+
width: 100%;
|
|
123
|
+
height: 100%;
|
|
124
|
+
transform: rotate(-90deg); /* arc starts at 12 o'clock */
|
|
125
|
+
pointer-events: none;
|
|
126
|
+
}
|
|
127
|
+
.toast__ring circle {
|
|
128
|
+
fill: none;
|
|
129
|
+
stroke-width: var(--border-emphasis);
|
|
130
|
+
}
|
|
131
|
+
.toast__ring-track {
|
|
132
|
+
stroke: color-mix(in oklab, var(--toast-hue) 16%, transparent);
|
|
133
|
+
}
|
|
134
|
+
.toast__ring-arc {
|
|
135
|
+
stroke: var(--toast-hue);
|
|
136
|
+
stroke-linecap: round;
|
|
137
|
+
stroke-dasharray: 1; /* pathLength=1 - offset is a fraction */
|
|
138
|
+
animation-name: toast-ring;
|
|
139
|
+
animation-timing-function: linear;
|
|
140
|
+
animation-fill-mode: both;
|
|
141
|
+
}
|
|
142
|
+
.toast-viewport.is-paused .toast__ring-arc {
|
|
143
|
+
animation-play-state: paused;
|
|
144
|
+
}
|
|
145
|
+
@keyframes toast-ring {
|
|
146
|
+
from {
|
|
147
|
+
stroke-dashoffset: 0;
|
|
148
|
+
}
|
|
149
|
+
to {
|
|
150
|
+
stroke-dashoffset: 1;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.toast__text {
|
|
155
|
+
flex: 1 1 auto;
|
|
156
|
+
min-width: 0;
|
|
157
|
+
}
|
|
158
|
+
.toast__message {
|
|
159
|
+
margin: 0;
|
|
160
|
+
font: var(--type-label);
|
|
161
|
+
color: var(--glass-fg);
|
|
162
|
+
text-wrap: pretty;
|
|
163
|
+
}
|
|
164
|
+
.toast__desc {
|
|
165
|
+
margin: var(--space-1) 0 0;
|
|
166
|
+
font: var(--type-caption);
|
|
167
|
+
color: color-mix(in oklab, var(--glass-fg) 76%, transparent);
|
|
168
|
+
text-wrap: pretty;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* coalesce counter - same toast refired becomes a xN counter (mono) */
|
|
172
|
+
.toast__count {
|
|
173
|
+
flex: none;
|
|
174
|
+
font-family: var(--font-mono);
|
|
175
|
+
font-size: var(--size-micro);
|
|
176
|
+
line-height: var(--leading-micro);
|
|
177
|
+
font-variant-numeric: tabular-nums;
|
|
178
|
+
color: var(--glass-fg);
|
|
179
|
+
background: color-mix(in oklab, var(--glass-fg) 9%, transparent);
|
|
180
|
+
border-radius: var(--radius-sm);
|
|
181
|
+
padding: 0 var(--space-1);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.toast__action {
|
|
185
|
+
flex: none;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* dismiss - appears only while the card is hovered or holds focus */
|
|
189
|
+
.toast__close {
|
|
190
|
+
flex: none;
|
|
191
|
+
appearance: none;
|
|
192
|
+
display: grid;
|
|
193
|
+
place-items: center;
|
|
194
|
+
width: var(--space-6);
|
|
195
|
+
height: var(--space-6);
|
|
196
|
+
margin: 0;
|
|
197
|
+
padding: 0;
|
|
198
|
+
border: none;
|
|
199
|
+
border-radius: var(--radius-sm);
|
|
200
|
+
background: transparent;
|
|
201
|
+
color: color-mix(in oklab, var(--glass-fg) 64%, transparent);
|
|
202
|
+
cursor: pointer;
|
|
203
|
+
opacity: 0;
|
|
204
|
+
transition:
|
|
205
|
+
var(--transition-control),
|
|
206
|
+
opacity var(--duration-fast) var(--ease-standard);
|
|
207
|
+
}
|
|
208
|
+
.toast:hover .toast__close,
|
|
209
|
+
.toast:focus-within .toast__close {
|
|
210
|
+
opacity: 1;
|
|
211
|
+
}
|
|
212
|
+
.toast__close:hover {
|
|
213
|
+
background: color-mix(in oklab, var(--glass-fg) 9%, transparent);
|
|
214
|
+
color: var(--glass-fg);
|
|
215
|
+
}
|
|
216
|
+
.toast__close:active {
|
|
217
|
+
background: color-mix(in oklab, var(--glass-fg) 14%, transparent);
|
|
218
|
+
}
|
|
219
|
+
.toast__close:focus-visible {
|
|
220
|
+
outline: none;
|
|
221
|
+
box-shadow: var(--focus-ring);
|
|
222
|
+
opacity: 1;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/* Loading spinner - continuous --duration-spin (linear is allowed for loops; reduced motion slows, never freezes) */
|
|
226
|
+
.toast__spinner {
|
|
227
|
+
display: block;
|
|
228
|
+
box-sizing: border-box;
|
|
229
|
+
width: var(--icon-sm);
|
|
230
|
+
height: var(--icon-sm);
|
|
231
|
+
border-radius: var(--radius-full);
|
|
232
|
+
border: var(--border-emphasis) solid currentColor;
|
|
233
|
+
border-right-color: transparent;
|
|
234
|
+
color: var(--text-muted);
|
|
235
|
+
animation: toast-spin var(--duration-spin) linear infinite;
|
|
236
|
+
}
|
|
237
|
+
@keyframes toast-spin {
|
|
238
|
+
to {
|
|
239
|
+
transform: rotate(1turn);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/* Custom slot - bare neutral glass; the consumer owns the interior. */
|
|
244
|
+
.toast__custom {
|
|
245
|
+
padding: var(--space-3) var(--space-4);
|
|
246
|
+
transition: var(--transition-opacity);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
@media (prefers-reduced-motion: reduce) {
|
|
250
|
+
/* travel/gestures collapse via the tokens; the ring is data, so it keeps running */
|
|
251
|
+
.toast,
|
|
252
|
+
.toast__close,
|
|
253
|
+
.toast__inner,
|
|
254
|
+
.toast__custom {
|
|
255
|
+
transition: none;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import './tooltip.css';
|
|
4
|
+
import {
|
|
5
|
+
Fragment,
|
|
6
|
+
useEffect,
|
|
7
|
+
useId,
|
|
8
|
+
useRef,
|
|
9
|
+
type FocusEvent,
|
|
10
|
+
type HTMLAttributes,
|
|
11
|
+
type PointerEvent,
|
|
12
|
+
type ReactElement,
|
|
13
|
+
type ReactNode,
|
|
14
|
+
} from 'react';
|
|
15
|
+
import { store, useHostElection, OPEN_DELAY, CLOSE_GRACE, TOOLTIP_DOM_ID, type Placement } from './tooltip-store';
|
|
16
|
+
import { TooltipHost } from './tooltip-host';
|
|
17
|
+
import type { DataAttributes } from '../../../dom-props';
|
|
18
|
+
import { cx } from '../../internal/utils/cx';
|
|
19
|
+
|
|
20
|
+
export interface TooltipProps {
|
|
21
|
+
/** The hint - a string or small node; never interactive content. */
|
|
22
|
+
content: ReactNode;
|
|
23
|
+
/** Optional keyboard hint, rendered as mono metadata ("⌘↩", "S"). */
|
|
24
|
+
shortcut?: string | null;
|
|
25
|
+
/** Preferred side; flips automatically when out of viewport room. */
|
|
26
|
+
placement?: Placement;
|
|
27
|
+
/** Suppress the tooltip entirely (trigger renders untouched). */
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
/** ms before a cold hover shows; warm hovers + focus are instant. Default 350. */
|
|
30
|
+
openDelay?: number;
|
|
31
|
+
/** ms the bubble lingers after leaving (bridges moving to a neighbour). Default 140. */
|
|
32
|
+
closeDelay?: number;
|
|
33
|
+
/** Stable id for this trigger's store entry - lets overlapping triggers race safely under
|
|
34
|
+
* the one shared tooltip host. Not rendered to the DOM. Auto-generated when omitted. */
|
|
35
|
+
id?: string;
|
|
36
|
+
/** Exactly one element. */
|
|
37
|
+
children: ReactElement;
|
|
38
|
+
/** Standard <span> attributes forwarded to the anchor wrapper. */
|
|
39
|
+
htmlProps?: HTMLAttributes<HTMLSpanElement> & DataAttributes;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function Tooltip({
|
|
43
|
+
content,
|
|
44
|
+
shortcut = null,
|
|
45
|
+
placement = 'top',
|
|
46
|
+
disabled = false,
|
|
47
|
+
openDelay = OPEN_DELAY,
|
|
48
|
+
closeDelay = CLOSE_GRACE,
|
|
49
|
+
id,
|
|
50
|
+
children,
|
|
51
|
+
htmlProps,
|
|
52
|
+
}: TooltipProps) {
|
|
53
|
+
const wrapRef = useRef<HTMLSpanElement | null>(null);
|
|
54
|
+
const openTimer = useRef<ReturnType<typeof setTimeout> | 0>(0);
|
|
55
|
+
const myId = id || 'tip-' + useId();
|
|
56
|
+
const isHost = useHostElection();
|
|
57
|
+
|
|
58
|
+
const anchorEl = (): HTMLElement | null => (wrapRef.current?.firstElementChild as HTMLElement) ?? null;
|
|
59
|
+
|
|
60
|
+
useEffect(
|
|
61
|
+
() => () => {
|
|
62
|
+
clearTimeout(openTimer.current);
|
|
63
|
+
store.close(myId, 0);
|
|
64
|
+
},
|
|
65
|
+
[],
|
|
66
|
+
); // eslint-disable-line react-hooks/exhaustive-deps
|
|
67
|
+
|
|
68
|
+
function show(immediate: boolean) {
|
|
69
|
+
if (disabled) return;
|
|
70
|
+
clearTimeout(openTimer.current);
|
|
71
|
+
const el = anchorEl();
|
|
72
|
+
if (!el) return;
|
|
73
|
+
el.setAttribute('aria-describedby', TOOLTIP_DOM_ID);
|
|
74
|
+
const open = () =>
|
|
75
|
+
store.open({ id: myId, content, shortcut, placement, rect: () => anchorEl()!.getBoundingClientRect() });
|
|
76
|
+
if (immediate || store.isWarm()) open();
|
|
77
|
+
else openTimer.current = setTimeout(open, openDelay);
|
|
78
|
+
}
|
|
79
|
+
function hide() {
|
|
80
|
+
clearTimeout(openTimer.current);
|
|
81
|
+
const el = anchorEl();
|
|
82
|
+
if (el) el.removeAttribute('aria-describedby');
|
|
83
|
+
store.close(myId, closeDelay);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const onEnter = (e: PointerEvent) => {
|
|
87
|
+
if (e.pointerType !== 'touch') show(false);
|
|
88
|
+
};
|
|
89
|
+
const onFocusIn = (e: FocusEvent) => {
|
|
90
|
+
if ((e.target as HTMLElement).matches(':focus-visible')) show(true);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<Fragment>
|
|
95
|
+
<span
|
|
96
|
+
{...htmlProps}
|
|
97
|
+
ref={wrapRef}
|
|
98
|
+
className={cx('tooltip-anchor', htmlProps?.className)}
|
|
99
|
+
onPointerEnter={onEnter}
|
|
100
|
+
onPointerLeave={hide}
|
|
101
|
+
onPointerDown={hide}
|
|
102
|
+
onFocus={onFocusIn}
|
|
103
|
+
onBlur={hide}
|
|
104
|
+
>
|
|
105
|
+
{children}
|
|
106
|
+
</span>
|
|
107
|
+
{isHost && <TooltipHost />}
|
|
108
|
+
</Fragment>
|
|
109
|
+
);
|
|
110
|
+
}
|