@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,791 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Embla Carousel initialization and controls.
|
|
5
|
+
* @description Discovers carousel roots (`<ui-carousel>` or `.ui-carousel`),
|
|
6
|
+
* initializes Embla instances with optional plugins, and wires navigation,
|
|
7
|
+
* keyboard, and dialog-open behaviors (subscribing to `zazz:dialog-open`
|
|
8
|
+
* from base/dialog-lifecycle.ts; lightbox choreography lives in lightbox.ts).
|
|
9
|
+
*
|
|
10
|
+
* Structure: root is the element/class itself; parts are slots (`data-slot="carousel-<part>"`):
|
|
11
|
+
* - root (`<ui-carousel>` | `.ui-carousel`): Carousel container; holds all config attributes
|
|
12
|
+
* - `carousel-viewport`: Visible window (required)
|
|
13
|
+
* - `carousel-container`: Slides flex track
|
|
14
|
+
* - `carousel-slide`: Individual slide
|
|
15
|
+
* - `carousel-prev` / `carousel-next`: Navigation buttons (optional)
|
|
16
|
+
* - `carousel-dots` / `carousel-dot`: Dot pagination container and template dot (optional)
|
|
17
|
+
* - `carousel-thumbs`: Linked thumb carousel container (optional)
|
|
18
|
+
*
|
|
19
|
+
* thumb navigation (on `data-slot="carousel-thumbs"`):
|
|
20
|
+
* - `data-carousel-thumbs-*`: thumb carousel options (defaults: containScroll keepSnaps, dragFree true)
|
|
21
|
+
* - Syncs with the main carousel in the same root
|
|
22
|
+
*
|
|
23
|
+
* Lifecycle and dialog start index:
|
|
24
|
+
* - `data-carousel-init`: Set by script when a carousel is initialized
|
|
25
|
+
* - `data-carousel-start`: On a trigger; slide index to open to (pairs with commandfor)
|
|
26
|
+
* - `data-carousel-start-index`: Set on root by script; consumed when dialog opens
|
|
27
|
+
* - `data-carousel-keyboard`: Set to `"false"` to disable ArrowLeft/ArrowRight navigation
|
|
28
|
+
*
|
|
29
|
+
* Configuration (on the carousel root: `<ui-carousel>` or `.ui-carousel`):
|
|
30
|
+
* - `data-carousel-*`: Core Embla options
|
|
31
|
+
* - `data-carousel-plugins`: Space-separated plugin slugs (`autoplay`, `auto-scroll`, `class-names`)
|
|
32
|
+
* - `data-carousel-autoplay-*`: Autoplay plugin options (requires `autoplay` in plugins)
|
|
33
|
+
* - `data-carousel-auto-scroll-*`: Auto scroll plugin options (requires `auto-scroll` in plugins)
|
|
34
|
+
* - `data-carousel-class-names-*`: Class names plugin options (requires `class-names` in plugins)
|
|
35
|
+
*
|
|
36
|
+
* @see https://www.embla-carousel.com/docs/api/options#reference
|
|
37
|
+
* @see https://www.embla-carousel.com/docs/plugins/autoplay#options
|
|
38
|
+
* @see https://www.embla-carousel.com/docs/plugins/auto-scroll#options
|
|
39
|
+
* @see https://www.embla-carousel.com/docs/plugins/class-names#options
|
|
40
|
+
* @see https://www.embla-carousel.com/docs/plugins/ssr#options
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* data-carousel-loop="true"
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* data-carousel-align="start"
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* data-carousel-plugins="autoplay" data-carousel-autoplay-delay="3000"
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* data-carousel-plugins="auto-scroll" data-carousel-auto-scroll-speed="2"
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* data-carousel-plugins="class-names" data-carousel-class-names-snapped="is-snapped"
|
|
56
|
+
*
|
|
57
|
+
* @example Barebones carousel (4 text slides; auto-inits on DOMContentLoaded):
|
|
58
|
+
* <div class="ui-carousel">
|
|
59
|
+
* <div data-slot="carousel-viewport">
|
|
60
|
+
* <div data-slot="carousel-container">
|
|
61
|
+
* <div data-slot="carousel-slide">Slide 1</div>
|
|
62
|
+
* <div data-slot="carousel-slide">Slide 2</div>
|
|
63
|
+
* <div data-slot="carousel-slide">Slide 3</div>
|
|
64
|
+
* <div data-slot="carousel-slide">Slide 4</div>
|
|
65
|
+
* </div>
|
|
66
|
+
* </div>
|
|
67
|
+
* <button type="button" data-slot="carousel-prev">Prev</button>
|
|
68
|
+
* <button type="button" data-slot="carousel-next">Next</button>
|
|
69
|
+
* </div>
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
import { Utils } from "./utils.ts";
|
|
73
|
+
import { registerRefresh } from "./zazz-element.ts";
|
|
74
|
+
|
|
75
|
+
// Embla ships as real ES modules: bare specifiers resolve through the page's
|
|
76
|
+
// import map in browsers (pinned jsDelivr URLs; see `head.ts`) and through
|
|
77
|
+
// node_modules in tests/bundlers. No more UMD globals, no tag-order contract.
|
|
78
|
+
import EmblaCarousel from "embla-carousel";
|
|
79
|
+
import type { EmblaOptionsType } from "embla-carousel";
|
|
80
|
+
import EmblaCarouselAutoplay from "embla-carousel-autoplay";
|
|
81
|
+
import type { AutoplayOptionsType } from "embla-carousel-autoplay";
|
|
82
|
+
import EmblaCarouselAutoScroll from "embla-carousel-auto-scroll";
|
|
83
|
+
import type { AutoScrollOptionsType } from "embla-carousel-auto-scroll";
|
|
84
|
+
import EmblaCarouselClassNames from "embla-carousel-class-names";
|
|
85
|
+
import type { ClassNamesOptionsType } from "embla-carousel-class-names";
|
|
86
|
+
|
|
87
|
+
// --- Configuration reading ---
|
|
88
|
+
|
|
89
|
+
/** Known Embla plugin slugs accepted by `data-carousel-plugins`. */
|
|
90
|
+
type CarouselPluginSlug = "autoplay" | "auto-scroll" | "class-names";
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @description Parses a space-separated `data-carousel-plugins` value into
|
|
94
|
+
* plugin slug tokens. Empty / null / whitespace-only values yield `[]`.
|
|
95
|
+
*
|
|
96
|
+
* @param value - Raw attribute value (or `null` when absent).
|
|
97
|
+
* @returns Plugin slug tokens in authoring order.
|
|
98
|
+
*/
|
|
99
|
+
function parseCarouselPlugins(value: string | null): string[] {
|
|
100
|
+
if (!value) return [];
|
|
101
|
+
return value.trim().split(/\s+/).filter(Boolean);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Map of plugin slug → Embla factory. Slugs match Embla package names
|
|
106
|
+
* (`embla-carousel-auto-scroll`, etc.).
|
|
107
|
+
*/
|
|
108
|
+
const PLUGIN_BY_SLUG: Record<CarouselPluginSlug, (node: Element) => EmblaPlugin> = {
|
|
109
|
+
autoplay: (node) =>
|
|
110
|
+
EmblaCarouselAutoplay(
|
|
111
|
+
readCarouselOptions<AutoplayOptionsType>(node, "data-carousel-autoplay-"),
|
|
112
|
+
),
|
|
113
|
+
"auto-scroll": (node) =>
|
|
114
|
+
EmblaCarouselAutoScroll(
|
|
115
|
+
readCarouselOptions<AutoScrollOptionsType>(node, "data-carousel-auto-scroll-"),
|
|
116
|
+
),
|
|
117
|
+
"class-names": (node) =>
|
|
118
|
+
EmblaCarouselClassNames(
|
|
119
|
+
readCarouselOptions<ClassNamesOptionsType>(node, "data-carousel-class-names-"),
|
|
120
|
+
),
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @description Reads a node's prefixed `data-carousel-*` attributes as an
|
|
125
|
+
* options object of the shape Embla (or one of its plugins) expects.
|
|
126
|
+
*
|
|
127
|
+
* Attributes are strings, so the parsed values have to be asserted into Embla's
|
|
128
|
+
* option types somewhere. This is that single boundary: the assertion lives
|
|
129
|
+
* here with its justification (Embla validates its own options at runtime)
|
|
130
|
+
* instead of being repeated at every call site.
|
|
131
|
+
*
|
|
132
|
+
* @param node - Element carrying the configuration attributes.
|
|
133
|
+
* @param prefix - Attribute prefix, e.g. `"data-carousel-autoplay-"`.
|
|
134
|
+
* @returns The parsed attributes, typed as `T`.
|
|
135
|
+
* @private
|
|
136
|
+
*/
|
|
137
|
+
function readCarouselOptions<T>(node: Element, prefix: string): T {
|
|
138
|
+
return Utils.parseDataAttributes(node, prefix) as T;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// --- Active-index sync ---
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @description Toggles `.is-active` on the node at `selected` and clears it
|
|
145
|
+
* from every other node: the shared "which one is current" marker used by
|
|
146
|
+
* both dot pagination and thumb navigation.
|
|
147
|
+
*
|
|
148
|
+
* @param nodes - Candidate nodes, in slide order.
|
|
149
|
+
* @param selected - The active index.
|
|
150
|
+
* @param options - Set `ariaCurrent` to also toggle `aria-current` (thumb
|
|
151
|
+
* navigation exposes the active thumb to assistive tech; dots don't need it,
|
|
152
|
+
* as their `.is-active` state is purely visual pagination).
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
155
|
+
function setActiveIndex(
|
|
156
|
+
nodes: readonly HTMLElement[],
|
|
157
|
+
selected: number,
|
|
158
|
+
options: { ariaCurrent?: boolean } = {},
|
|
159
|
+
): void {
|
|
160
|
+
nodes.forEach((node, idx) => {
|
|
161
|
+
const active = idx === selected;
|
|
162
|
+
node.classList.toggle("is-active", active);
|
|
163
|
+
if (!options.ariaCurrent) return;
|
|
164
|
+
if (active) {
|
|
165
|
+
node.setAttribute("aria-current", "true");
|
|
166
|
+
} else {
|
|
167
|
+
node.removeAttribute("aria-current");
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// --- Dot navigation ---
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @description Adds dot navigation buttons and click handlers for an Embla carousel.
|
|
176
|
+
*
|
|
177
|
+
* @param emblaApi - The Embla carousel API instance.
|
|
178
|
+
* @param dotsNode - Container element for dot navigation.
|
|
179
|
+
* @param signal - Aborts the dot click listeners on teardown.
|
|
180
|
+
* @returns Cleanup function to remove dots.
|
|
181
|
+
*/
|
|
182
|
+
const addDotBtnsAndClickHandlers = (
|
|
183
|
+
emblaApi: EmblaCarouselType,
|
|
184
|
+
dotsNode: Element,
|
|
185
|
+
signal?: AbortSignal,
|
|
186
|
+
): (() => void) | undefined => {
|
|
187
|
+
if (!dotsNode) return;
|
|
188
|
+
|
|
189
|
+
const templateDot = dotsNode.querySelector('[data-slot~="carousel-dot"]');
|
|
190
|
+
if (!templateDot) return;
|
|
191
|
+
|
|
192
|
+
let dotNodes: HTMLElement[] = [];
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @description Creates dot buttons for each slide and binds click handlers.
|
|
196
|
+
*
|
|
197
|
+
* @private
|
|
198
|
+
*/
|
|
199
|
+
const addDotBtnsWithClickHandlers = () => {
|
|
200
|
+
const snapCount = emblaApi.scrollSnapList().length;
|
|
201
|
+
|
|
202
|
+
dotsNode.innerHTML = "";
|
|
203
|
+
|
|
204
|
+
if (snapCount <= 1) {
|
|
205
|
+
dotNodes = [];
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
dotNodes = [];
|
|
210
|
+
for (let i = 0; i < snapCount; i++) {
|
|
211
|
+
const dot = templateDot.cloneNode(true) as HTMLElement;
|
|
212
|
+
dotNodes.push(dot);
|
|
213
|
+
dotsNode.appendChild(dot);
|
|
214
|
+
|
|
215
|
+
dot.addEventListener("click", () => emblaApi.scrollTo(i), { signal });
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @description Updates the active state of dot buttons based on the current slide.
|
|
221
|
+
*
|
|
222
|
+
* @private
|
|
223
|
+
*/
|
|
224
|
+
const toggleDotBtnsActive = () => {
|
|
225
|
+
if (!dotNodes.length) return;
|
|
226
|
+
setActiveIndex(dotNodes, emblaApi.selectedScrollSnap());
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
emblaApi
|
|
230
|
+
.on("init", addDotBtnsWithClickHandlers)
|
|
231
|
+
.on("reInit", addDotBtnsWithClickHandlers)
|
|
232
|
+
.on("init", toggleDotBtnsActive)
|
|
233
|
+
.on("reInit", toggleDotBtnsActive)
|
|
234
|
+
.on("select", toggleDotBtnsActive);
|
|
235
|
+
|
|
236
|
+
return () => {
|
|
237
|
+
dotsNode.innerHTML = "";
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// --- thumb navigation ---
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* @description Adds click handlers on thumb slides to scroll the main carousel.
|
|
245
|
+
*
|
|
246
|
+
* @param emblaApiMain - Main carousel API instance.
|
|
247
|
+
* @param emblaApiThumb - thumb carousel API instance.
|
|
248
|
+
* @param signal - Aborts the thumb click listeners on teardown.
|
|
249
|
+
*/
|
|
250
|
+
const addThumbClickHandlers = (
|
|
251
|
+
emblaApiMain: EmblaCarouselType,
|
|
252
|
+
emblaApiThumb: EmblaCarouselType,
|
|
253
|
+
signal?: AbortSignal,
|
|
254
|
+
) => {
|
|
255
|
+
const slidesthumbs = emblaApiThumb.slideNodes();
|
|
256
|
+
|
|
257
|
+
slidesthumbs.forEach((slideNode: HTMLElement, index: number) => {
|
|
258
|
+
slideNode.addEventListener("click", () => emblaApiMain.scrollTo(index), { signal });
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* @description Keeps the thumb carousel and active state in sync with the main carousel.
|
|
264
|
+
*
|
|
265
|
+
* @param emblaApiMain - Main carousel API instance.
|
|
266
|
+
* @param emblaApiThumb - thumb carousel API instance.
|
|
267
|
+
*/
|
|
268
|
+
const addTogglethumbsActive = (
|
|
269
|
+
emblaApiMain: EmblaCarouselType,
|
|
270
|
+
emblaApiThumb: EmblaCarouselType,
|
|
271
|
+
) => {
|
|
272
|
+
const slidesthumbs = emblaApiThumb.slideNodes();
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* @description Scrolls thumbs to the selected snap and toggles active classes.
|
|
276
|
+
*
|
|
277
|
+
* @private
|
|
278
|
+
*/
|
|
279
|
+
const toggleThumbBtnsActive = () => {
|
|
280
|
+
const selected = emblaApiMain.selectedScrollSnap();
|
|
281
|
+
emblaApiThumb.scrollTo(selected);
|
|
282
|
+
setActiveIndex(slidesthumbs, selected, { ariaCurrent: true });
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
emblaApiMain.on("select", toggleThumbBtnsActive);
|
|
286
|
+
toggleThumbBtnsActive();
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// --- Drag-aware click suppression ---
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Deadline (ms, `performance.now()`) until which invoker `command` events are
|
|
293
|
+
* cancelled because a carousel drag just ended. Set by bindDragClickSuppression
|
|
294
|
+
* for command-bearing slides (the lightbox stage) and enforced by
|
|
295
|
+
* initCommandDragGuard. A bare `click` guard is not enough: the native /
|
|
296
|
+
* polyfilled invoker opens the dialog from its own `command` dispatch.
|
|
297
|
+
*/
|
|
298
|
+
let suppressCommandsUntil = 0;
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @description Suppresses click activation immediately after a drag gesture.
|
|
302
|
+
*
|
|
303
|
+
* This prevents click-style actions (invoker commands, thumb navigation clicks)
|
|
304
|
+
* from firing when the intent was dragging the carousel.
|
|
305
|
+
*
|
|
306
|
+
* @param root - Embla root/subtree to monitor.
|
|
307
|
+
* @param emblaApi - Carousel instance tied to this root.
|
|
308
|
+
* @param clickSelector - Click targets to suppress after dragging.
|
|
309
|
+
* @param options - Per-carousel drag tolerance.
|
|
310
|
+
* @param signal - Aborts the pointer/click listeners on teardown.
|
|
311
|
+
*/
|
|
312
|
+
function bindDragClickSuppression(
|
|
313
|
+
root: Element,
|
|
314
|
+
emblaApi: EmblaCarouselType,
|
|
315
|
+
clickSelector: string,
|
|
316
|
+
options: { dragThresholdPx?: number } = {},
|
|
317
|
+
signal?: AbortSignal,
|
|
318
|
+
): void {
|
|
319
|
+
const DRAG_THRESHOLD_PX = options.dragThresholdPx ?? 6;
|
|
320
|
+
const SUPPRESS_WINDOW_MS = 250;
|
|
321
|
+
// Slides that open a dialog (command="show-modal") also need command suppression.
|
|
322
|
+
const guardsCommands = clickSelector.includes("commandfor");
|
|
323
|
+
|
|
324
|
+
let pointerDown = false;
|
|
325
|
+
let moved = false;
|
|
326
|
+
let startX = 0;
|
|
327
|
+
let startY = 0;
|
|
328
|
+
let lastX = 0;
|
|
329
|
+
let lastY = 0;
|
|
330
|
+
let snapAtDown = 0;
|
|
331
|
+
let suppressUntil = 0;
|
|
332
|
+
|
|
333
|
+
const pointerDistance = (): number => Math.hypot(lastX - startX, lastY - startY);
|
|
334
|
+
|
|
335
|
+
root.addEventListener(
|
|
336
|
+
"pointerdown",
|
|
337
|
+
(e) => {
|
|
338
|
+
if (!(e instanceof PointerEvent)) return;
|
|
339
|
+
if (e.pointerType === "mouse" && e.button !== 0) return;
|
|
340
|
+
|
|
341
|
+
pointerDown = true;
|
|
342
|
+
moved = false;
|
|
343
|
+
startX = e.clientX;
|
|
344
|
+
startY = e.clientY;
|
|
345
|
+
lastX = e.clientX;
|
|
346
|
+
lastY = e.clientY;
|
|
347
|
+
snapAtDown = emblaApi.selectedScrollSnap();
|
|
348
|
+
},
|
|
349
|
+
{ passive: true, signal },
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
root.addEventListener(
|
|
353
|
+
"pointermove",
|
|
354
|
+
(e) => {
|
|
355
|
+
if (!pointerDown || !(e instanceof PointerEvent)) return;
|
|
356
|
+
lastX = e.clientX;
|
|
357
|
+
lastY = e.clientY;
|
|
358
|
+
if (moved) return;
|
|
359
|
+
|
|
360
|
+
if (pointerDistance() >= DRAG_THRESHOLD_PX) moved = true;
|
|
361
|
+
},
|
|
362
|
+
{ passive: true, signal },
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
const finalizePointer = () => {
|
|
366
|
+
if (pointerDown && moved) {
|
|
367
|
+
suppressUntil = performance.now() + SUPPRESS_WINDOW_MS;
|
|
368
|
+
if (guardsCommands) suppressCommandsUntil = suppressUntil;
|
|
369
|
+
}
|
|
370
|
+
pointerDown = false;
|
|
371
|
+
moved = false;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
root.addEventListener("pointerup", finalizePointer, { passive: true, signal });
|
|
375
|
+
root.addEventListener("pointercancel", finalizePointer, { passive: true, signal });
|
|
376
|
+
emblaApi.on("pointerUp", finalizePointer);
|
|
377
|
+
|
|
378
|
+
// Touch/trackpad drags can move with tiny deltas. Treat Embla scroll as drag intent,
|
|
379
|
+
// but lightbox openers need extra tolerance so minor carousel settle does not block clicks.
|
|
380
|
+
emblaApi.on("scroll", () => {
|
|
381
|
+
if (!pointerDown) return;
|
|
382
|
+
|
|
383
|
+
if (guardsCommands) {
|
|
384
|
+
if (pointerDistance() >= DRAG_THRESHOLD_PX || emblaApi.selectedScrollSnap() !== snapAtDown) {
|
|
385
|
+
moved = true;
|
|
386
|
+
}
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
moved = true;
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
root.addEventListener(
|
|
394
|
+
"click",
|
|
395
|
+
(e) => {
|
|
396
|
+
if (performance.now() > suppressUntil) return;
|
|
397
|
+
|
|
398
|
+
const target = e.target instanceof Element ? e.target.closest(clickSelector) : null;
|
|
399
|
+
if (!target || !root.contains(target)) return;
|
|
400
|
+
|
|
401
|
+
e.preventDefault();
|
|
402
|
+
e.stopImmediatePropagation();
|
|
403
|
+
},
|
|
404
|
+
{ capture: true, signal },
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
let commandDragGuardBound = false;
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* @description Cancels invoker `command` events fired right after a carousel drag.
|
|
412
|
+
*
|
|
413
|
+
* Lightbox stage slides carry `command="show-modal"`, so a drag would both scroll
|
|
414
|
+
* the carousel and open the dialog. The `command` event (native or polyfill) is
|
|
415
|
+
* the single point that runs the built-in command, and it is cancelable, so we
|
|
416
|
+
* preventDefault() it within the drag-suppress window. Bound once on `document` in
|
|
417
|
+
* the capture phase; `command` does not bubble, but capture still reaches it.
|
|
418
|
+
*/
|
|
419
|
+
function initCommandDragGuard(): void {
|
|
420
|
+
if (commandDragGuardBound) return;
|
|
421
|
+
commandDragGuardBound = true;
|
|
422
|
+
|
|
423
|
+
document.addEventListener(
|
|
424
|
+
"command",
|
|
425
|
+
(e) => {
|
|
426
|
+
if (e.cancelable && performance.now() <= suppressCommandsUntil) e.preventDefault();
|
|
427
|
+
},
|
|
428
|
+
true,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// --- Carousel initialization ---
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* @description Initializes a single Embla carousel root.
|
|
436
|
+
*
|
|
437
|
+
* Configures the carousel from its `data-carousel-*` attributes, wires navigation
|
|
438
|
+
* (prev/next, dots, thumbs), and stores the API on `root._emblaApi`. Idempotent:
|
|
439
|
+
* skips roots that are already initialized or inside a closed dialog (no
|
|
440
|
+
* measurable viewport until open).
|
|
441
|
+
*
|
|
442
|
+
* Called by `initEmblaCarousels()` for class-form (`.ui-carousel`) markup and by
|
|
443
|
+
* the `<ui-carousel>` web component (zazz/scripts/carousel.js) on connect.
|
|
444
|
+
*
|
|
445
|
+
* @param emblaNode - The carousel root element.
|
|
446
|
+
*/
|
|
447
|
+
function initEmblaRoot(emblaNode: Element): void {
|
|
448
|
+
if (emblaNode.hasAttribute("data-carousel-init")) return;
|
|
449
|
+
|
|
450
|
+
// Defer init inside closed dialogs: viewport has no measurable size until open
|
|
451
|
+
if (emblaNode.closest("dialog:not([open])")) return;
|
|
452
|
+
|
|
453
|
+
emblaNode.setAttribute("data-carousel-init", "");
|
|
454
|
+
|
|
455
|
+
const emblathumbsNode = emblaNode.querySelector('[data-slot~="carousel-thumbs"]');
|
|
456
|
+
const emblaViewportNode = emblathumbsNode
|
|
457
|
+
? emblaNode.querySelector(
|
|
458
|
+
'[data-slot~="carousel-viewport"]:not([data-slot~="carousel-thumbs"] *)',
|
|
459
|
+
)
|
|
460
|
+
: emblaNode.querySelector('[data-slot~="carousel-viewport"]');
|
|
461
|
+
const emblaPrevButtonNode = emblaNode.querySelector('[data-slot~="carousel-prev"]');
|
|
462
|
+
const emblaNextButtonNode = emblaNode.querySelector('[data-slot~="carousel-next"]');
|
|
463
|
+
const emblaDotsNode = emblaNode.querySelector('[data-slot~="carousel-dots"]');
|
|
464
|
+
|
|
465
|
+
if (!emblaViewportNode) return;
|
|
466
|
+
|
|
467
|
+
if (!emblaViewportNode.hasAttribute("tabindex")) {
|
|
468
|
+
emblaViewportNode.setAttribute("tabindex", "0");
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const apiOptions = readCarouselOptions<Record<string, unknown>>(emblaNode, "data-carousel-");
|
|
472
|
+
|
|
473
|
+
// Keep kit-only / plugin keys out of core Embla options
|
|
474
|
+
Object.keys(apiOptions).forEach(function (key) {
|
|
475
|
+
if (
|
|
476
|
+
key === "name" ||
|
|
477
|
+
key === "keyboard" ||
|
|
478
|
+
key === "plugins" ||
|
|
479
|
+
key.startsWith("autoplay") ||
|
|
480
|
+
key.startsWith("autoScroll") ||
|
|
481
|
+
key.startsWith("classNames")
|
|
482
|
+
) {
|
|
483
|
+
delete apiOptions[key];
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
// Veto drag gestures when every slide already fits in the viewport: with a
|
|
488
|
+
// single snap Embla still rubber-bands on drag, which feels broken. The
|
|
489
|
+
// callback re-evaluates on every pointer down, so it stays correct across
|
|
490
|
+
// resizes/reInit. Respect an explicit data-carousel-watch-drag override.
|
|
491
|
+
// @see https://github.com/davidjerleke/embla-carousel/issues/416
|
|
492
|
+
if (!("watchDrag" in apiOptions)) {
|
|
493
|
+
apiOptions.watchDrag = (api: EmblaCarouselType) => api.canScrollPrev() || api.canScrollNext();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Plugins load only when listed in data-carousel-plugins; option attrs alone
|
|
497
|
+
// do not enable them. Unknown slugs are ignored.
|
|
498
|
+
const plugins: EmblaPlugin[] = [];
|
|
499
|
+
for (const slug of parseCarouselPlugins(emblaNode.getAttribute("data-carousel-plugins"))) {
|
|
500
|
+
const create = PLUGIN_BY_SLUG[slug as CarouselPluginSlug];
|
|
501
|
+
if (!create) continue;
|
|
502
|
+
plugins.push(create(emblaNode));
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// The options come from untyped data attributes; Embla validates at runtime.
|
|
506
|
+
const emblaApi = EmblaCarousel(
|
|
507
|
+
emblaViewportNode as HTMLElement,
|
|
508
|
+
apiOptions as EmblaOptionsType,
|
|
509
|
+
plugins,
|
|
510
|
+
);
|
|
511
|
+
|
|
512
|
+
emblaNode._emblaApi = emblaApi;
|
|
513
|
+
|
|
514
|
+
// One controller per root scopes every carousel listener below; the owner
|
|
515
|
+
// (`<ui-carousel>` disconnectedCallback) aborts it on teardown so the DOM
|
|
516
|
+
// listeners are removed alongside the destroyed Embla instance.
|
|
517
|
+
const controller = new AbortController();
|
|
518
|
+
const { signal } = controller;
|
|
519
|
+
emblaNode._emblaController = controller;
|
|
520
|
+
|
|
521
|
+
if (emblaPrevButtonNode) {
|
|
522
|
+
emblaPrevButtonNode.addEventListener("click", () => emblaApi.scrollPrev(), { signal });
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (emblaNextButtonNode) {
|
|
526
|
+
emblaNextButtonNode.addEventListener("click", () => emblaApi.scrollNext(), { signal });
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
if (emblaDotsNode) {
|
|
530
|
+
addDotBtnsAndClickHandlers(emblaApi, emblaDotsNode, signal);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// Any command-bearing slide (e.g. a lightbox stage slide that opens the
|
|
534
|
+
// dialog) needs its click suppressed when it was really a drag.
|
|
535
|
+
if (emblaNode.querySelector('[data-slot~="carousel-slide"][commandfor]')) {
|
|
536
|
+
bindDragClickSuppression(
|
|
537
|
+
emblaNode,
|
|
538
|
+
emblaApi,
|
|
539
|
+
'[data-slot~="carousel-slide"][commandfor]',
|
|
540
|
+
{ dragThresholdPx: 14 },
|
|
541
|
+
signal,
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (emblathumbsNode) {
|
|
546
|
+
const emblathumbsViewportNode = emblathumbsNode.querySelector(
|
|
547
|
+
'[data-slot~="carousel-viewport"]',
|
|
548
|
+
);
|
|
549
|
+
if (emblathumbsViewportNode) {
|
|
550
|
+
const thumbDefaults: EmblaOptionsType = { containScroll: "keepSnaps", dragFree: true };
|
|
551
|
+
const thumbOptions = readCarouselOptions<EmblaOptionsType>(
|
|
552
|
+
emblathumbsNode,
|
|
553
|
+
"data-carousel-thumbs-",
|
|
554
|
+
);
|
|
555
|
+
const emblaApiThumb = EmblaCarousel(emblathumbsViewportNode as HTMLElement, {
|
|
556
|
+
...thumbDefaults,
|
|
557
|
+
...thumbOptions,
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
emblaNode._emblaApiThumb = emblaApiThumb;
|
|
561
|
+
addThumbClickHandlers(emblaApi, emblaApiThumb, signal);
|
|
562
|
+
addTogglethumbsActive(emblaApi, emblaApiThumb);
|
|
563
|
+
|
|
564
|
+
bindDragClickSuppression(
|
|
565
|
+
emblathumbsNode,
|
|
566
|
+
emblaApiThumb,
|
|
567
|
+
'[data-slot~="carousel-slide"]',
|
|
568
|
+
{},
|
|
569
|
+
signal,
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* @description Initializes all Embla carousels within a scope.
|
|
577
|
+
*
|
|
578
|
+
* Discovers carousel elements via `:is(ui-carousel, .ui-carousel)` and configures them
|
|
579
|
+
* based on their data attributes. Roots managed by the `<ui-carousel>` web
|
|
580
|
+
* component are skipped: they initialize themselves via `connectedCallback()`.
|
|
581
|
+
*
|
|
582
|
+
* @param scope - Root element to search within. Defaults to `document`.
|
|
583
|
+
*/
|
|
584
|
+
function initEmblaCarousels(scope?: Document | Element): void {
|
|
585
|
+
const root = scope || document;
|
|
586
|
+
const emblaRoots = root.querySelectorAll(":is(ui-carousel, .ui-carousel)");
|
|
587
|
+
|
|
588
|
+
emblaRoots.forEach(function (emblaNode) {
|
|
589
|
+
// <ui-carousel> elements own their lifecycle (init on connect, destroy
|
|
590
|
+
// on disconnect); double-initializing would leak a second Embla instance.
|
|
591
|
+
if (emblaNode.closest("ui-carousel")) return;
|
|
592
|
+
|
|
593
|
+
initEmblaRoot(emblaNode);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// --- Dialog open subscription ---
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* @description Reacts to dialogs opening (via `zazz:dialog-open` from
|
|
601
|
+
* base/dialog-lifecycle.ts; ADR-0003) with the carousel-domain work:
|
|
602
|
+
* initializes class-form roots that deferred while the dialog was
|
|
603
|
+
* `display: none`, applies a stored `data-carousel-start-index`, and focuses
|
|
604
|
+
* the viewport for keyboard navigation.
|
|
605
|
+
*
|
|
606
|
+
* Listens on `document`, so it runs after any dialog- or ancestor-level
|
|
607
|
+
* subscriber (`<ui-carousel>` self-init, `<ui-lightbox>` choreography).
|
|
608
|
+
*/
|
|
609
|
+
function initDialogOpenSubscription(): void {
|
|
610
|
+
document.addEventListener("zazz:dialog-open", function (e) {
|
|
611
|
+
if (!(e.target instanceof HTMLDialogElement)) return;
|
|
612
|
+
const dialog = e.target;
|
|
613
|
+
initEmblaCarousels(dialog);
|
|
614
|
+
|
|
615
|
+
const roots = dialog.querySelectorAll(":is(ui-carousel, .ui-carousel)");
|
|
616
|
+
roots.forEach(function (root) {
|
|
617
|
+
const startIndex = root.getAttribute("data-carousel-start-index");
|
|
618
|
+
if (startIndex != null && root._emblaApi) {
|
|
619
|
+
root._emblaApi.scrollTo(Number(startIndex), true);
|
|
620
|
+
root.removeAttribute("data-carousel-start-index");
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
const viewport = root.querySelector('[data-slot~="carousel-viewport"]');
|
|
624
|
+
if (viewport instanceof HTMLElement) {
|
|
625
|
+
viewport.focus({ preventScroll: true });
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// --- Keyboard navigation ---
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* @description Returns the active Embla root for keyboard navigation.
|
|
635
|
+
*
|
|
636
|
+
* Prefers a carousel inside an open dialog, then the carousel containing focus.
|
|
637
|
+
* Respects `data-carousel-keyboard="false"`.
|
|
638
|
+
*
|
|
639
|
+
* @returns The active carousel root, or null when none applies.
|
|
640
|
+
* @private
|
|
641
|
+
*/
|
|
642
|
+
function getActiveEmblaRoot(): (Element & { _emblaApi: EmblaCarouselType }) | null {
|
|
643
|
+
const openDialog = document.querySelector("dialog[open]");
|
|
644
|
+
if (openDialog) {
|
|
645
|
+
const dialogRoot = openDialog.querySelector(
|
|
646
|
+
":is(ui-carousel, .ui-carousel)[data-carousel-init]",
|
|
647
|
+
);
|
|
648
|
+
if (dialogRoot?._emblaApi && dialogRoot.getAttribute("data-carousel-keyboard") !== "false") {
|
|
649
|
+
return dialogRoot as Element & { _emblaApi: EmblaCarouselType };
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const focusedRoot = document.activeElement?.closest(
|
|
654
|
+
":is(ui-carousel, .ui-carousel)[data-carousel-init]",
|
|
655
|
+
);
|
|
656
|
+
if (focusedRoot?._emblaApi && focusedRoot.getAttribute("data-carousel-keyboard") !== "false") {
|
|
657
|
+
return focusedRoot as Element & { _emblaApi: EmblaCarouselType };
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return null;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* @description Binds ArrowLeft/ArrowRight keyboard navigation for active carousels.
|
|
665
|
+
*
|
|
666
|
+
* Embla is headless: arrow keys are not built in. Opt out per carousel with
|
|
667
|
+
* `data-carousel-keyboard="false"`.
|
|
668
|
+
*/
|
|
669
|
+
const initEmblaKeyboardNav: InitEmblaKeyboardNavFn = function () {
|
|
670
|
+
if (initEmblaKeyboardNav._bound) return;
|
|
671
|
+
initEmblaKeyboardNav._bound = true;
|
|
672
|
+
|
|
673
|
+
document.addEventListener("keydown", function (e) {
|
|
674
|
+
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
|
|
675
|
+
if (e.defaultPrevented) return;
|
|
676
|
+
|
|
677
|
+
const target = e.target;
|
|
678
|
+
if (!(target instanceof HTMLElement)) return;
|
|
679
|
+
|
|
680
|
+
if (
|
|
681
|
+
target instanceof HTMLInputElement ||
|
|
682
|
+
target instanceof HTMLTextAreaElement ||
|
|
683
|
+
target instanceof HTMLSelectElement ||
|
|
684
|
+
target.isContentEditable
|
|
685
|
+
) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
const root = getActiveEmblaRoot();
|
|
690
|
+
if (!root) return;
|
|
691
|
+
|
|
692
|
+
e.preventDefault();
|
|
693
|
+
if (e.key === "ArrowLeft") root._emblaApi.scrollPrev();
|
|
694
|
+
else root._emblaApi.scrollNext();
|
|
695
|
+
});
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* @namespace EmblaInit
|
|
700
|
+
* @description Public API for Embla carousel initialization and helpers.
|
|
701
|
+
*
|
|
702
|
+
* @property init - Initializes all carousels within a scope.
|
|
703
|
+
* @property initRoot - Initializes a single carousel root (used by `<ui-carousel>`).
|
|
704
|
+
* @property addDotBtnsAndClickHandlers - Wires dot pagination.
|
|
705
|
+
* @property addThumbClickHandlers - Wires thumb click handlers.
|
|
706
|
+
* @property addTogglethumbsActive - Syncs thumb active state.
|
|
707
|
+
*/
|
|
708
|
+
const EmblaInit = {
|
|
709
|
+
init: initEmblaCarousels,
|
|
710
|
+
initRoot: initEmblaRoot,
|
|
711
|
+
addDotBtnsAndClickHandlers,
|
|
712
|
+
addThumbClickHandlers,
|
|
713
|
+
addTogglethumbsActive,
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
// --- Start index control ---
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* @description Stores or applies a start slide index from `[data-carousel-start]` triggers.
|
|
720
|
+
*
|
|
721
|
+
* Clicking an element with `data-carousel-start="N"` stores that index on the target
|
|
722
|
+
* carousel (found via `commandfor` → dialog → `:is(ui-carousel, .ui-carousel)`). The dialog
|
|
723
|
+
* open observer scrolls to it on open.
|
|
724
|
+
*/
|
|
725
|
+
function initEmblaStartLinks(): void {
|
|
726
|
+
document.addEventListener("click", function (e) {
|
|
727
|
+
if (!(e.target instanceof HTMLElement)) return;
|
|
728
|
+
|
|
729
|
+
const trigger = e.target.closest(
|
|
730
|
+
"[data-carousel-start], [data-slot~='carousel-slide'][commandfor]",
|
|
731
|
+
);
|
|
732
|
+
if (!trigger) return;
|
|
733
|
+
|
|
734
|
+
let index = trigger.getAttribute("data-carousel-start");
|
|
735
|
+
if (index == null && trigger.hasAttribute("commandfor")) {
|
|
736
|
+
const emblaRoot = trigger.closest(":is(ui-carousel, .ui-carousel)");
|
|
737
|
+
if (emblaRoot?._emblaApi) {
|
|
738
|
+
index = String(emblaRoot._emblaApi.selectedScrollSnap());
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
const dialogId = trigger.getAttribute("commandfor");
|
|
743
|
+
if (!dialogId) return;
|
|
744
|
+
|
|
745
|
+
const dialog = document.getElementById(dialogId);
|
|
746
|
+
if (!dialog) return;
|
|
747
|
+
|
|
748
|
+
const root = dialog.querySelector(":is(ui-carousel, .ui-carousel)");
|
|
749
|
+
if (!root) return;
|
|
750
|
+
|
|
751
|
+
if (root._emblaApi) {
|
|
752
|
+
root._emblaApi.scrollTo(Number(index), true);
|
|
753
|
+
} else if (index != null) {
|
|
754
|
+
root.setAttribute("data-carousel-start-index", index);
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// Auto-initialize when DOM is ready (only in browser environment)
|
|
760
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
761
|
+
// After a SPA <main> swap, initialize class-form carousels in the new content
|
|
762
|
+
// (<ui-carousel> elements ride the swap via their own lifecycle; init() skips
|
|
763
|
+
// already-initialized roots and closed dialogs).
|
|
764
|
+
registerRefresh((scope) => initEmblaCarousels(scope));
|
|
765
|
+
|
|
766
|
+
if (document.readyState === "loading") {
|
|
767
|
+
document.addEventListener("DOMContentLoaded", function () {
|
|
768
|
+
initEmblaCarousels();
|
|
769
|
+
initDialogOpenSubscription();
|
|
770
|
+
initEmblaStartLinks();
|
|
771
|
+
initEmblaKeyboardNav();
|
|
772
|
+
initCommandDragGuard();
|
|
773
|
+
});
|
|
774
|
+
} else {
|
|
775
|
+
initEmblaCarousels();
|
|
776
|
+
initDialogOpenSubscription();
|
|
777
|
+
initEmblaStartLinks();
|
|
778
|
+
initEmblaKeyboardNav();
|
|
779
|
+
initCommandDragGuard();
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// Attach to window for the documented public API (`window.EmblaInit`), and export
|
|
784
|
+
// for module consumers (carousel.js / navigation.js import it via the main.js bundle).
|
|
785
|
+
if (typeof window !== "undefined") {
|
|
786
|
+
window.EmblaInit = EmblaInit;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// setActiveIndex and parseCarouselPlugins are exported for unit tests only;
|
|
790
|
+
// not part of the public API.
|
|
791
|
+
export { EmblaInit, setActiveIndex, parseCarouselPlugins };
|