@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,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* otp.css: OTP (ui-otp wrapping .ui-otp-input on one real <input>)
|
|
3
|
+
*
|
|
4
|
+
* @layer variables, components
|
|
5
|
+
* @requires layers.css, _variables.css, fields.css, input.css
|
|
6
|
+
* @uses Invisible-overlay input: the real input stretches over the
|
|
7
|
+
* stamped cell rail with transparent text/caret so autofill,
|
|
8
|
+
* paste, and focus stay native; never display:none
|
|
9
|
+
* @uses :focus-within: active-cell ring only while the input has focus
|
|
10
|
+
* @tokens --ui-otp-* (@layer variables; aliases --ui-field-*)
|
|
11
|
+
* @see otp.ts: stamps the rail and mirrors value/caret into the cells
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
@layer variables {
|
|
15
|
+
:root {
|
|
16
|
+
--ui-otp-slot-inline-size: var(--ui-field-height);
|
|
17
|
+
--ui-otp-slot-block-size: var(--ui-field-height);
|
|
18
|
+
--ui-otp-slot-background: var(--ui-field-background);
|
|
19
|
+
--ui-otp-slot-border: 1px solid var(--ui-field-border);
|
|
20
|
+
--ui-otp-slot-radius: var(--ui-field-radius);
|
|
21
|
+
--ui-otp-slot-font-size: var(--font-size-md);
|
|
22
|
+
--ui-otp-gap: var(--step-1_5);
|
|
23
|
+
--ui-otp-separator-color: var(--muted-foreground);
|
|
24
|
+
--ui-otp-caret-color: var(--ui-field-border--focus);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@layer zazz.components {
|
|
29
|
+
/* ===========================================================================
|
|
30
|
+
OTP: one real input, presentational cells
|
|
31
|
+
- Before enhancement (.ui-otp-input alone): a centered code field.
|
|
32
|
+
- After ([data-otp-ready]): the rail is the visual and the input becomes an
|
|
33
|
+
invisible overlay (focusable, autofillable, never display:none).
|
|
34
|
+
=========================================================================== */
|
|
35
|
+
|
|
36
|
+
:where(ui-otp) {
|
|
37
|
+
position: relative;
|
|
38
|
+
display: inline-grid;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* No-JS baseline: a plain code field in the field family */
|
|
42
|
+
.ui-otp-input {
|
|
43
|
+
text-align: center;
|
|
44
|
+
letter-spacing: 0.5ch;
|
|
45
|
+
font-family: var(--font-family-mono);
|
|
46
|
+
font-size: var(--ui-otp-slot-font-size);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/* Enhanced: the input stretches invisibly over the rail. Opacity stays just
|
|
50
|
+
above zero and the element keeps its size so taps, focus, and browser
|
|
51
|
+
autofill UI keep landing on a real, visible-to-the-UA control. */
|
|
52
|
+
:where(ui-otp[data-otp-ready]) .ui-otp-input {
|
|
53
|
+
position: absolute;
|
|
54
|
+
inset: 0;
|
|
55
|
+
inline-size: 100%;
|
|
56
|
+
block-size: 100%;
|
|
57
|
+
opacity: 0.02;
|
|
58
|
+
color: transparent;
|
|
59
|
+
caret-color: transparent;
|
|
60
|
+
letter-spacing: normal;
|
|
61
|
+
background: none;
|
|
62
|
+
border: none;
|
|
63
|
+
box-shadow: none;
|
|
64
|
+
outline: none;
|
|
65
|
+
/* Guards iOS focus zoom */
|
|
66
|
+
font-size: max(16px, 1em);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
[data-slot~="otp-rail"] {
|
|
70
|
+
display: flex;
|
|
71
|
+
align-items: center;
|
|
72
|
+
gap: var(--ui-otp-gap);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
[data-slot~="otp-slot"] {
|
|
76
|
+
display: grid;
|
|
77
|
+
place-items: center;
|
|
78
|
+
inline-size: var(--ui-otp-slot-inline-size);
|
|
79
|
+
block-size: var(--ui-otp-slot-block-size);
|
|
80
|
+
|
|
81
|
+
font-family: var(--font-family-mono);
|
|
82
|
+
font-size: var(--ui-otp-slot-font-size);
|
|
83
|
+
color: var(--ui-field-foreground);
|
|
84
|
+
|
|
85
|
+
background-color: var(--ui-otp-slot-background);
|
|
86
|
+
border: var(--ui-otp-slot-border);
|
|
87
|
+
border-radius: var(--ui-otp-slot-radius);
|
|
88
|
+
|
|
89
|
+
transition: var(--default-transition);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* Active cell: ring + focus border, only while the input has focus */
|
|
93
|
+
:where(ui-otp:focus-within) [data-slot~="otp-slot"][data-active] {
|
|
94
|
+
border-color: var(--ui-field-border--focus);
|
|
95
|
+
background-color: var(--ui-field-background--focus);
|
|
96
|
+
box-shadow:
|
|
97
|
+
0 0 0 var(--ring-offset-width) var(--ring-offset-color),
|
|
98
|
+
0 0 0 calc(var(--ring-offset-width) + var(--ring-width))
|
|
99
|
+
color-mix(in oklch, var(--ui-field-ring-color) var(--ring-opacity), transparent);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* Fake caret in the active empty cell */
|
|
103
|
+
:where(ui-otp:focus-within) [data-slot~="otp-slot"][data-active]:empty::after {
|
|
104
|
+
content: "";
|
|
105
|
+
inline-size: 1px;
|
|
106
|
+
block-size: 1.2em;
|
|
107
|
+
background-color: var(--ui-otp-caret-color);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
111
|
+
:where(ui-otp:focus-within) [data-slot~="otp-slot"][data-active]:empty::after {
|
|
112
|
+
animation: ui-otp-caret-blink 1.1s steps(2, start) infinite;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@keyframes ui-otp-caret-blink {
|
|
117
|
+
to {
|
|
118
|
+
visibility: hidden;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
[data-slot~="otp-separator"] {
|
|
123
|
+
color: var(--ui-otp-separator-color);
|
|
124
|
+
user-select: none;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `<ui-otp>`: a one-time password field with character slots.
|
|
3
|
+
* @description Light-DOM custom element that augments a single
|
|
4
|
+
* `<input class="ui-input ui-otp-input">` (the standard OTP shape with
|
|
5
|
+
* `autocomplete="one-time-code"`, `inputmode`, `pattern`, and `maxlength`)
|
|
6
|
+
* with a presentational slot rail. The input stays the source of truth:
|
|
7
|
+
* autofill, paste, IME, undo, form submission, and `:user-invalid` are all
|
|
8
|
+
* native. The script stamps an `aria-hidden` rail of character cells behind
|
|
9
|
+
* the input and stretches the input invisibly over it; each cell mirrors one
|
|
10
|
+
* character and the caret position. Without JavaScript the markup is a plain,
|
|
11
|
+
* fully functional OTP input.
|
|
12
|
+
*
|
|
13
|
+
* The code length comes from the input `maxlength`. There is no
|
|
14
|
+
* `data-otp-length`.
|
|
15
|
+
*
|
|
16
|
+
* Attributes on `<ui-otp>`:
|
|
17
|
+
* - `data-otp-groups`: separator layout, e.g. `"3-3"` or `"2-2-2"`. The
|
|
18
|
+
* groups must sum to `maxlength`; otherwise one ungrouped run renders.
|
|
19
|
+
* - `data-otp-type`: sanitization charset: `numeric` (default), `alpha`,
|
|
20
|
+
* `alphanumeric`, `none`. Keep it in agreement with the input `pattern`
|
|
21
|
+
* and `inputmode`, which carry the no-JS validation.
|
|
22
|
+
* - `data-otp-mask`: render `•` in the cells instead of characters.
|
|
23
|
+
* - `data-otp-auto-submit`: request the owning form submission once when
|
|
24
|
+
* the code is complete.
|
|
25
|
+
*
|
|
26
|
+
* Stamped state: cells are `[data-slot~="otp-slot"]` with `data-filled` /
|
|
27
|
+
* `data-active`; separators are `[data-slot~="otp-separator"]`; the root
|
|
28
|
+
* gains `data-otp-ready` when enhanced and `data-otp-complete` when full.
|
|
29
|
+
*/
|
|
30
|
+
import { ZazzElement } from "../../base/zazz-element.ts";
|
|
31
|
+
/** Sanitization charset for typed and pasted text. */
|
|
32
|
+
type OtpType = "numeric" | "alpha" | "alphanumeric" | "none";
|
|
33
|
+
/** One rendered cell: its character and its state flags. */
|
|
34
|
+
interface SlotState {
|
|
35
|
+
/** The character to render ("" when empty; "•" when masked and filled). */
|
|
36
|
+
char: string;
|
|
37
|
+
/** Whether the cell holds a character. */
|
|
38
|
+
filled: boolean;
|
|
39
|
+
/** Whether the caret sits on this cell. */
|
|
40
|
+
active: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @description Parses `data-otp-groups` ("3-3") into group sizes. Groups that
|
|
44
|
+
* don't sum to the code length fall back to one ungrouped run.
|
|
45
|
+
*
|
|
46
|
+
* @param raw - The attribute value, or null.
|
|
47
|
+
* @param length - The code length.
|
|
48
|
+
* @returns Group sizes summing to `length`.
|
|
49
|
+
*/
|
|
50
|
+
declare function parseGroups(raw: string | null, length: number): number[];
|
|
51
|
+
/**
|
|
52
|
+
* @description Filters raw input text to the allowed charset and clamps it to
|
|
53
|
+
* the code length. Whitespace never survives (pasted codes often carry it).
|
|
54
|
+
*
|
|
55
|
+
* @param raw - The input's current value.
|
|
56
|
+
* @param type - The allowed charset.
|
|
57
|
+
* @param length - The code length.
|
|
58
|
+
* @returns The sanitized value.
|
|
59
|
+
*/
|
|
60
|
+
declare function sanitizeOtp(raw: string, type: OtpType, length: number): string;
|
|
61
|
+
/**
|
|
62
|
+
* @description Derives every cell's character and state from the input's
|
|
63
|
+
* value and caret. The active cell tracks the caret; with the code full, the
|
|
64
|
+
* last cell stays active.
|
|
65
|
+
*
|
|
66
|
+
* @param value - The sanitized value.
|
|
67
|
+
* @param length - The code length.
|
|
68
|
+
* @param caret - The input's caret position.
|
|
69
|
+
* @param focused - Whether the input has focus.
|
|
70
|
+
* @param mask - Whether to obscure characters.
|
|
71
|
+
* @returns One state per cell.
|
|
72
|
+
*/
|
|
73
|
+
declare function resolveSlots(value: string, length: number, caret: number, focused: boolean, mask: boolean): SlotState[];
|
|
74
|
+
/**
|
|
75
|
+
* @description Whether the code is complete.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The sanitized value.
|
|
78
|
+
* @param length - The code length.
|
|
79
|
+
* @returns True when every cell is filled.
|
|
80
|
+
*/
|
|
81
|
+
declare function isComplete(value: string, length: number): boolean;
|
|
82
|
+
declare class UiOtp extends ZazzElement {
|
|
83
|
+
#private;
|
|
84
|
+
protected setup(signal: AbortSignal): void;
|
|
85
|
+
protected teardown(): void;
|
|
86
|
+
}
|
|
87
|
+
export { UiOtp, parseGroups, sanitizeOtp, resolveSlots, isComplete };
|
|
88
|
+
export type { OtpType, SlotState };
|
|
89
|
+
//# sourceMappingURL=otp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp.d.ts","sourceRoot":"","sources":["otp.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,WAAW,EAAqB,MAAM,4BAA4B,CAAC;AAK5E,sDAAsD;AACtD,KAAK,OAAO,GAAG,SAAS,GAAG,OAAO,GAAG,cAAc,GAAG,MAAM,CAAC;AAE7D,4DAA4D;AAC5D,UAAU,SAAS;IACjB,2EAA2E;IAC3E,IAAI,EAAE,MAAM,CAAC;IACb,0CAA0C;IAC1C,MAAM,EAAE,OAAO,CAAC;IAChB,2CAA2C;IAC3C,MAAM,EAAE,OAAO,CAAC;CACjB;AAID;;;;;;;GAOG;AACH,iBAAS,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAQjE;AAED;;;;;;;;GAQG;AACH,iBAAS,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAiBvE;AAED;;;;;;;;;;;GAWG;AACH,iBAAS,YAAY,CACnB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,OAAO,GACZ,SAAS,EAAE,CAUb;AAED;;;;;;GAMG;AACH,iBAAS,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAE1D;AAID,cAAM,KAAM,SAAQ,WAAW;;IAI7B,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CAqFzC;IAED,SAAS,CAAC,QAAQ,IAAI,IAAI,CAKzB;CAoCF;AAID,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC;AACrE,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<form class="ui-field" style="max-inline-size: max-content">
|
|
2
|
+
<label data-slot="field-label" for="f-otp">Verification code</label>
|
|
3
|
+
<ui-otp data-otp-groups="3-3">
|
|
4
|
+
<input
|
|
5
|
+
class="ui-input ui-otp-input"
|
|
6
|
+
id="f-otp"
|
|
7
|
+
name="otp"
|
|
8
|
+
type="text"
|
|
9
|
+
autocomplete="one-time-code"
|
|
10
|
+
inputmode="numeric"
|
|
11
|
+
pattern="\d{6}"
|
|
12
|
+
maxlength="6"
|
|
13
|
+
required
|
|
14
|
+
spellcheck="false"
|
|
15
|
+
autocapitalize="off"
|
|
16
|
+
/>
|
|
17
|
+
</ui-otp>
|
|
18
|
+
<div data-slot="field-description">
|
|
19
|
+
<span data-slot="field-hint">Enter the 6-digit code we sent you.</span>
|
|
20
|
+
<span data-slot="field-error">Enter all 6 digits.</span>
|
|
21
|
+
</div>
|
|
22
|
+
</form>
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview `<ui-otp>`: a one-time password field with character slots.
|
|
4
|
+
* @description Light-DOM custom element that augments a single
|
|
5
|
+
* `<input class="ui-input ui-otp-input">` (the standard OTP shape with
|
|
6
|
+
* `autocomplete="one-time-code"`, `inputmode`, `pattern`, and `maxlength`)
|
|
7
|
+
* with a presentational slot rail. The input stays the source of truth:
|
|
8
|
+
* autofill, paste, IME, undo, form submission, and `:user-invalid` are all
|
|
9
|
+
* native. The script stamps an `aria-hidden` rail of character cells behind
|
|
10
|
+
* the input and stretches the input invisibly over it; each cell mirrors one
|
|
11
|
+
* character and the caret position. Without JavaScript the markup is a plain,
|
|
12
|
+
* fully functional OTP input.
|
|
13
|
+
*
|
|
14
|
+
* The code length comes from the input `maxlength`. There is no
|
|
15
|
+
* `data-otp-length`.
|
|
16
|
+
*
|
|
17
|
+
* Attributes on `<ui-otp>`:
|
|
18
|
+
* - `data-otp-groups`: separator layout, e.g. `"3-3"` or `"2-2-2"`. The
|
|
19
|
+
* groups must sum to `maxlength`; otherwise one ungrouped run renders.
|
|
20
|
+
* - `data-otp-type`: sanitization charset: `numeric` (default), `alpha`,
|
|
21
|
+
* `alphanumeric`, `none`. Keep it in agreement with the input `pattern`
|
|
22
|
+
* and `inputmode`, which carry the no-JS validation.
|
|
23
|
+
* - `data-otp-mask`: render `•` in the cells instead of characters.
|
|
24
|
+
* - `data-otp-auto-submit`: request the owning form submission once when
|
|
25
|
+
* the code is complete.
|
|
26
|
+
*
|
|
27
|
+
* Stamped state: cells are `[data-slot~="otp-slot"]` with `data-filled` /
|
|
28
|
+
* `data-active`; separators are `[data-slot~="otp-separator"]`; the root
|
|
29
|
+
* gains `data-otp-ready` when enhanced and `data-otp-complete` when full.
|
|
30
|
+
*/
|
|
31
|
+
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
|
|
32
|
+
import { effect, state } from "../../base/signals.js";
|
|
33
|
+
// --- Pure derivations (exported for unit tests only) ---
|
|
34
|
+
/**
|
|
35
|
+
* @description Parses `data-otp-groups` ("3-3") into group sizes. Groups that
|
|
36
|
+
* don't sum to the code length fall back to one ungrouped run.
|
|
37
|
+
*
|
|
38
|
+
* @param raw - The attribute value, or null.
|
|
39
|
+
* @param length - The code length.
|
|
40
|
+
* @returns Group sizes summing to `length`.
|
|
41
|
+
*/
|
|
42
|
+
function parseGroups(raw, length) {
|
|
43
|
+
if (!raw)
|
|
44
|
+
return [length];
|
|
45
|
+
const groups = raw
|
|
46
|
+
.split("-")
|
|
47
|
+
.map((part) => Number.parseInt(part, 10))
|
|
48
|
+
.filter((size) => Number.isFinite(size) && size > 0);
|
|
49
|
+
const total = groups.reduce((sum, size) => sum + size, 0);
|
|
50
|
+
return total === length && groups.length > 0 ? groups : [length];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @description Filters raw input text to the allowed charset and clamps it to
|
|
54
|
+
* the code length. Whitespace never survives (pasted codes often carry it).
|
|
55
|
+
*
|
|
56
|
+
* @param raw - The input's current value.
|
|
57
|
+
* @param type - The allowed charset.
|
|
58
|
+
* @param length - The code length.
|
|
59
|
+
* @returns The sanitized value.
|
|
60
|
+
*/
|
|
61
|
+
function sanitizeOtp(raw, type, length) {
|
|
62
|
+
let allowed;
|
|
63
|
+
switch (type) {
|
|
64
|
+
case "numeric":
|
|
65
|
+
allowed = /[^0-9]/g;
|
|
66
|
+
break;
|
|
67
|
+
case "alpha":
|
|
68
|
+
allowed = /[^a-zA-Z]/g;
|
|
69
|
+
break;
|
|
70
|
+
case "alphanumeric":
|
|
71
|
+
allowed = /[^a-zA-Z0-9]/g;
|
|
72
|
+
break;
|
|
73
|
+
case "none":
|
|
74
|
+
allowed = /\s/g;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
return raw.replace(allowed, "").slice(0, length);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @description Derives every cell's character and state from the input's
|
|
81
|
+
* value and caret. The active cell tracks the caret; with the code full, the
|
|
82
|
+
* last cell stays active.
|
|
83
|
+
*
|
|
84
|
+
* @param value - The sanitized value.
|
|
85
|
+
* @param length - The code length.
|
|
86
|
+
* @param caret - The input's caret position.
|
|
87
|
+
* @param focused - Whether the input has focus.
|
|
88
|
+
* @param mask - Whether to obscure characters.
|
|
89
|
+
* @returns One state per cell.
|
|
90
|
+
*/
|
|
91
|
+
function resolveSlots(value, length, caret, focused, mask) {
|
|
92
|
+
const activeIndex = focused ? Math.min(caret, length - 1) : -1;
|
|
93
|
+
return Array.from({ length }, (_, index) => {
|
|
94
|
+
const char = value.charAt(index);
|
|
95
|
+
return {
|
|
96
|
+
char: char === "" ? "" : mask ? "•" : char,
|
|
97
|
+
filled: char !== "",
|
|
98
|
+
active: index === activeIndex,
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* @description Whether the code is complete.
|
|
104
|
+
*
|
|
105
|
+
* @param value - The sanitized value.
|
|
106
|
+
* @param length - The code length.
|
|
107
|
+
* @returns True when every cell is filled.
|
|
108
|
+
*/
|
|
109
|
+
function isComplete(value, length) {
|
|
110
|
+
return length > 0 && value.length === length;
|
|
111
|
+
}
|
|
112
|
+
// --- Element ---
|
|
113
|
+
class UiOtp extends ZazzElement {
|
|
114
|
+
#rail = null;
|
|
115
|
+
#lastSubmitted = "";
|
|
116
|
+
setup(signal) {
|
|
117
|
+
const input = this.querySelector("input");
|
|
118
|
+
if (!(input instanceof HTMLInputElement))
|
|
119
|
+
return;
|
|
120
|
+
const length = input.maxLength > 0 ? input.maxLength : 6;
|
|
121
|
+
const type = (this.getAttribute("data-otp-type") ?? "numeric");
|
|
122
|
+
const mask = this.hasAttribute("data-otp-mask");
|
|
123
|
+
const groups = parseGroups(this.getAttribute("data-otp-groups"), length);
|
|
124
|
+
const rail = this.#stampRail(groups);
|
|
125
|
+
this.setAttribute("data-otp-ready", "");
|
|
126
|
+
const value = state(sanitizeOtp(input.value, type, length));
|
|
127
|
+
const caret = state(input.selectionStart ?? 0);
|
|
128
|
+
const focused = state(document.activeElement === input);
|
|
129
|
+
// Input adapters: sanitize in place, then mirror value and caret
|
|
130
|
+
input.addEventListener("input", () => {
|
|
131
|
+
const clean = sanitizeOtp(input.value, type, length);
|
|
132
|
+
if (input.value !== clean)
|
|
133
|
+
input.value = clean;
|
|
134
|
+
value.set(clean);
|
|
135
|
+
caret.set(input.selectionStart ?? clean.length);
|
|
136
|
+
}, { signal });
|
|
137
|
+
document.addEventListener("selectionchange", () => {
|
|
138
|
+
if (document.activeElement !== input)
|
|
139
|
+
return;
|
|
140
|
+
caret.set(input.selectionStart ?? 0);
|
|
141
|
+
}, { signal });
|
|
142
|
+
input.addEventListener("focus", () => focused.set(true), { signal });
|
|
143
|
+
input.addEventListener("blur", () => focused.set(false), { signal });
|
|
144
|
+
// Clicking a cell moves the caret to it
|
|
145
|
+
rail.addEventListener("pointerdown", (event) => {
|
|
146
|
+
const cell = event.target instanceof Element ? event.target.closest("[data-otp-index]") : null;
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
input.focus();
|
|
149
|
+
const index = cell ? Number(cell.getAttribute("data-otp-index")) : length;
|
|
150
|
+
const position = Math.min(index, input.value.length);
|
|
151
|
+
input.setSelectionRange(position, position);
|
|
152
|
+
caret.set(position);
|
|
153
|
+
}, { signal });
|
|
154
|
+
// Output adapter: one effect writes all derived attributes together
|
|
155
|
+
effect(() => {
|
|
156
|
+
const slots = resolveSlots(value.get(), length, caret.get(), focused.get(), mask);
|
|
157
|
+
const cells = rail.querySelectorAll("[data-otp-index]");
|
|
158
|
+
slots.forEach((slot, index) => {
|
|
159
|
+
const cell = cells[index];
|
|
160
|
+
if (!(cell instanceof HTMLElement))
|
|
161
|
+
return;
|
|
162
|
+
cell.textContent = slot.char;
|
|
163
|
+
if (slot.filled)
|
|
164
|
+
cell.setAttribute("data-filled", "");
|
|
165
|
+
else
|
|
166
|
+
cell.removeAttribute("data-filled");
|
|
167
|
+
if (slot.active)
|
|
168
|
+
cell.setAttribute("data-active", "");
|
|
169
|
+
else
|
|
170
|
+
cell.removeAttribute("data-active");
|
|
171
|
+
});
|
|
172
|
+
const complete = isComplete(value.get(), length);
|
|
173
|
+
if (complete)
|
|
174
|
+
this.setAttribute("data-otp-complete", "");
|
|
175
|
+
else
|
|
176
|
+
this.removeAttribute("data-otp-complete");
|
|
177
|
+
if (complete && this.hasAttribute("data-otp-auto-submit")) {
|
|
178
|
+
// Once per distinct complete value, so corrections can resubmit
|
|
179
|
+
if (this.#lastSubmitted !== value.get()) {
|
|
180
|
+
this.#lastSubmitted = value.get();
|
|
181
|
+
input.form?.requestSubmit();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}, { signal });
|
|
185
|
+
}
|
|
186
|
+
teardown() {
|
|
187
|
+
this.#rail?.remove();
|
|
188
|
+
this.#rail = null;
|
|
189
|
+
this.removeAttribute("data-otp-ready");
|
|
190
|
+
this.removeAttribute("data-otp-complete");
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* @description Builds the aria-hidden cell rail (with separators between
|
|
194
|
+
* groups) and inserts it after the input.
|
|
195
|
+
*
|
|
196
|
+
* @param groups - Group sizes, summing to the code length.
|
|
197
|
+
* @returns The rail element.
|
|
198
|
+
* @private
|
|
199
|
+
*/
|
|
200
|
+
#stampRail(groups) {
|
|
201
|
+
const rail = document.createElement("div");
|
|
202
|
+
rail.setAttribute("data-slot", "otp-rail");
|
|
203
|
+
rail.setAttribute("aria-hidden", "true");
|
|
204
|
+
let index = 0;
|
|
205
|
+
groups.forEach((size, groupIndex) => {
|
|
206
|
+
if (groupIndex > 0) {
|
|
207
|
+
const separator = document.createElement("span");
|
|
208
|
+
separator.setAttribute("data-slot", "otp-separator");
|
|
209
|
+
separator.setAttribute("aria-role", "presentation");
|
|
210
|
+
separator.textContent = "–";
|
|
211
|
+
rail.append(separator);
|
|
212
|
+
}
|
|
213
|
+
for (let i = 0; i < size; i++) {
|
|
214
|
+
const cell = document.createElement("span");
|
|
215
|
+
cell.setAttribute("data-slot", "otp-slot");
|
|
216
|
+
cell.setAttribute("data-otp-index", String(index++));
|
|
217
|
+
rail.append(cell);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
this.append(rail);
|
|
221
|
+
this.#rail = rail;
|
|
222
|
+
return rail;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
defineZazzElement("ui-otp", UiOtp);
|
|
226
|
+
export { UiOtp, parseGroups, sanitizeOtp, resolveSlots, isComplete };
|