@zazz-ui/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +61 -0
- package/dist/sri.json +75 -0
- package/dist/zazz.css +1 -0
- package/dist/zazz.js +1 -0
- package/examples/about.html +590 -0
- package/examples/components.html +1040 -0
- package/examples/forms.html +511 -0
- package/examples/index.html +268 -0
- package/examples/layout.html +320 -0
- package/examples/products.html +846 -0
- package/examples/responsive.html +339 -0
- package/package.json +85 -0
- package/src/base/_layers.css +12 -0
- package/src/base/_layout.css +568 -0
- package/src/base/_reset.css +515 -0
- package/src/base/_typography.css +495 -0
- package/src/base/_utilities.css +6628 -0
- package/src/base/_variables.css +682 -0
- package/src/base/_view-transitions.css +103 -0
- package/src/base/command-score.d.ts +12 -0
- package/src/base/command-score.d.ts.map +1 -0
- package/src/base/command-score.js +151 -0
- package/src/base/command-score.ts +192 -0
- package/src/base/dialog-lifecycle.d.ts +11 -0
- package/src/base/dialog-lifecycle.d.ts.map +1 -0
- package/src/base/dialog-lifecycle.js +65 -0
- package/src/base/dialog-lifecycle.ts +80 -0
- package/src/base/embla.d.ts +90 -0
- package/src/base/embla.d.ts.map +1 -0
- package/src/base/embla.js +643 -0
- package/src/base/embla.ts +791 -0
- package/src/base/hotkeys.d.ts +82 -0
- package/src/base/hotkeys.d.ts.map +1 -0
- package/src/base/hotkeys.js +141 -0
- package/src/base/hotkeys.ts +195 -0
- package/src/base/navigation.d.ts +2 -0
- package/src/base/navigation.d.ts.map +1 -0
- package/src/base/navigation.js +136 -0
- package/src/base/navigation.ts +151 -0
- package/src/base/reveal.d.ts +80 -0
- package/src/base/reveal.d.ts.map +1 -0
- package/src/base/reveal.js +305 -0
- package/src/base/reveal.ts +379 -0
- package/src/base/signals.d.ts +89 -0
- package/src/base/signals.d.ts.map +1 -0
- package/src/base/signals.js +134 -0
- package/src/base/signals.ts +164 -0
- package/src/base/typeahead.d.ts +105 -0
- package/src/base/typeahead.d.ts.map +1 -0
- package/src/base/typeahead.js +411 -0
- package/src/base/typeahead.ts +500 -0
- package/src/base/utils.d.ts +56 -0
- package/src/base/utils.d.ts.map +1 -0
- package/src/base/utils.js +88 -0
- package/src/base/utils.ts +98 -0
- package/src/base/zazz-element.d.ts +44 -0
- package/src/base/zazz-element.d.ts.map +1 -0
- package/src/base/zazz-element.js +59 -0
- package/src/base/zazz-element.ts +73 -0
- package/src/globals.d.ts +148 -0
- package/src/head.d.ts +95 -0
- package/src/head.d.ts.map +1 -0
- package/src/head.js +311 -0
- package/src/head.ts +433 -0
- package/src/index.css +71 -0
- package/src/index.d.ts +36 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +35 -0
- package/src/index.ts +36 -0
- package/src/manifest.d.ts +90 -0
- package/src/manifest.d.ts.map +1 -0
- package/src/manifest.js +614 -0
- package/src/manifest.ts +667 -0
- package/src/primitives/accordion/accordion.css +104 -0
- package/src/primitives/accordion/accordion.html +69 -0
- package/src/primitives/alert-dialog/alert-dialog.css +38 -0
- package/src/primitives/alert-dialog/alert-dialog.html +53 -0
- package/src/primitives/autocomplete/autocomplete-groups.html +77 -0
- package/src/primitives/autocomplete/autocomplete.css +114 -0
- package/src/primitives/autocomplete/autocomplete.d.ts +30 -0
- package/src/primitives/autocomplete/autocomplete.d.ts.map +1 -0
- package/src/primitives/autocomplete/autocomplete.html +66 -0
- package/src/primitives/autocomplete/autocomplete.js +41 -0
- package/src/primitives/autocomplete/autocomplete.ts +46 -0
- package/src/primitives/avatar/avatar.html +44 -0
- package/src/primitives/badge/badge-icon.html +36 -0
- package/src/primitives/badge/badge.css +251 -0
- package/src/primitives/badge/badge.html +7 -0
- package/src/primitives/breadcrumbs/breadcrumbs.html +9 -0
- package/src/primitives/button/button-icon-only.html +84 -0
- package/src/primitives/button/button-icon.html +82 -0
- package/src/primitives/button/button.css +319 -0
- package/src/primitives/button/button.html +8 -0
- package/src/primitives/button-group/button-group-vertical.html +6 -0
- package/src/primitives/button-group/button-group.css +94 -0
- package/src/primitives/button-group/button-group.html +64 -0
- package/src/primitives/card/card-subgrid.html +118 -0
- package/src/primitives/card/card.html +53 -0
- package/src/primitives/carousel/carousel.css +61 -0
- package/src/primitives/carousel/carousel.d.ts +16 -0
- package/src/primitives/carousel/carousel.d.ts.map +1 -0
- package/src/primitives/carousel/carousel.html +315 -0
- package/src/primitives/carousel/carousel.js +73 -0
- package/src/primitives/carousel/carousel.ts +84 -0
- package/src/primitives/checkbox/checkbox-tasklist.html +71 -0
- package/src/primitives/checkbox/checkbox.css +82 -0
- package/src/primitives/checkbox/checkbox.d.ts +21 -0
- package/src/primitives/checkbox/checkbox.d.ts.map +1 -0
- package/src/primitives/checkbox/checkbox.html +17 -0
- package/src/primitives/checkbox/checkbox.js +190 -0
- package/src/primitives/checkbox/checkbox.ts +219 -0
- package/src/primitives/combobox/combobox-multiselect.html +91 -0
- package/src/primitives/combobox/combobox.css +307 -0
- package/src/primitives/combobox/combobox.d.ts +100 -0
- package/src/primitives/combobox/combobox.d.ts.map +1 -0
- package/src/primitives/combobox/combobox.html +73 -0
- package/src/primitives/combobox/combobox.js +503 -0
- package/src/primitives/combobox/combobox.ts +559 -0
- package/src/primitives/command/command-actions.d.ts +31 -0
- package/src/primitives/command/command-actions.d.ts.map +1 -0
- package/src/primitives/command/command-actions.html +89 -0
- package/src/primitives/command/command-actions.js +70 -0
- package/src/primitives/command/command-actions.ts +77 -0
- package/src/primitives/command/command-dialog.html +77 -0
- package/src/primitives/command/command.css +151 -0
- package/src/primitives/command/command.d.ts +52 -0
- package/src/primitives/command/command.d.ts.map +1 -0
- package/src/primitives/command/command.html +119 -0
- package/src/primitives/command/command.js +146 -0
- package/src/primitives/command/command.ts +157 -0
- package/src/primitives/dialog/dialog-with-form.html +200 -0
- package/src/primitives/dialog/dialog.css +229 -0
- package/src/primitives/dialog/dialog.html +90 -0
- package/src/primitives/fields/fields.css +278 -0
- package/src/primitives/input/input-icon-leading.html +19 -0
- package/src/primitives/input/input-icon-trailing.html +18 -0
- package/src/primitives/input/input.css +114 -0
- package/src/primitives/input/input.html +61 -0
- package/src/primitives/input-group/input-group-password-group.html +108 -0
- package/src/primitives/input-group/input-group.css +215 -0
- package/src/primitives/input-group/input-group.html +132 -0
- package/src/primitives/kbd/kbd-group.html +18 -0
- package/src/primitives/kbd/kbd.css +87 -0
- package/src/primitives/kbd/kbd.html +7 -0
- package/src/primitives/lightbox/lightbox.css +459 -0
- package/src/primitives/lightbox/lightbox.d.ts +7 -0
- package/src/primitives/lightbox/lightbox.d.ts.map +1 -0
- package/src/primitives/lightbox/lightbox.html +665 -0
- package/src/primitives/lightbox/lightbox.js +96 -0
- package/src/primitives/lightbox/lightbox.ts +113 -0
- package/src/primitives/menu/menu-interest.html +52 -0
- package/src/primitives/menu/menu.css +55 -0
- package/src/primitives/menu/menu.d.ts +38 -0
- package/src/primitives/menu/menu.d.ts.map +1 -0
- package/src/primitives/menu/menu.html +70 -0
- package/src/primitives/menu/menu.js +117 -0
- package/src/primitives/menu/menu.ts +131 -0
- package/src/primitives/menubar/menubar-help-search.html +103 -0
- package/src/primitives/menubar/menubar.html +166 -0
- package/src/primitives/meter/meter.css +178 -0
- package/src/primitives/meter/meter.html +38 -0
- package/src/primitives/mobile-menu/mobile-menu.css +145 -0
- package/src/primitives/mobile-menu/mobile-menu.html +614 -0
- package/src/primitives/navigation-menu/navigation-menu-featured.html +76 -0
- package/src/primitives/navigation-menu/navigation-menu-icon-grid.html +158 -0
- package/src/primitives/navigation-menu/navigation-menu-interest.html +103 -0
- package/src/primitives/navigation-menu/navigation-menu-megamenu.html +141 -0
- package/src/primitives/navigation-menu/navigation-menu-simple.html +96 -0
- package/src/primitives/navigation-menu/navigation-menu.css +295 -0
- package/src/primitives/navigation-menu/navigation-menu.html +576 -0
- package/src/primitives/otp/otp.css +126 -0
- package/src/primitives/otp/otp.d.ts +89 -0
- package/src/primitives/otp/otp.d.ts.map +1 -0
- package/src/primitives/otp/otp.html +22 -0
- package/src/primitives/otp/otp.js +226 -0
- package/src/primitives/otp/otp.ts +279 -0
- package/src/primitives/password-group/password-group.css +238 -0
- package/src/primitives/password-group/password-group.d.ts +25 -0
- package/src/primitives/password-group/password-group.d.ts.map +1 -0
- package/src/primitives/password-group/password-group.html +109 -0
- package/src/primitives/password-group/password-group.js +76 -0
- package/src/primitives/password-group/password-group.ts +95 -0
- package/src/primitives/popover/popover.css +233 -0
- package/src/primitives/progress/progress-indeterminate.html +1 -0
- package/src/primitives/progress/progress.css +105 -0
- package/src/primitives/progress/progress.html +7 -0
- package/src/primitives/prose/prose.html +65 -0
- package/src/primitives/radio/radio.css +84 -0
- package/src/primitives/radio/radio.html +17 -0
- package/src/primitives/reveal/reveal.css +167 -0
- package/src/primitives/select/multiselect.d.ts +43 -0
- package/src/primitives/select/multiselect.d.ts.map +1 -0
- package/src/primitives/select/multiselect.js +147 -0
- package/src/primitives/select/multiselect.ts +188 -0
- package/src/primitives/select/select-align.html +49 -0
- package/src/primitives/select/select-multiple.html +12 -0
- package/src/primitives/select/select-sides.html +65 -0
- package/src/primitives/select/select.css +481 -0
- package/src/primitives/select/select.html +16 -0
- package/src/primitives/separator/separator.css +49 -0
- package/src/primitives/separator/separator.html +16 -0
- package/src/primitives/slider/slider.css +122 -0
- package/src/primitives/slider/slider.html +1 -0
- package/src/primitives/switch/switch.css +126 -0
- package/src/primitives/switch/switch.html +13 -0
- package/src/primitives/table/table-alternating.html +36 -0
- package/src/primitives/table/table-grid.html +30 -0
- package/src/primitives/table/table-size-sm.html +26 -0
- package/src/primitives/table/table.css +158 -0
- package/src/primitives/table/table.html +45 -0
- package/src/primitives/tabs/tabs.css +328 -0
- package/src/primitives/tabs/tabs.d.ts +35 -0
- package/src/primitives/tabs/tabs.d.ts.map +1 -0
- package/src/primitives/tabs/tabs.html +75 -0
- package/src/primitives/tabs/tabs.js +84 -0
- package/src/primitives/tabs/tabs.ts +93 -0
- package/src/primitives/textarea/textarea.css +78 -0
- package/src/primitives/textarea/textarea.html +10 -0
- package/src/primitives/toaster/toaster.css +309 -0
- package/src/primitives/toaster/toaster.d.ts +138 -0
- package/src/primitives/toaster/toaster.d.ts.map +1 -0
- package/src/primitives/toaster/toaster.html +77 -0
- package/src/primitives/toaster/toaster.js +736 -0
- package/src/primitives/toaster/toaster.ts +823 -0
- package/src/primitives/toggle/toggle.css +295 -0
- package/src/primitives/toggle/toggle.html +68 -0
- package/src/primitives/toggle-group/toggle-group-multiple.html +14 -0
- package/src/primitives/toggle-group/toggle-group-vertical.html +14 -0
- package/src/primitives/toggle-group/toggle-group.css +73 -0
- package/src/primitives/toggle-group/toggle-group.html +53 -0
- package/src/primitives/toolbar/toolbar.html +45 -0
- package/src/primitives/tooltip/tooltip-disabled.html +19 -0
- package/src/primitives/tooltip/tooltip-sides.html +57 -0
- package/src/primitives/tooltip/tooltip-with-kbd.html +31 -0
- package/src/primitives/tooltip/tooltip.css +235 -0
- package/src/primitives/tooltip/tooltip.html +12 -0
- package/src/primitives/utilities/aspect-ratio.html +17 -0
- package/src/primitives/utilities/basis.html +27 -0
- package/src/primitives/utilities/col-span.html +23 -0
- package/src/primitives/utilities/compose.html +19 -0
- package/src/primitives/utilities/flex-align.html +26 -0
- package/src/primitives/utilities/flex-direction.html +36 -0
- package/src/primitives/utilities/flex-grow.html +17 -0
- package/src/primitives/utilities/gap.html +62 -0
- package/src/primitives/utilities/grid-cols.html +20 -0
- package/src/primitives/utilities/grid-pile.html +5 -0
- package/src/primitives/utilities/grid-responsive.html +31 -0
- package/src/primitives/utilities/margin-auto.html +13 -0
- package/src/primitives/utilities/object-fit.html +41 -0
- package/src/primitives/utilities/order.html +17 -0
- package/src/primitives/utilities/overflow.html +26 -0
- package/src/primitives/utilities/padding-compose.html +23 -0
- package/src/primitives/utilities/padding.html +30 -0
- package/src/primitives/utilities/position.html +13 -0
- package/src/primitives/utilities/responsive-flex.html +22 -0
- package/src/primitives/utilities/responsive-grid.html +31 -0
- package/src/primitives/utilities/size.html +22 -0
- package/src/primitives/utilities/width.html +22 -0
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
3
|
+
if (value !== null && value !== void 0) {
|
|
4
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
5
|
+
var dispose, inner;
|
|
6
|
+
if (async) {
|
|
7
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
8
|
+
dispose = value[Symbol.asyncDispose];
|
|
9
|
+
}
|
|
10
|
+
if (dispose === void 0) {
|
|
11
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
12
|
+
dispose = value[Symbol.dispose];
|
|
13
|
+
if (async) inner = dispose;
|
|
14
|
+
}
|
|
15
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
16
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
17
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
18
|
+
}
|
|
19
|
+
else if (async) {
|
|
20
|
+
env.stack.push({ async: true });
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
};
|
|
24
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
25
|
+
return function (env) {
|
|
26
|
+
function fail(e) {
|
|
27
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
28
|
+
env.hasError = true;
|
|
29
|
+
}
|
|
30
|
+
var r, s = 0;
|
|
31
|
+
function next() {
|
|
32
|
+
while (r = env.stack.pop()) {
|
|
33
|
+
try {
|
|
34
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
35
|
+
if (r.dispose) {
|
|
36
|
+
var result = r.dispose.call(r.value);
|
|
37
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
38
|
+
}
|
|
39
|
+
else s |= 1;
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
fail(e);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
46
|
+
if (env.hasError) throw env.error;
|
|
47
|
+
}
|
|
48
|
+
return next();
|
|
49
|
+
};
|
|
50
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
51
|
+
var e = new Error(message);
|
|
52
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* @fileoverview `<ui-toaster>`: HTML web component for stacked toast notifications.
|
|
56
|
+
* @description Light-DOM custom element that hosts a top-layer toast stack, plus
|
|
57
|
+
* the `window.Toaster` imperative API. The stacking model (newest toast in front,
|
|
58
|
+
* older toasts peeking behind, expand on hover, timer pause on hover/hidden tab)
|
|
59
|
+
* is adapted from Sonner by Emil Kowalski (https://sonner.emilkowal.ski, MIT).
|
|
60
|
+
*
|
|
61
|
+
* The region is a `popover="manual"` element: it enters the top layer via
|
|
62
|
+
* `showPopover()` when the first toast arrives and leaves it after the last
|
|
63
|
+
* toast's exit transition. Toasts are plain `<li>` children, so the collapsed
|
|
64
|
+
* stack offsets in `_toaster.css` work with normal CSS transforms.
|
|
65
|
+
*
|
|
66
|
+
* Fire toasts two ways:
|
|
67
|
+
* - Declaratively, from any button, via a custom Invoker Command:
|
|
68
|
+
* `command="--toast"` (or `--toast-success|info|warning|destructive`) with
|
|
69
|
+
* `commandfor="<region id>"`. Toast content comes from the button's
|
|
70
|
+
* `data-title`, `data-description`, `data-variant`, `data-duration`, and
|
|
71
|
+
* `data-close-button` attributes.
|
|
72
|
+
* - Imperatively: `window.Toaster.toast({ title, description, variant, … })`
|
|
73
|
+
* and the `.success()/.info()/.warning()/.error()` shorthands.
|
|
74
|
+
*
|
|
75
|
+
* Region attributes:
|
|
76
|
+
* - `data-position`: `top-start | top-center | top-end | bottom-start |
|
|
77
|
+
* bottom-center | bottom-end` (logical; default `bottom-end`).
|
|
78
|
+
*
|
|
79
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Popover_API
|
|
80
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* <ui-toaster class="ui-toaster" id="toaster" popover="manual"></ui-toaster>
|
|
84
|
+
* <button commandfor="toaster" command="--toast" data-title="Saved">Save</button>
|
|
85
|
+
*/
|
|
86
|
+
import { computed, effect, state } from "../../base/signals.js";
|
|
87
|
+
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
|
|
88
|
+
// `using` compiles (target ES2022) to try/finally helpers that read this
|
|
89
|
+
// well-known symbol at runtime; engines without native Explicit Resource
|
|
90
|
+
// Management (Safari) don't define it, so give them a local stand-in.
|
|
91
|
+
Symbol.dispose ??= Symbol("Symbol.dispose");
|
|
92
|
+
// --- Constants ---
|
|
93
|
+
/** Default toast lifetime in milliseconds. */
|
|
94
|
+
const TOAST_LIFETIME = 4000;
|
|
95
|
+
/** Maximum number of toasts shown in the collapsed stack. */
|
|
96
|
+
const VISIBLE_TOASTS = 3;
|
|
97
|
+
/** Safety net for node removal when no exit `transitionend` fires. */
|
|
98
|
+
const EXIT_FALLBACK_MS = 600;
|
|
99
|
+
const VARIANTS = ["success", "info", "warning", "destructive"];
|
|
100
|
+
// --- Icons (adapted from Sonner's assets.tsx (MIT, Emil Kowalski)) ---
|
|
101
|
+
const ICONS = {
|
|
102
|
+
success: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clip-rule="evenodd"/></svg>',
|
|
103
|
+
info: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a.75.75 0 000 1.5h.253a.25.25 0 01.244.304l-.459 2.066A1.75 1.75 0 0010.747 15H11a.75.75 0 000-1.5h-.253a.25.25 0 01-.244-.304l.459-2.066A1.75 1.75 0 009.253 9H9z" clip-rule="evenodd"/></svg>',
|
|
104
|
+
warning: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M9.401 3.003c1.155-2 4.043-2 5.197 0l7.355 12.748c1.154 2-.29 4.5-2.599 4.5H4.645c-2.309 0-3.752-2.5-2.598-4.5L9.4 3.003zM12 8.25a.75.75 0 01.75.75v3.75a.75.75 0 01-1.5 0V9a.75.75 0 01.75-.75zm0 8.25a.75.75 0 100-1.5.75.75 0 000 1.5z" clip-rule="evenodd"/></svg>',
|
|
105
|
+
destructive: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-5a.75.75 0 01.75.75v4.5a.75.75 0 01-1.5 0v-4.5A.75.75 0 0110 5zm0 10a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd"/></svg>',
|
|
106
|
+
close: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>',
|
|
107
|
+
};
|
|
108
|
+
// --- Measurement ---
|
|
109
|
+
/**
|
|
110
|
+
* @description Unclamps every toast's block-size for a batch measurement (the
|
|
111
|
+
* inline style beats the collapsed block-size rule) and returns a disposer
|
|
112
|
+
* that restores the clamp. Bind it with `using` so the restore is guaranteed
|
|
113
|
+
* at scope exit, even if a measure in between throws.
|
|
114
|
+
*
|
|
115
|
+
* @param toasts - The live toasts to unclamp.
|
|
116
|
+
* @returns A `Disposable` that removes the inline block-size again.
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
function unclampForMeasure(toasts) {
|
|
120
|
+
for (const toast of toasts)
|
|
121
|
+
toast.style.blockSize = "auto";
|
|
122
|
+
return {
|
|
123
|
+
[Symbol.dispose]() {
|
|
124
|
+
for (const toast of toasts)
|
|
125
|
+
toast.style.removeProperty("block-size");
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
// --- Stack math (pure) ---
|
|
130
|
+
/**
|
|
131
|
+
* @description Computes the collapsed-stack placement for every toast from the
|
|
132
|
+
* measured heights alone (oldest first, matching DOM order). Pure: the
|
|
133
|
+
* measure step feeds it and an effect writes the results to the DOM, so this
|
|
134
|
+
* is the unit-testable core of the stacking model.
|
|
135
|
+
*
|
|
136
|
+
* @param heights - Natural toast heights in px, oldest first.
|
|
137
|
+
* @returns Per-toast layout (same order) and the front toast's height.
|
|
138
|
+
*/
|
|
139
|
+
function computeStackLayout(heights) {
|
|
140
|
+
const count = heights.length;
|
|
141
|
+
const toasts = Array.from({ length: count });
|
|
142
|
+
let heightsBefore = 0;
|
|
143
|
+
for (let i = count - 1; i >= 0; i--) {
|
|
144
|
+
const stackIndex = count - 1 - i; // 0 = front (newest, last in DOM)
|
|
145
|
+
toasts[i] = {
|
|
146
|
+
stackIndex,
|
|
147
|
+
offsetPx: heightsBefore,
|
|
148
|
+
zIndex: count - stackIndex,
|
|
149
|
+
front: stackIndex === 0,
|
|
150
|
+
visible: stackIndex < VISIBLE_TOASTS,
|
|
151
|
+
};
|
|
152
|
+
heightsBefore += heights[i];
|
|
153
|
+
}
|
|
154
|
+
return { toasts, frontToastHeightPx: count > 0 ? heights[count - 1] : null };
|
|
155
|
+
}
|
|
156
|
+
// --- <ui-toaster> element ---
|
|
157
|
+
/**
|
|
158
|
+
* @class
|
|
159
|
+
* @description Hosts the toast stack: builds toast markup, maintains the
|
|
160
|
+
* collapsed-stack CSS custom properties, runs auto-dismiss timers, and shows or
|
|
161
|
+
* hides the `popover="manual"` region as toasts come and go.
|
|
162
|
+
*/
|
|
163
|
+
class UiToaster extends ZazzElement {
|
|
164
|
+
#timers = new Map();
|
|
165
|
+
#counter = 0;
|
|
166
|
+
#resizeFrame = 0;
|
|
167
|
+
#handledCommand = null;
|
|
168
|
+
#collapseTimer = 0;
|
|
169
|
+
// Signal state: DOM events and observers write in; computed holds the pure
|
|
170
|
+
// derivations; effects (bound in connectedCallback) write back to the DOM.
|
|
171
|
+
/** Whether the stack is expanded (hover/focus). */
|
|
172
|
+
#expanded = state(false);
|
|
173
|
+
/** Whether the tab is hidden (mirrors `document.hidden`). */
|
|
174
|
+
#hidden = state(typeof document !== "undefined" ? document.hidden : false);
|
|
175
|
+
/** Timers run only while neither expanded nor hidden. */
|
|
176
|
+
#paused = computed(() => this.#expanded.get() || this.#hidden.get());
|
|
177
|
+
/** Measured stack (oldest first): written by `#reindex`'s measure pass. */
|
|
178
|
+
#stack = state([]);
|
|
179
|
+
/** Pure placement derived from the measured heights. */
|
|
180
|
+
#layout = computed(() => computeStackLayout(this.#stack.get().map((entry) => entry.height)));
|
|
181
|
+
setup(signal) {
|
|
182
|
+
// Region baseline: manual popover (no light dismiss), reachable landmark.
|
|
183
|
+
if (!this.hasAttribute("popover"))
|
|
184
|
+
this.setAttribute("popover", "manual");
|
|
185
|
+
if (!this.hasAttribute("role"))
|
|
186
|
+
this.setAttribute("role", "region");
|
|
187
|
+
if (!this.hasAttribute("aria-label"))
|
|
188
|
+
this.setAttribute("aria-label", "Notifications");
|
|
189
|
+
if (!this.hasAttribute("tabindex"))
|
|
190
|
+
this.setAttribute("tabindex", "-1");
|
|
191
|
+
// The live region must exist before toasts are inserted so additions announce.
|
|
192
|
+
if (!this.querySelector('[data-slot~="toaster-list"]')) {
|
|
193
|
+
const list = document.createElement("ol");
|
|
194
|
+
list.setAttribute("data-slot", "toaster-list");
|
|
195
|
+
list.setAttribute("aria-live", "polite");
|
|
196
|
+
list.setAttribute("aria-relevant", "additions text");
|
|
197
|
+
list.setAttribute("aria-atomic", "false");
|
|
198
|
+
this.append(list);
|
|
199
|
+
}
|
|
200
|
+
this.addEventListener("command", (event) => this.#onCommand(event), { signal });
|
|
201
|
+
// Hover/focus expands the stack; expanded (or hidden tab) pauses timers.
|
|
202
|
+
// Leave events only *request* a collapse: dismissal animations and node
|
|
203
|
+
// removal fire spurious mouseleave/focusout while the pointer never moved,
|
|
204
|
+
// so the collapse is verified against real hover/focus state first.
|
|
205
|
+
this.addEventListener("mouseenter", () => this.#setExpanded(true), { signal });
|
|
206
|
+
this.addEventListener("mouseleave", () => this.#requestCollapse(), { signal });
|
|
207
|
+
this.addEventListener("focusin", () => this.#setExpanded(true), { signal });
|
|
208
|
+
this.addEventListener("focusout", () => this.#requestCollapse(), { signal });
|
|
209
|
+
document.addEventListener("visibilitychange", () => this.#hidden.set(document.hidden), {
|
|
210
|
+
signal,
|
|
211
|
+
});
|
|
212
|
+
// Viewport resizes rewrap toast text: remeasure the stack.
|
|
213
|
+
window.addEventListener("resize", () => {
|
|
214
|
+
cancelAnimationFrame(this.#resizeFrame);
|
|
215
|
+
this.#resizeFrame = requestAnimationFrame(() => this.#reindex());
|
|
216
|
+
}, { signal });
|
|
217
|
+
// Output effects (disposed by the same controller as the listeners):
|
|
218
|
+
// one owns the expanded attribute, one owns pausing/resuming the timers,
|
|
219
|
+
// one writes the computed stack placement to the DOM.
|
|
220
|
+
effect(() => {
|
|
221
|
+
this.dataset.expanded = String(this.#expanded.get());
|
|
222
|
+
}, { signal });
|
|
223
|
+
effect(() => {
|
|
224
|
+
if (this.#paused.get()) {
|
|
225
|
+
this.#pauseTimers();
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
this.#resumeTimers();
|
|
229
|
+
}
|
|
230
|
+
}, { signal });
|
|
231
|
+
effect(() => {
|
|
232
|
+
const entries = this.#stack.get();
|
|
233
|
+
const { toasts, frontToastHeightPx } = this.#layout.get();
|
|
234
|
+
for (let i = 0; i < entries.length; i++) {
|
|
235
|
+
const { node, height } = entries[i];
|
|
236
|
+
const layout = toasts[i];
|
|
237
|
+
node.dataset.front = String(layout.front);
|
|
238
|
+
node.dataset.visible = String(layout.visible);
|
|
239
|
+
node.style.zIndex = String(layout.zIndex);
|
|
240
|
+
node.style.setProperty("--toasts-before", String(layout.stackIndex));
|
|
241
|
+
node.style.setProperty("--initial-height", `${height}px`);
|
|
242
|
+
node.style.setProperty("--offset", `calc(${layout.offsetPx}px + var(--ui-toaster-gap) * ${layout.stackIndex})`);
|
|
243
|
+
}
|
|
244
|
+
if (frontToastHeightPx !== null) {
|
|
245
|
+
this.style.setProperty("--front-toast-height", `${frontToastHeightPx}px`);
|
|
246
|
+
}
|
|
247
|
+
}, { signal });
|
|
248
|
+
}
|
|
249
|
+
teardown() {
|
|
250
|
+
cancelAnimationFrame(this.#resizeFrame);
|
|
251
|
+
window.clearTimeout(this.#collapseTimer);
|
|
252
|
+
for (const timer of this.#timers.values())
|
|
253
|
+
clearTimeout(timer.timeoutId);
|
|
254
|
+
this.#timers.clear();
|
|
255
|
+
}
|
|
256
|
+
// --- Public API ---
|
|
257
|
+
/**
|
|
258
|
+
* @description Adds a toast to this region and shows the region if needed.
|
|
259
|
+
*
|
|
260
|
+
* @param options - Toast content and behavior.
|
|
261
|
+
* @returns The toast id (usable with `dismiss()`).
|
|
262
|
+
*/
|
|
263
|
+
addToast(options = {}) {
|
|
264
|
+
const id = `toast-${++this.#counter}`;
|
|
265
|
+
const list = this.#list();
|
|
266
|
+
// Show the popover before inserting so the insertion is announced and the
|
|
267
|
+
// toast's @starting-style enter transition runs inside an open region.
|
|
268
|
+
this.#showRegion();
|
|
269
|
+
list.append(this.#buildToast(options, id));
|
|
270
|
+
this.#reindex();
|
|
271
|
+
this.#startTimer(id, options.duration ?? TOAST_LIFETIME);
|
|
272
|
+
return id;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* @description Dismisses one toast by id, or every toast when omitted.
|
|
276
|
+
*
|
|
277
|
+
* @param id - Toast id returned by `addToast()`.
|
|
278
|
+
*/
|
|
279
|
+
dismiss(id) {
|
|
280
|
+
if (id === undefined) {
|
|
281
|
+
this.dismissAll();
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const toast = this.#list().querySelector(`[data-toast-id="${CSS.escape(id)}"]`);
|
|
285
|
+
if (!(toast instanceof HTMLElement) || toast.dataset.removed === "true")
|
|
286
|
+
return;
|
|
287
|
+
const timer = this.#timers.get(id);
|
|
288
|
+
if (timer)
|
|
289
|
+
clearTimeout(timer.timeoutId);
|
|
290
|
+
this.#timers.delete(id);
|
|
291
|
+
toast.dataset.removed = "true";
|
|
292
|
+
// Keyboard users keep their place: hand focus to the next toast before this
|
|
293
|
+
// one goes. A mouse click's incidental focus is left to drop to <body>;
|
|
294
|
+
// holding it would pin the stack expanded after the pointer leaves, and
|
|
295
|
+
// #requestCollapse's :hover check already keeps the stack open meanwhile.
|
|
296
|
+
const active = document.activeElement;
|
|
297
|
+
if (active instanceof HTMLElement &&
|
|
298
|
+
toast.contains(active) &&
|
|
299
|
+
active.matches(":focus-visible")) {
|
|
300
|
+
this.#toasts()
|
|
301
|
+
.filter((t) => t.dataset.removed !== "true")
|
|
302
|
+
.at(-1)
|
|
303
|
+
?.focus({ preventScroll: true });
|
|
304
|
+
}
|
|
305
|
+
this.#reindex();
|
|
306
|
+
this.#finalizeRemoval(toast);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* @description Dismisses every toast in this region.
|
|
310
|
+
*/
|
|
311
|
+
dismissAll() {
|
|
312
|
+
for (const toast of this.#toasts()) {
|
|
313
|
+
const toastId = toast.dataset.toastId;
|
|
314
|
+
if (toastId)
|
|
315
|
+
this.dismiss(toastId);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
// --- Invoker Commands ---
|
|
319
|
+
/**
|
|
320
|
+
* @description Handles the custom `--toast` Invoker Command fired by buttons
|
|
321
|
+
* with `commandfor` pointing at this region. `--toast-success` (etc.) sets the
|
|
322
|
+
* variant; everything else comes from the invoker's `data-*` attributes.
|
|
323
|
+
*
|
|
324
|
+
* @param event - The command event.
|
|
325
|
+
*/
|
|
326
|
+
#onCommand(event) {
|
|
327
|
+
const command = event.command;
|
|
328
|
+
if (typeof command !== "string" || !command.startsWith("--toast"))
|
|
329
|
+
return;
|
|
330
|
+
// The invokers polyfill re-dispatches commands even in browsers with a
|
|
331
|
+
// native CommandEvent, so one click can deliver the same command twice in
|
|
332
|
+
// the same task. Handle the first and drop the same-task duplicate.
|
|
333
|
+
const handled = this.#handledCommand;
|
|
334
|
+
if (handled && handled.source === event.source && handled.command === command)
|
|
335
|
+
return;
|
|
336
|
+
this.#handledCommand = { source: event.source, command };
|
|
337
|
+
window.setTimeout(() => {
|
|
338
|
+
this.#handledCommand = null;
|
|
339
|
+
}, 0);
|
|
340
|
+
const source = event.source;
|
|
341
|
+
const options = {};
|
|
342
|
+
const suffix = command.slice("--toast-".length);
|
|
343
|
+
if (VARIANTS.includes(suffix)) {
|
|
344
|
+
options.variant = suffix;
|
|
345
|
+
}
|
|
346
|
+
if (source instanceof HTMLElement) {
|
|
347
|
+
const { title, description, variant, duration, closeButton } = source.dataset;
|
|
348
|
+
if (title)
|
|
349
|
+
options.title = title;
|
|
350
|
+
if (description)
|
|
351
|
+
options.description = description;
|
|
352
|
+
if (!options.variant && variant && VARIANTS.includes(variant)) {
|
|
353
|
+
options.variant = variant;
|
|
354
|
+
}
|
|
355
|
+
if (duration !== undefined) {
|
|
356
|
+
// A duration is only ever a number: parse it as one rather than running
|
|
357
|
+
// it through the polymorphic attribute parser and narrowing after.
|
|
358
|
+
// NaN-guarded rather than finite-guarded: `Infinity` is a documented
|
|
359
|
+
// value (persist until dismissed). Blank strings coerce to 0, so they
|
|
360
|
+
// are rejected before Number() sees them.
|
|
361
|
+
const parsed = duration.trim() === "" ? Number.NaN : Number(duration);
|
|
362
|
+
if (!Number.isNaN(parsed))
|
|
363
|
+
options.duration = parsed;
|
|
364
|
+
}
|
|
365
|
+
if (closeButton !== undefined)
|
|
366
|
+
options.closeButton = closeButton !== "false";
|
|
367
|
+
}
|
|
368
|
+
this.addToast(options);
|
|
369
|
+
}
|
|
370
|
+
// --- Toast construction ---
|
|
371
|
+
/**
|
|
372
|
+
* @description Builds a toast `<li>`: status icon, title/description, and the
|
|
373
|
+
* optional action and close buttons. Text is set via `textContent`.
|
|
374
|
+
*
|
|
375
|
+
* @param options - Toast content and behavior.
|
|
376
|
+
* @param id - The generated toast id.
|
|
377
|
+
* @returns The toast element (not yet inserted).
|
|
378
|
+
*/
|
|
379
|
+
#buildToast(options, id) {
|
|
380
|
+
const toast = document.createElement("li");
|
|
381
|
+
toast.setAttribute("data-slot", "toaster-toast");
|
|
382
|
+
toast.dataset.toastId = id;
|
|
383
|
+
toast.tabIndex = 0;
|
|
384
|
+
if (options.variant)
|
|
385
|
+
toast.dataset.variant = options.variant;
|
|
386
|
+
if (options.variant) {
|
|
387
|
+
const icon = document.createElement("span");
|
|
388
|
+
icon.setAttribute("data-slot", "toaster-icon");
|
|
389
|
+
icon.setAttribute("aria-hidden", "true");
|
|
390
|
+
icon.innerHTML = ICONS[options.variant];
|
|
391
|
+
toast.append(icon);
|
|
392
|
+
}
|
|
393
|
+
const content = document.createElement("div");
|
|
394
|
+
content.setAttribute("data-slot", "toaster-content");
|
|
395
|
+
if (options.title) {
|
|
396
|
+
const title = document.createElement("div");
|
|
397
|
+
title.setAttribute("data-slot", "toaster-title");
|
|
398
|
+
title.textContent = options.title;
|
|
399
|
+
content.append(title);
|
|
400
|
+
}
|
|
401
|
+
if (options.description) {
|
|
402
|
+
const description = document.createElement("div");
|
|
403
|
+
description.setAttribute("data-slot", "toaster-description");
|
|
404
|
+
description.textContent = options.description;
|
|
405
|
+
content.append(description);
|
|
406
|
+
}
|
|
407
|
+
toast.append(content);
|
|
408
|
+
const action = options.action;
|
|
409
|
+
if (action) {
|
|
410
|
+
const button = document.createElement("button");
|
|
411
|
+
button.type = "button";
|
|
412
|
+
button.className = "ui-button";
|
|
413
|
+
button.setAttribute("data-slot", "toaster-action");
|
|
414
|
+
button.dataset.size = "sm";
|
|
415
|
+
button.textContent = action.label;
|
|
416
|
+
button.addEventListener("click", (event) => {
|
|
417
|
+
action.onClick?.(event);
|
|
418
|
+
if (!event.defaultPrevented)
|
|
419
|
+
this.dismiss(id);
|
|
420
|
+
});
|
|
421
|
+
toast.append(button);
|
|
422
|
+
}
|
|
423
|
+
if (options.closeButton !== false) {
|
|
424
|
+
const close = document.createElement("button");
|
|
425
|
+
close.type = "button";
|
|
426
|
+
close.className = "ui-button";
|
|
427
|
+
close.setAttribute("data-slot", "toaster-close");
|
|
428
|
+
close.dataset.variant = "ghost";
|
|
429
|
+
close.dataset.size = "icon-sm";
|
|
430
|
+
close.setAttribute("aria-label", "Close notification");
|
|
431
|
+
close.innerHTML = ICONS.close;
|
|
432
|
+
close.addEventListener("click", () => this.dismiss(id));
|
|
433
|
+
toast.append(close);
|
|
434
|
+
}
|
|
435
|
+
return toast;
|
|
436
|
+
}
|
|
437
|
+
// --- Stack math ---
|
|
438
|
+
/**
|
|
439
|
+
* @description Remeasures the stack: the measure half of the stacking
|
|
440
|
+
* model. Batch-reads every live toast's natural height and writes the
|
|
441
|
+
* result into `#stack`; `#layout` derives the placement purely
|
|
442
|
+
* (`computeStackLayout`) and the stack effect writes the CSS custom
|
|
443
|
+
* properties `_toaster.css` reads (`--toasts-before`, `--offset`,
|
|
444
|
+
* `--initial-height`, `--front-toast-height`) plus `data-front`,
|
|
445
|
+
* `data-visible`, and z-index. DOM order is chronological; the last child
|
|
446
|
+
* is the front (newest) toast.
|
|
447
|
+
*/
|
|
448
|
+
#reindex() {
|
|
449
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
450
|
+
try {
|
|
451
|
+
const toasts = this.#toasts().filter((toast) => toast.dataset.removed !== "true");
|
|
452
|
+
// Batch-measure with heights unclamped, restored at scope exit: one
|
|
453
|
+
// layout pass, no visible change (the stack effect is microtask-batched,
|
|
454
|
+
// so its DOM writes land after the restore).
|
|
455
|
+
const _measure = __addDisposableResource(env_1, unclampForMeasure(toasts), false);
|
|
456
|
+
const entries = toasts.map((toast) => ({ node: toast, height: toast.offsetHeight }));
|
|
457
|
+
this.#stack.set(entries);
|
|
458
|
+
}
|
|
459
|
+
catch (e_1) {
|
|
460
|
+
env_1.error = e_1;
|
|
461
|
+
env_1.hasError = true;
|
|
462
|
+
}
|
|
463
|
+
finally {
|
|
464
|
+
__disposeResources(env_1);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* @returns The toast list (created in `connectedCallback`).
|
|
469
|
+
*/
|
|
470
|
+
#list() {
|
|
471
|
+
let list = this.querySelector('[data-slot~="toaster-list"]');
|
|
472
|
+
if (!(list instanceof HTMLOListElement)) {
|
|
473
|
+
list = document.createElement("ol");
|
|
474
|
+
list.setAttribute("data-slot", "toaster-list");
|
|
475
|
+
this.append(list);
|
|
476
|
+
}
|
|
477
|
+
return list;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* @returns All toast elements, oldest first (DOM order).
|
|
481
|
+
*/
|
|
482
|
+
#toasts() {
|
|
483
|
+
return Array.from(this.#list().children)
|
|
484
|
+
.filter((child) => child instanceof HTMLElement)
|
|
485
|
+
.filter((child) => child.matches('[data-slot~="toaster-toast"]'));
|
|
486
|
+
}
|
|
487
|
+
// --- Expand / collapse ---
|
|
488
|
+
/**
|
|
489
|
+
* @description Expands or collapses the stack by writing the signal state.
|
|
490
|
+
* The attribute write and the timer pause/resume (matching Sonner) are owned
|
|
491
|
+
* by the effects in `connectedCallback`: no caller has to remember them.
|
|
492
|
+
*
|
|
493
|
+
* @param expanded - Whether the stack is expanded.
|
|
494
|
+
*/
|
|
495
|
+
#setExpanded(expanded) {
|
|
496
|
+
if (expanded)
|
|
497
|
+
window.clearTimeout(this.#collapseTimer);
|
|
498
|
+
this.#expanded.set(expanded);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* @description Collapses the stack only if the user has really left it.
|
|
502
|
+
* Waits a beat, then checks actual hover and focus state: dismissals fire
|
|
503
|
+
* spurious mouseleave/focusout (exit transforms, node removal, focus drops)
|
|
504
|
+
* that a raw event handler would mistake for the pointer leaving.
|
|
505
|
+
*/
|
|
506
|
+
#requestCollapse() {
|
|
507
|
+
window.clearTimeout(this.#collapseTimer);
|
|
508
|
+
this.#collapseTimer = window.setTimeout(() => {
|
|
509
|
+
const activeElement = document.activeElement;
|
|
510
|
+
const focusWithin = activeElement instanceof Node && this.contains(activeElement);
|
|
511
|
+
if (!this.matches(":hover") && !focusWithin)
|
|
512
|
+
this.#setExpanded(false);
|
|
513
|
+
}, 100);
|
|
514
|
+
}
|
|
515
|
+
// --- Timers ---
|
|
516
|
+
/**
|
|
517
|
+
* @description Registers a toast's auto-dismiss timer and schedules it unless
|
|
518
|
+
* the stack is currently paused (expanded or hidden tab).
|
|
519
|
+
*
|
|
520
|
+
* @param id - Toast id.
|
|
521
|
+
* @param duration - Lifetime in ms; `Infinity` skips the timer.
|
|
522
|
+
*/
|
|
523
|
+
#startTimer(id, duration) {
|
|
524
|
+
if (duration === Infinity || Number.isNaN(duration))
|
|
525
|
+
return;
|
|
526
|
+
const timer = { remaining: duration, startedAt: 0, timeoutId: 0 };
|
|
527
|
+
this.#timers.set(id, timer);
|
|
528
|
+
if (!this.#paused.get())
|
|
529
|
+
this.#schedule(id, timer);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* @description Arms a timer's timeout for its remaining lifetime.
|
|
533
|
+
*
|
|
534
|
+
* @param id - Toast id.
|
|
535
|
+
* @param timer - The timer record.
|
|
536
|
+
*/
|
|
537
|
+
#schedule(id, timer) {
|
|
538
|
+
timer.startedAt = Date.now();
|
|
539
|
+
timer.timeoutId = window.setTimeout(() => this.dismiss(id), timer.remaining);
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* @description Pauses all running timers, banking each one's remaining time.
|
|
543
|
+
*/
|
|
544
|
+
#pauseTimers() {
|
|
545
|
+
for (const timer of this.#timers.values()) {
|
|
546
|
+
if (!timer.timeoutId)
|
|
547
|
+
continue;
|
|
548
|
+
clearTimeout(timer.timeoutId);
|
|
549
|
+
timer.timeoutId = 0;
|
|
550
|
+
timer.remaining = Math.max(0, timer.remaining - (Date.now() - timer.startedAt));
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* @description Resumes paused timers (no-op while still expanded or hidden).
|
|
555
|
+
*/
|
|
556
|
+
#resumeTimers() {
|
|
557
|
+
if (this.#paused.get())
|
|
558
|
+
return;
|
|
559
|
+
for (const [id, timer] of this.#timers) {
|
|
560
|
+
if (!timer.timeoutId)
|
|
561
|
+
this.#schedule(id, timer);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
// --- Region show/hide ---
|
|
565
|
+
/**
|
|
566
|
+
* @description Puts the region on the top layer if it isn't already.
|
|
567
|
+
*/
|
|
568
|
+
#showRegion() {
|
|
569
|
+
try {
|
|
570
|
+
if (!this.matches(":popover-open, .\\:popover-open"))
|
|
571
|
+
this.showPopover();
|
|
572
|
+
}
|
|
573
|
+
catch {
|
|
574
|
+
// Older engines without the Popover API: the region stays a fixed-position
|
|
575
|
+
// element, which still renders (just not on the top layer).
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* @description Removes the region from the top layer.
|
|
580
|
+
*/
|
|
581
|
+
#hideRegion() {
|
|
582
|
+
try {
|
|
583
|
+
if (this.matches(":popover-open, .\\:popover-open"))
|
|
584
|
+
this.hidePopover();
|
|
585
|
+
}
|
|
586
|
+
catch {
|
|
587
|
+
// See #showRegion.
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* @description Removes a dismissed toast after its exit transition (with a
|
|
592
|
+
* timeout safety net), then hides the region once the stack is empty.
|
|
593
|
+
*
|
|
594
|
+
* @param toast - The toast marked `data-removed="true"`.
|
|
595
|
+
*/
|
|
596
|
+
#finalizeRemoval(toast) {
|
|
597
|
+
let done = false;
|
|
598
|
+
const remove = () => {
|
|
599
|
+
if (done)
|
|
600
|
+
return;
|
|
601
|
+
done = true;
|
|
602
|
+
toast.remove();
|
|
603
|
+
if (this.#toasts().length === 0) {
|
|
604
|
+
this.#setExpanded(false);
|
|
605
|
+
this.#hideRegion();
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
toast.addEventListener("transitionend", (event) => {
|
|
609
|
+
if (event.target === toast)
|
|
610
|
+
remove();
|
|
611
|
+
});
|
|
612
|
+
// Safety net for when no transitionend fires (reduced motion → duration 0).
|
|
613
|
+
// Scaled to the computed duration so retuned --ui-toaster-transition-duration
|
|
614
|
+
// themes aren't yanked out mid-exit.
|
|
615
|
+
const duration = getComputedStyle(toast)
|
|
616
|
+
.transitionDuration.split(",")
|
|
617
|
+
.reduce((max, value) => Math.max(max, Number.parseFloat(value) || 0), 0);
|
|
618
|
+
window.setTimeout(remove, Math.max(EXIT_FALLBACK_MS, duration * 1000 + 100));
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
// --- Imperative API ---
|
|
622
|
+
/**
|
|
623
|
+
* @description Resolves a target region from an id, element, or the document.
|
|
624
|
+
*
|
|
625
|
+
* @param region - Region id or element.
|
|
626
|
+
* @returns The region, or `null` when none exists.
|
|
627
|
+
* @private
|
|
628
|
+
*/
|
|
629
|
+
function resolveRegion(region) {
|
|
630
|
+
const node = region instanceof Element
|
|
631
|
+
? region
|
|
632
|
+
: typeof region === "string"
|
|
633
|
+
? document.getElementById(region)
|
|
634
|
+
: document.querySelector("ui-toaster");
|
|
635
|
+
if (node instanceof UiToaster)
|
|
636
|
+
return node;
|
|
637
|
+
console.warn('Toaster: no <ui-toaster> found. Add `<ui-toaster class="ui-toaster" popover="manual"></ui-toaster>` to the page.');
|
|
638
|
+
return null;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* @description Normalizes the `toast("message")` string shorthand.
|
|
642
|
+
*
|
|
643
|
+
* @param options - Options object or title string.
|
|
644
|
+
* @returns The options object.
|
|
645
|
+
* @private
|
|
646
|
+
*/
|
|
647
|
+
function toOptions(options) {
|
|
648
|
+
if (typeof options === "string")
|
|
649
|
+
return { title: options };
|
|
650
|
+
return options ?? {};
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* @namespace Toaster
|
|
654
|
+
* @description Imperative toast API. Requires a `<ui-toaster>` in the page:
|
|
655
|
+
* the region is never auto-created (HTML-first, like every Zazz component).
|
|
656
|
+
*
|
|
657
|
+
* @property toast - Shows a toast; returns its id.
|
|
658
|
+
* @property success - Success shorthand.
|
|
659
|
+
* @property info - Info shorthand.
|
|
660
|
+
* @property warning - Warning shorthand.
|
|
661
|
+
* @property error - Destructive shorthand.
|
|
662
|
+
* @property dismiss - Dismisses one toast, or all when omitted.
|
|
663
|
+
*/
|
|
664
|
+
const Toaster = {
|
|
665
|
+
/**
|
|
666
|
+
* @description Shows a toast in the target (or first) region.
|
|
667
|
+
*
|
|
668
|
+
* @param options - Options object or title string.
|
|
669
|
+
* @returns The toast id, or `null` when no region exists.
|
|
670
|
+
*/
|
|
671
|
+
toast(options) {
|
|
672
|
+
const resolved = toOptions(options);
|
|
673
|
+
const region = resolveRegion(resolved.region);
|
|
674
|
+
return region ? region.addToast(resolved) : null;
|
|
675
|
+
},
|
|
676
|
+
/**
|
|
677
|
+
* @description Shows a success toast.
|
|
678
|
+
*
|
|
679
|
+
* @param message - Toast title.
|
|
680
|
+
* @param options - Additional options.
|
|
681
|
+
* @returns The toast id, or `null` when no region exists.
|
|
682
|
+
*/
|
|
683
|
+
success(message, options) {
|
|
684
|
+
return Toaster.toast({ ...options, title: message, variant: "success" });
|
|
685
|
+
},
|
|
686
|
+
/**
|
|
687
|
+
* @description Shows an info toast.
|
|
688
|
+
*
|
|
689
|
+
* @param message - Toast title.
|
|
690
|
+
* @param options - Additional options.
|
|
691
|
+
* @returns The toast id, or `null` when no region exists.
|
|
692
|
+
*/
|
|
693
|
+
info(message, options) {
|
|
694
|
+
return Toaster.toast({ ...options, title: message, variant: "info" });
|
|
695
|
+
},
|
|
696
|
+
/**
|
|
697
|
+
* @description Shows a warning toast.
|
|
698
|
+
*
|
|
699
|
+
* @param message - Toast title.
|
|
700
|
+
* @param options - Additional options.
|
|
701
|
+
* @returns The toast id, or `null` when no region exists.
|
|
702
|
+
*/
|
|
703
|
+
warning(message, options) {
|
|
704
|
+
return Toaster.toast({ ...options, title: message, variant: "warning" });
|
|
705
|
+
},
|
|
706
|
+
/**
|
|
707
|
+
* @description Shows a destructive/error toast.
|
|
708
|
+
*
|
|
709
|
+
* @param message - Toast title.
|
|
710
|
+
* @param options - Additional options.
|
|
711
|
+
* @returns The toast id, or `null` when no region exists.
|
|
712
|
+
*/
|
|
713
|
+
error(message, options) {
|
|
714
|
+
return Toaster.toast({ ...options, title: message, variant: "destructive" });
|
|
715
|
+
},
|
|
716
|
+
/**
|
|
717
|
+
* @description Dismisses a toast by id in any region, or every toast everywhere.
|
|
718
|
+
*
|
|
719
|
+
* @param id - Toast id returned by `toast()`.
|
|
720
|
+
*/
|
|
721
|
+
dismiss(id) {
|
|
722
|
+
for (const region of document.querySelectorAll("ui-toaster")) {
|
|
723
|
+
if (region instanceof UiToaster)
|
|
724
|
+
region.dismiss(id);
|
|
725
|
+
}
|
|
726
|
+
},
|
|
727
|
+
};
|
|
728
|
+
defineZazzElement("ui-toaster", UiToaster);
|
|
729
|
+
// Attach the imperative toast API to window (the documented public surface app
|
|
730
|
+
// authors call), then export for module consumers.
|
|
731
|
+
if (typeof window !== "undefined") {
|
|
732
|
+
window.Toaster = Toaster;
|
|
733
|
+
}
|
|
734
|
+
// computeStackLayout is exported for unit tests only; not part of the
|
|
735
|
+
// documented public API (window.Toaster is the surface app authors use).
|
|
736
|
+
export { Toaster, UiToaster, computeStackLayout };
|