@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,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* _view-transitions.css — Cross-document view transitions
|
|
3
|
+
*
|
|
4
|
+
* @layer variables, utilities
|
|
5
|
+
* @requires layers.css, _variables.css, _reset.css ([data-transition-layer] names)
|
|
6
|
+
* @uses ::view-transition-old/new — cross-document page transitions
|
|
7
|
+
* @uses @view-transition { navigation: auto } — enabled here, behind @supports
|
|
8
|
+
* @uses ::view-transition-group(toaster) — frozen group; excludes the toast
|
|
9
|
+
* stack (named in _toaster.css) from transition repaints
|
|
10
|
+
* @tokens --view-transition-* (@layer variables)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
@layer variables {
|
|
14
|
+
@supports at-rule(@view-transition) and (navigation: auto) {
|
|
15
|
+
@view-transition {
|
|
16
|
+
navigation: auto;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
:root {
|
|
21
|
+
--view-transition-duration: 0ms;
|
|
22
|
+
--view-transition-ease: var(--bezier-easing);
|
|
23
|
+
--view-transition-wait: 0ms;
|
|
24
|
+
--view-transition-origin: center top;
|
|
25
|
+
--view-transition-scale--new: 1;
|
|
26
|
+
--view-transition-scale--old: 1;
|
|
27
|
+
--view-transition-translate--new: 0, 0, 0;
|
|
28
|
+
--view-transition-translate--old: 0, 0, 0;
|
|
29
|
+
--view-transition-opacity--new: 0;
|
|
30
|
+
--view-transition-opacity--old: 0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@layer zazz.utilities {
|
|
35
|
+
/* View transitions — main is isolated from header/footer (see _reset.css view-transition-name) */
|
|
36
|
+
@media screen and (prefers-reduced-motion: no-preference) {
|
|
37
|
+
::view-transition-old(main-content) {
|
|
38
|
+
animation: global-view-transition--old var(--view-transition-duration)
|
|
39
|
+
var(--view-transition-ease) both;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
::view-transition-new(main-content) {
|
|
43
|
+
animation: global-view-transition--new var(--view-transition-duration)
|
|
44
|
+
var(--view-transition-ease) both;
|
|
45
|
+
animation-delay: var(--view-transition-wait);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* Persist shared chrome without cross-fading */
|
|
49
|
+
::view-transition-old(global-header),
|
|
50
|
+
::view-transition-new(global-footer) {
|
|
51
|
+
z-index: 2;
|
|
52
|
+
animation: none;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* Stacking: keep header/footer snapshots above main so a taller main snapshot
|
|
57
|
+
(when page heights differ) can't paint over the chrome during the crossfade.
|
|
58
|
+
Group z-index applies regardless of motion preference. */
|
|
59
|
+
::view-transition-group(main-content) {
|
|
60
|
+
z-index: 1;
|
|
61
|
+
overflow: clip;
|
|
62
|
+
transform-origin: var(--view-transition-origin);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* Toasts are ephemeral chrome, not page content. The toaster region names its
|
|
66
|
+
own snapshot group (view-transition-name: toaster in _toaster.css); pin it
|
|
67
|
+
above everything and never animate it, so same-document swaps and
|
|
68
|
+
cross-document navigations can't cross-fade or repaint an active stack.
|
|
69
|
+
Applies regardless of motion preference. */
|
|
70
|
+
::view-transition-group(toaster) {
|
|
71
|
+
z-index: 3;
|
|
72
|
+
animation: none;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
::view-transition-old(toaster),
|
|
76
|
+
::view-transition-new(toaster) {
|
|
77
|
+
animation: none;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@keyframes global-view-transition--old {
|
|
81
|
+
to {
|
|
82
|
+
opacity: var(--view-transition-opacity--new);
|
|
83
|
+
transform: scale(var(--view-transition-scale--old))
|
|
84
|
+
translate3d(var(--view-transition-translate--old));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@keyframes global-view-transition--new {
|
|
89
|
+
from {
|
|
90
|
+
opacity: var(--view-transition-opacity--new);
|
|
91
|
+
transform: scale(var(--view-transition-scale--new))
|
|
92
|
+
translate3d(var(--view-transition-translate--new));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@media (prefers-reduced-motion: reduce) {
|
|
97
|
+
::view-transition-group(*),
|
|
98
|
+
::view-transition-old(*),
|
|
99
|
+
::view-transition-new(*) {
|
|
100
|
+
animation: none !important;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Scores how well `query` matches `target`, optionally widening
|
|
3
|
+
* the candidate with alias strings (extra keywords that should also match).
|
|
4
|
+
*
|
|
5
|
+
* @param target - The candidate string to score against.
|
|
6
|
+
* @param query - What the user typed.
|
|
7
|
+
* @param aliases - Extra match targets appended to the candidate.
|
|
8
|
+
* @returns 0 (no match) to 1 (perfect continuous match).
|
|
9
|
+
*/
|
|
10
|
+
declare function commandScore(target: string, query: string, aliases?: readonly string[]): number;
|
|
11
|
+
export { commandScore };
|
|
12
|
+
//# sourceMappingURL=command-score.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-score.d.ts","sourceRoot":"","sources":["command-score.ts"],"names":[],"mappings":"AAiLA;;;;;;;;GAQG;AACH,iBAAS,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,SAAS,MAAM,EAAO,GAAG,MAAM,CAG5F;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Fuzzy command-scoring for the typeahead family.
|
|
4
|
+
* @description Rates how well a search query matches a candidate string,
|
|
5
|
+
* returning 0 (no match) to 1 (perfect continuous match). Continuous runs and
|
|
6
|
+
* word-boundary jumps score high; scattered character jumps, transpositions,
|
|
7
|
+
* case mismatches, and skipped characters decay the score. Autocomplete,
|
|
8
|
+
* combobox, and command all rank their items with this single function.
|
|
9
|
+
*
|
|
10
|
+
* Vendored from cmdk's `command-score.ts` by @pacocoursey (MIT), itself
|
|
11
|
+
* adapted from Superhuman's `command-score` (MIT), which builds on Joshaven
|
|
12
|
+
* Potter's `string_score`. The constants and recursion are kept verbatim so
|
|
13
|
+
* results match cmdk's ranking; only the module shape is Zazz's.
|
|
14
|
+
*
|
|
15
|
+
* @see https://github.com/pacocoursey/cmdk
|
|
16
|
+
* @see https://github.com/superhuman/command-score
|
|
17
|
+
*/
|
|
18
|
+
// --- Scoring constants ---
|
|
19
|
+
// The scores are arranged so that a continuous match of characters will
|
|
20
|
+
// result in a total score of 1.
|
|
21
|
+
//
|
|
22
|
+
// The best case: this character is a match, and either this is the start of
|
|
23
|
+
// the string or the previous character was also a match.
|
|
24
|
+
const SCORE_CONTINUE_MATCH = 1;
|
|
25
|
+
// A new match at the start of a word scores better than a new match
|
|
26
|
+
// elsewhere, as it's more likely that the user will type the starts of
|
|
27
|
+
// fragments. Word jumps between spaces score slightly higher than slashes,
|
|
28
|
+
// brackets, hyphens, etc.
|
|
29
|
+
const SCORE_SPACE_WORD_JUMP = 0.9;
|
|
30
|
+
const SCORE_NON_SPACE_WORD_JUMP = 0.8;
|
|
31
|
+
// Any other match isn't ideal, but is included for completeness.
|
|
32
|
+
const SCORE_CHARACTER_JUMP = 0.17;
|
|
33
|
+
// If the user transposed two letters, it should be significantly penalized:
|
|
34
|
+
// "ouch" is more likely than "curtain" when "uc" is typed.
|
|
35
|
+
const SCORE_TRANSPOSITION = 0.1;
|
|
36
|
+
// The goodness of a match decays slightly with each skipped character:
|
|
37
|
+
// "bad" is more likely than "bard" when "bd" is typed.
|
|
38
|
+
const PENALTY_SKIPPED = 0.999;
|
|
39
|
+
// An exact-case match beats a case-insensitive match by a small amount:
|
|
40
|
+
// "HTML" is more likely than "haml" when "HM" is typed.
|
|
41
|
+
const PENALTY_CASE_MISMATCH = 0.9999;
|
|
42
|
+
// If the candidate has more characters than the user typed, penalize
|
|
43
|
+
// slightly: "html" is more likely than "html5" when "html" is typed.
|
|
44
|
+
const PENALTY_NOT_COMPLETE = 0.99;
|
|
45
|
+
const IS_GAP_REGEXP = /[\\/_+.#"@[({&]/;
|
|
46
|
+
const COUNT_GAPS_REGEXP = /[\\/_+.#"@[({&]/g;
|
|
47
|
+
const IS_SPACE_REGEXP = /[\s-]/;
|
|
48
|
+
const COUNT_SPACE_REGEXP = /[\s-]/g;
|
|
49
|
+
// --- Scoring ---
|
|
50
|
+
/**
|
|
51
|
+
* @description Recursive scorer over (candidate index, query index) pairs,
|
|
52
|
+
* memoized per pair so repeated subproblems resolve in constant time.
|
|
53
|
+
*
|
|
54
|
+
* @param target - The candidate string (original casing).
|
|
55
|
+
* @param query - The query string (original casing).
|
|
56
|
+
* @param lowerTarget - Pre-lowercased candidate.
|
|
57
|
+
* @param lowerQuery - Pre-lowercased query.
|
|
58
|
+
* @param targetIndex - Current position in the candidate.
|
|
59
|
+
* @param queryIndex - Current position in the query.
|
|
60
|
+
* @param memo - Shared memoization table for this scoring run.
|
|
61
|
+
* @returns The best score reachable from this position.
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
function commandScoreInner(target, query, lowerTarget, lowerQuery, targetIndex, queryIndex, memo) {
|
|
65
|
+
if (queryIndex === query.length) {
|
|
66
|
+
if (targetIndex === target.length) {
|
|
67
|
+
return SCORE_CONTINUE_MATCH;
|
|
68
|
+
}
|
|
69
|
+
return PENALTY_NOT_COMPLETE;
|
|
70
|
+
}
|
|
71
|
+
const memoKey = `${targetIndex},${queryIndex}`;
|
|
72
|
+
const memoized = memo[memoKey];
|
|
73
|
+
if (memoized !== undefined) {
|
|
74
|
+
return memoized;
|
|
75
|
+
}
|
|
76
|
+
const queryChar = lowerQuery.charAt(queryIndex);
|
|
77
|
+
let index = lowerTarget.indexOf(queryChar, targetIndex);
|
|
78
|
+
let highScore = 0;
|
|
79
|
+
while (index >= 0) {
|
|
80
|
+
let score = commandScoreInner(target, query, lowerTarget, lowerQuery, index + 1, queryIndex + 1, memo);
|
|
81
|
+
if (score > highScore) {
|
|
82
|
+
if (index === targetIndex) {
|
|
83
|
+
score *= SCORE_CONTINUE_MATCH;
|
|
84
|
+
}
|
|
85
|
+
else if (IS_GAP_REGEXP.test(target.charAt(index - 1))) {
|
|
86
|
+
score *= SCORE_NON_SPACE_WORD_JUMP;
|
|
87
|
+
const wordBreaks = target.slice(targetIndex, index - 1).match(COUNT_GAPS_REGEXP);
|
|
88
|
+
if (wordBreaks && targetIndex > 0) {
|
|
89
|
+
score *= Math.pow(PENALTY_SKIPPED, wordBreaks.length);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else if (IS_SPACE_REGEXP.test(target.charAt(index - 1))) {
|
|
93
|
+
score *= SCORE_SPACE_WORD_JUMP;
|
|
94
|
+
const spaceBreaks = target.slice(targetIndex, index - 1).match(COUNT_SPACE_REGEXP);
|
|
95
|
+
if (spaceBreaks && targetIndex > 0) {
|
|
96
|
+
score *= Math.pow(PENALTY_SKIPPED, spaceBreaks.length);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
score *= SCORE_CHARACTER_JUMP;
|
|
101
|
+
if (targetIndex > 0) {
|
|
102
|
+
score *= Math.pow(PENALTY_SKIPPED, index - targetIndex);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (target.charAt(index) !== query.charAt(queryIndex)) {
|
|
106
|
+
score *= PENALTY_CASE_MISMATCH;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if ((score < SCORE_TRANSPOSITION &&
|
|
110
|
+
lowerTarget.charAt(index - 1) === lowerQuery.charAt(queryIndex + 1)) ||
|
|
111
|
+
// Allow duplicate letters (cmdk ref #7428)
|
|
112
|
+
(lowerQuery.charAt(queryIndex + 1) === lowerQuery.charAt(queryIndex) &&
|
|
113
|
+
lowerTarget.charAt(index - 1) !== lowerQuery.charAt(queryIndex))) {
|
|
114
|
+
const transposedScore = commandScoreInner(target, query, lowerTarget, lowerQuery, index + 1, queryIndex + 2, memo);
|
|
115
|
+
if (transposedScore * SCORE_TRANSPOSITION > score) {
|
|
116
|
+
score = transposedScore * SCORE_TRANSPOSITION;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (score > highScore) {
|
|
120
|
+
highScore = score;
|
|
121
|
+
}
|
|
122
|
+
index = lowerTarget.indexOf(queryChar, index + 1);
|
|
123
|
+
}
|
|
124
|
+
memo[memoKey] = highScore;
|
|
125
|
+
return highScore;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* @description Lowercases a string and folds every space-like character to a
|
|
129
|
+
* plain space so variants match each other.
|
|
130
|
+
*
|
|
131
|
+
* @param value - The string to normalize.
|
|
132
|
+
* @returns The normalized string.
|
|
133
|
+
* @private
|
|
134
|
+
*/
|
|
135
|
+
function formatInput(value) {
|
|
136
|
+
return value.toLowerCase().replace(COUNT_SPACE_REGEXP, " ");
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* @description Scores how well `query` matches `target`, optionally widening
|
|
140
|
+
* the candidate with alias strings (extra keywords that should also match).
|
|
141
|
+
*
|
|
142
|
+
* @param target - The candidate string to score against.
|
|
143
|
+
* @param query - What the user typed.
|
|
144
|
+
* @param aliases - Extra match targets appended to the candidate.
|
|
145
|
+
* @returns 0 (no match) to 1 (perfect continuous match).
|
|
146
|
+
*/
|
|
147
|
+
function commandScore(target, query, aliases = []) {
|
|
148
|
+
const haystack = aliases.length > 0 ? `${target} ${aliases.join(" ")}` : target;
|
|
149
|
+
return commandScoreInner(haystack, query, formatInput(haystack), formatInput(query), 0, 0, {});
|
|
150
|
+
}
|
|
151
|
+
export { commandScore };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Fuzzy command-scoring for the typeahead family.
|
|
5
|
+
* @description Rates how well a search query matches a candidate string,
|
|
6
|
+
* returning 0 (no match) to 1 (perfect continuous match). Continuous runs and
|
|
7
|
+
* word-boundary jumps score high; scattered character jumps, transpositions,
|
|
8
|
+
* case mismatches, and skipped characters decay the score. Autocomplete,
|
|
9
|
+
* combobox, and command all rank their items with this single function.
|
|
10
|
+
*
|
|
11
|
+
* Vendored from cmdk's `command-score.ts` by @pacocoursey (MIT), itself
|
|
12
|
+
* adapted from Superhuman's `command-score` (MIT), which builds on Joshaven
|
|
13
|
+
* Potter's `string_score`. The constants and recursion are kept verbatim so
|
|
14
|
+
* results match cmdk's ranking; only the module shape is Zazz's.
|
|
15
|
+
*
|
|
16
|
+
* @see https://github.com/pacocoursey/cmdk
|
|
17
|
+
* @see https://github.com/superhuman/command-score
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// --- Scoring constants ---
|
|
21
|
+
|
|
22
|
+
// The scores are arranged so that a continuous match of characters will
|
|
23
|
+
// result in a total score of 1.
|
|
24
|
+
//
|
|
25
|
+
// The best case: this character is a match, and either this is the start of
|
|
26
|
+
// the string or the previous character was also a match.
|
|
27
|
+
const SCORE_CONTINUE_MATCH = 1;
|
|
28
|
+
// A new match at the start of a word scores better than a new match
|
|
29
|
+
// elsewhere, as it's more likely that the user will type the starts of
|
|
30
|
+
// fragments. Word jumps between spaces score slightly higher than slashes,
|
|
31
|
+
// brackets, hyphens, etc.
|
|
32
|
+
const SCORE_SPACE_WORD_JUMP = 0.9;
|
|
33
|
+
const SCORE_NON_SPACE_WORD_JUMP = 0.8;
|
|
34
|
+
// Any other match isn't ideal, but is included for completeness.
|
|
35
|
+
const SCORE_CHARACTER_JUMP = 0.17;
|
|
36
|
+
// If the user transposed two letters, it should be significantly penalized:
|
|
37
|
+
// "ouch" is more likely than "curtain" when "uc" is typed.
|
|
38
|
+
const SCORE_TRANSPOSITION = 0.1;
|
|
39
|
+
// The goodness of a match decays slightly with each skipped character:
|
|
40
|
+
// "bad" is more likely than "bard" when "bd" is typed.
|
|
41
|
+
const PENALTY_SKIPPED = 0.999;
|
|
42
|
+
// An exact-case match beats a case-insensitive match by a small amount:
|
|
43
|
+
// "HTML" is more likely than "haml" when "HM" is typed.
|
|
44
|
+
const PENALTY_CASE_MISMATCH = 0.9999;
|
|
45
|
+
// If the candidate has more characters than the user typed, penalize
|
|
46
|
+
// slightly: "html" is more likely than "html5" when "html" is typed.
|
|
47
|
+
const PENALTY_NOT_COMPLETE = 0.99;
|
|
48
|
+
|
|
49
|
+
const IS_GAP_REGEXP = /[\\/_+.#"@[({&]/;
|
|
50
|
+
const COUNT_GAPS_REGEXP = /[\\/_+.#"@[({&]/g;
|
|
51
|
+
const IS_SPACE_REGEXP = /[\s-]/;
|
|
52
|
+
const COUNT_SPACE_REGEXP = /[\s-]/g;
|
|
53
|
+
|
|
54
|
+
// --- Scoring ---
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @description Recursive scorer over (candidate index, query index) pairs,
|
|
58
|
+
* memoized per pair so repeated subproblems resolve in constant time.
|
|
59
|
+
*
|
|
60
|
+
* @param target - The candidate string (original casing).
|
|
61
|
+
* @param query - The query string (original casing).
|
|
62
|
+
* @param lowerTarget - Pre-lowercased candidate.
|
|
63
|
+
* @param lowerQuery - Pre-lowercased query.
|
|
64
|
+
* @param targetIndex - Current position in the candidate.
|
|
65
|
+
* @param queryIndex - Current position in the query.
|
|
66
|
+
* @param memo - Shared memoization table for this scoring run.
|
|
67
|
+
* @returns The best score reachable from this position.
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
function commandScoreInner(
|
|
71
|
+
target: string,
|
|
72
|
+
query: string,
|
|
73
|
+
lowerTarget: string,
|
|
74
|
+
lowerQuery: string,
|
|
75
|
+
targetIndex: number,
|
|
76
|
+
queryIndex: number,
|
|
77
|
+
memo: Record<string, number>,
|
|
78
|
+
): number {
|
|
79
|
+
if (queryIndex === query.length) {
|
|
80
|
+
if (targetIndex === target.length) {
|
|
81
|
+
return SCORE_CONTINUE_MATCH;
|
|
82
|
+
}
|
|
83
|
+
return PENALTY_NOT_COMPLETE;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const memoKey = `${targetIndex},${queryIndex}`;
|
|
87
|
+
const memoized = memo[memoKey];
|
|
88
|
+
if (memoized !== undefined) {
|
|
89
|
+
return memoized;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const queryChar = lowerQuery.charAt(queryIndex);
|
|
93
|
+
let index = lowerTarget.indexOf(queryChar, targetIndex);
|
|
94
|
+
let highScore = 0;
|
|
95
|
+
|
|
96
|
+
while (index >= 0) {
|
|
97
|
+
let score = commandScoreInner(
|
|
98
|
+
target,
|
|
99
|
+
query,
|
|
100
|
+
lowerTarget,
|
|
101
|
+
lowerQuery,
|
|
102
|
+
index + 1,
|
|
103
|
+
queryIndex + 1,
|
|
104
|
+
memo,
|
|
105
|
+
);
|
|
106
|
+
if (score > highScore) {
|
|
107
|
+
if (index === targetIndex) {
|
|
108
|
+
score *= SCORE_CONTINUE_MATCH;
|
|
109
|
+
} else if (IS_GAP_REGEXP.test(target.charAt(index - 1))) {
|
|
110
|
+
score *= SCORE_NON_SPACE_WORD_JUMP;
|
|
111
|
+
const wordBreaks = target.slice(targetIndex, index - 1).match(COUNT_GAPS_REGEXP);
|
|
112
|
+
if (wordBreaks && targetIndex > 0) {
|
|
113
|
+
score *= Math.pow(PENALTY_SKIPPED, wordBreaks.length);
|
|
114
|
+
}
|
|
115
|
+
} else if (IS_SPACE_REGEXP.test(target.charAt(index - 1))) {
|
|
116
|
+
score *= SCORE_SPACE_WORD_JUMP;
|
|
117
|
+
const spaceBreaks = target.slice(targetIndex, index - 1).match(COUNT_SPACE_REGEXP);
|
|
118
|
+
if (spaceBreaks && targetIndex > 0) {
|
|
119
|
+
score *= Math.pow(PENALTY_SKIPPED, spaceBreaks.length);
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
score *= SCORE_CHARACTER_JUMP;
|
|
123
|
+
if (targetIndex > 0) {
|
|
124
|
+
score *= Math.pow(PENALTY_SKIPPED, index - targetIndex);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (target.charAt(index) !== query.charAt(queryIndex)) {
|
|
129
|
+
score *= PENALTY_CASE_MISMATCH;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (
|
|
134
|
+
(score < SCORE_TRANSPOSITION &&
|
|
135
|
+
lowerTarget.charAt(index - 1) === lowerQuery.charAt(queryIndex + 1)) ||
|
|
136
|
+
// Allow duplicate letters (cmdk ref #7428)
|
|
137
|
+
(lowerQuery.charAt(queryIndex + 1) === lowerQuery.charAt(queryIndex) &&
|
|
138
|
+
lowerTarget.charAt(index - 1) !== lowerQuery.charAt(queryIndex))
|
|
139
|
+
) {
|
|
140
|
+
const transposedScore = commandScoreInner(
|
|
141
|
+
target,
|
|
142
|
+
query,
|
|
143
|
+
lowerTarget,
|
|
144
|
+
lowerQuery,
|
|
145
|
+
index + 1,
|
|
146
|
+
queryIndex + 2,
|
|
147
|
+
memo,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
if (transposedScore * SCORE_TRANSPOSITION > score) {
|
|
151
|
+
score = transposedScore * SCORE_TRANSPOSITION;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (score > highScore) {
|
|
156
|
+
highScore = score;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
index = lowerTarget.indexOf(queryChar, index + 1);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
memo[memoKey] = highScore;
|
|
163
|
+
return highScore;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @description Lowercases a string and folds every space-like character to a
|
|
168
|
+
* plain space so variants match each other.
|
|
169
|
+
*
|
|
170
|
+
* @param value - The string to normalize.
|
|
171
|
+
* @returns The normalized string.
|
|
172
|
+
* @private
|
|
173
|
+
*/
|
|
174
|
+
function formatInput(value: string): string {
|
|
175
|
+
return value.toLowerCase().replace(COUNT_SPACE_REGEXP, " ");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @description Scores how well `query` matches `target`, optionally widening
|
|
180
|
+
* the candidate with alias strings (extra keywords that should also match).
|
|
181
|
+
*
|
|
182
|
+
* @param target - The candidate string to score against.
|
|
183
|
+
* @param query - What the user typed.
|
|
184
|
+
* @param aliases - Extra match targets appended to the candidate.
|
|
185
|
+
* @returns 0 (no match) to 1 (perfect continuous match).
|
|
186
|
+
*/
|
|
187
|
+
function commandScore(target: string, query: string, aliases: readonly string[] = []): number {
|
|
188
|
+
const haystack = aliases.length > 0 ? `${target} ${aliases.join(" ")}` : target;
|
|
189
|
+
return commandScoreInner(haystack, query, formatInput(haystack), formatInput(query), 0, 0, {});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export { commandScore };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface InitDialogLifecycleFn {
|
|
2
|
+
(): void;
|
|
3
|
+
_bound?: boolean;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* @description Starts the page-wide dialog watcher. Idempotent and safe to call
|
|
7
|
+
* from double script loads.
|
|
8
|
+
*/
|
|
9
|
+
declare const initDialogLifecycle: InitDialogLifecycleFn;
|
|
10
|
+
export { initDialogLifecycle };
|
|
11
|
+
//# sourceMappingURL=dialog-lifecycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dialog-lifecycle.d.ts","sourceRoot":"","sources":["dialog-lifecycle.ts"],"names":[],"mappings":"AA4BA,UAAU,qBAAqB;IAC7B,IAAI,IAAI,CAAC;IACT,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;GAGG;AACH,QAAA,MAAM,mBAAmB,EAAE,qBA8B1B,CAAC;AAYF,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Single owner of `<dialog>` visibility for the kit.
|
|
4
|
+
* @description Watches every dialog on the page and re-emits its lifecycle as
|
|
5
|
+
* two **bubbling** events dispatched on the dialog itself (ADR-0003):
|
|
6
|
+
*
|
|
7
|
+
* - `zazz:dialog-open`: the `open` attribute was added (works for both
|
|
8
|
+
* `showModal()` and `show()`, and for invoker commands).
|
|
9
|
+
* - `zazz:dialog-close`: the dialog fired its native `close` event, which
|
|
10
|
+
* does not bubble; this one does.
|
|
11
|
+
*
|
|
12
|
+
* Components subscribe instead of running their own observers: a carousel
|
|
13
|
+
* inside a closed dialog listens on the dialog it found via `closest("dialog")`;
|
|
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.
|
|
16
|
+
* Dispatch order is the DOM order: dialog listeners, then ancestors, then
|
|
17
|
+
* document, so init-on-open (dialog level) always precedes scroll/focus
|
|
18
|
+
* choreography (ancestor and document level).
|
|
19
|
+
*
|
|
20
|
+
* No `detail` payload: the dialog is the event target.
|
|
21
|
+
*/
|
|
22
|
+
/** Dispatches a bubbling lifecycle event on a dialog. */
|
|
23
|
+
function emit(dialog, type) {
|
|
24
|
+
dialog.dispatchEvent(new Event(type, { bubbles: true }));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @description Starts the page-wide dialog watcher. Idempotent and safe to call
|
|
28
|
+
* from double script loads.
|
|
29
|
+
*/
|
|
30
|
+
const initDialogLifecycle = function () {
|
|
31
|
+
if (initDialogLifecycle._bound)
|
|
32
|
+
return;
|
|
33
|
+
initDialogLifecycle._bound = true;
|
|
34
|
+
const observer = new MutationObserver(function (mutations) {
|
|
35
|
+
for (const mutation of mutations) {
|
|
36
|
+
if (mutation.type === "attributes" &&
|
|
37
|
+
mutation.attributeName === "open" &&
|
|
38
|
+
mutation.target instanceof HTMLDialogElement &&
|
|
39
|
+
mutation.target.hasAttribute("open")) {
|
|
40
|
+
emit(mutation.target, "zazz:dialog-open");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
observer.observe(document.body, {
|
|
45
|
+
attributes: true,
|
|
46
|
+
attributeFilter: ["open"],
|
|
47
|
+
subtree: true,
|
|
48
|
+
});
|
|
49
|
+
// Native `close` does not bubble: capture it once and re-emit as a bubbling event.
|
|
50
|
+
document.addEventListener("close", function (e) {
|
|
51
|
+
if (e.target instanceof HTMLDialogElement)
|
|
52
|
+
emit(e.target, "zazz:dialog-close");
|
|
53
|
+
}, true);
|
|
54
|
+
};
|
|
55
|
+
// Auto-initialize when DOM is ready (only in browser environment)
|
|
56
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
57
|
+
if (document.readyState === "loading") {
|
|
58
|
+
document.addEventListener("DOMContentLoaded", initDialogLifecycle);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
initDialogLifecycle();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Exported for tests and manual bootstrapping; the events are the public API.
|
|
65
|
+
export { initDialogLifecycle };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Single owner of `<dialog>` visibility for the kit.
|
|
5
|
+
* @description Watches every dialog on the page and re-emits its lifecycle as
|
|
6
|
+
* two **bubbling** events dispatched on the dialog itself (ADR-0003):
|
|
7
|
+
*
|
|
8
|
+
* - `zazz:dialog-open`: the `open` attribute was added (works for both
|
|
9
|
+
* `showModal()` and `show()`, and for invoker commands).
|
|
10
|
+
* - `zazz:dialog-close`: the dialog fired its native `close` event, which
|
|
11
|
+
* does not bubble; this one does.
|
|
12
|
+
*
|
|
13
|
+
* Components subscribe instead of running their own observers: a carousel
|
|
14
|
+
* inside a closed dialog listens on the dialog it found via `closest("dialog")`;
|
|
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.
|
|
17
|
+
* Dispatch order is the DOM order: dialog listeners, then ancestors, then
|
|
18
|
+
* document, so init-on-open (dialog level) always precedes scroll/focus
|
|
19
|
+
* choreography (ancestor and document level).
|
|
20
|
+
*
|
|
21
|
+
* No `detail` payload: the dialog is the event target.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** Dispatches a bubbling lifecycle event on a dialog. */
|
|
25
|
+
function emit(dialog: HTMLDialogElement, type: "zazz:dialog-open" | "zazz:dialog-close"): void {
|
|
26
|
+
dialog.dispatchEvent(new Event(type, { bubbles: true }));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface InitDialogLifecycleFn {
|
|
30
|
+
(): void;
|
|
31
|
+
_bound?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @description Starts the page-wide dialog watcher. Idempotent and safe to call
|
|
36
|
+
* from double script loads.
|
|
37
|
+
*/
|
|
38
|
+
const initDialogLifecycle: InitDialogLifecycleFn = function () {
|
|
39
|
+
if (initDialogLifecycle._bound) return;
|
|
40
|
+
initDialogLifecycle._bound = true;
|
|
41
|
+
|
|
42
|
+
const observer = new MutationObserver(function (mutations) {
|
|
43
|
+
for (const mutation of mutations) {
|
|
44
|
+
if (
|
|
45
|
+
mutation.type === "attributes" &&
|
|
46
|
+
mutation.attributeName === "open" &&
|
|
47
|
+
mutation.target instanceof HTMLDialogElement &&
|
|
48
|
+
mutation.target.hasAttribute("open")
|
|
49
|
+
) {
|
|
50
|
+
emit(mutation.target, "zazz:dialog-open");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
observer.observe(document.body, {
|
|
55
|
+
attributes: true,
|
|
56
|
+
attributeFilter: ["open"],
|
|
57
|
+
subtree: true,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Native `close` does not bubble: capture it once and re-emit as a bubbling event.
|
|
61
|
+
document.addEventListener(
|
|
62
|
+
"close",
|
|
63
|
+
function (e) {
|
|
64
|
+
if (e.target instanceof HTMLDialogElement) emit(e.target, "zazz:dialog-close");
|
|
65
|
+
},
|
|
66
|
+
true,
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// Auto-initialize when DOM is ready (only in browser environment)
|
|
71
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
72
|
+
if (document.readyState === "loading") {
|
|
73
|
+
document.addEventListener("DOMContentLoaded", initDialogLifecycle);
|
|
74
|
+
} else {
|
|
75
|
+
initDialogLifecycle();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Exported for tests and manual bootstrapping; the events are the public API.
|
|
80
|
+
export { initDialogLifecycle };
|