@luxalgo/vela-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/CHANGELOG.md +89 -0
- package/README.md +5 -2
- package/dist/badge.js +1 -1
- package/dist/bar-chart.js +1 -1
- package/dist/button.d.ts +18 -4
- package/dist/button.js +4 -3
- package/dist/calendar.d.ts +3 -1
- package/dist/calendar.js +1 -1
- package/dist/callout-bubble.d.ts +6 -1
- package/dist/callout-bubble.js +12 -8
- package/dist/card.d.ts +52 -0
- package/dist/card.js +179 -0
- package/dist/checkbox.js +5 -5
- package/dist/{chunk-X4YPSIW6.js → chunk-5BJV5FY5.js} +1 -1
- package/dist/{chunk-FCO3YZLT.js → chunk-ALAHGSLC.js} +1 -1
- package/dist/{chunk-5QHXOAZA.js → chunk-B2EZB5ZG.js} +4 -4
- package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
- package/dist/chunk-DXDCKZ2M.js +117 -0
- package/dist/{chunk-6NXY62JS.js → chunk-FYVQO5TN.js} +1 -1
- package/dist/{chunk-V6TTTMTE.js → chunk-GVRX3UAV.js} +4 -1
- package/dist/{chunk-XJAPCCOJ.js → chunk-IOOYDSSK.js} +4 -4
- package/dist/{chunk-OBLIX3VN.js → chunk-KKS6NEGI.js} +2 -2
- package/dist/{chunk-SAPURP46.js → chunk-OJ2SEMN6.js} +3 -3
- package/dist/{chunk-2Q4JRINP.js → chunk-OQXFQMZM.js} +3 -3
- package/dist/{chunk-AJ2IYSOI.js → chunk-TBXUF5YX.js} +35 -3
- package/dist/{chunk-ZB3BZHSN.js → chunk-UEO6W37Z.js} +4 -4
- package/dist/{chunk-CH3DV6FD.js → chunk-WWHNUKSZ.js} +5 -5
- package/dist/chunk-XBEL4K33.js +142 -0
- package/dist/collapsible.d.ts +49 -0
- package/dist/collapsible.js +106 -0
- package/dist/color-picker.js +5 -5
- package/dist/context-menu.js +5 -5
- package/dist/css.d.ts +3 -1
- package/dist/css.js +1 -1
- package/dist/data-table.d.ts +39 -2
- package/dist/data-table.js +1 -1
- package/dist/date-picker.js +3 -3
- package/dist/dialog.d.ts +8 -0
- package/dist/dialog.js +15 -5
- package/dist/donut.js +1 -1
- package/dist/drawer.js +2 -2
- package/dist/dropdown-menu.js +4 -4
- package/dist/empty.js +2 -2
- package/dist/field.js +13 -13
- package/dist/glyph-select.js +5 -5
- package/dist/h-Bl37qUis.d.ts +24 -0
- package/dist/hover-card.d.ts +1 -1
- package/dist/hover-card.js +3 -3
- package/dist/icons.js +1 -1
- package/dist/index.d.ts +10 -2
- package/dist/index.js +3 -3
- package/dist/input-group.js +4 -3
- package/dist/input.js +2 -2
- package/dist/item.d.ts +72 -0
- package/dist/item.js +131 -0
- package/dist/kbd.js +2 -2
- package/dist/line-chart.js +1 -1
- package/dist/manifest.json +1336 -65
- package/dist/month-grid.d.ts +45 -0
- package/dist/month-grid.js +157 -0
- package/dist/number-input.js +5 -5
- package/dist/popover.js +3 -3
- package/dist/scatter-chart.js +1 -1
- package/dist/select.js +7 -7
- package/dist/slider.d.ts +46 -0
- package/dist/slider.js +111 -0
- package/dist/sort-button.d.ts +41 -0
- package/dist/sort-button.js +88 -0
- package/dist/sparkline.js +1 -1
- package/dist/stat.js +1 -1
- package/dist/switch.js +3 -3
- package/dist/tabs.d.ts +3 -1
- package/dist/tabs.js +13 -8
- package/dist/text-area.js +2 -2
- package/dist/theme-Ct-q58xz.d.ts +341 -0
- package/dist/theme.d.ts +42 -181
- package/dist/theme.js +2 -2
- package/dist/theme.schema.json +541 -1
- package/dist/toast.d.ts +49 -0
- package/dist/toast.js +463 -0
- package/dist/toggle-group.js +4 -4
- package/dist/tooltip.js +2 -2
- package/dist/treemap.d.ts +80 -0
- package/dist/treemap.js +470 -0
- package/docs/theming.md +6 -0
- package/docs/writing-a-component.md +4 -1
- package/llms.txt +11 -3
- package/package.json +1 -1
- package/registry/button.ts +105 -21
- package/registry/callout-bubble.ts +14 -3
- package/registry/card.ts +379 -0
- package/registry/collapsible.ts +271 -0
- package/registry/dialog.ts +16 -0
- package/registry/dropdown-menu.ts +3 -3
- package/registry/item.ts +376 -0
- package/registry/month-grid.ts +328 -0
- package/registry/slider.ts +259 -0
- package/registry/sort-button.ts +189 -0
- package/registry/tabs.ts +38 -4
- package/registry/toast.ts +965 -0
- package/registry/treemap.ts +847 -0
- package/skills/using-vela-ui-components/SKILL.md +14 -9
- package/src/components/button.ts +105 -21
- package/src/components/callout-bubble.ts +14 -3
- package/src/components/card.ts +379 -0
- package/src/components/collapsible.ts +271 -0
- package/src/components/dialog.ts +16 -0
- package/src/components/dropdown-menu.ts +3 -3
- package/src/components/item.ts +376 -0
- package/src/components/month-grid.ts +328 -0
- package/src/components/slider.ts +259 -0
- package/src/components/sort-button.ts +189 -0
- package/src/components/tabs.ts +38 -4
- package/src/components/toast.ts +965 -0
- package/src/components/treemap.ts +847 -0
- package/src/lib/calendar.ts +5 -0
- package/src/lib/component-index.ts +34 -2
- package/src/lib/css.ts +6 -2
- package/src/lib/icons.ts +7 -0
- package/src/lib/scope.ts +135 -0
- package/src/lib/theme.ts +21 -5
- package/dist/chunk-EZQXTO6K.js +0 -89
- package/dist/chunk-IDA2BT7D.js +0 -29
- package/dist/data-table-DtU3FX9j.d.ts +0 -61
|
@@ -0,0 +1,965 @@
|
|
|
1
|
+
// Toast: short notices that stack in a corner and leave on their own, shaped after shadcn's
|
|
2
|
+
// toast (Sonner). `toast()` raises one from anywhere on the page, with `toast.success`,
|
|
3
|
+
// `.info`, `.warning`, `.error` and `.loading` for its kinds, `toast.promise` for a task's
|
|
4
|
+
// three states and `toast.dismiss` to take one or all away; calling it again with the same
|
|
5
|
+
// `id` updates the toast in place. A toaster shows them: `createToaster` mounts one where the
|
|
6
|
+
// notices belong, over the viewport or contained in a host. The newest toast is in front and
|
|
7
|
+
// the older ones fold behind it; hovering the stack (or Alt+T) spreads it out and pauses the
|
|
8
|
+
// timers, and a toast swipes away towards its edge. Positions name the corner on the
|
|
9
|
+
// writing direction's terms: `bottom-right` sits at the end side, mirrored right to left.
|
|
10
|
+
// The queue is page-wide, so a toast raised by another copy of the kit reaches this toaster.
|
|
11
|
+
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
12
|
+
import { defineComponent } from '@luxalgo/vela-ui';
|
|
13
|
+
import { h } from '@luxalgo/vela-ui';
|
|
14
|
+
import { msg, textOf, type Translator } from '@luxalgo/vela-ui/i18n';
|
|
15
|
+
import { iconEl } from '@luxalgo/vela-ui/icons';
|
|
16
|
+
import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
|
|
17
|
+
import { pageState } from '@luxalgo/vela-ui/page';
|
|
18
|
+
import { t } from '@luxalgo/vela-ui';
|
|
19
|
+
import { ensureUIHost } from '@luxalgo/vela-ui/theme';
|
|
20
|
+
|
|
21
|
+
export const toastDef = defineComponent({
|
|
22
|
+
name: 'toast',
|
|
23
|
+
parts: ['root', 'list', 'item', 'close', 'icon', 'content', 'title', 'description', 'cancel', 'action'],
|
|
24
|
+
settings: {
|
|
25
|
+
width: '356px',
|
|
26
|
+
radius: t.radius.lg,
|
|
27
|
+
background: t.surface.elev,
|
|
28
|
+
border: t.border,
|
|
29
|
+
foreground: t.fg.bright,
|
|
30
|
+
'description-color': t.fg.muted,
|
|
31
|
+
shadow: t.shadow,
|
|
32
|
+
success: t.profit,
|
|
33
|
+
info: t.accent,
|
|
34
|
+
warning: t.highlight,
|
|
35
|
+
error: t.danger,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export type ToastType = 'default' | 'success' | 'info' | 'warning' | 'error' | 'loading';
|
|
40
|
+
export type ToasterPosition = 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
|
|
41
|
+
export type SwipeDirection = 'top' | 'right' | 'bottom' | 'left';
|
|
42
|
+
export type ToastId = string | number;
|
|
43
|
+
/** Text, an element, or a function returning either (read when the toast renders). */
|
|
44
|
+
export type ToastContent = string | Node | (() => string | Node);
|
|
45
|
+
|
|
46
|
+
export interface ToastAction {
|
|
47
|
+
label: string;
|
|
48
|
+
/** Runs on a press. The toast then closes, unless the handler calls `preventDefault()`. */
|
|
49
|
+
onClick: (event: MouseEvent) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A toast's options: the second argument of `toast()`. */
|
|
53
|
+
export interface ExternalToast {
|
|
54
|
+
/** Reuse an id to update that toast in place (a loading toast turned into its result). */
|
|
55
|
+
id?: ToastId;
|
|
56
|
+
/** The muted line under the title. */
|
|
57
|
+
description?: ToastContent;
|
|
58
|
+
/** An icon id from the registry or an element; null hides the kind's icon. */
|
|
59
|
+
icon?: string | Node | null;
|
|
60
|
+
/** How long it stays, in milliseconds (default the toaster's). `Infinity` keeps it until dismissed. */
|
|
61
|
+
duration?: number;
|
|
62
|
+
/** A button at the end: a label and a handler, or an element. */
|
|
63
|
+
action?: ToastAction | Node;
|
|
64
|
+
/** A muted button before the action; a press runs its handler and closes the toast. */
|
|
65
|
+
cancel?: ToastAction | Node;
|
|
66
|
+
/** A close button on the toast's corner (default the toaster's). */
|
|
67
|
+
closeButton?: boolean;
|
|
68
|
+
/** False: no close button, no swipe, and `toast.dismiss` is the only way out (default true). */
|
|
69
|
+
dismissible?: boolean;
|
|
70
|
+
/** Tint the toast with its kind's color (default the toaster's). */
|
|
71
|
+
richColors?: boolean;
|
|
72
|
+
/** Swap the toast's background and text colors (default the toaster's). */
|
|
73
|
+
invert?: boolean;
|
|
74
|
+
/** Show it on a toaster's other corner. */
|
|
75
|
+
position?: ToasterPosition;
|
|
76
|
+
/** Show it only on the toaster created with this `id`. */
|
|
77
|
+
toasterId?: string;
|
|
78
|
+
/** Extra class names (layout hooks). */
|
|
79
|
+
className?: string;
|
|
80
|
+
/** Called when the toast is closed: the close button, a swipe, an action or `toast.dismiss`. */
|
|
81
|
+
onDismiss?: (toast: ToastT) => void;
|
|
82
|
+
/** Called when the toast's time runs out. */
|
|
83
|
+
onAutoClose?: (toast: ToastT) => void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A toast as the queue holds it. */
|
|
87
|
+
export interface ToastT extends ExternalToast {
|
|
88
|
+
id: ToastId;
|
|
89
|
+
type: ToastType;
|
|
90
|
+
title?: ToastContent;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type PromiseSource<T> = Promise<T> | (() => Promise<T>);
|
|
94
|
+
|
|
95
|
+
export interface PromiseData<T> extends Omit<ExternalToast, 'description'> {
|
|
96
|
+
/** Shown while the promise runs; without it the toast appears once the promise settles. */
|
|
97
|
+
loading?: ToastContent;
|
|
98
|
+
success?: ToastContent | ((data: T) => ToastContent);
|
|
99
|
+
error?: ToastContent | ((error: unknown) => ToastContent);
|
|
100
|
+
description?: ToastContent | ((result: T | unknown) => ToastContent);
|
|
101
|
+
/** Runs once the promise settles, either way. */
|
|
102
|
+
finally?: () => void;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface ToastPromise<T> {
|
|
106
|
+
/** The toast's id, or undefined while no `loading` toast shows. */
|
|
107
|
+
readonly id: ToastId | undefined;
|
|
108
|
+
/** The promise itself, to await its value after the toast. */
|
|
109
|
+
unwrap(): Promise<T>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
type ToastFn = (message: ToastContent, data?: ExternalToast) => ToastId;
|
|
113
|
+
|
|
114
|
+
export interface ToastFunction extends ToastFn {
|
|
115
|
+
/** A plain toast (the same as calling `toast()`). */
|
|
116
|
+
message: ToastFn;
|
|
117
|
+
success: ToastFn;
|
|
118
|
+
info: ToastFn;
|
|
119
|
+
warning: ToastFn;
|
|
120
|
+
error: ToastFn;
|
|
121
|
+
/** A toast with a spinner that stays until it is updated or dismissed. */
|
|
122
|
+
loading: ToastFn;
|
|
123
|
+
/** A loading toast that turns into the promise's success or error. */
|
|
124
|
+
promise<T>(promise: PromiseSource<T>, data?: PromiseData<T>): ToastPromise<T>;
|
|
125
|
+
/** Close one toast, or every toast without an id. */
|
|
126
|
+
dismiss(id?: ToastId): ToastId | undefined;
|
|
127
|
+
/** The toasts on the page now. */
|
|
128
|
+
getToasts(): ToastT[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
type QueueEvent = { kind: 'show'; toast: ToastT } | { kind: 'dismiss'; id: ToastId | undefined };
|
|
132
|
+
|
|
133
|
+
interface Queue {
|
|
134
|
+
toasts: ToastT[];
|
|
135
|
+
listeners: Set<(e: QueueEvent) => void>;
|
|
136
|
+
n: number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** A page with no toaster mounted keeps only this many toasts. */
|
|
140
|
+
const QUEUE_CAP = 50;
|
|
141
|
+
|
|
142
|
+
const queue = pageState('toasts@1', (): Queue => ({ toasts: [], listeners: new Set(), n: 0 }));
|
|
143
|
+
|
|
144
|
+
function emit(e: QueueEvent): void {
|
|
145
|
+
for (const fn of [...queue.listeners]) fn(e);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function raise(title: ToastContent | undefined, type: ToastType, data: ExternalToast = {}): ToastId {
|
|
149
|
+
const id = data.id ?? ++queue.n;
|
|
150
|
+
const at = queue.toasts.findIndex((x) => x.id === id);
|
|
151
|
+
const next: ToastT = { ...(at >= 0 ? queue.toasts[at] : {}), ...data, id, type, title };
|
|
152
|
+
if (at >= 0) queue.toasts[at] = next;
|
|
153
|
+
else {
|
|
154
|
+
queue.toasts.push(next);
|
|
155
|
+
if (queue.toasts.length > QUEUE_CAP) queue.toasts.shift();
|
|
156
|
+
}
|
|
157
|
+
emit({ kind: 'show', toast: next });
|
|
158
|
+
return id;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Take a toast off the queue and tell it why, once: every toaster showing it lets it go. */
|
|
162
|
+
function settle(id: ToastId, reason: 'dismiss' | 'auto'): void {
|
|
163
|
+
const at = queue.toasts.findIndex((x) => x.id === id);
|
|
164
|
+
if (at >= 0) {
|
|
165
|
+
const [gone] = queue.toasts.splice(at, 1);
|
|
166
|
+
if (reason === 'auto') gone!.onAutoClose?.(gone!);
|
|
167
|
+
else gone!.onDismiss?.(gone!);
|
|
168
|
+
}
|
|
169
|
+
emit({ kind: 'dismiss', id });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const resolve = <A>(v: ToastContent | ((a: A) => ToastContent) | undefined, arg: A): ToastContent | undefined => (typeof v === 'function' && v.length > 0 ? (v as (a: A) => ToastContent)(arg) : (v as ToastContent | undefined));
|
|
173
|
+
|
|
174
|
+
function promiseToast<T>(source: PromiseSource<T>, data: PromiseData<T> = {}): ToastPromise<T> {
|
|
175
|
+
const { loading, success, error, description, finally: done, ...rest } = data;
|
|
176
|
+
let id: ToastId | undefined = rest.id;
|
|
177
|
+
if (loading !== undefined) id = raise(loading, 'loading', { ...rest, description: resolve(description, undefined) });
|
|
178
|
+
const p = typeof source === 'function' ? source() : source;
|
|
179
|
+
p.then(
|
|
180
|
+
(value) => {
|
|
181
|
+
const title = resolve(success, value);
|
|
182
|
+
if (title !== undefined) raise(title, 'success', { ...rest, id, description: resolve(description, value) });
|
|
183
|
+
else if (id !== undefined) settle(id, 'dismiss');
|
|
184
|
+
},
|
|
185
|
+
(err: unknown) => {
|
|
186
|
+
const title = resolve(error, err);
|
|
187
|
+
if (title !== undefined) raise(title, 'error', { ...rest, id, description: resolve(description, err) });
|
|
188
|
+
else if (id !== undefined) settle(id, 'dismiss');
|
|
189
|
+
},
|
|
190
|
+
).finally(() => done?.());
|
|
191
|
+
return { id, unwrap: () => p };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const kind =
|
|
195
|
+
(type: ToastType): ToastFn =>
|
|
196
|
+
(message, data) =>
|
|
197
|
+
raise(message, type, data);
|
|
198
|
+
|
|
199
|
+
/** Raise a toast. Every toaster on the page shows it (or the one named by `toasterId`). */
|
|
200
|
+
export const toast: ToastFunction = Object.assign(kind('default'), {
|
|
201
|
+
message: kind('default'),
|
|
202
|
+
success: kind('success'),
|
|
203
|
+
info: kind('info'),
|
|
204
|
+
warning: kind('warning'),
|
|
205
|
+
error: kind('error'),
|
|
206
|
+
loading: kind('loading'),
|
|
207
|
+
promise: promiseToast,
|
|
208
|
+
dismiss(id?: ToastId): ToastId | undefined {
|
|
209
|
+
if (id === undefined) for (const x of [...queue.toasts]) settle(x.id, 'dismiss');
|
|
210
|
+
else settle(id, 'dismiss');
|
|
211
|
+
return id;
|
|
212
|
+
},
|
|
213
|
+
getToasts: (): ToastT[] => queue.toasts.slice(),
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
export type ToastOffset = number | string | { top?: number | string; right?: number | string; bottom?: number | string; left?: number | string };
|
|
217
|
+
|
|
218
|
+
export interface ToasterProps {
|
|
219
|
+
/** Where the toaster mounts (default the document's body). */
|
|
220
|
+
host?: HTMLElement;
|
|
221
|
+
/** Stack in the host's corners instead of the viewport's; the host must be positioned. */
|
|
222
|
+
contained?: boolean;
|
|
223
|
+
/** The corner the toasts stack in (default `bottom-right`). */
|
|
224
|
+
position?: ToasterPosition;
|
|
225
|
+
/** Keep the stack spread out instead of folding the older toasts behind the newest. */
|
|
226
|
+
expand?: boolean;
|
|
227
|
+
/** Tint each toast with its kind's color. */
|
|
228
|
+
richColors?: boolean;
|
|
229
|
+
/** A close button on every toast. */
|
|
230
|
+
closeButton?: boolean;
|
|
231
|
+
/** Swap the toasts' background and text colors. */
|
|
232
|
+
invert?: boolean;
|
|
233
|
+
/** How long a toast stays, in milliseconds (default 4000). */
|
|
234
|
+
duration?: number;
|
|
235
|
+
/** Space between spread toasts, in pixels (default 14). */
|
|
236
|
+
gap?: number;
|
|
237
|
+
/** How many toasts show at once (default 3). */
|
|
238
|
+
visibleToasts?: number;
|
|
239
|
+
/** Distance from the edges: one value, or one per side (default 24px, 16px contained). */
|
|
240
|
+
offset?: ToastOffset;
|
|
241
|
+
/** The keys that spread the stack and focus it: event flags and codes (default `['altKey', 'KeyT']`). */
|
|
242
|
+
hotkey?: string[];
|
|
243
|
+
/** The writing direction of the toasts (default the page's). */
|
|
244
|
+
dir?: 'ltr' | 'rtl' | 'auto';
|
|
245
|
+
/** Swipe directions that close a toast (default its corner's edges). */
|
|
246
|
+
swipeDirections?: SwipeDirection[];
|
|
247
|
+
/** Icons in place of the kinds' own: registry ids or elements. */
|
|
248
|
+
icons?: Partial<Record<Exclude<ToastType, 'default'> | 'close', string | Node>>;
|
|
249
|
+
/** Defaults for every toast of this toaster. */
|
|
250
|
+
toastOptions?: Pick<ExternalToast, 'className' | 'duration' | 'closeButton' | 'richColors' | 'invert' | 'dismissible'>;
|
|
251
|
+
/** The region's accessible name (default "Notifications"). */
|
|
252
|
+
containerAriaLabel?: string;
|
|
253
|
+
/** Name the toaster, to show only the toasts raised with this `toasterId`. */
|
|
254
|
+
id?: string;
|
|
255
|
+
/** A kit theme, for a toaster mounted outside a themed host. */
|
|
256
|
+
theme?: Parameters<typeof ensureUIHost>[1];
|
|
257
|
+
/** Translates the close button's and the region's names (default the kit's). */
|
|
258
|
+
i18n?: Translator | null;
|
|
259
|
+
/** Extra class names on the region (layout hooks). */
|
|
260
|
+
className?: string;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export interface ToasterHandle {
|
|
264
|
+
readonly el: HTMLElement;
|
|
265
|
+
/** Re-read the region's and the close buttons' names from the translator, after its language changed. */
|
|
266
|
+
relabel(): void;
|
|
267
|
+
destroy(): void;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const TEXT = {
|
|
271
|
+
region: msg('ui.toast.region', 'Notifications'),
|
|
272
|
+
close: msg('ui.toast.close', 'Close toast'),
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const DEFAULT_ICONS: Record<Exclude<ToastType, 'default'> | 'close', string> = {
|
|
276
|
+
success: 'circle-check',
|
|
277
|
+
info: 'circle-info',
|
|
278
|
+
warning: 'triangle-alert',
|
|
279
|
+
error: 'circle-x',
|
|
280
|
+
loading: 'loader',
|
|
281
|
+
close: 'close',
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/** `aria-keyshortcuts` names of the hotkey's modifier flags. */
|
|
285
|
+
const KEY_NAMES: Record<string, string> = { altKey: 'Alt', ctrlKey: 'Control', metaKey: 'Meta', shiftKey: 'Shift' };
|
|
286
|
+
|
|
287
|
+
const SWIPE_THRESHOLD = 45;
|
|
288
|
+
const VELOCITY_THRESHOLD = 0.11;
|
|
289
|
+
/** How long a closing toast stays in the document while it animates out. */
|
|
290
|
+
const EXIT_MS = 400;
|
|
291
|
+
|
|
292
|
+
const injectStyles = defineStyles(
|
|
293
|
+
'vela-ui-toast',
|
|
294
|
+
css`
|
|
295
|
+
@layer vela.ui {
|
|
296
|
+
[data-slot='toast'] {
|
|
297
|
+
display: contents;
|
|
298
|
+
}
|
|
299
|
+
[data-slot='toast-list'] {
|
|
300
|
+
--_offset-top: 24px;
|
|
301
|
+
--_offset-bottom: 24px;
|
|
302
|
+
--_offset-start: 24px;
|
|
303
|
+
--_offset-end: 24px;
|
|
304
|
+
--_gap: 14px;
|
|
305
|
+
|
|
306
|
+
position: fixed;
|
|
307
|
+
z-index: ${t.z.popover};
|
|
308
|
+
box-sizing: border-box;
|
|
309
|
+
width: min(${toastDef.vars.width}, calc(100% - var(--_offset-start) - var(--_offset-end)));
|
|
310
|
+
margin: 0;
|
|
311
|
+
padding: 0;
|
|
312
|
+
list-style: none;
|
|
313
|
+
font-family: ${t.font};
|
|
314
|
+
transition: height calc(${t.dur.med} * 2.5) ${t.ease};
|
|
315
|
+
|
|
316
|
+
&[data-contained] {
|
|
317
|
+
position: absolute;
|
|
318
|
+
}
|
|
319
|
+
&[data-y='top'] {
|
|
320
|
+
top: var(--_offset-top);
|
|
321
|
+
}
|
|
322
|
+
&[data-y='bottom'] {
|
|
323
|
+
bottom: var(--_offset-bottom);
|
|
324
|
+
}
|
|
325
|
+
&[data-x='left'] {
|
|
326
|
+
inset-inline-start: var(--_offset-start);
|
|
327
|
+
}
|
|
328
|
+
&[data-x='right'] {
|
|
329
|
+
inset-inline-end: var(--_offset-end);
|
|
330
|
+
}
|
|
331
|
+
&[data-x='center'] {
|
|
332
|
+
inset-inline: 0;
|
|
333
|
+
margin-inline: auto;
|
|
334
|
+
}
|
|
335
|
+
&:focus-visible {
|
|
336
|
+
outline: 2px solid ${t.focus};
|
|
337
|
+
outline-offset: 4px;
|
|
338
|
+
border-radius: ${toastDef.vars.radius};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
[data-slot='toast-item'] {
|
|
342
|
+
--_y: 0px;
|
|
343
|
+
--_scale: 1;
|
|
344
|
+
--_swipe-x: 0px;
|
|
345
|
+
--_swipe-y: 0px;
|
|
346
|
+
--_tint: ${toastDef.vars.foreground};
|
|
347
|
+
|
|
348
|
+
position: absolute;
|
|
349
|
+
inset-inline: 0;
|
|
350
|
+
box-sizing: border-box;
|
|
351
|
+
gap: 6px;
|
|
352
|
+
padding: 16px;
|
|
353
|
+
border: 1px solid ${toastDef.vars.border};
|
|
354
|
+
border-radius: ${toastDef.vars.radius};
|
|
355
|
+
background: ${toastDef.vars.background};
|
|
356
|
+
box-shadow: ${toastDef.vars.shadow};
|
|
357
|
+
color: ${toastDef.vars.foreground};
|
|
358
|
+
font-size: 13px;
|
|
359
|
+
line-height: 1.4;
|
|
360
|
+
opacity: 0;
|
|
361
|
+
overflow-wrap: anywhere;
|
|
362
|
+
touch-action: none;
|
|
363
|
+
user-select: none;
|
|
364
|
+
transition:
|
|
365
|
+
transform calc(${t.dur.med} * 2.5) ${t.ease},
|
|
366
|
+
opacity calc(${t.dur.med} * 2.5) ${t.ease},
|
|
367
|
+
height calc(${t.dur.med} * 2.5) ${t.ease};
|
|
368
|
+
|
|
369
|
+
&:where(:not([hidden])) {
|
|
370
|
+
display: flex;
|
|
371
|
+
align-items: center;
|
|
372
|
+
}
|
|
373
|
+
&[data-y='bottom'] {
|
|
374
|
+
bottom: 0;
|
|
375
|
+
transform: translateY(100%);
|
|
376
|
+
transform-origin: bottom center;
|
|
377
|
+
}
|
|
378
|
+
&[data-y='top'] {
|
|
379
|
+
top: 0;
|
|
380
|
+
transform: translateY(-100%);
|
|
381
|
+
transform-origin: top center;
|
|
382
|
+
}
|
|
383
|
+
&[data-mounted] {
|
|
384
|
+
opacity: 1;
|
|
385
|
+
transform: translate(var(--_swipe-x), calc(var(--_y) + var(--_swipe-y))) scale(var(--_scale));
|
|
386
|
+
}
|
|
387
|
+
&[data-mounted][data-visible='false'] {
|
|
388
|
+
opacity: 0;
|
|
389
|
+
pointer-events: none;
|
|
390
|
+
}
|
|
391
|
+
&[data-expanded='false'][data-front='false'] {
|
|
392
|
+
overflow: hidden;
|
|
393
|
+
}
|
|
394
|
+
&[data-expanded='false'][data-front='false'] > * {
|
|
395
|
+
opacity: 0;
|
|
396
|
+
}
|
|
397
|
+
&[data-removed] {
|
|
398
|
+
opacity: 0;
|
|
399
|
+
pointer-events: none;
|
|
400
|
+
}
|
|
401
|
+
&[data-removed='front'][data-y='bottom'] {
|
|
402
|
+
transform: translateY(100%);
|
|
403
|
+
}
|
|
404
|
+
&[data-removed='front'][data-y='top'] {
|
|
405
|
+
transform: translateY(-100%);
|
|
406
|
+
}
|
|
407
|
+
&[data-swiping] {
|
|
408
|
+
transition: none;
|
|
409
|
+
}
|
|
410
|
+
&::after {
|
|
411
|
+
position: absolute;
|
|
412
|
+
inset-inline: 0;
|
|
413
|
+
height: var(--_gap);
|
|
414
|
+
content: '';
|
|
415
|
+
}
|
|
416
|
+
&[data-y='bottom']::after {
|
|
417
|
+
top: 100%;
|
|
418
|
+
}
|
|
419
|
+
&[data-y='top']::after {
|
|
420
|
+
bottom: 100%;
|
|
421
|
+
}
|
|
422
|
+
&:focus-visible {
|
|
423
|
+
outline: 2px solid ${t.focus};
|
|
424
|
+
outline-offset: 2px;
|
|
425
|
+
}
|
|
426
|
+
&[data-invert] {
|
|
427
|
+
--_tint: ${toastDef.vars.background};
|
|
428
|
+
|
|
429
|
+
border-color: ${toastDef.vars.foreground};
|
|
430
|
+
background: ${toastDef.vars.foreground};
|
|
431
|
+
color: ${toastDef.vars.background};
|
|
432
|
+
}
|
|
433
|
+
&[data-rich-colors][data-type='success'] {
|
|
434
|
+
--_tint: ${toastDef.vars.success};
|
|
435
|
+
}
|
|
436
|
+
&[data-rich-colors][data-type='info'] {
|
|
437
|
+
--_tint: ${toastDef.vars.info};
|
|
438
|
+
}
|
|
439
|
+
&[data-rich-colors][data-type='warning'] {
|
|
440
|
+
--_tint: ${toastDef.vars.warning};
|
|
441
|
+
}
|
|
442
|
+
&[data-rich-colors][data-type='error'] {
|
|
443
|
+
--_tint: ${toastDef.vars.error};
|
|
444
|
+
}
|
|
445
|
+
&[data-rich-colors]:is([data-type='success'], [data-type='info'], [data-type='warning'], [data-type='error']) {
|
|
446
|
+
border-color: color-mix(in srgb, var(--_tint) 40%, ${toastDef.vars.background});
|
|
447
|
+
background: color-mix(in srgb, var(--_tint) 14%, ${toastDef.vars.background});
|
|
448
|
+
color: color-mix(in srgb, var(--_tint) 80%, ${toastDef.vars.foreground});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
[data-slot='toast-icon'] {
|
|
452
|
+
flex: none;
|
|
453
|
+
width: 16px;
|
|
454
|
+
height: 16px;
|
|
455
|
+
margin-inline: -3px 4px;
|
|
456
|
+
font-size: 16px;
|
|
457
|
+
line-height: 0;
|
|
458
|
+
|
|
459
|
+
&:where(:not([hidden])) {
|
|
460
|
+
display: flex;
|
|
461
|
+
align-items: center;
|
|
462
|
+
justify-content: center;
|
|
463
|
+
}
|
|
464
|
+
&:is([data-type='loading'] *) {
|
|
465
|
+
animation: vela-toast-spin calc(${t.dur.med} * 5) linear infinite;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
[data-slot='toast-content'] {
|
|
469
|
+
flex: 1 1 auto;
|
|
470
|
+
min-width: 0;
|
|
471
|
+
gap: 2px;
|
|
472
|
+
|
|
473
|
+
&:where(:not([hidden])) {
|
|
474
|
+
display: flex;
|
|
475
|
+
flex-direction: column;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
[data-slot='toast-title'] {
|
|
479
|
+
font-weight: 500;
|
|
480
|
+
line-height: 1.5;
|
|
481
|
+
}
|
|
482
|
+
[data-slot='toast-description'] {
|
|
483
|
+
color: ${toastDef.vars['description-color']};
|
|
484
|
+
font-weight: 400;
|
|
485
|
+
line-height: 1.4;
|
|
486
|
+
|
|
487
|
+
&:is([data-rich-colors] *),
|
|
488
|
+
&:is([data-invert] *) {
|
|
489
|
+
color: inherit;
|
|
490
|
+
opacity: 0.85;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
[data-slot='toast-action'],
|
|
494
|
+
[data-slot='toast-cancel'] {
|
|
495
|
+
flex: none;
|
|
496
|
+
height: 24px;
|
|
497
|
+
padding: 0 8px;
|
|
498
|
+
border: 0;
|
|
499
|
+
border-radius: ${t.radius.sm};
|
|
500
|
+
font: inherit;
|
|
501
|
+
font-size: 12px;
|
|
502
|
+
font-weight: 500;
|
|
503
|
+
cursor: pointer;
|
|
504
|
+
transition: opacity ${t.dur.fast} ease;
|
|
505
|
+
|
|
506
|
+
&:hover {
|
|
507
|
+
opacity: 0.88;
|
|
508
|
+
}
|
|
509
|
+
&:focus-visible {
|
|
510
|
+
outline: 2px solid ${t.focus};
|
|
511
|
+
outline-offset: 2px;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
[data-slot='toast-action'] {
|
|
515
|
+
background: var(--_tint);
|
|
516
|
+
color: ${toastDef.vars.background};
|
|
517
|
+
}
|
|
518
|
+
[data-slot='toast-cancel'] {
|
|
519
|
+
background: color-mix(in srgb, currentcolor 12%, transparent);
|
|
520
|
+
color: inherit;
|
|
521
|
+
}
|
|
522
|
+
[data-slot='toast-close'] {
|
|
523
|
+
position: absolute;
|
|
524
|
+
top: 0;
|
|
525
|
+
inset-inline-start: 0;
|
|
526
|
+
width: 20px;
|
|
527
|
+
height: 20px;
|
|
528
|
+
padding: 0;
|
|
529
|
+
border: 1px solid ${toastDef.vars.border};
|
|
530
|
+
border-radius: 50%;
|
|
531
|
+
background: ${toastDef.vars.background};
|
|
532
|
+
color: ${toastDef.vars.foreground};
|
|
533
|
+
font-size: 12px;
|
|
534
|
+
line-height: 0;
|
|
535
|
+
cursor: pointer;
|
|
536
|
+
translate: -35% -35%;
|
|
537
|
+
transition:
|
|
538
|
+
opacity ${t.dur.fast} ease,
|
|
539
|
+
background ${t.dur.fast} ease;
|
|
540
|
+
|
|
541
|
+
&:where(:not([hidden])) {
|
|
542
|
+
display: flex;
|
|
543
|
+
align-items: center;
|
|
544
|
+
justify-content: center;
|
|
545
|
+
}
|
|
546
|
+
&:is([dir='rtl'] *) {
|
|
547
|
+
translate: 35% -35%;
|
|
548
|
+
}
|
|
549
|
+
&:hover {
|
|
550
|
+
background: ${t.surface.overlay};
|
|
551
|
+
}
|
|
552
|
+
&:focus-visible {
|
|
553
|
+
outline: 2px solid ${t.focus};
|
|
554
|
+
outline-offset: 1px;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
@keyframes vela-toast-spin {
|
|
559
|
+
to {
|
|
560
|
+
transform: rotate(360deg);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
`,
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
interface View {
|
|
568
|
+
toast: ToastT;
|
|
569
|
+
li: HTMLLIElement;
|
|
570
|
+
position: ToasterPosition;
|
|
571
|
+
height: number;
|
|
572
|
+
removed: boolean;
|
|
573
|
+
duration: number;
|
|
574
|
+
remaining: number;
|
|
575
|
+
started: number;
|
|
576
|
+
timer: ReturnType<typeof setTimeout> | null;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
interface Stack {
|
|
580
|
+
ol: HTMLOListElement;
|
|
581
|
+
y: 'top' | 'bottom';
|
|
582
|
+
x: 'left' | 'center' | 'right';
|
|
583
|
+
hovered: boolean;
|
|
584
|
+
interacting: boolean;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const px = (v: number | string): string => (typeof v === 'number' ? `${v}px` : v);
|
|
588
|
+
|
|
589
|
+
class ToasterImpl implements ToasterHandle {
|
|
590
|
+
readonly el: HTMLElement;
|
|
591
|
+
private readonly stacks = new Map<ToasterPosition, Stack>();
|
|
592
|
+
private readonly views = new Map<ToastId, View>();
|
|
593
|
+
private readonly byLi = new WeakMap<Element, View>();
|
|
594
|
+
private readonly i18n: Translator | null;
|
|
595
|
+
private readonly position: ToasterPosition;
|
|
596
|
+
private readonly gap: number;
|
|
597
|
+
private readonly visible: number;
|
|
598
|
+
private paused = false;
|
|
599
|
+
private hotkeyOpen = false;
|
|
600
|
+
|
|
601
|
+
private readonly onQueue = (e: QueueEvent): void => {
|
|
602
|
+
if (e.kind === 'show') {
|
|
603
|
+
if ((e.toast.toasterId ?? null) !== (this.props.id ?? null)) return;
|
|
604
|
+
this.show(e.toast);
|
|
605
|
+
} else if (e.id === undefined) for (const v of [...this.views.values()]) this.remove(v);
|
|
606
|
+
else {
|
|
607
|
+
const v = this.views.get(e.id);
|
|
608
|
+
if (v) this.remove(v);
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
private readonly onKey = (e: KeyboardEvent): void => {
|
|
613
|
+
const keys = this.props.hotkey ?? ['altKey', 'KeyT'];
|
|
614
|
+
const pressed = keys.length > 0 && keys.every((k) => (e as unknown as Record<string, unknown>)[k] === true || e.code === k);
|
|
615
|
+
if (pressed) {
|
|
616
|
+
const first = [...this.stacks.values()].find((s) => s.ol.childElementCount > 0);
|
|
617
|
+
if (!first) return;
|
|
618
|
+
this.hotkeyOpen = true;
|
|
619
|
+
first.ol.focus();
|
|
620
|
+
this.relayoutAll();
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
if (e.key === 'Escape' && this.hotkeyOpen && this.el.contains(document.activeElement)) {
|
|
624
|
+
this.hotkeyOpen = false;
|
|
625
|
+
(document.activeElement as HTMLElement | null)?.blur();
|
|
626
|
+
this.relayoutAll();
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
private readonly onVisibility = (): void => this.syncTimers();
|
|
631
|
+
|
|
632
|
+
constructor(private readonly props: ToasterProps) {
|
|
633
|
+
const host = props.host ?? document.body;
|
|
634
|
+
const root = host.getRootNode();
|
|
635
|
+
injectStyles(root instanceof ShadowRoot ? root : host.ownerDocument);
|
|
636
|
+
this.i18n = props.i18n ?? kitTranslator();
|
|
637
|
+
this.position = props.position ?? 'bottom-right';
|
|
638
|
+
this.gap = props.gap ?? 14;
|
|
639
|
+
this.visible = props.visibleToasts ?? 3;
|
|
640
|
+
const keys = (props.hotkey ?? ['altKey', 'KeyT']).map((k) => KEY_NAMES[k] ?? k.replace(/^(Key|Digit)/, '')).join('+');
|
|
641
|
+
this.el = h('section', {
|
|
642
|
+
className: props.className,
|
|
643
|
+
slot: toastDef.slot('root'),
|
|
644
|
+
attrs: {
|
|
645
|
+
'aria-keyshortcuts': keys || null,
|
|
646
|
+
tabindex: -1,
|
|
647
|
+
'aria-live': 'polite',
|
|
648
|
+
'aria-relevant': 'additions text',
|
|
649
|
+
'aria-atomic': 'false',
|
|
650
|
+
dir: props.dir && props.dir !== 'auto' ? props.dir : null,
|
|
651
|
+
},
|
|
652
|
+
});
|
|
653
|
+
this.relabel();
|
|
654
|
+
host.append(this.el);
|
|
655
|
+
if (props.theme || !this.el.parentElement?.closest('.vela-ui')) ensureUIHost(this.el, props.theme);
|
|
656
|
+
queue.listeners.add(this.onQueue);
|
|
657
|
+
document.addEventListener('keydown', this.onKey);
|
|
658
|
+
document.addEventListener('visibilitychange', this.onVisibility);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
relabel(): void {
|
|
662
|
+
this.el.setAttribute('aria-label', this.props.containerAriaLabel ?? textOf(this.i18n, TEXT.region));
|
|
663
|
+
const close = textOf(this.i18n, TEXT.close);
|
|
664
|
+
for (const b of this.el.querySelectorAll(`[data-slot='${toastDef.slot('close')}']`)) b.setAttribute('aria-label', close);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
destroy(): void {
|
|
668
|
+
queue.listeners.delete(this.onQueue);
|
|
669
|
+
document.removeEventListener('keydown', this.onKey);
|
|
670
|
+
document.removeEventListener('visibilitychange', this.onVisibility);
|
|
671
|
+
for (const v of this.views.values()) if (v.timer) clearTimeout(v.timer);
|
|
672
|
+
this.views.clear();
|
|
673
|
+
this.stacks.clear();
|
|
674
|
+
this.el.remove();
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
private stack(position: ToasterPosition): Stack {
|
|
678
|
+
let s = this.stacks.get(position);
|
|
679
|
+
if (s) return s;
|
|
680
|
+
const [y, x] = position.split('-') as ['top' | 'bottom', 'left' | 'center' | 'right'];
|
|
681
|
+
const ol = h('ol', { slot: toastDef.slot('list'), data: { y, x, contained: this.props.contained ?? false }, attrs: { tabindex: -1 } });
|
|
682
|
+
this.applyOffset(ol);
|
|
683
|
+
const stack: Stack = { ol, y, x, hovered: false, interacting: false };
|
|
684
|
+
ol.addEventListener('mouseenter', () => this.setHover(stack, true));
|
|
685
|
+
ol.addEventListener('mousemove', () => this.setHover(stack, true));
|
|
686
|
+
ol.addEventListener('mouseleave', () => {
|
|
687
|
+
if (!stack.interacting) this.setHover(stack, false);
|
|
688
|
+
});
|
|
689
|
+
ol.addEventListener('pointerdown', (e) => {
|
|
690
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
|
691
|
+
stack.interacting = true;
|
|
692
|
+
this.relayout(position);
|
|
693
|
+
});
|
|
694
|
+
ol.addEventListener('pointerup', () => {
|
|
695
|
+
stack.interacting = false;
|
|
696
|
+
this.relayout(position);
|
|
697
|
+
});
|
|
698
|
+
ol.addEventListener('focusout', (e) => {
|
|
699
|
+
if (e.relatedTarget instanceof Node && ol.contains(e.relatedTarget)) return;
|
|
700
|
+
this.hotkeyOpen = false;
|
|
701
|
+
this.relayout(position);
|
|
702
|
+
});
|
|
703
|
+
// A new corner keeps the corners' order stable in the reading order of the region.
|
|
704
|
+
this.el.append(ol);
|
|
705
|
+
this.stacks.set(position, stack);
|
|
706
|
+
return stack;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
private applyOffset(ol: HTMLElement): void {
|
|
710
|
+
const fallback = this.props.contained ? '16px' : '24px';
|
|
711
|
+
const offset = this.props.offset ?? fallback;
|
|
712
|
+
const sides = typeof offset === 'object' ? offset : { top: offset, right: offset, bottom: offset, left: offset };
|
|
713
|
+
const rtl = this.props.dir === 'rtl';
|
|
714
|
+
ol.style.setProperty('--_offset-top', px(sides.top ?? fallback));
|
|
715
|
+
ol.style.setProperty('--_offset-bottom', px(sides.bottom ?? fallback));
|
|
716
|
+
ol.style.setProperty('--_offset-start', px((rtl ? sides.right : sides.left) ?? fallback));
|
|
717
|
+
ol.style.setProperty('--_offset-end', px((rtl ? sides.left : sides.right) ?? fallback));
|
|
718
|
+
ol.style.setProperty('--_gap', `${this.gap}px`);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
private setHover(stack: Stack, hovered: boolean): void {
|
|
722
|
+
if (stack.hovered === hovered) return;
|
|
723
|
+
stack.hovered = hovered;
|
|
724
|
+
this.relayoutAll();
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
private show(toast: ToastT): void {
|
|
728
|
+
const existing = this.views.get(toast.id);
|
|
729
|
+
if (existing && !existing.removed) {
|
|
730
|
+
existing.toast = toast;
|
|
731
|
+
this.render(existing);
|
|
732
|
+
existing.height = this.measure(existing.li);
|
|
733
|
+
this.restartTimer(existing);
|
|
734
|
+
this.relayout(existing.position);
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
const position = toast.position ?? this.position;
|
|
738
|
+
const stack = this.stack(position);
|
|
739
|
+
const li = h('li', { slot: toastDef.slot('item'), data: { y: stack.y, x: stack.x }, attrs: { tabindex: 0 } });
|
|
740
|
+
const view: View = { toast, li, position, height: 0, removed: false, duration: 0, remaining: 0, started: 0, timer: null };
|
|
741
|
+
this.views.set(toast.id, view);
|
|
742
|
+
this.byLi.set(li, view);
|
|
743
|
+
this.render(view);
|
|
744
|
+
stack.ol.prepend(li);
|
|
745
|
+
view.height = this.measure(li);
|
|
746
|
+
this.bindSwipe(view, stack);
|
|
747
|
+
this.restartTimer(view);
|
|
748
|
+
this.relayout(position);
|
|
749
|
+
requestAnimationFrame(() => li.setAttribute('data-mounted', ''));
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
private render(view: View): void {
|
|
753
|
+
const { toast: x, li } = view;
|
|
754
|
+
const opts = this.props.toastOptions ?? {};
|
|
755
|
+
const dismissible = x.dismissible ?? opts.dismissible ?? true;
|
|
756
|
+
li.className = [opts.className, x.className].filter(Boolean).join(' ');
|
|
757
|
+
li.setAttribute('data-type', x.type);
|
|
758
|
+
li.setAttribute('data-dismissible', String(dismissible));
|
|
759
|
+
li.toggleAttribute('data-rich-colors', x.richColors ?? opts.richColors ?? this.props.richColors ?? false);
|
|
760
|
+
li.toggleAttribute('data-invert', x.invert ?? opts.invert ?? this.props.invert ?? false);
|
|
761
|
+
li.replaceChildren();
|
|
762
|
+
if (dismissible && x.type !== 'loading' && (x.closeButton ?? opts.closeButton ?? this.props.closeButton ?? false)) {
|
|
763
|
+
const close = h('button', {
|
|
764
|
+
slot: toastDef.slot('close'),
|
|
765
|
+
attrs: { type: 'button', 'aria-label': textOf(this.i18n, TEXT.close) },
|
|
766
|
+
on: { click: () => settle(x.id, 'dismiss') },
|
|
767
|
+
});
|
|
768
|
+
close.append(this.icon(this.props.icons?.close ?? DEFAULT_ICONS.close));
|
|
769
|
+
li.append(close);
|
|
770
|
+
}
|
|
771
|
+
const icon = x.icon === null ? null : (x.icon ?? (x.type === 'default' ? null : (this.props.icons?.[x.type] ?? DEFAULT_ICONS[x.type])));
|
|
772
|
+
if (icon !== null) li.append(h('div', { slot: toastDef.slot('icon'), data: { type: x.type } }, this.icon(icon)));
|
|
773
|
+
const content = h('div', { slot: toastDef.slot('content') });
|
|
774
|
+
const title = read(x.title);
|
|
775
|
+
if (title !== undefined) content.append(h('div', { slot: toastDef.slot('title') }, title));
|
|
776
|
+
const description = read(x.description);
|
|
777
|
+
if (description !== undefined) content.append(h('div', { slot: toastDef.slot('description') }, description));
|
|
778
|
+
li.append(content);
|
|
779
|
+
if (x.cancel) li.append(this.button(x, x.cancel, 'cancel'));
|
|
780
|
+
if (x.action) li.append(this.button(x, x.action, 'action'));
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
private icon(icon: string | Node): Node {
|
|
784
|
+
return typeof icon === 'string' ? iconEl(icon, this.el.ownerDocument) : icon.cloneNode(true);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
private button(x: ToastT, action: ToastAction | Node, part: 'action' | 'cancel'): Node {
|
|
788
|
+
if (action instanceof Node) return action;
|
|
789
|
+
return h('button', {
|
|
790
|
+
slot: toastDef.slot(part),
|
|
791
|
+
attrs: { type: 'button' },
|
|
792
|
+
text: action.label,
|
|
793
|
+
on: {
|
|
794
|
+
click: (e) => {
|
|
795
|
+
action.onClick(e);
|
|
796
|
+
if (part === 'cancel' || !e.defaultPrevented) settle(x.id, 'dismiss');
|
|
797
|
+
},
|
|
798
|
+
},
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/** The toast's natural height, whatever height the stack gives it. */
|
|
803
|
+
private measure(li: HTMLElement): number {
|
|
804
|
+
const height = li.style.height;
|
|
805
|
+
li.style.height = 'auto';
|
|
806
|
+
const natural = li.offsetHeight;
|
|
807
|
+
li.style.height = height;
|
|
808
|
+
return natural;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
private remove(view: View): void {
|
|
812
|
+
if (view.removed) return;
|
|
813
|
+
view.removed = true;
|
|
814
|
+
if (view.timer) clearTimeout(view.timer);
|
|
815
|
+
view.timer = null;
|
|
816
|
+
const front = view.li.getAttribute('data-front') === 'true' && !view.li.hasAttribute('data-swiped');
|
|
817
|
+
view.li.setAttribute('data-removed', front ? 'front' : '');
|
|
818
|
+
this.views.delete(view.toast.id);
|
|
819
|
+
this.relayout(view.position);
|
|
820
|
+
setTimeout(() => view.li.remove(), EXIT_MS);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
private expanded(stack: Stack): boolean {
|
|
824
|
+
return (this.props.expand ?? false) || stack.hovered || stack.interacting || this.hotkeyOpen;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
private relayoutAll(): void {
|
|
828
|
+
for (const position of this.stacks.keys()) this.relayout(position);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
private relayout(position: ToasterPosition): void {
|
|
832
|
+
const stack = this.stacks.get(position);
|
|
833
|
+
if (!stack) return;
|
|
834
|
+
// Newest first: a toast is prepended to its list.
|
|
835
|
+
const items = [...stack.ol.children].flatMap((li) => {
|
|
836
|
+
const v = this.byLi.get(li);
|
|
837
|
+
return v && !v.removed ? [v] : [];
|
|
838
|
+
});
|
|
839
|
+
const expanded = this.expanded(stack);
|
|
840
|
+
const lift = stack.y === 'bottom' ? -1 : 1;
|
|
841
|
+
const front = items[0]?.height ?? 0;
|
|
842
|
+
let offset = 0;
|
|
843
|
+
let extent = 0;
|
|
844
|
+
items.forEach((v, i) => {
|
|
845
|
+
const visible = i < this.visible;
|
|
846
|
+
const y = expanded ? lift * offset : lift * this.gap * i;
|
|
847
|
+
const scale = expanded ? 1 : 1 - i * 0.05;
|
|
848
|
+
const li = v.li;
|
|
849
|
+
li.style.setProperty('--_y', `${y}px`);
|
|
850
|
+
li.style.setProperty('--_scale', String(scale));
|
|
851
|
+
li.style.height = `${expanded || i === 0 ? v.height : front}px`;
|
|
852
|
+
li.style.zIndex = String(items.length - i);
|
|
853
|
+
li.setAttribute('data-index', String(i));
|
|
854
|
+
li.setAttribute('data-front', String(i === 0));
|
|
855
|
+
li.setAttribute('data-expanded', String(expanded));
|
|
856
|
+
li.setAttribute('data-visible', String(visible));
|
|
857
|
+
if (visible) extent = expanded ? offset + v.height : front + this.gap * i;
|
|
858
|
+
offset += v.height + this.gap;
|
|
859
|
+
});
|
|
860
|
+
stack.ol.style.height = `${extent}px`;
|
|
861
|
+
stack.ol.toggleAttribute('data-expanded', expanded);
|
|
862
|
+
this.syncTimers();
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
private restartTimer(view: View): void {
|
|
866
|
+
if (view.timer) clearTimeout(view.timer);
|
|
867
|
+
view.timer = null;
|
|
868
|
+
const opts = this.props.toastOptions ?? {};
|
|
869
|
+
view.duration = view.toast.type === 'loading' ? Infinity : (view.toast.duration ?? opts.duration ?? this.props.duration ?? 4000);
|
|
870
|
+
view.remaining = view.duration;
|
|
871
|
+
view.started = 0;
|
|
872
|
+
if (!this.paused) this.runTimer(view);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
private runTimer(view: View): void {
|
|
876
|
+
if (view.timer || view.removed || !Number.isFinite(view.remaining)) return;
|
|
877
|
+
view.started = Date.now();
|
|
878
|
+
view.timer = setTimeout(() => {
|
|
879
|
+
view.timer = null;
|
|
880
|
+
settle(view.toast.id, 'auto');
|
|
881
|
+
}, view.remaining);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/** Timers stand still while a stack is spread out or held, and while the page is hidden. */
|
|
885
|
+
private syncTimers(): void {
|
|
886
|
+
const paused = document.hidden || [...this.stacks.values()].some((s) => s.hovered || s.interacting) || this.hotkeyOpen;
|
|
887
|
+
if (paused === this.paused) return;
|
|
888
|
+
this.paused = paused;
|
|
889
|
+
for (const v of this.views.values()) {
|
|
890
|
+
if (paused) {
|
|
891
|
+
if (!v.timer) continue;
|
|
892
|
+
clearTimeout(v.timer);
|
|
893
|
+
v.timer = null;
|
|
894
|
+
v.remaining = Math.max(0, v.remaining - (Date.now() - v.started));
|
|
895
|
+
} else this.runTimer(v);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
private bindSwipe(view: View, stack: Stack): void {
|
|
900
|
+
const li = view.li;
|
|
901
|
+
const allowed = new Set<SwipeDirection>(this.props.swipeDirections ?? [stack.y, ...(stack.x === 'center' ? [] : [this.physicalX(stack.x)])]);
|
|
902
|
+
let start: { x: number; y: number; time: number } | null = null;
|
|
903
|
+
let axis: 'x' | 'y' | null = null;
|
|
904
|
+
let amount = 0;
|
|
905
|
+
const reset = (): void => {
|
|
906
|
+
start = null;
|
|
907
|
+
axis = null;
|
|
908
|
+
amount = 0;
|
|
909
|
+
li.removeAttribute('data-swiping');
|
|
910
|
+
li.style.setProperty('--_swipe-x', '0px');
|
|
911
|
+
li.style.setProperty('--_swipe-y', '0px');
|
|
912
|
+
};
|
|
913
|
+
li.addEventListener('pointerdown', (e) => {
|
|
914
|
+
if (view.removed || li.getAttribute('data-dismissible') === 'false' || view.toast.type === 'loading') return;
|
|
915
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
|
916
|
+
if (e.target instanceof Element && e.target.closest('button, a, input, select, textarea')) return;
|
|
917
|
+
start = { x: e.clientX, y: e.clientY, time: Date.now() };
|
|
918
|
+
li.setPointerCapture(e.pointerId);
|
|
919
|
+
});
|
|
920
|
+
li.addEventListener('pointermove', (e) => {
|
|
921
|
+
if (!start) return;
|
|
922
|
+
const dx = e.clientX - start.x;
|
|
923
|
+
const dy = e.clientY - start.y;
|
|
924
|
+
if (!axis) {
|
|
925
|
+
if (Math.abs(dx) < 2 && Math.abs(dy) < 2) return;
|
|
926
|
+
axis = Math.abs(dx) > Math.abs(dy) ? 'x' : 'y';
|
|
927
|
+
li.setAttribute('data-swiping', '');
|
|
928
|
+
}
|
|
929
|
+
const delta = axis === 'x' ? dx : dy;
|
|
930
|
+
const dir: SwipeDirection = axis === 'x' ? (delta > 0 ? 'right' : 'left') : delta > 0 ? 'bottom' : 'top';
|
|
931
|
+
amount = allowed.has(dir) ? delta : delta / (1.5 + Math.abs(delta) / 20);
|
|
932
|
+
li.style.setProperty(axis === 'x' ? '--_swipe-x' : '--_swipe-y', `${amount}px`);
|
|
933
|
+
});
|
|
934
|
+
const end = (): void => {
|
|
935
|
+
if (!start) return;
|
|
936
|
+
const elapsed = Math.max(1, Date.now() - start.time);
|
|
937
|
+
const dir: SwipeDirection | null = axis === 'x' ? (amount > 0 ? 'right' : 'left') : axis === 'y' ? (amount > 0 ? 'bottom' : 'top') : null;
|
|
938
|
+
if (dir && allowed.has(dir) && (Math.abs(amount) >= SWIPE_THRESHOLD || Math.abs(amount) / elapsed > VELOCITY_THRESHOLD)) {
|
|
939
|
+
const out = (axis === 'x' ? li.offsetWidth : li.offsetHeight) * Math.sign(amount);
|
|
940
|
+
li.removeAttribute('data-swiping');
|
|
941
|
+
li.setAttribute('data-swiped', '');
|
|
942
|
+
li.style.setProperty(axis === 'x' ? '--_swipe-x' : '--_swipe-y', `${out}px`);
|
|
943
|
+
start = null;
|
|
944
|
+
settle(view.toast.id, 'dismiss');
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
reset();
|
|
948
|
+
};
|
|
949
|
+
li.addEventListener('pointerup', end);
|
|
950
|
+
li.addEventListener('pointercancel', reset);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
private physicalX(x: 'left' | 'right'): SwipeDirection {
|
|
954
|
+
const rtl = (this.props.dir === 'rtl' || getComputedStyle(this.el.parentElement ?? document.documentElement).direction === 'rtl') && this.props.dir !== 'ltr';
|
|
955
|
+
return rtl ? (x === 'left' ? 'right' : 'left') : x;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
function read(content: ToastContent | undefined): string | Node | undefined {
|
|
960
|
+
return typeof content === 'function' ? content() : content;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
export function createToaster(props: ToasterProps = {}): ToasterHandle {
|
|
964
|
+
return new ToasterImpl(props);
|
|
965
|
+
}
|