@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,379 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { registerRefresh } from "./zazz-element.ts";
|
|
4
|
+
|
|
5
|
+
interface RevealConfig {
|
|
6
|
+
/** Margin around the root (viewport) for IntersectionObserver. */
|
|
7
|
+
margin: string;
|
|
8
|
+
/** Visibility threshold (0–1) to trigger animations. */
|
|
9
|
+
threshold: number;
|
|
10
|
+
/** Default animation duration (ms number or CSS time string). */
|
|
11
|
+
duration: number | string;
|
|
12
|
+
/** Default animation timing function. */
|
|
13
|
+
ease: string;
|
|
14
|
+
/** Default base animation delay in milliseconds. */
|
|
15
|
+
wait: number;
|
|
16
|
+
/** Default translation distance for slide animations. */
|
|
17
|
+
distance: string;
|
|
18
|
+
/** Default delay between staggered elements in milliseconds. */
|
|
19
|
+
step: number;
|
|
20
|
+
/** Scale factor for grow animations (< 1). */
|
|
21
|
+
grow: number;
|
|
22
|
+
/** Scale factor for shrink animations (> 1). */
|
|
23
|
+
shrink: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface RevealOptions {
|
|
27
|
+
/** Configuration options for the animation system. */
|
|
28
|
+
config?: Partial<RevealConfig>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @fileoverview Scroll-based reveal animations.
|
|
33
|
+
* @description A lightweight, configurable animation system for scroll-based
|
|
34
|
+
* reveals and staggered animations, focused on viewport entry.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* <!-- Single element -->
|
|
38
|
+
* <div data-reveal="slide-up" data-reveal-duration="300">Content</div>
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* <!-- Stagger group (direct children are animated) -->
|
|
42
|
+
* <div data-reveal-each="fade" data-reveal-step="100">
|
|
43
|
+
* <div>Item 1</div>
|
|
44
|
+
* <div>Item 2</div>
|
|
45
|
+
* </div>
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @description Reads a CSS custom property from `:root` computed styles.
|
|
50
|
+
* @param name - Custom property name (e.g. "--ui-reveal-global-duration").
|
|
51
|
+
* @returns Trimmed value, or "" when unset.
|
|
52
|
+
*/
|
|
53
|
+
function getRootCssVar(name: string): string {
|
|
54
|
+
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @description Whether the engine computes stagger delays natively: reveal.css
|
|
59
|
+
* derives each child's `--ui-reveal-wait` from `sibling-index()`. Where
|
|
60
|
+
* unsupported (Firefox), `#configureStaggerGroup` falls back to writing a
|
|
61
|
+
* per-child `--ui-reveal-wait` inline.
|
|
62
|
+
*/
|
|
63
|
+
const supportsSiblingIndex =
|
|
64
|
+
typeof CSS !== "undefined" && CSS.supports("top", "calc(sibling-index() * 1px)");
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @description Parses a CSS time value into milliseconds.
|
|
68
|
+
* @param value - CSS time string (e.g. "300ms", "0.5s") or bare number string.
|
|
69
|
+
* @returns Duration in milliseconds.
|
|
70
|
+
*/
|
|
71
|
+
function parseCssTimeMs(value: string): number {
|
|
72
|
+
const str = value.trim();
|
|
73
|
+
if (!str) return 0;
|
|
74
|
+
if (/^-?\d*\.?\d+ms$/.test(str)) return parseFloat(str);
|
|
75
|
+
if (/^-?\d*\.?\d+s$/.test(str)) return parseFloat(str) * 1000;
|
|
76
|
+
return parseInt(str, 10) || 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @class
|
|
81
|
+
* @description Initializes viewport entry animations.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const reveal = new Reveal();
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const reveal = new Reveal({
|
|
88
|
+
* config: {
|
|
89
|
+
* duration: 400,
|
|
90
|
+
* ease: "ease-in-out",
|
|
91
|
+
* threshold: 0.3,
|
|
92
|
+
* margin: "100px",
|
|
93
|
+
* step: 40,
|
|
94
|
+
* },
|
|
95
|
+
* });
|
|
96
|
+
*/
|
|
97
|
+
class Reveal {
|
|
98
|
+
/**
|
|
99
|
+
* @description Prevents automatic initialization on DOM ready.
|
|
100
|
+
* Assigned below (browser environments only).
|
|
101
|
+
*/
|
|
102
|
+
declare static disableAutoInit: () => void;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @description Returns the auto-initialized Reveal instance, if any.
|
|
106
|
+
* Assigned below (browser environments only).
|
|
107
|
+
*/
|
|
108
|
+
declare static getAutoInstance: () => Reveal | null;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @description Default config from `--ui-reveal-global-*` tokens in `_reveal.css`.
|
|
112
|
+
*/
|
|
113
|
+
static #readDefaultConfig(): RevealConfig {
|
|
114
|
+
return {
|
|
115
|
+
margin: "0px",
|
|
116
|
+
threshold: 0.2,
|
|
117
|
+
duration: getRootCssVar("--ui-reveal-global-duration") || "400ms",
|
|
118
|
+
ease: getRootCssVar("--ui-reveal-global-ease") || "cubic-bezier(0.4, 0, 0.2, 1)",
|
|
119
|
+
wait: parseCssTimeMs(getRootCssVar("--ui-reveal-global-wait")),
|
|
120
|
+
distance: getRootCssVar("--ui-reveal-global-distance") || "1rem",
|
|
121
|
+
step: 80,
|
|
122
|
+
grow: parseFloat(getRootCssVar("--ui-reveal-global-grow")) || 0.97,
|
|
123
|
+
shrink: parseFloat(getRootCssVar("--ui-reveal-global-shrink")) || 1.03,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static get defaultConfig(): RevealConfig {
|
|
128
|
+
return Reveal.#readDefaultConfig();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#observers = new Map<string, IntersectionObserver>();
|
|
132
|
+
|
|
133
|
+
config: RevealConfig;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @description Creates a new Reveal instance.
|
|
137
|
+
*
|
|
138
|
+
* @param options - Configuration options.
|
|
139
|
+
*/
|
|
140
|
+
constructor(options: RevealOptions = {}) {
|
|
141
|
+
this.config = { ...Reveal.defaultConfig, ...options.config };
|
|
142
|
+
this.init();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @description Gets or creates an IntersectionObserver for a given set of options.
|
|
147
|
+
*
|
|
148
|
+
* @param options - Observer options.
|
|
149
|
+
* @returns The observer instance.
|
|
150
|
+
*/
|
|
151
|
+
#getObserver(options: IntersectionObserverInit): IntersectionObserver {
|
|
152
|
+
const optionsKey = JSON.stringify(options);
|
|
153
|
+
let observer = this.#observers.get(optionsKey);
|
|
154
|
+
if (!observer) {
|
|
155
|
+
observer = new IntersectionObserver((entries, obs) => {
|
|
156
|
+
entries.forEach((entry) => {
|
|
157
|
+
if (entry.isIntersecting) {
|
|
158
|
+
entry.target.classList.add("in-viewport");
|
|
159
|
+
obs.unobserve(entry.target);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}, options);
|
|
163
|
+
this.#observers.set(optionsKey, observer);
|
|
164
|
+
}
|
|
165
|
+
return observer;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @description Gets IntersectionObserver options for an element.
|
|
170
|
+
*
|
|
171
|
+
* @param element - The element to get options for.
|
|
172
|
+
* @returns Observer configuration options.
|
|
173
|
+
*/
|
|
174
|
+
#getElementObserverOptions(element: HTMLElement): IntersectionObserverInit {
|
|
175
|
+
const margin = element.dataset.revealMargin || this.config.margin;
|
|
176
|
+
const threshold = parseFloat(
|
|
177
|
+
element.dataset.revealThreshold || this.config.threshold.toString(),
|
|
178
|
+
);
|
|
179
|
+
return {
|
|
180
|
+
rootMargin: margin,
|
|
181
|
+
threshold: Math.min(Math.max(threshold, 0), 1),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @description Normalizes a duration to a CSS time value without duplicating units.
|
|
187
|
+
*
|
|
188
|
+
* @param value - Milliseconds as a number, or a CSS time string.
|
|
189
|
+
* @returns A CSS time value (e.g. "300ms", "0.333s").
|
|
190
|
+
*/
|
|
191
|
+
#formatTime(value: number | string): string {
|
|
192
|
+
const str = value.toString().trim();
|
|
193
|
+
if (/^-?\d*\.?\d+(ms|s)$/.test(str)) return str;
|
|
194
|
+
return `${Math.max(0, parseInt(str, 10) || 0)}ms`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @description Sets CSS custom properties on an element when a value is provided.
|
|
199
|
+
*
|
|
200
|
+
* @param element - The element to set properties on.
|
|
201
|
+
* @param properties - CSS custom properties to set.
|
|
202
|
+
*/
|
|
203
|
+
#setRevealProperties(
|
|
204
|
+
element: HTMLElement,
|
|
205
|
+
properties: Record<string, string | number | null | undefined>,
|
|
206
|
+
): void {
|
|
207
|
+
Object.entries(properties).forEach(([key, value]) => {
|
|
208
|
+
if (value !== null && value !== undefined) {
|
|
209
|
+
element.style.setProperty(key, value.toString());
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @description Configures stagger animation properties and observes child elements.
|
|
216
|
+
*
|
|
217
|
+
* The stagger delay (`base + step × position`) is computed natively where
|
|
218
|
+
* possible: the group carries `--ui-reveal-stagger-base`/`--ui-reveal-stagger-step`
|
|
219
|
+
* and each child derives its own `--ui-reveal-wait` via `sibling-index()` in
|
|
220
|
+
* reveal.css (reversed groups count from the end via `sibling-count()`).
|
|
221
|
+
* Where unsupported (Firefox), the same math runs here and lands as a
|
|
222
|
+
* per-child inline `--ui-reveal-wait`.
|
|
223
|
+
*
|
|
224
|
+
* @param groupElement - The parent stagger container.
|
|
225
|
+
*/
|
|
226
|
+
#configureStaggerGroup(groupElement: HTMLElement): void {
|
|
227
|
+
const dataset = groupElement.dataset;
|
|
228
|
+
const groupOptions = this.#getElementObserverOptions(groupElement);
|
|
229
|
+
const groupObserver = this.#getObserver(groupOptions);
|
|
230
|
+
|
|
231
|
+
const groupProps = {
|
|
232
|
+
step: Math.max(0, parseInt(dataset.revealStep || this.config.step.toString(), 10) || 0),
|
|
233
|
+
duration: dataset.revealDuration
|
|
234
|
+
? Math.max(0, parseInt(dataset.revealDuration, 10) || 0)
|
|
235
|
+
: this.config.duration,
|
|
236
|
+
ease: dataset.revealEase || this.config.ease,
|
|
237
|
+
baseWait: Math.max(0, parseInt(dataset.revealWait || this.config.wait.toString(), 10) || 0),
|
|
238
|
+
distance: dataset.revealDistance || this.config.distance,
|
|
239
|
+
order: dataset.revealOrder,
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const childrenArray = Array.from(groupElement.children);
|
|
243
|
+
const sequence = groupProps.order === "reversed" ? childrenArray.reverse() : childrenArray;
|
|
244
|
+
|
|
245
|
+
if (supportsSiblingIndex) {
|
|
246
|
+
this.#setRevealProperties(groupElement, {
|
|
247
|
+
"--ui-reveal-stagger-base": `${groupProps.baseWait}ms`,
|
|
248
|
+
"--ui-reveal-stagger-step": `${groupProps.step}ms`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
sequence.forEach((child, i) => {
|
|
253
|
+
if (!(child instanceof HTMLElement)) return;
|
|
254
|
+
|
|
255
|
+
this.#setRevealProperties(child, {
|
|
256
|
+
"--ui-reveal-duration": this.#formatTime(groupProps.duration),
|
|
257
|
+
"--ui-reveal-ease": groupProps.ease,
|
|
258
|
+
// Fallback only: natively each child computes this in reveal.css.
|
|
259
|
+
"--ui-reveal-wait": supportsSiblingIndex
|
|
260
|
+
? null
|
|
261
|
+
: `${groupProps.baseWait + groupProps.step * i}ms`,
|
|
262
|
+
"--ui-reveal-distance": groupProps.distance,
|
|
263
|
+
"--ui-reveal-scale": dataset.revealScale || null,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
groupObserver.observe(child);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* @description Configures animation properties for a single element and observes it.
|
|
272
|
+
*
|
|
273
|
+
* @param element - The element to configure and observe.
|
|
274
|
+
*/
|
|
275
|
+
#configureSingleElement(element: HTMLElement): void {
|
|
276
|
+
const dataset = element.dataset;
|
|
277
|
+
const elementOptions = this.#getElementObserverOptions(element);
|
|
278
|
+
const elementObserver = this.#getObserver(elementOptions);
|
|
279
|
+
|
|
280
|
+
const elementDuration = dataset.revealDuration
|
|
281
|
+
? Math.max(0, parseInt(dataset.revealDuration, 10) || 0)
|
|
282
|
+
: null;
|
|
283
|
+
const elementWait = dataset.revealWait
|
|
284
|
+
? Math.max(0, parseInt(dataset.revealWait, 10) || 0)
|
|
285
|
+
: null;
|
|
286
|
+
|
|
287
|
+
this.#setRevealProperties(element, {
|
|
288
|
+
"--ui-reveal-duration": elementDuration !== null ? this.#formatTime(elementDuration) : null,
|
|
289
|
+
"--ui-reveal-wait": elementWait !== null ? this.#formatTime(elementWait) : null,
|
|
290
|
+
"--ui-reveal-ease": dataset.revealEase || null,
|
|
291
|
+
"--ui-reveal-distance": dataset.revealDistance || null,
|
|
292
|
+
"--ui-reveal-scale": dataset.revealScale || null,
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
elementObserver.observe(element);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* @description Initializes the animation system by setting global CSS variables
|
|
300
|
+
* and configuring all animated elements.
|
|
301
|
+
*/
|
|
302
|
+
init(): void {
|
|
303
|
+
this.#observers.forEach((observer) => observer.disconnect());
|
|
304
|
+
this.#observers.clear();
|
|
305
|
+
|
|
306
|
+
const rootStyle = document.documentElement.style;
|
|
307
|
+
rootStyle.setProperty("--ui-reveal-global-duration", this.#formatTime(this.config.duration));
|
|
308
|
+
rootStyle.setProperty("--ui-reveal-global-ease", this.config.ease);
|
|
309
|
+
rootStyle.setProperty("--ui-reveal-global-wait", this.#formatTime(this.config.wait));
|
|
310
|
+
rootStyle.setProperty("--ui-reveal-global-distance", this.config.distance);
|
|
311
|
+
rootStyle.setProperty("--ui-reveal-global-grow", this.config.grow.toString());
|
|
312
|
+
rootStyle.setProperty("--ui-reveal-global-shrink", this.config.shrink.toString());
|
|
313
|
+
|
|
314
|
+
document.querySelectorAll("[data-reveal]").forEach((element) => {
|
|
315
|
+
if (element instanceof HTMLElement) this.#configureSingleElement(element);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
document.querySelectorAll("[data-reveal-each]").forEach((group) => {
|
|
319
|
+
if (group instanceof HTMLElement) this.#configureStaggerGroup(group);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* @description Reinitializes the animation system after dynamically adding elements.
|
|
325
|
+
*
|
|
326
|
+
* Disconnects old observers and rescans the document.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* container.innerHTML = newContent;
|
|
330
|
+
* reveal.refresh();
|
|
331
|
+
*/
|
|
332
|
+
refresh(): void {
|
|
333
|
+
this.init();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Auto-initialize when DOM is ready (only in browser environment)
|
|
338
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
339
|
+
let autoInstance: Reveal | null | "disabled" = null;
|
|
340
|
+
|
|
341
|
+
const autoInit = () => {
|
|
342
|
+
if (!autoInstance) {
|
|
343
|
+
autoInstance = new Reveal();
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
if (document.readyState === "loading") {
|
|
348
|
+
document.addEventListener("DOMContentLoaded", autoInit);
|
|
349
|
+
} else {
|
|
350
|
+
autoInit();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// After a SPA <main> swap, re-scan the new content for [data-reveal] targets.
|
|
354
|
+
registerRefresh(() => Reveal.getAutoInstance()?.refresh());
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @description Prevents automatic initialization on DOM ready.
|
|
358
|
+
*/
|
|
359
|
+
Reveal.disableAutoInit = () => {
|
|
360
|
+
autoInstance = "disabled";
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* @description Returns the auto-initialized Reveal instance, if any.
|
|
365
|
+
*
|
|
366
|
+
* @returns The auto instance, or null when disabled or not yet created.
|
|
367
|
+
*/
|
|
368
|
+
Reveal.getAutoInstance = () => {
|
|
369
|
+
return autoInstance === "disabled" ? null : autoInstance;
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Attach to window for the documented public API (`window.Reveal`, `new Reveal()`),
|
|
374
|
+
// and export for module consumers (navigation.js imports it via the main.js bundle).
|
|
375
|
+
if (typeof window !== "undefined") {
|
|
376
|
+
window.Reveal = Reveal;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export { Reveal };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview TC39 Signals wrapper: the kit reactive-state seam.
|
|
3
|
+
* @description Zazz bets on the TC39 Signals proposal for component state. This
|
|
4
|
+
* module is the only file in the kit allowed to import `signal-polyfill`;
|
|
5
|
+
* component scripts import `state`, `computed`, and `effect` from here, so when
|
|
6
|
+
* the proposal's API shifts or engines ship native signals, one file changes.
|
|
7
|
+
*
|
|
8
|
+
* Division of labor (how Zazz components use signals):
|
|
9
|
+
* - DOM events and observers are **input adapters**: they write into `state`.
|
|
10
|
+
* - `computed` holds **pure derived logic**: the unit-testable part.
|
|
11
|
+
* - `effect` is the **output adapter**: it writes results back to the DOM.
|
|
12
|
+
* Imperative concerns (timers, transition choreography, DOM construction) stay
|
|
13
|
+
* imperative; the DOM itself remains the source of truth for element lists.
|
|
14
|
+
*
|
|
15
|
+
* The bare `signal-polyfill` specifier resolves through the page's import map in
|
|
16
|
+
* browsers (pinned jsDelivr URL) and through `node_modules` in tests/bundlers.
|
|
17
|
+
*
|
|
18
|
+
* @see https://github.com/tc39/proposal-signals
|
|
19
|
+
* @see https://github.com/proposal-signals/signal-polyfill
|
|
20
|
+
*/
|
|
21
|
+
import { Signal } from "signal-polyfill";
|
|
22
|
+
/**
|
|
23
|
+
* @description Creates a mutable signal: a reactive value read with `.get()`
|
|
24
|
+
* and written with `.set()`. Reads inside `computed`/`effect` are tracked.
|
|
25
|
+
*
|
|
26
|
+
* @param initialValue - The starting value.
|
|
27
|
+
* @param options - Signal options (e.g. a custom `equals`).
|
|
28
|
+
* @returns The state signal.
|
|
29
|
+
* @example
|
|
30
|
+
* const expanded = state(false);
|
|
31
|
+
* expanded.set(true);
|
|
32
|
+
*/
|
|
33
|
+
declare function state<T>(initialValue: T, options?: Signal.Options<T>): Signal.State<T>;
|
|
34
|
+
/**
|
|
35
|
+
* @description Creates a derived signal: recomputed lazily when a tracked
|
|
36
|
+
* dependency changes, cached otherwise. Keep the computation pure.
|
|
37
|
+
*
|
|
38
|
+
* @param computation - Pure function deriving the value from other signals.
|
|
39
|
+
* @param options - Signal options (e.g. a custom `equals`).
|
|
40
|
+
* @returns The computed signal.
|
|
41
|
+
* @example
|
|
42
|
+
* const paused = computed(() => expanded.get() || hidden.get());
|
|
43
|
+
*/
|
|
44
|
+
declare function computed<T>(computation: () => T, options?: Signal.Options<T>): Signal.Computed<T>;
|
|
45
|
+
/** Options for `effect`. */
|
|
46
|
+
interface EffectOptions {
|
|
47
|
+
/** Aborting disposes the effect: pass an element's controller signal. */
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
}
|
|
50
|
+
/** An effect callback returns either nothing or a cleanup function. */
|
|
51
|
+
type EffectCallback = () => void | (() => void);
|
|
52
|
+
/** The disposer `effect` returns: callable, and a `using`-compatible `Disposable`. */
|
|
53
|
+
type EffectDispose = (() => void) & Disposable;
|
|
54
|
+
/**
|
|
55
|
+
* @description Runs `callback` immediately, tracks every signal it reads, and
|
|
56
|
+
* re-runs it (batched to a microtask) whenever one changes. The callback may
|
|
57
|
+
* return a cleanup function, run before each re-run and on disposal.
|
|
58
|
+
*
|
|
59
|
+
* @param callback - The tracked side effect.
|
|
60
|
+
* @param options - Pass an element's `AbortSignal` to tie disposal to teardown.
|
|
61
|
+
* @returns A dispose function (idempotent; also invoked by the abort signal).
|
|
62
|
+
* It implements `Symbol.dispose`, so a scoped effect (TypeScript sources only)
|
|
63
|
+
* can be bound with `using` and disposed automatically at scope exit.
|
|
64
|
+
* @example
|
|
65
|
+
* const dispose = effect(() => {
|
|
66
|
+
* node.dataset.paused = String(paused.get());
|
|
67
|
+
* });
|
|
68
|
+
* @example
|
|
69
|
+
* using dispose = effect(() => track(count.get())); // disposed at scope exit
|
|
70
|
+
*/
|
|
71
|
+
declare function effect(callback: EffectCallback, options?: EffectOptions): EffectDispose;
|
|
72
|
+
/**
|
|
73
|
+
* @namespace Signals
|
|
74
|
+
* @description Reactive state for Zazz components, wrapping the TC39 Signals
|
|
75
|
+
* polyfill. Import `state`/`computed`/`effect` from this module, never from
|
|
76
|
+
* `signal-polyfill` directly.
|
|
77
|
+
*
|
|
78
|
+
* @property state - Creates a mutable signal.
|
|
79
|
+
* @property computed - Creates a pure derived signal.
|
|
80
|
+
* @property effect - Runs a tracked side effect with batched re-runs.
|
|
81
|
+
*/
|
|
82
|
+
declare const Signals: {
|
|
83
|
+
state: typeof state;
|
|
84
|
+
computed: typeof computed;
|
|
85
|
+
effect: typeof effect;
|
|
86
|
+
};
|
|
87
|
+
export { Signals, state, computed, effect };
|
|
88
|
+
export type { EffectCallback, EffectDispose, EffectOptions };
|
|
89
|
+
//# sourceMappingURL=signals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signals.d.ts","sourceRoot":"","sources":["signals.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAUzC;;;;;;;;;;GAUG;AACH,iBAAS,KAAK,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAE/E;AAED;;;;;;;;;GASG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,WAAW,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAE1F;AAID,4BAA4B;AAC5B,UAAU,aAAa;IACrB,yEAAyE;IACzE,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,uEAAuE;AACvE,KAAK,cAAc,GAAG,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,CAAC;AAEhD,sFAAsF;AACtF,KAAK,aAAa,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,UAAU,CAAC;AA4B/C;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAS,MAAM,CAAC,QAAQ,EAAE,cAAc,EAAE,OAAO,GAAE,aAAkB,GAAG,aAAa,CAsBpF;AAID;;;;;;;;;GASG;AACH,QAAA,MAAM,OAAO;;;;CAA8B,CAAC;AAO5C,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;AAC5C,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview TC39 Signals wrapper: the kit reactive-state seam.
|
|
4
|
+
* @description Zazz bets on the TC39 Signals proposal for component state. This
|
|
5
|
+
* module is the only file in the kit allowed to import `signal-polyfill`;
|
|
6
|
+
* component scripts import `state`, `computed`, and `effect` from here, so when
|
|
7
|
+
* the proposal's API shifts or engines ship native signals, one file changes.
|
|
8
|
+
*
|
|
9
|
+
* Division of labor (how Zazz components use signals):
|
|
10
|
+
* - DOM events and observers are **input adapters**: they write into `state`.
|
|
11
|
+
* - `computed` holds **pure derived logic**: the unit-testable part.
|
|
12
|
+
* - `effect` is the **output adapter**: it writes results back to the DOM.
|
|
13
|
+
* Imperative concerns (timers, transition choreography, DOM construction) stay
|
|
14
|
+
* imperative; the DOM itself remains the source of truth for element lists.
|
|
15
|
+
*
|
|
16
|
+
* The bare `signal-polyfill` specifier resolves through the page's import map in
|
|
17
|
+
* browsers (pinned jsDelivr URL) and through `node_modules` in tests/bundlers.
|
|
18
|
+
*
|
|
19
|
+
* @see https://github.com/tc39/proposal-signals
|
|
20
|
+
* @see https://github.com/proposal-signals/signal-polyfill
|
|
21
|
+
*/
|
|
22
|
+
import { Signal } from "signal-polyfill";
|
|
23
|
+
// The disposer `effect` returns implements the dispose protocol, and `using`
|
|
24
|
+
// compiles (target ES2022) to try/finally helpers that read this well-known
|
|
25
|
+
// symbol at runtime; engines without native Explicit Resource Management
|
|
26
|
+
// (Safari) don't define it, so give them a local stand-in.
|
|
27
|
+
Symbol.dispose ??= Symbol("Symbol.dispose");
|
|
28
|
+
// --- State and computed ---
|
|
29
|
+
/**
|
|
30
|
+
* @description Creates a mutable signal: a reactive value read with `.get()`
|
|
31
|
+
* and written with `.set()`. Reads inside `computed`/`effect` are tracked.
|
|
32
|
+
*
|
|
33
|
+
* @param initialValue - The starting value.
|
|
34
|
+
* @param options - Signal options (e.g. a custom `equals`).
|
|
35
|
+
* @returns The state signal.
|
|
36
|
+
* @example
|
|
37
|
+
* const expanded = state(false);
|
|
38
|
+
* expanded.set(true);
|
|
39
|
+
*/
|
|
40
|
+
function state(initialValue, options) {
|
|
41
|
+
return new Signal.State(initialValue, options);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @description Creates a derived signal: recomputed lazily when a tracked
|
|
45
|
+
* dependency changes, cached otherwise. Keep the computation pure.
|
|
46
|
+
*
|
|
47
|
+
* @param computation - Pure function deriving the value from other signals.
|
|
48
|
+
* @param options - Signal options (e.g. a custom `equals`).
|
|
49
|
+
* @returns The computed signal.
|
|
50
|
+
* @example
|
|
51
|
+
* const paused = computed(() => expanded.get() || hidden.get());
|
|
52
|
+
*/
|
|
53
|
+
function computed(computation, options) {
|
|
54
|
+
return new Signal.Computed(computation, options);
|
|
55
|
+
}
|
|
56
|
+
// The proposal deliberately ships no effect(): this is the reference
|
|
57
|
+
// implementation from the polyfill README: one shared Watcher notifies
|
|
58
|
+
// synchronously on the first dirty signal, and re-runs are batched to a
|
|
59
|
+
// microtask so N writes in one task trigger one re-run.
|
|
60
|
+
let needsEnqueue = true;
|
|
61
|
+
const watcher = new Signal.subtle.Watcher(() => {
|
|
62
|
+
if (needsEnqueue) {
|
|
63
|
+
needsEnqueue = false;
|
|
64
|
+
queueMicrotask(processPending);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
/**
|
|
68
|
+
* @description Re-runs every dirty effect and re-arms the watcher.
|
|
69
|
+
*
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
function processPending() {
|
|
73
|
+
needsEnqueue = true;
|
|
74
|
+
for (const dirty of watcher.getPending()) {
|
|
75
|
+
dirty.get();
|
|
76
|
+
}
|
|
77
|
+
watcher.watch();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @description Runs `callback` immediately, tracks every signal it reads, and
|
|
81
|
+
* re-runs it (batched to a microtask) whenever one changes. The callback may
|
|
82
|
+
* return a cleanup function, run before each re-run and on disposal.
|
|
83
|
+
*
|
|
84
|
+
* @param callback - The tracked side effect.
|
|
85
|
+
* @param options - Pass an element's `AbortSignal` to tie disposal to teardown.
|
|
86
|
+
* @returns A dispose function (idempotent; also invoked by the abort signal).
|
|
87
|
+
* It implements `Symbol.dispose`, so a scoped effect (TypeScript sources only)
|
|
88
|
+
* can be bound with `using` and disposed automatically at scope exit.
|
|
89
|
+
* @example
|
|
90
|
+
* const dispose = effect(() => {
|
|
91
|
+
* node.dataset.paused = String(paused.get());
|
|
92
|
+
* });
|
|
93
|
+
* @example
|
|
94
|
+
* using dispose = effect(() => track(count.get())); // disposed at scope exit
|
|
95
|
+
*/
|
|
96
|
+
function effect(callback, options = {}) {
|
|
97
|
+
let cleanup;
|
|
98
|
+
let disposed = false;
|
|
99
|
+
const tracked = new Signal.Computed(() => {
|
|
100
|
+
if (typeof cleanup === "function")
|
|
101
|
+
cleanup();
|
|
102
|
+
cleanup = callback();
|
|
103
|
+
});
|
|
104
|
+
watcher.watch(tracked);
|
|
105
|
+
tracked.get();
|
|
106
|
+
const dispose = () => {
|
|
107
|
+
if (disposed)
|
|
108
|
+
return;
|
|
109
|
+
disposed = true;
|
|
110
|
+
watcher.unwatch(tracked);
|
|
111
|
+
if (typeof cleanup === "function")
|
|
112
|
+
cleanup();
|
|
113
|
+
cleanup = undefined;
|
|
114
|
+
};
|
|
115
|
+
options.signal?.addEventListener("abort", dispose, { once: true });
|
|
116
|
+
return Object.assign(dispose, { [Symbol.dispose]: dispose });
|
|
117
|
+
}
|
|
118
|
+
// --- Public API ---
|
|
119
|
+
/**
|
|
120
|
+
* @namespace Signals
|
|
121
|
+
* @description Reactive state for Zazz components, wrapping the TC39 Signals
|
|
122
|
+
* polyfill. Import `state`/`computed`/`effect` from this module, never from
|
|
123
|
+
* `signal-polyfill` directly.
|
|
124
|
+
*
|
|
125
|
+
* @property state - Creates a mutable signal.
|
|
126
|
+
* @property computed - Creates a pure derived signal.
|
|
127
|
+
* @property effect - Runs a tracked side effect with batched re-runs.
|
|
128
|
+
*/
|
|
129
|
+
const Signals = { state, computed, effect };
|
|
130
|
+
// Attach to window for the documented public API, then export for module consumers.
|
|
131
|
+
if (typeof window !== "undefined") {
|
|
132
|
+
window.Signals = Signals;
|
|
133
|
+
}
|
|
134
|
+
export { Signals, state, computed, effect };
|