digitojs 1.0.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 +32 -0
- package/LICENSE +21 -0
- package/README.md +753 -0
- package/dist/adapters/alpine.d.ts +71 -0
- package/dist/adapters/alpine.d.ts.map +1 -0
- package/dist/adapters/alpine.js +560 -0
- package/dist/adapters/alpine.js.map +1 -0
- package/dist/adapters/react.d.ts +223 -0
- package/dist/adapters/react.d.ts.map +1 -0
- package/dist/adapters/react.js +337 -0
- package/dist/adapters/react.js.map +1 -0
- package/dist/adapters/svelte.d.ts +139 -0
- package/dist/adapters/svelte.d.ts.map +1 -0
- package/dist/adapters/svelte.js +295 -0
- package/dist/adapters/svelte.js.map +1 -0
- package/dist/adapters/vanilla.d.ts +110 -0
- package/dist/adapters/vanilla.d.ts.map +1 -0
- package/dist/adapters/vanilla.js +650 -0
- package/dist/adapters/vanilla.js.map +1 -0
- package/dist/adapters/vue.d.ts +163 -0
- package/dist/adapters/vue.d.ts.map +1 -0
- package/dist/adapters/vue.js +298 -0
- package/dist/adapters/vue.js.map +1 -0
- package/dist/adapters/web-component.d.ts +192 -0
- package/dist/adapters/web-component.d.ts.map +1 -0
- package/dist/adapters/web-component.js +832 -0
- package/dist/adapters/web-component.js.map +1 -0
- package/dist/core/feedback.d.ts +26 -0
- package/dist/core/feedback.d.ts.map +1 -0
- package/dist/core/feedback.js +47 -0
- package/dist/core/feedback.js.map +1 -0
- package/dist/core/filter.d.ts +24 -0
- package/dist/core/filter.d.ts.map +1 -0
- package/dist/core/filter.js +47 -0
- package/dist/core/filter.js.map +1 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +15 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/machine.d.ts +67 -0
- package/dist/core/machine.d.ts.map +1 -0
- package/dist/core/machine.js +328 -0
- package/dist/core/machine.js.map +1 -0
- package/dist/core/timer.d.ts +24 -0
- package/dist/core/timer.d.ts.map +1 -0
- package/dist/core/timer.js +67 -0
- package/dist/core/timer.js.map +1 -0
- package/dist/core/types.d.ts +162 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +10 -0
- package/dist/core/types.js.map +1 -0
- package/dist/digito-wc.min.js +254 -0
- package/dist/digito-wc.min.js.map +7 -0
- package/dist/digito.min.js +91 -0
- package/dist/digito.min.js.map +7 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/package.json +109 -0
- package/src/adapters/alpine.ts +666 -0
- package/src/adapters/react.tsx +603 -0
- package/src/adapters/svelte.ts +444 -0
- package/src/adapters/vanilla.ts +810 -0
- package/src/adapters/vue.ts +462 -0
- package/src/adapters/web-component.ts +858 -0
- package/src/core/feedback.ts +44 -0
- package/src/core/filter.ts +48 -0
- package/src/core/index.ts +16 -0
- package/src/core/machine.ts +373 -0
- package/src/core/timer.ts +75 -0
- package/src/core/types.ts +167 -0
- package/src/index.ts +51 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* digito/web-component
|
|
3
|
+
* ─────────────────────────────────────────────────────────────────────────────
|
|
4
|
+
* Framework-agnostic Web Component — <digito-input>
|
|
5
|
+
* Uses single hidden-input architecture for correct SMS autofill + a11y.
|
|
6
|
+
*
|
|
7
|
+
* Attributes:
|
|
8
|
+
* length Number of slots (default: 6)
|
|
9
|
+
* type Character set: numeric | alphabet | alphanumeric | any (default: numeric)
|
|
10
|
+
* timer Countdown seconds (default: 0 = no timer)
|
|
11
|
+
* resend-after Cooldown seconds before the built-in Resend re-enables (default: 30)
|
|
12
|
+
* disabled Boolean attribute — disables all input when present
|
|
13
|
+
* separator-after Slot index (1-based) or comma-separated list, e.g. "3" or "2,4" (default: none)
|
|
14
|
+
* separator Separator character to render (default: —)
|
|
15
|
+
* name Sets the hidden input's name attr for native form submission
|
|
16
|
+
* placeholder Character shown in empty slots (e.g. "○" or "_")
|
|
17
|
+
* auto-focus Boolean attribute — focus input on mount (default: true when absent)
|
|
18
|
+
* select-on-focus Boolean attribute — selects the current slot char on focus
|
|
19
|
+
* blur-on-complete Boolean attribute — blurs the input when all slots are filled
|
|
20
|
+
*
|
|
21
|
+
* Events:
|
|
22
|
+
* complete CustomEvent<{ code: string }> — fired when all slots filled
|
|
23
|
+
* expire CustomEvent — fired when timer reaches zero
|
|
24
|
+
* change CustomEvent<{ code: string }> — fired on every input change
|
|
25
|
+
*
|
|
26
|
+
* DOM API:
|
|
27
|
+
* el.reset()
|
|
28
|
+
* el.setError(boolean)
|
|
29
|
+
* el.setSuccess(boolean)
|
|
30
|
+
* el.setDisabled(boolean)
|
|
31
|
+
* el.getCode() -> string
|
|
32
|
+
* el.pattern = /^[0-9A-F]$/ (JS property, not attribute)
|
|
33
|
+
* el.pasteTransformer = fn (JS property)
|
|
34
|
+
* el.onComplete = code => {} (JS property)
|
|
35
|
+
* el.onResend = () => {} (JS property)
|
|
36
|
+
* el.onFocus = () => {} (JS property)
|
|
37
|
+
* el.onBlur = () => {} (JS property)
|
|
38
|
+
* el.onInvalidChar = (char, i) => {} (JS property)
|
|
39
|
+
*
|
|
40
|
+
* @author Olawale Balo — Product Designer + Design Engineer
|
|
41
|
+
* @license MIT
|
|
42
|
+
*/
|
|
43
|
+
declare class DigitoInput extends HTMLElement {
|
|
44
|
+
/**
|
|
45
|
+
* HTML attribute names whose changes trigger `attributeChangedCallback`.
|
|
46
|
+
* Any change to these attributes causes a full shadow DOM rebuild so the
|
|
47
|
+
* component always reflects its attribute state without manual reconciliation.
|
|
48
|
+
*/
|
|
49
|
+
static observedAttributes: string[];
|
|
50
|
+
private slotEls;
|
|
51
|
+
private caretEls;
|
|
52
|
+
private hiddenInput;
|
|
53
|
+
private timerEl;
|
|
54
|
+
private timerBadgeEl;
|
|
55
|
+
private resendEl;
|
|
56
|
+
private timerCtrl;
|
|
57
|
+
private resendCountdown;
|
|
58
|
+
private digito;
|
|
59
|
+
private shadow;
|
|
60
|
+
private _isDisabled;
|
|
61
|
+
private _isSuccess;
|
|
62
|
+
private _pattern;
|
|
63
|
+
private _pasteTransformer;
|
|
64
|
+
private _onComplete;
|
|
65
|
+
private _onResend;
|
|
66
|
+
private _onFocus;
|
|
67
|
+
private _onBlur;
|
|
68
|
+
private _onInvalidChar;
|
|
69
|
+
/** Called when all slots are filled. Also dispatches the `complete` CustomEvent. */
|
|
70
|
+
set onComplete(fn: ((code: string) => void) | undefined);
|
|
71
|
+
/** Called when the built-in Resend button is clicked. */
|
|
72
|
+
set onResend(fn: (() => void) | undefined);
|
|
73
|
+
/** Fires when the hidden input receives focus. Set as JS property. */
|
|
74
|
+
set onFocus(fn: (() => void) | undefined);
|
|
75
|
+
/** Fires when the hidden input loses focus. Set as JS property. */
|
|
76
|
+
set onBlur(fn: (() => void) | undefined);
|
|
77
|
+
/**
|
|
78
|
+
* Fires when a typed character is rejected by type/pattern validation.
|
|
79
|
+
* Receives the character and the slot index it was attempted on.
|
|
80
|
+
* Set as JS property.
|
|
81
|
+
*/
|
|
82
|
+
set onInvalidChar(fn: ((char: string, index: number) => void) | undefined);
|
|
83
|
+
/**
|
|
84
|
+
* Arbitrary per-character regex. When set, each typed/pasted character must
|
|
85
|
+
* match to be accepted. Takes precedence over the type attribute for
|
|
86
|
+
* character validation. Cannot be expressed as an HTML attribute — set as a
|
|
87
|
+
* JS property instead.
|
|
88
|
+
* @example el.pattern = /^[0-9A-F]$/
|
|
89
|
+
*/
|
|
90
|
+
set pattern(re: RegExp | undefined);
|
|
91
|
+
/**
|
|
92
|
+
* Optional paste transformer function. Applied to raw clipboard text before
|
|
93
|
+
* filtering. Use to strip formatting (e.g. `"G-123456"` → `"123456"`).
|
|
94
|
+
* Cannot be expressed as an HTML attribute — set as a JS property.
|
|
95
|
+
* @example el.pasteTransformer = (raw) => raw.replace(/\s+|-/g, '')
|
|
96
|
+
*/
|
|
97
|
+
set pasteTransformer(fn: ((raw: string) => string) | undefined);
|
|
98
|
+
constructor();
|
|
99
|
+
/** Called when the element is inserted into the DOM. Triggers the initial build. */
|
|
100
|
+
connectedCallback(): void;
|
|
101
|
+
/**
|
|
102
|
+
* Called when the element is removed from the DOM.
|
|
103
|
+
* Stops both timers and cancels any pending `onComplete` timeout to avoid
|
|
104
|
+
* callbacks firing after the element is detached.
|
|
105
|
+
*/
|
|
106
|
+
disconnectedCallback(): void;
|
|
107
|
+
/**
|
|
108
|
+
* Called when any observed attribute changes after the initial connection.
|
|
109
|
+
* Guards on `shadow.children.length > 0` so it does not fire before
|
|
110
|
+
* `connectedCallback` has completed the first build.
|
|
111
|
+
*/
|
|
112
|
+
attributeChangedCallback(): void;
|
|
113
|
+
private get _length();
|
|
114
|
+
private get _type();
|
|
115
|
+
private get _timer();
|
|
116
|
+
private get _resendAfter();
|
|
117
|
+
private get _disabledAttr();
|
|
118
|
+
/** Parses `separator-after="2,4"` into `[2, 4]`. Filters NaN and zero values. */
|
|
119
|
+
private get _separatorAfter();
|
|
120
|
+
private get _separator();
|
|
121
|
+
/** `masked` is a boolean attribute — present means true, absent means false. */
|
|
122
|
+
private get _masked();
|
|
123
|
+
private get _maskChar();
|
|
124
|
+
private get _name();
|
|
125
|
+
private get _placeholder();
|
|
126
|
+
/**
|
|
127
|
+
* `auto-focus` defaults to `true` when the attribute is absent.
|
|
128
|
+
* Setting `auto-focus="false"` explicitly suppresses focus on mount.
|
|
129
|
+
*/
|
|
130
|
+
private get _autoFocus();
|
|
131
|
+
private get _selectOnFocus();
|
|
132
|
+
private get _blurOnComplete();
|
|
133
|
+
/**
|
|
134
|
+
* Constructs the entire shadow DOM from scratch.
|
|
135
|
+
*
|
|
136
|
+
* Called on first connect, on every observed attribute change, and when
|
|
137
|
+
* certain JS-property setters (`pattern`, `pasteTransformer`, `onInvalidChar`)
|
|
138
|
+
* are assigned after mount. Tears down any running timer and resets the
|
|
139
|
+
* state machine before rebuilding to prevent duplicate intervals or stale
|
|
140
|
+
* closure references from the previous build.
|
|
141
|
+
*/
|
|
142
|
+
private build;
|
|
143
|
+
/**
|
|
144
|
+
* Reconcile the shadow slot divs with the current core state using CSS class
|
|
145
|
+
* toggles. Called after every user action (input, keydown, paste, focus, click).
|
|
146
|
+
*
|
|
147
|
+
* Uses `this.shadow.activeElement` instead of `document.activeElement` to
|
|
148
|
+
* correctly detect focus within the shadow root across all browsers — the
|
|
149
|
+
* document active element is the host `<digito-input>` element, not the
|
|
150
|
+
* internal hidden input.
|
|
151
|
+
*/
|
|
152
|
+
private syncSlotsToDOM;
|
|
153
|
+
/**
|
|
154
|
+
* Apply or remove the disabled state directly on existing DOM nodes without
|
|
155
|
+
* triggering a full rebuild. Used by both `build()` (initial disabled attr)
|
|
156
|
+
* and `setDisabled()` (runtime toggle).
|
|
157
|
+
*/
|
|
158
|
+
private applyDisabledDOM;
|
|
159
|
+
/**
|
|
160
|
+
* Wire all event listeners to the hidden input element.
|
|
161
|
+
* Called once at the end of each `build()`. Because `build()` creates a fresh
|
|
162
|
+
* `hiddenInput` element, there is no need to `removeEventListener` — the old
|
|
163
|
+
* element is discarded and its listeners are garbage-collected with it.
|
|
164
|
+
*
|
|
165
|
+
* @param selectOnFocus When `true`, focusing a filled slot selects its character.
|
|
166
|
+
* @param blurOnComplete When `true`, blurs the input after the last slot is filled.
|
|
167
|
+
*/
|
|
168
|
+
private attachEvents;
|
|
169
|
+
/**
|
|
170
|
+
* Dispatch a `change` CustomEvent carrying the current code string.
|
|
171
|
+
* Fired after every input, paste, and backspace action.
|
|
172
|
+
* `composed: true` lets the event cross the shadow root boundary so host-page
|
|
173
|
+
* listeners registered with `el.addEventListener('change', ...)` receive it.
|
|
174
|
+
*/
|
|
175
|
+
private dispatchChange;
|
|
176
|
+
/** Clear all slots, reset the timer display, and re-focus the hidden input. */
|
|
177
|
+
reset(): void;
|
|
178
|
+
/** Apply or clear the error state on all visual slots. */
|
|
179
|
+
setError(isError: boolean): void;
|
|
180
|
+
/** Apply or clear the success state on all visual slots. Stops the timer on success. */
|
|
181
|
+
setSuccess(isSuccess: boolean): void;
|
|
182
|
+
/**
|
|
183
|
+
* Enable or disable the input at runtime.
|
|
184
|
+
* Equivalent to toggling the `disabled` HTML attribute but without triggering
|
|
185
|
+
* a full rebuild. Re-enabling automatically restores focus to the active slot.
|
|
186
|
+
*/
|
|
187
|
+
setDisabled(value: boolean): void;
|
|
188
|
+
/** Returns the current code as a joined string (e.g. `"123456"`). */
|
|
189
|
+
getCode(): string;
|
|
190
|
+
}
|
|
191
|
+
export { DigitoInput };
|
|
192
|
+
//# sourceMappingURL=web-component.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-component.d.ts","sourceRoot":"","sources":["../../src/adapters/web-component.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAuLH,cAAM,WAAY,SAAQ,WAAW;IACnC;;;;OAIG;IACH,MAAM,CAAC,kBAAkB,WAA6L;IAGtN,OAAO,CAAC,OAAO,CAA2C;IAC1D,OAAO,CAAC,QAAQ,CAA0C;IAC1D,OAAO,CAAC,WAAW,CAA0C;IAC7D,OAAO,CAAC,OAAO,CAA8C;IAC7D,OAAO,CAAC,YAAY,CAAyC;IAC7D,OAAO,CAAC,QAAQ,CAA6C;IAC7D,OAAO,CAAC,SAAS,CAAmD;IACpE,OAAO,CAAC,eAAe,CAA8C;IACrE,OAAO,CAAC,MAAM,CAAuD;IACrE,OAAO,CAAC,MAAM,CAAoB;IAGlC,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,UAAU,CAAU;IAK5B,OAAO,CAAC,QAAQ,CAAyC;IACzD,OAAO,CAAC,iBAAiB,CAAmD;IAC5E,OAAO,CAAC,WAAW,CAAwD;IAC3E,OAAO,CAAC,SAAS,CAA8C;IAC/D,OAAO,CAAC,QAAQ,CAA+C;IAC/D,OAAO,CAAC,OAAO,CAAgD;IAC/D,OA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