@zazz-ui/core 0.13.0 → 0.14.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/README.md +5 -9
  3. package/dist/sri.json +34 -30
  4. package/dist/zazz.css +1 -1
  5. package/dist/zazz.js +1 -1
  6. package/editor/zazz.css-data.json +173 -3
  7. package/editor/zazz.html-data.json +138 -0
  8. package/editor/zazz.language-data.json +313 -125
  9. package/editor/zazz.lint-data.json +1501 -1383
  10. package/package.json +1 -5
  11. package/src/base/_defaults.css +1 -1
  12. package/src/base/_reset.css +16 -2
  13. package/src/base/_variables.css +43 -31
  14. package/src/base/carousel-scroll.d.ts +161 -0
  15. package/src/base/carousel-scroll.d.ts.map +1 -0
  16. package/src/base/carousel-scroll.js +1035 -0
  17. package/src/base/carousel-scroll.ts +1227 -0
  18. package/src/base/carousel.d.ts +34 -0
  19. package/src/base/carousel.d.ts.map +1 -0
  20. package/src/base/carousel.js +279 -0
  21. package/src/base/carousel.ts +292 -0
  22. package/src/base/dialog-lifecycle.js +1 -1
  23. package/src/base/dialog-lifecycle.ts +1 -1
  24. package/src/base/gesture.d.ts +201 -0
  25. package/src/base/gesture.d.ts.map +1 -0
  26. package/src/base/gesture.js +317 -0
  27. package/src/base/gesture.ts +494 -0
  28. package/src/base/kit-styling.d.ts.map +1 -1
  29. package/src/base/kit-styling.js +182 -162
  30. package/src/base/kit-styling.ts +182 -162
  31. package/src/base/utils.d.ts +1 -1
  32. package/src/base/utils.js +2 -2
  33. package/src/base/utils.ts +2 -2
  34. package/src/globals.d.ts +7 -30
  35. package/src/head.d.ts.map +1 -1
  36. package/src/head.js +1 -25
  37. package/src/head.ts +1 -25
  38. package/src/index.css +1 -0
  39. package/src/index.d.ts +6 -5
  40. package/src/index.d.ts.map +1 -1
  41. package/src/index.js +6 -5
  42. package/src/index.ts +6 -5
  43. package/src/manifest.d.ts +1 -1
  44. package/src/manifest.d.ts.map +1 -1
  45. package/src/manifest.js +25 -13
  46. package/src/manifest.ts +26 -14
  47. package/src/primitives/button/button.css +1 -1
  48. package/src/primitives/carousel/carousel.css +103 -20
  49. package/src/primitives/carousel/carousel.d.ts +4 -32
  50. package/src/primitives/carousel/carousel.d.ts.map +1 -1
  51. package/src/primitives/carousel/carousel.js +39 -14
  52. package/src/primitives/carousel/carousel.ts +21 -25
  53. package/src/primitives/dialog/dialog.css +14 -8
  54. package/src/primitives/drawer/drawer-indent.html +27 -0
  55. package/src/primitives/drawer/drawer-nested.html +72 -0
  56. package/src/primitives/drawer/drawer-responsive-dialog.html +44 -0
  57. package/src/primitives/drawer/drawer-snap.html +26 -0
  58. package/src/primitives/drawer/drawer.css +528 -0
  59. package/src/primitives/drawer/drawer.d.ts +24 -0
  60. package/src/primitives/drawer/drawer.d.ts.map +1 -0
  61. package/src/primitives/drawer/drawer.html +42 -0
  62. package/src/primitives/drawer/drawer.js +765 -0
  63. package/src/primitives/drawer/drawer.ts +823 -0
  64. package/src/primitives/fields/fields.css +4 -2
  65. package/src/primitives/input/input.css +4 -3
  66. package/src/primitives/input-group/input-group.css +5 -5
  67. package/src/primitives/lightbox/lightbox.css +2 -2
  68. package/src/primitives/lightbox/lightbox.d.ts.map +1 -1
  69. package/src/primitives/lightbox/lightbox.js +9 -8
  70. package/src/primitives/lightbox/lightbox.ts +9 -8
  71. package/src/primitives/password-group/password-group.css +5 -5
  72. package/src/primitives/radio/radio.css +15 -10
  73. package/src/primitives/slider/slider.css +2 -2
  74. package/src/primitives/switch/switch.css +6 -6
  75. package/src/primitives/tabs/tabs.css +10 -10
  76. package/src/primitives/toaster/toaster.css +29 -1
  77. package/src/primitives/toaster/toaster.d.ts.map +1 -1
  78. package/src/primitives/toaster/toaster.js +117 -9
  79. package/src/primitives/toaster/toaster.ts +136 -9
  80. package/src/primitives/tooltip/tooltip.css +93 -10
  81. package/src/base/embla.d.ts +0 -90
  82. package/src/base/embla.d.ts.map +0 -1
  83. package/src/base/embla.js +0 -878
  84. package/src/base/embla.ts +0 -1066
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @description Initializes a single carousel root: starts the engine from its
3
+ * `data-carousel-*` attributes, links a thumb strip when it has one, and
4
+ * stores the API on `root._carousel`. Idempotent: skips roots that are already
5
+ * initialized, inside a closed dialog (no measurable viewport until open), or
6
+ * that are another carousel's thumb strip (its owner runs it).
7
+ *
8
+ * Called by `initCarousels()` for attribute-form markup and by the
9
+ * `<ui-carousel>` web component on connect.
10
+ *
11
+ * @param root - The carousel root element.
12
+ */
13
+ declare function initCarouselRoot(root: Element): void;
14
+ /**
15
+ * @description Initializes all carousels within a scope. Roots managed by the
16
+ * `<ui-carousel>` web component are skipped: they initialize themselves on
17
+ * connect.
18
+ *
19
+ * @param scope - Root element to search within. Defaults to `document`.
20
+ */
21
+ declare function initCarousels(scope?: Document | Element): void;
22
+ /**
23
+ * @namespace Carousel
24
+ * @description Public API for carousel initialization.
25
+ *
26
+ * @property init - Initializes all carousels within a scope.
27
+ * @property initRoot - Initializes a single carousel root (used by `<ui-carousel>`).
28
+ */
29
+ declare const Carousel: {
30
+ init: typeof initCarousels;
31
+ initRoot: typeof initCarouselRoot;
32
+ };
33
+ export { Carousel };
34
+ //# sourceMappingURL=carousel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"carousel.d.ts","sourceRoot":"","sources":["carousel.ts"],"names":[],"mappings":"AAsFA;;;;;;;;;;;GAWG;AACH,iBAAS,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAuB7C;AAED;;;;;;GAMG;AACH,iBAAS,aAAa,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAG,OAAO,GAAG,IAAI,CAKvD;AA4HD;;;;;;GAMG;AACH,QAAA,MAAM,QAAQ;IACZ,IAAI;IACJ,QAAQ;CACT,CAAC;AAsBF,OAAO,EAAE,QAAQ,EAAE,CAAC"}
@@ -0,0 +1,279 @@
1
+ "use strict";
2
+ /**
3
+ * @fileoverview Carousel initialization and controls.
4
+ * @description Discovers carousel roots (`<ui-carousel>` or `[data-ui~="carousel"]`),
5
+ * starts the scroll-snap engine on each (base/carousel-scroll.ts), and wires
6
+ * the page-level behaviors: arrow keys, dialog start index (subscribing to
7
+ * `zazz:dialog-open` from base/dialog-lifecycle.ts; lightbox choreography
8
+ * lives in lightbox.ts), and re-init after an in-page navigation.
9
+ *
10
+ * Structure: root is the element itself; parts are slots (`data-carousel-slot="<part>"`):
11
+ * - root (`<ui-carousel>` | `[data-ui~="carousel"]`): Carousel container; holds all config attributes
12
+ * - `viewport`: The scroll container (required)
13
+ * - `container`: Slides flex track
14
+ * - `slide`: Individual slide
15
+ * - `prev` / `next`: Navigation buttons (optional)
16
+ * - `dots` / `dot`: Dot pagination container and template dot (optional)
17
+ * - `thumbs`: Linked thumb strip (optional), with its own viewport, container, and slides
18
+ *
19
+ * Configuration (on the carousel root):
20
+ * - `data-carousel-loop`: `"true"` wraps from the last slide to the first (needs three slides)
21
+ * - `data-carousel-align`: `start` (default) | `center` | `end`
22
+ * - `data-carousel-axis`: `x` (default) | `y`; a vertical viewport needs a block size
23
+ * - `data-carousel-draggable`: `"false"` turns mouse dragging off (touch still scrolls)
24
+ * - `data-carousel-drag-free`: `"true"` scrolls freely with momentum, no snapping
25
+ * - `data-carousel-skip-snaps`: `"true"` lets a mouse flick pass several snaps
26
+ * - `data-carousel-start-snap`: Index to start on
27
+ * - `data-carousel-keyboard`: `"false"` leaves the arrow keys to native scrolling
28
+ * - `data-carousel-plugins`: `autoplay` or `auto-scroll`
29
+ * - `data-carousel-autoplay-*`: `delay` (ms, 4000), `stop-on-last-snap`
30
+ * - `data-carousel-auto-scroll-*`: `speed` (px per 60 Hz frame, 2), `start-delay` (ms, 1000),
31
+ * `direction` (`forward` | `backward`)
32
+ * - both plugins: `…-play-on-init` (`"false"` waits for `play()`), `…-stop-on-interaction`
33
+ * (`"false"` holds during a drag, a scroll, or slide focus and resumes after; Prev/Next,
34
+ * the dots, the thumbs, and the arrow keys restart the delay; by default any of those
35
+ * stops it for good), `…-stop-on-mouse-enter`
36
+ * - Right to left: no option; the track follows the inherited `dir`
37
+ *
38
+ * Thumb strip (on `data-carousel-slot="thumbs"`):
39
+ * - `data-carousel-thumbs-*`: the options above for the strip (default: drag-free)
40
+ *
41
+ * Lifecycle and dialog start index:
42
+ * - `data-carousel-init`: Set by script when a carousel is initialized
43
+ * - `data-carousel-start`: On a trigger; slide index to open to (pairs with commandfor)
44
+ * - `data-carousel-start-index`: Set on root by script; consumed when dialog opens
45
+ *
46
+ * State (`data-carousel-state` tokens, set by script): `snapped` and `in-view` on slides,
47
+ * `active` on the current dot and thumb, `dragging` on the viewport during a mouse drag,
48
+ * `playing` on the root while autoplay or auto-scroll is on. The root dispatches
49
+ * `zazz:carousel-select` (`detail.index`) when the current snap changes.
50
+ *
51
+ * @example
52
+ * data-carousel-loop="true"
53
+ *
54
+ * @example
55
+ * data-carousel-align="center"
56
+ *
57
+ * @example
58
+ * data-carousel-plugins="autoplay" data-carousel-autoplay-delay="3000"
59
+ *
60
+ * @example
61
+ * data-carousel-plugins="auto-scroll" data-carousel-auto-scroll-speed="2"
62
+ *
63
+ * @example Barebones carousel (4 text slides; auto-inits on DOMContentLoaded):
64
+ * <div data-ui="carousel">
65
+ * <div data-carousel-slot="viewport">
66
+ * <div data-carousel-slot="container">
67
+ * <div data-carousel-slot="slide">Slide 1</div>
68
+ * <div data-carousel-slot="slide">Slide 2</div>
69
+ * <div data-carousel-slot="slide">Slide 3</div>
70
+ * <div data-carousel-slot="slide">Slide 4</div>
71
+ * </div>
72
+ * </div>
73
+ * <button type="button" data-carousel-slot="prev">Prev</button>
74
+ * <button type="button" data-carousel-slot="next">Next</button>
75
+ * </div>
76
+ */
77
+ import { createCarousel, linkThumbs } from "./carousel-scroll.js";
78
+ import { registerRefresh } from "./zazz-element.js";
79
+ /** Carousel roots, in tag and attribute form. */
80
+ const ROOT = ':is(ui-carousel, [data-ui~="carousel"])';
81
+ // --- Carousel initialization ---
82
+ /**
83
+ * @description Initializes a single carousel root: starts the engine from its
84
+ * `data-carousel-*` attributes, links a thumb strip when it has one, and
85
+ * stores the API on `root._carousel`. Idempotent: skips roots that are already
86
+ * initialized, inside a closed dialog (no measurable viewport until open), or
87
+ * that are another carousel's thumb strip (its owner runs it).
88
+ *
89
+ * Called by `initCarousels()` for attribute-form markup and by the
90
+ * `<ui-carousel>` web component on connect.
91
+ *
92
+ * @param root - The carousel root element.
93
+ */
94
+ function initCarouselRoot(root) {
95
+ if (root.hasAttribute("data-carousel-init"))
96
+ return;
97
+ if (root.closest("dialog:not([open])"))
98
+ return;
99
+ if (root.matches('[data-carousel-slot~="thumbs"]'))
100
+ return;
101
+ const thumbsNode = root.querySelector('[data-carousel-slot~="thumbs"]');
102
+ const viewport = root.querySelector(thumbsNode
103
+ ? '[data-carousel-slot~="viewport"]:not([data-carousel-slot~="thumbs"] *)'
104
+ : '[data-carousel-slot~="viewport"]');
105
+ if (!viewport)
106
+ return;
107
+ root.setAttribute("data-carousel-init", "");
108
+ // a scroll container is keyboard-focusable in some engines only; make it so in all
109
+ if (!viewport.hasAttribute("tabindex"))
110
+ viewport.setAttribute("tabindex", "0");
111
+ // One controller per root scopes everything the engine binds; the owner
112
+ // (`<ui-carousel>` disconnectedCallback) aborts it on teardown.
113
+ const controller = new AbortController();
114
+ root._carouselController = controller;
115
+ const api = createCarousel(root, viewport, controller.signal);
116
+ if (api && thumbsNode)
117
+ linkThumbs(api, root, thumbsNode, controller.signal);
118
+ }
119
+ /**
120
+ * @description Initializes all carousels within a scope. Roots managed by the
121
+ * `<ui-carousel>` web component are skipped: they initialize themselves on
122
+ * connect.
123
+ *
124
+ * @param scope - Root element to search within. Defaults to `document`.
125
+ */
126
+ function initCarousels(scope) {
127
+ for (const root of (scope ?? document).querySelectorAll(ROOT)) {
128
+ if (root.closest("ui-carousel"))
129
+ continue;
130
+ initCarouselRoot(root);
131
+ }
132
+ }
133
+ // --- Dialog open subscription ---
134
+ /**
135
+ * @description Reacts to dialogs opening (via `zazz:dialog-open` from
136
+ * base/dialog-lifecycle.ts): initializes attribute-form roots that deferred
137
+ * while the dialog was `display: none`, applies a stored
138
+ * `data-carousel-start-index`, and focuses the viewport for keyboard
139
+ * navigation.
140
+ *
141
+ * Listens on `document`, so it runs after any dialog- or ancestor-level
142
+ * subscriber (`<ui-carousel>` self-init, `<ui-lightbox>` choreography).
143
+ * @private
144
+ */
145
+ function initDialogOpenSubscription() {
146
+ document.addEventListener("zazz:dialog-open", (event) => {
147
+ if (!(event.target instanceof HTMLDialogElement))
148
+ return;
149
+ const dialog = event.target;
150
+ initCarousels(dialog);
151
+ for (const root of dialog.querySelectorAll(ROOT)) {
152
+ const startIndex = root.getAttribute("data-carousel-start-index");
153
+ if (startIndex != null && root._carousel) {
154
+ root._carousel.goTo(Number(startIndex), true);
155
+ root.removeAttribute("data-carousel-start-index");
156
+ }
157
+ const viewport = root.querySelector('[data-carousel-slot~="viewport"]');
158
+ if (viewport instanceof HTMLElement)
159
+ viewport.focus({ preventScroll: true });
160
+ }
161
+ });
162
+ }
163
+ // --- Keyboard navigation ---
164
+ /**
165
+ * @description The carousel the arrow keys drive: one inside an open dialog,
166
+ * else the one containing focus. Respects `data-carousel-keyboard="false"`.
167
+ * @private
168
+ */
169
+ function activeRoot() {
170
+ const usable = (root) => !!root?._carousel && root.getAttribute("data-carousel-keyboard") !== "false";
171
+ const dialogRoot = document
172
+ .querySelector("dialog[open]")
173
+ ?.querySelector(`${ROOT}[data-carousel-init]`);
174
+ if (usable(dialogRoot))
175
+ return dialogRoot;
176
+ const focusedRoot = document.activeElement?.closest(`${ROOT}[data-carousel-init]`);
177
+ return usable(focusedRoot) ? focusedRoot : null;
178
+ }
179
+ /**
180
+ * @description Steps the active carousel on its axis's arrow keys: Left and
181
+ * Right (mirrored in RTL), or Up and Down on a vertical track. Native arrow
182
+ * scrolling would stop at the ends of a looped track and ignore the pending
183
+ * step; keys off the axis are left alone.
184
+ * @private
185
+ */
186
+ function initKeyboardNav() {
187
+ document.addEventListener("keydown", (event) => {
188
+ if (!event.key.startsWith("Arrow") || event.defaultPrevented)
189
+ return;
190
+ const target = event.target;
191
+ if (!(target instanceof HTMLElement) ||
192
+ target instanceof HTMLInputElement ||
193
+ target instanceof HTMLTextAreaElement ||
194
+ target instanceof HTMLSelectElement ||
195
+ target.isContentEditable) {
196
+ return;
197
+ }
198
+ const api = activeRoot()?._carousel;
199
+ if (!api)
200
+ return;
201
+ const [back, forward] = api.vertical
202
+ ? ["ArrowUp", "ArrowDown"]
203
+ : api.rtl
204
+ ? ["ArrowRight", "ArrowLeft"]
205
+ : ["ArrowLeft", "ArrowRight"];
206
+ if (event.key !== back && event.key !== forward)
207
+ return;
208
+ event.preventDefault();
209
+ api.interact();
210
+ if (event.key === back)
211
+ api.goToPrev();
212
+ else
213
+ api.goToNext();
214
+ });
215
+ }
216
+ // --- Start index control ---
217
+ /**
218
+ * @description Stores or applies a start slide index from `[data-carousel-start]` triggers.
219
+ *
220
+ * Clicking an element with `data-carousel-start="N"` stores that index on the
221
+ * target carousel (found via `commandfor` → dialog → carousel root); a slide
222
+ * with `commandfor` opens the dialog at the carousel's current slide. The
223
+ * dialog-open subscription scrolls to it on open.
224
+ * @private
225
+ */
226
+ function initStartLinks() {
227
+ document.addEventListener("click", (event) => {
228
+ if (!(event.target instanceof HTMLElement))
229
+ return;
230
+ const trigger = event.target.closest("[data-carousel-start], [data-carousel-slot~='slide'][commandfor]");
231
+ if (!trigger)
232
+ return;
233
+ let index = trigger.getAttribute("data-carousel-start");
234
+ if (index == null) {
235
+ const source = trigger.closest(ROOT)?._carousel;
236
+ if (source)
237
+ index = String(source.selectedSnap());
238
+ }
239
+ const dialogId = trigger.getAttribute("commandfor");
240
+ const root = dialogId ? document.getElementById(dialogId)?.querySelector(ROOT) : null;
241
+ if (!root || index == null)
242
+ return;
243
+ if (root._carousel)
244
+ root._carousel.goTo(Number(index), true);
245
+ else
246
+ root.setAttribute("data-carousel-start-index", index);
247
+ });
248
+ }
249
+ /**
250
+ * @namespace Carousel
251
+ * @description Public API for carousel initialization.
252
+ *
253
+ * @property init - Initializes all carousels within a scope.
254
+ * @property initRoot - Initializes a single carousel root (used by `<ui-carousel>`).
255
+ */
256
+ const Carousel = {
257
+ init: initCarousels,
258
+ initRoot: initCarouselRoot,
259
+ };
260
+ // Auto-initialize when DOM is ready (only in browser environment)
261
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
262
+ // After an in-page navigation swap, initialize attribute-form carousels in
263
+ // the new content (<ui-carousel> elements ride the swap via their own
264
+ // lifecycle; init() skips already-initialized roots and closed dialogs).
265
+ registerRefresh((scope) => initCarousels(scope));
266
+ const start = () => {
267
+ initCarousels();
268
+ initDialogOpenSubscription();
269
+ initStartLinks();
270
+ initKeyboardNav();
271
+ };
272
+ if (document.readyState === "loading")
273
+ document.addEventListener("DOMContentLoaded", start);
274
+ else
275
+ start();
276
+ // The documented public API (`window.Carousel`); also exported for module consumers.
277
+ window.Carousel = Carousel;
278
+ }
279
+ export { Carousel };
@@ -0,0 +1,292 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * @fileoverview Carousel initialization and controls.
5
+ * @description Discovers carousel roots (`<ui-carousel>` or `[data-ui~="carousel"]`),
6
+ * starts the scroll-snap engine on each (base/carousel-scroll.ts), and wires
7
+ * the page-level behaviors: arrow keys, dialog start index (subscribing to
8
+ * `zazz:dialog-open` from base/dialog-lifecycle.ts; lightbox choreography
9
+ * lives in lightbox.ts), and re-init after an in-page navigation.
10
+ *
11
+ * Structure: root is the element itself; parts are slots (`data-carousel-slot="<part>"`):
12
+ * - root (`<ui-carousel>` | `[data-ui~="carousel"]`): Carousel container; holds all config attributes
13
+ * - `viewport`: The scroll container (required)
14
+ * - `container`: Slides flex track
15
+ * - `slide`: Individual slide
16
+ * - `prev` / `next`: Navigation buttons (optional)
17
+ * - `dots` / `dot`: Dot pagination container and template dot (optional)
18
+ * - `thumbs`: Linked thumb strip (optional), with its own viewport, container, and slides
19
+ *
20
+ * Configuration (on the carousel root):
21
+ * - `data-carousel-loop`: `"true"` wraps from the last slide to the first (needs three slides)
22
+ * - `data-carousel-align`: `start` (default) | `center` | `end`
23
+ * - `data-carousel-axis`: `x` (default) | `y`; a vertical viewport needs a block size
24
+ * - `data-carousel-draggable`: `"false"` turns mouse dragging off (touch still scrolls)
25
+ * - `data-carousel-drag-free`: `"true"` scrolls freely with momentum, no snapping
26
+ * - `data-carousel-skip-snaps`: `"true"` lets a mouse flick pass several snaps
27
+ * - `data-carousel-start-snap`: Index to start on
28
+ * - `data-carousel-keyboard`: `"false"` leaves the arrow keys to native scrolling
29
+ * - `data-carousel-plugins`: `autoplay` or `auto-scroll`
30
+ * - `data-carousel-autoplay-*`: `delay` (ms, 4000), `stop-on-last-snap`
31
+ * - `data-carousel-auto-scroll-*`: `speed` (px per 60 Hz frame, 2), `start-delay` (ms, 1000),
32
+ * `direction` (`forward` | `backward`)
33
+ * - both plugins: `…-play-on-init` (`"false"` waits for `play()`), `…-stop-on-interaction`
34
+ * (`"false"` holds during a drag, a scroll, or slide focus and resumes after; Prev/Next,
35
+ * the dots, the thumbs, and the arrow keys restart the delay; by default any of those
36
+ * stops it for good), `…-stop-on-mouse-enter`
37
+ * - Right to left: no option; the track follows the inherited `dir`
38
+ *
39
+ * Thumb strip (on `data-carousel-slot="thumbs"`):
40
+ * - `data-carousel-thumbs-*`: the options above for the strip (default: drag-free)
41
+ *
42
+ * Lifecycle and dialog start index:
43
+ * - `data-carousel-init`: Set by script when a carousel is initialized
44
+ * - `data-carousel-start`: On a trigger; slide index to open to (pairs with commandfor)
45
+ * - `data-carousel-start-index`: Set on root by script; consumed when dialog opens
46
+ *
47
+ * State (`data-carousel-state` tokens, set by script): `snapped` and `in-view` on slides,
48
+ * `active` on the current dot and thumb, `dragging` on the viewport during a mouse drag,
49
+ * `playing` on the root while autoplay or auto-scroll is on. The root dispatches
50
+ * `zazz:carousel-select` (`detail.index`) when the current snap changes.
51
+ *
52
+ * @example
53
+ * data-carousel-loop="true"
54
+ *
55
+ * @example
56
+ * data-carousel-align="center"
57
+ *
58
+ * @example
59
+ * data-carousel-plugins="autoplay" data-carousel-autoplay-delay="3000"
60
+ *
61
+ * @example
62
+ * data-carousel-plugins="auto-scroll" data-carousel-auto-scroll-speed="2"
63
+ *
64
+ * @example Barebones carousel (4 text slides; auto-inits on DOMContentLoaded):
65
+ * <div data-ui="carousel">
66
+ * <div data-carousel-slot="viewport">
67
+ * <div data-carousel-slot="container">
68
+ * <div data-carousel-slot="slide">Slide 1</div>
69
+ * <div data-carousel-slot="slide">Slide 2</div>
70
+ * <div data-carousel-slot="slide">Slide 3</div>
71
+ * <div data-carousel-slot="slide">Slide 4</div>
72
+ * </div>
73
+ * </div>
74
+ * <button type="button" data-carousel-slot="prev">Prev</button>
75
+ * <button type="button" data-carousel-slot="next">Next</button>
76
+ * </div>
77
+ */
78
+
79
+ import { createCarousel, linkThumbs } from "./carousel-scroll.ts";
80
+ import { registerRefresh } from "./zazz-element.ts";
81
+
82
+ /** Carousel roots, in tag and attribute form. */
83
+ const ROOT = ':is(ui-carousel, [data-ui~="carousel"])';
84
+
85
+ // --- Carousel initialization ---
86
+
87
+ /**
88
+ * @description Initializes a single carousel root: starts the engine from its
89
+ * `data-carousel-*` attributes, links a thumb strip when it has one, and
90
+ * stores the API on `root._carousel`. Idempotent: skips roots that are already
91
+ * initialized, inside a closed dialog (no measurable viewport until open), or
92
+ * that are another carousel's thumb strip (its owner runs it).
93
+ *
94
+ * Called by `initCarousels()` for attribute-form markup and by the
95
+ * `<ui-carousel>` web component on connect.
96
+ *
97
+ * @param root - The carousel root element.
98
+ */
99
+ function initCarouselRoot(root: Element): void {
100
+ if (root.hasAttribute("data-carousel-init")) return;
101
+ if (root.closest("dialog:not([open])")) return;
102
+ if (root.matches('[data-carousel-slot~="thumbs"]')) return;
103
+
104
+ const thumbsNode = root.querySelector('[data-carousel-slot~="thumbs"]');
105
+ const viewport = root.querySelector<HTMLElement>(
106
+ thumbsNode
107
+ ? '[data-carousel-slot~="viewport"]:not([data-carousel-slot~="thumbs"] *)'
108
+ : '[data-carousel-slot~="viewport"]',
109
+ );
110
+ if (!viewport) return;
111
+
112
+ root.setAttribute("data-carousel-init", "");
113
+ // a scroll container is keyboard-focusable in some engines only; make it so in all
114
+ if (!viewport.hasAttribute("tabindex")) viewport.setAttribute("tabindex", "0");
115
+
116
+ // One controller per root scopes everything the engine binds; the owner
117
+ // (`<ui-carousel>` disconnectedCallback) aborts it on teardown.
118
+ const controller = new AbortController();
119
+ root._carouselController = controller;
120
+ const api = createCarousel(root, viewport, controller.signal);
121
+ if (api && thumbsNode) linkThumbs(api, root, thumbsNode, controller.signal);
122
+ }
123
+
124
+ /**
125
+ * @description Initializes all carousels within a scope. Roots managed by the
126
+ * `<ui-carousel>` web component are skipped: they initialize themselves on
127
+ * connect.
128
+ *
129
+ * @param scope - Root element to search within. Defaults to `document`.
130
+ */
131
+ function initCarousels(scope?: Document | Element): void {
132
+ for (const root of (scope ?? document).querySelectorAll(ROOT)) {
133
+ if (root.closest("ui-carousel")) continue;
134
+ initCarouselRoot(root);
135
+ }
136
+ }
137
+
138
+ // --- Dialog open subscription ---
139
+
140
+ /**
141
+ * @description Reacts to dialogs opening (via `zazz:dialog-open` from
142
+ * base/dialog-lifecycle.ts): initializes attribute-form roots that deferred
143
+ * while the dialog was `display: none`, applies a stored
144
+ * `data-carousel-start-index`, and focuses the viewport for keyboard
145
+ * navigation.
146
+ *
147
+ * Listens on `document`, so it runs after any dialog- or ancestor-level
148
+ * subscriber (`<ui-carousel>` self-init, `<ui-lightbox>` choreography).
149
+ * @private
150
+ */
151
+ function initDialogOpenSubscription(): void {
152
+ document.addEventListener("zazz:dialog-open", (event) => {
153
+ if (!(event.target instanceof HTMLDialogElement)) return;
154
+ const dialog = event.target;
155
+ initCarousels(dialog);
156
+
157
+ for (const root of dialog.querySelectorAll(ROOT)) {
158
+ const startIndex = root.getAttribute("data-carousel-start-index");
159
+ if (startIndex != null && root._carousel) {
160
+ root._carousel.goTo(Number(startIndex), true);
161
+ root.removeAttribute("data-carousel-start-index");
162
+ }
163
+ const viewport = root.querySelector('[data-carousel-slot~="viewport"]');
164
+ if (viewport instanceof HTMLElement) viewport.focus({ preventScroll: true });
165
+ }
166
+ });
167
+ }
168
+
169
+ // --- Keyboard navigation ---
170
+
171
+ /**
172
+ * @description The carousel the arrow keys drive: one inside an open dialog,
173
+ * else the one containing focus. Respects `data-carousel-keyboard="false"`.
174
+ * @private
175
+ */
176
+ function activeRoot(): Element | null {
177
+ const usable = (root: Element | null | undefined): root is Element =>
178
+ !!root?._carousel && root.getAttribute("data-carousel-keyboard") !== "false";
179
+
180
+ const dialogRoot = document
181
+ .querySelector("dialog[open]")
182
+ ?.querySelector(`${ROOT}[data-carousel-init]`);
183
+ if (usable(dialogRoot)) return dialogRoot;
184
+
185
+ const focusedRoot = document.activeElement?.closest(`${ROOT}[data-carousel-init]`);
186
+ return usable(focusedRoot) ? focusedRoot : null;
187
+ }
188
+
189
+ /**
190
+ * @description Steps the active carousel on its axis's arrow keys: Left and
191
+ * Right (mirrored in RTL), or Up and Down on a vertical track. Native arrow
192
+ * scrolling would stop at the ends of a looped track and ignore the pending
193
+ * step; keys off the axis are left alone.
194
+ * @private
195
+ */
196
+ function initKeyboardNav(): void {
197
+ document.addEventListener("keydown", (event) => {
198
+ if (!event.key.startsWith("Arrow") || event.defaultPrevented) return;
199
+ const target = event.target;
200
+ if (
201
+ !(target instanceof HTMLElement) ||
202
+ target instanceof HTMLInputElement ||
203
+ target instanceof HTMLTextAreaElement ||
204
+ target instanceof HTMLSelectElement ||
205
+ target.isContentEditable
206
+ ) {
207
+ return;
208
+ }
209
+
210
+ const api = activeRoot()?._carousel;
211
+ if (!api) return;
212
+ const [back, forward] = api.vertical
213
+ ? ["ArrowUp", "ArrowDown"]
214
+ : api.rtl
215
+ ? ["ArrowRight", "ArrowLeft"]
216
+ : ["ArrowLeft", "ArrowRight"];
217
+ if (event.key !== back && event.key !== forward) return;
218
+
219
+ event.preventDefault();
220
+ api.interact();
221
+ if (event.key === back) api.goToPrev();
222
+ else api.goToNext();
223
+ });
224
+ }
225
+
226
+ // --- Start index control ---
227
+
228
+ /**
229
+ * @description Stores or applies a start slide index from `[data-carousel-start]` triggers.
230
+ *
231
+ * Clicking an element with `data-carousel-start="N"` stores that index on the
232
+ * target carousel (found via `commandfor` → dialog → carousel root); a slide
233
+ * with `commandfor` opens the dialog at the carousel's current slide. The
234
+ * dialog-open subscription scrolls to it on open.
235
+ * @private
236
+ */
237
+ function initStartLinks(): void {
238
+ document.addEventListener("click", (event) => {
239
+ if (!(event.target instanceof HTMLElement)) return;
240
+ const trigger = event.target.closest(
241
+ "[data-carousel-start], [data-carousel-slot~='slide'][commandfor]",
242
+ );
243
+ if (!trigger) return;
244
+
245
+ let index = trigger.getAttribute("data-carousel-start");
246
+ if (index == null) {
247
+ const source = trigger.closest(ROOT)?._carousel;
248
+ if (source) index = String(source.selectedSnap());
249
+ }
250
+
251
+ const dialogId = trigger.getAttribute("commandfor");
252
+ const root = dialogId ? document.getElementById(dialogId)?.querySelector(ROOT) : null;
253
+ if (!root || index == null) return;
254
+
255
+ if (root._carousel) root._carousel.goTo(Number(index), true);
256
+ else root.setAttribute("data-carousel-start-index", index);
257
+ });
258
+ }
259
+
260
+ /**
261
+ * @namespace Carousel
262
+ * @description Public API for carousel initialization.
263
+ *
264
+ * @property init - Initializes all carousels within a scope.
265
+ * @property initRoot - Initializes a single carousel root (used by `<ui-carousel>`).
266
+ */
267
+ const Carousel = {
268
+ init: initCarousels,
269
+ initRoot: initCarouselRoot,
270
+ };
271
+
272
+ // Auto-initialize when DOM is ready (only in browser environment)
273
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
274
+ // After an in-page navigation swap, initialize attribute-form carousels in
275
+ // the new content (<ui-carousel> elements ride the swap via their own
276
+ // lifecycle; init() skips already-initialized roots and closed dialogs).
277
+ registerRefresh((scope) => initCarousels(scope));
278
+
279
+ const start = () => {
280
+ initCarousels();
281
+ initDialogOpenSubscription();
282
+ initStartLinks();
283
+ initKeyboardNav();
284
+ };
285
+ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", start);
286
+ else start();
287
+
288
+ // The documented public API (`window.Carousel`); also exported for module consumers.
289
+ window.Carousel = Carousel;
290
+ }
291
+
292
+ export { Carousel };
@@ -12,7 +12,7 @@
12
12
  * Components subscribe instead of running their own observers: a carousel
13
13
  * inside a closed dialog listens on the dialog it found via `closest("dialog")`;
14
14
  * `<ui-lightbox>` listens on itself (its dialog is a descendant, so the events
15
- * bubble through it); embla.js listens on `document` for class-form roots.
15
+ * bubble through it); carousel.js listens on `document` for class-form roots.
16
16
  * Dispatch order is the DOM order: dialog listeners, then ancestors, then
17
17
  * document, so init-on-open (dialog level) always precedes scroll/focus
18
18
  * choreography (ancestor and document level).
@@ -13,7 +13,7 @@
13
13
  * Components subscribe instead of running their own observers: a carousel
14
14
  * inside a closed dialog listens on the dialog it found via `closest("dialog")`;
15
15
  * `<ui-lightbox>` listens on itself (its dialog is a descendant, so the events
16
- * bubble through it); embla.js listens on `document` for class-form roots.
16
+ * bubble through it); carousel.js listens on `document` for class-form roots.
17
17
  * Dispatch order is the DOM order: dialog listeners, then ancestors, then
18
18
  * document, so init-on-open (dialog level) always precedes scroll/focus
19
19
  * choreography (ancestor and document level).