@funstack/ui 0.1.0-alpha.20 → 0.1.0-alpha.21
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/dist/alert-dialog/alert-dialog-root.mjs +2 -1
- package/dist/alert-dialog/alert-dialog-root.mjs.map +1 -1
- package/dist/avatar/avatar-context.mjs +24 -0
- package/dist/avatar/avatar-context.mjs.map +1 -0
- package/dist/avatar/avatar-fallback.d.mts +32 -0
- package/dist/avatar/avatar-fallback.d.mts.map +1 -0
- package/dist/avatar/avatar-fallback.mjs +31 -0
- package/dist/avatar/avatar-fallback.mjs.map +1 -0
- package/dist/avatar/avatar-image.d.mts +37 -0
- package/dist/avatar/avatar-image.d.mts.map +1 -0
- package/dist/avatar/avatar-image.mjs +91 -0
- package/dist/avatar/avatar-image.mjs.map +1 -0
- package/dist/avatar/avatar-root.d.mts +48 -0
- package/dist/avatar/avatar-root.d.mts.map +1 -0
- package/dist/avatar/avatar-root.mjs +81 -0
- package/dist/avatar/avatar-root.mjs.map +1 -0
- package/dist/avatar/index.d.mts +24 -0
- package/dist/avatar/index.d.mts.map +1 -0
- package/dist/avatar/index.mjs +24 -0
- package/dist/avatar/index.mjs.map +1 -0
- package/dist/dialog/dialog-close.d.mts.map +1 -1
- package/dist/dialog/dialog-close.mjs +14 -3
- package/dist/dialog/dialog-close.mjs.map +1 -1
- package/dist/dialog/dialog-context.mjs +7 -1
- package/dist/dialog/dialog-context.mjs.map +1 -1
- package/dist/dialog/dialog-popup.d.mts +1 -0
- package/dist/dialog/dialog-popup.d.mts.map +1 -1
- package/dist/dialog/dialog-popup.mjs +20 -2
- package/dist/dialog/dialog-popup.mjs.map +1 -1
- package/dist/dialog/dialog-root.mjs +6 -4
- package/dist/dialog/dialog-root.mjs.map +1 -1
- package/dist/docs/components/avatar.md +354 -0
- package/dist/docs/components/drawer.md +622 -0
- package/dist/docs/components/otp-field.md +501 -0
- package/dist/docs/components/scroll-area.md +294 -0
- package/dist/docs/index.md +5 -1
- package/dist/docs/roadmap.md +6 -0
- package/dist/drawer/drawer-close.d.mts +26 -0
- package/dist/drawer/drawer-close.d.mts.map +1 -0
- package/dist/drawer/drawer-close.mjs +19 -0
- package/dist/drawer/drawer-close.mjs.map +1 -0
- package/dist/drawer/drawer-description.d.mts +23 -0
- package/dist/drawer/drawer-description.d.mts.map +1 -0
- package/dist/drawer/drawer-description.mjs +18 -0
- package/dist/drawer/drawer-description.mjs.map +1 -0
- package/dist/drawer/drawer-form.d.mts +24 -0
- package/dist/drawer/drawer-form.d.mts.map +1 -0
- package/dist/drawer/drawer-form.mjs +19 -0
- package/dist/drawer/drawer-form.mjs.map +1 -0
- package/dist/drawer/drawer-popup.d.mts +77 -0
- package/dist/drawer/drawer-popup.d.mts.map +1 -0
- package/dist/drawer/drawer-popup.mjs +124 -0
- package/dist/drawer/drawer-popup.mjs.map +1 -0
- package/dist/drawer/drawer-root.d.mts +31 -0
- package/dist/drawer/drawer-root.d.mts.map +1 -0
- package/dist/drawer/drawer-root.mjs +23 -0
- package/dist/drawer/drawer-root.mjs.map +1 -0
- package/dist/drawer/drawer-swipe.d.mts +5 -0
- package/dist/drawer/drawer-swipe.d.mts.map +1 -0
- package/dist/drawer/drawer-swipe.mjs +193 -0
- package/dist/drawer/drawer-swipe.mjs.map +1 -0
- package/dist/drawer/drawer-title.d.mts +23 -0
- package/dist/drawer/drawer-title.d.mts.map +1 -0
- package/dist/drawer/drawer-title.mjs +18 -0
- package/dist/drawer/drawer-title.mjs.map +1 -0
- package/dist/drawer/drawer-trigger.d.mts +23 -0
- package/dist/drawer/drawer-trigger.d.mts.map +1 -0
- package/dist/drawer/drawer-trigger.mjs +18 -0
- package/dist/drawer/drawer-trigger.mjs.map +1 -0
- package/dist/drawer/index.d.mts +34 -0
- package/dist/drawer/index.d.mts.map +1 -0
- package/dist/drawer/index.mjs +33 -0
- package/dist/drawer/index.mjs.map +1 -0
- package/dist/index.d.mts +19 -1
- package/dist/index.mjs +4 -1
- package/dist/internal/swipe-arithmetic.mjs +75 -0
- package/dist/internal/swipe-arithmetic.mjs.map +1 -0
- package/dist/internal/swipe.mjs +169 -0
- package/dist/internal/swipe.mjs.map +1 -0
- package/dist/menu/menu-context-trigger.mjs +1 -1
- package/dist/menu/menu-popup.mjs +1 -1
- package/dist/otp-field/index.d.mts +23 -0
- package/dist/otp-field/index.d.mts.map +1 -0
- package/dist/otp-field/index.mjs +23 -0
- package/dist/otp-field/index.mjs.map +1 -0
- package/dist/otp-field/otp-field-context.mjs +13 -0
- package/dist/otp-field/otp-field-context.mjs.map +1 -0
- package/dist/otp-field/otp-field-filter.d.mts +5 -0
- package/dist/otp-field/otp-field-filter.d.mts.map +1 -0
- package/dist/otp-field/otp-field-filter.mjs +135 -0
- package/dist/otp-field/otp-field-filter.mjs.map +1 -0
- package/dist/otp-field/otp-field-input.d.mts +41 -0
- package/dist/otp-field/otp-field-input.d.mts.map +1 -0
- package/dist/otp-field/otp-field-input.mjs +177 -0
- package/dist/otp-field/otp-field-input.mjs.map +1 -0
- package/dist/otp-field/otp-field-root.d.mts +143 -0
- package/dist/otp-field/otp-field-root.d.mts.map +1 -0
- package/dist/otp-field/otp-field-root.mjs +397 -0
- package/dist/otp-field/otp-field-root.mjs.map +1 -0
- package/dist/otp-field/otp-field-slot.d.mts +52 -0
- package/dist/otp-field/otp-field-slot.d.mts.map +1 -0
- package/dist/otp-field/otp-field-slot.mjs +67 -0
- package/dist/otp-field/otp-field-slot.mjs.map +1 -0
- package/package.json +4 -1
- package/skills/funstack-ui-knowledge/SKILL.md +5 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
//#region src/otp-field/otp-field-filter.ts
|
|
2
|
+
const CHARSETS = {
|
|
3
|
+
numeric: {
|
|
4
|
+
test: /^[0-9]$/,
|
|
5
|
+
inputMode: "numeric",
|
|
6
|
+
pattern: "[0-9]"
|
|
7
|
+
},
|
|
8
|
+
alphanumeric: {
|
|
9
|
+
test: /^[A-Za-z0-9]$/,
|
|
10
|
+
inputMode: "text",
|
|
11
|
+
pattern: "[A-Za-z0-9]"
|
|
12
|
+
},
|
|
13
|
+
alphabetic: {
|
|
14
|
+
test: /^[A-Za-z]$/,
|
|
15
|
+
inputMode: "text",
|
|
16
|
+
pattern: "[A-Za-z]"
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
function rule(charset) {
|
|
20
|
+
return CHARSETS[charset] ?? CHARSETS.numeric;
|
|
21
|
+
}
|
|
22
|
+
/** `inputmode` for a charset: `numeric` for digits, `text` otherwise. */
|
|
23
|
+
function charsetInputMode(charset) {
|
|
24
|
+
return rule(charset).inputMode;
|
|
25
|
+
}
|
|
26
|
+
/** The derived `pattern`: `[0-9]{6}` for six digits. */
|
|
27
|
+
function charsetPattern(charset, length) {
|
|
28
|
+
return `${rule(charset).pattern}{${length}}`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Keeps the characters of `charset` in `text`, in order. Full-width digits (U+FF10 to U+FF19,
|
|
32
|
+
* what a Japanese or Chinese IME types) are converted to ASCII for `"numeric"` (section 3).
|
|
33
|
+
* Letters keep their case.
|
|
34
|
+
*/
|
|
35
|
+
function filterChars(text, charset) {
|
|
36
|
+
const { test } = rule(charset);
|
|
37
|
+
const numeric = charset === "numeric";
|
|
38
|
+
let kept = "";
|
|
39
|
+
for (const raw of text) {
|
|
40
|
+
let char = raw;
|
|
41
|
+
if (numeric) {
|
|
42
|
+
const code = raw.codePointAt(0);
|
|
43
|
+
if (code >= 65296 && code <= 65305) char = String.fromCharCode(code - 65296 + 48);
|
|
44
|
+
}
|
|
45
|
+
if (test.test(char)) kept += char;
|
|
46
|
+
}
|
|
47
|
+
return kept;
|
|
48
|
+
}
|
|
49
|
+
/** `filterChars`, cut to `length`: what a value may hold. */
|
|
50
|
+
function normalizeCode(text, charset, length) {
|
|
51
|
+
return filterChars(text, charset).slice(0, Math.max(0, length));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Applies one `input` event to the value (design/components/otp-field.md 4.1). The browser has
|
|
55
|
+
* already edited the text; this computes what the filtered text and the caret should be:
|
|
56
|
+
*
|
|
57
|
+
* 1. the inserted run is what the browser put between the kept prefix and suffix of the text
|
|
58
|
+
* before the edit, filtered by `charset`;
|
|
59
|
+
* 2. a run of `length` characters or more replaces everything (a pasted full code);
|
|
60
|
+
* 3. a selected range is replaced by the run;
|
|
61
|
+
* 4. a collapsed caret overtypes: the run replaces as many characters as it has;
|
|
62
|
+
* 5. a complete code with the caret at its end treats the last position as the caret's;
|
|
63
|
+
* 6. a deletion, and an `input` with no `beforeinput`, is `filter(dom)`;
|
|
64
|
+
* 7. the result is cut to `length`, the caret goes after the run.
|
|
65
|
+
*
|
|
66
|
+
* An insertion the filter dropped entirely leaves the value as it was and restores the
|
|
67
|
+
* selection (section 4.2: "no feedback beyond the text not changing").
|
|
68
|
+
*/
|
|
69
|
+
function applyEdit(input) {
|
|
70
|
+
const { before, dom, caret, inputType, charset, length } = input;
|
|
71
|
+
const insertion = inputType.startsWith("insert");
|
|
72
|
+
const fallback = () => {
|
|
73
|
+
const value = normalizeCode(dom, charset, length);
|
|
74
|
+
return {
|
|
75
|
+
value,
|
|
76
|
+
caret: caret === null ? null : Math.min(filterChars(dom.slice(0, caret), charset).length, value.length),
|
|
77
|
+
kept: 0,
|
|
78
|
+
selection: null
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
if (before === null || !insertion) return fallback();
|
|
82
|
+
const { text, selectionStart, selectionEnd } = before;
|
|
83
|
+
let start = Math.min(selectionStart, selectionEnd);
|
|
84
|
+
let end = Math.max(selectionStart, selectionEnd);
|
|
85
|
+
const prefix = text.slice(0, start);
|
|
86
|
+
const suffix = text.slice(end);
|
|
87
|
+
if (dom.length < prefix.length + suffix.length || !dom.startsWith(prefix) || !dom.endsWith(suffix)) return fallback();
|
|
88
|
+
const kept = filterChars(dom.slice(start, dom.length - suffix.length), charset);
|
|
89
|
+
if (kept.length === 0) {
|
|
90
|
+
const value = normalizeCode(text, charset, length);
|
|
91
|
+
return {
|
|
92
|
+
value,
|
|
93
|
+
caret: null,
|
|
94
|
+
kept: 0,
|
|
95
|
+
selection: [Math.min(start, value.length), Math.min(end, value.length)]
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (kept.length >= length) return {
|
|
99
|
+
value: kept.slice(0, length),
|
|
100
|
+
caret: length,
|
|
101
|
+
kept: kept.length,
|
|
102
|
+
selection: null
|
|
103
|
+
};
|
|
104
|
+
const base = normalizeCode(text, charset, length);
|
|
105
|
+
start = Math.min(start, base.length);
|
|
106
|
+
end = Math.min(end, base.length);
|
|
107
|
+
let next;
|
|
108
|
+
if (start !== end) next = base.slice(0, start) + kept + base.slice(end);
|
|
109
|
+
else {
|
|
110
|
+
if (base.length >= length && start >= length) start = length - 1;
|
|
111
|
+
next = base.slice(0, start) + kept + base.slice(start + kept.length);
|
|
112
|
+
}
|
|
113
|
+
next = next.slice(0, length);
|
|
114
|
+
return {
|
|
115
|
+
value: next,
|
|
116
|
+
caret: Math.min(start + kept.length, length),
|
|
117
|
+
kept: kept.length,
|
|
118
|
+
selection: null
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** The active slots for a selection (section 4.3): `[from, to)`, empty when not focused. */
|
|
122
|
+
function activeRange(focused, selectionStart, selectionEnd, length) {
|
|
123
|
+
if (!focused || selectionStart === null || length <= 0) return [0, 0];
|
|
124
|
+
const start = Math.min(selectionStart, selectionEnd ?? selectionStart);
|
|
125
|
+
const end = Math.max(selectionStart, selectionEnd ?? selectionStart);
|
|
126
|
+
if (start === end) {
|
|
127
|
+
const slot = Math.min(start, length - 1);
|
|
128
|
+
return [slot, slot + 1];
|
|
129
|
+
}
|
|
130
|
+
return [Math.min(start, length), Math.min(end, length)];
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { activeRange, applyEdit, charsetInputMode, charsetPattern, filterChars, normalizeCode };
|
|
134
|
+
|
|
135
|
+
//# sourceMappingURL=otp-field-filter.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp-field-filter.mjs","names":[],"sources":["../../src/otp-field/otp-field-filter.ts"],"sourcesContent":["// Directive-free: the pure filter of an OTPField (design/components/otp-field.md 4.1) and the\n// `charset` table (section 3). No hook, no DOM.\n\n/** Which characters a code may contain (design/components/otp-field.md 3). */\nexport type OTPFieldCharset = \"numeric\" | \"alphanumeric\" | \"alphabetic\";\n\ninterface CharsetRule {\n /** One kept character, after the full-width conversion. */\n readonly test: RegExp;\n readonly inputMode: \"numeric\" | \"text\";\n /** The `pattern` character class. */\n readonly pattern: string;\n}\n\nconst CHARSETS: Record<OTPFieldCharset, CharsetRule> = {\n numeric: { test: /^[0-9]$/, inputMode: \"numeric\", pattern: \"[0-9]\" },\n alphanumeric: { test: /^[A-Za-z0-9]$/, inputMode: \"text\", pattern: \"[A-Za-z0-9]\" },\n alphabetic: { test: /^[A-Za-z]$/, inputMode: \"text\", pattern: \"[A-Za-z]\" },\n};\n\nfunction rule(charset: OTPFieldCharset): CharsetRule {\n return CHARSETS[charset] ?? CHARSETS.numeric;\n}\n\n/** `inputmode` for a charset: `numeric` for digits, `text` otherwise. */\nexport function charsetInputMode(charset: OTPFieldCharset): \"numeric\" | \"text\" {\n return rule(charset).inputMode;\n}\n\n/** The derived `pattern`: `[0-9]{6}` for six digits. */\nexport function charsetPattern(charset: OTPFieldCharset, length: number): string {\n return `${rule(charset).pattern}{${length}}`;\n}\n\n/**\n * Keeps the characters of `charset` in `text`, in order. Full-width digits (U+FF10 to U+FF19,\n * what a Japanese or Chinese IME types) are converted to ASCII for `\"numeric\"` (section 3).\n * Letters keep their case.\n */\nexport function filterChars(text: string, charset: OTPFieldCharset): string {\n const { test } = rule(charset);\n const numeric = charset === \"numeric\";\n let kept = \"\";\n for (const raw of text) {\n let char = raw;\n if (numeric) {\n const code = raw.codePointAt(0)!;\n if (code >= 0xff10 && code <= 0xff19) char = String.fromCharCode(code - 0xff10 + 48);\n }\n if (test.test(char)) kept += char;\n }\n return kept;\n}\n\n/** `filterChars`, cut to `length`: what a value may hold. */\nexport function normalizeCode(text: string, charset: OTPFieldCharset, length: number): string {\n return filterChars(text, charset).slice(0, Math.max(0, length));\n}\n\n/** The input's text and selection when `beforeinput` fired. */\nexport interface EditBefore {\n readonly text: string;\n readonly selectionStart: number;\n readonly selectionEnd: number;\n}\n\nexport interface EditInput {\n /** The record from `beforeinput`; `null` when no `beforeinput` preceded the `input` (rule 6). */\n readonly before: EditBefore | null;\n /** The input's text after the browser's edit. */\n readonly dom: string;\n /** The input's `selectionStart` after the browser's edit (`null`: unknown). */\n readonly caret: number | null;\n /** `InputEvent.inputType` (`\"\"` when unknown). */\n readonly inputType: string;\n readonly charset: OTPFieldCharset;\n readonly length: number;\n}\n\nexport interface EditResult {\n /** The filtered value. */\n readonly value: string;\n /** Where the caret goes (collapsed), or `null` for \"where the browser left it\". */\n readonly caret: number | null;\n /**\n * Characters the edit inserted and the filter kept: `0` for a deletion and for an insertion\n * the filter dropped entirely (a letter typed into a numeric field). Completion runs only for\n * an edit that kept something or changed the value.\n */\n readonly kept: number;\n /** The selection to restore when the edit was dropped entirely: the one before it. */\n readonly selection: readonly [start: number, end: number] | null;\n}\n\n/**\n * Applies one `input` event to the value (design/components/otp-field.md 4.1). The browser has\n * already edited the text; this computes what the filtered text and the caret should be:\n *\n * 1. the inserted run is what the browser put between the kept prefix and suffix of the text\n * before the edit, filtered by `charset`;\n * 2. a run of `length` characters or more replaces everything (a pasted full code);\n * 3. a selected range is replaced by the run;\n * 4. a collapsed caret overtypes: the run replaces as many characters as it has;\n * 5. a complete code with the caret at its end treats the last position as the caret's;\n * 6. a deletion, and an `input` with no `beforeinput`, is `filter(dom)`;\n * 7. the result is cut to `length`, the caret goes after the run.\n *\n * An insertion the filter dropped entirely leaves the value as it was and restores the\n * selection (section 4.2: \"no feedback beyond the text not changing\").\n */\nexport function applyEdit(input: EditInput): EditResult {\n const { before, dom, caret, inputType, charset, length } = input;\n const insertion = inputType.startsWith(\"insert\");\n\n const fallback = (): EditResult => {\n const value = normalizeCode(dom, charset, length);\n // The caret keeps its place among the characters the filter kept before it.\n const at =\n caret === null\n ? null\n : Math.min(filterChars(dom.slice(0, caret), charset).length, value.length);\n return { value, caret: at, kept: 0, selection: null };\n };\n\n if (before === null || !insertion) return fallback();\n\n const { text, selectionStart, selectionEnd } = before;\n let start = Math.min(selectionStart, selectionEnd);\n let end = Math.max(selectionStart, selectionEnd);\n const prefix = text.slice(0, start);\n const suffix = text.slice(end);\n // Rule 1: the browser replaced [start, end) with the run; anything else (an engine that edits\n // elsewhere) falls back to rule 6.\n if (\n dom.length < prefix.length + suffix.length ||\n !dom.startsWith(prefix) ||\n !dom.endsWith(suffix)\n ) {\n return fallback();\n }\n const run = dom.slice(start, dom.length - suffix.length);\n const kept = filterChars(run, charset);\n\n if (kept.length === 0) {\n // Dropped entirely: the text before the edit, the selection where it was.\n const value = normalizeCode(text, charset, length);\n return {\n value,\n caret: null,\n kept: 0,\n selection: [Math.min(start, value.length), Math.min(end, value.length)],\n };\n }\n\n // Rule 2: a full code replaces everything.\n if (kept.length >= length) {\n return { value: kept.slice(0, length), caret: length, kept: kept.length, selection: null };\n }\n\n const base = normalizeCode(text, charset, length);\n start = Math.min(start, base.length);\n end = Math.min(end, base.length);\n let next: string;\n if (start !== end) {\n // Rule 3: the selected range is replaced.\n next = base.slice(0, start) + kept + base.slice(end);\n } else {\n // Rule 5: at the end of a complete code the last position is the caret's.\n if (base.length >= length && start >= length) start = length - 1;\n // Rule 4: overtype.\n next = base.slice(0, start) + kept + base.slice(start + kept.length);\n }\n next = next.slice(0, length);\n return {\n value: next,\n caret: Math.min(start + kept.length, length),\n kept: kept.length,\n selection: null,\n };\n}\n\n/** The active slots for a selection (section 4.3): `[from, to)`, empty when not focused. */\nexport function activeRange(\n focused: boolean,\n selectionStart: number | null,\n selectionEnd: number | null,\n length: number,\n): readonly [from: number, to: number] {\n if (!focused || selectionStart === null || length <= 0) return [0, 0];\n const start = Math.min(selectionStart, selectionEnd ?? selectionStart);\n const end = Math.max(selectionStart, selectionEnd ?? selectionStart);\n if (start === end) {\n const slot = Math.min(start, length - 1);\n return [slot, slot + 1];\n }\n return [Math.min(start, length), Math.min(end, length)];\n}\n"],"mappings":";AAcA,MAAM,WAAiD;CACrD,SAAS;EAAE,MAAM;EAAW,WAAW;EAAW,SAAS;CAAQ;CACnE,cAAc;EAAE,MAAM;EAAiB,WAAW;EAAQ,SAAS;CAAc;CACjF,YAAY;EAAE,MAAM;EAAc,WAAW;EAAQ,SAAS;CAAW;AAC3E;AAEA,SAAS,KAAK,SAAuC;CACnD,OAAO,SAAS,YAAY,SAAS;AACvC;;AAGA,SAAgB,iBAAiB,SAA8C;CAC7E,OAAO,KAAK,OAAO,CAAC,CAAC;AACvB;;AAGA,SAAgB,eAAe,SAA0B,QAAwB;CAC/E,OAAO,GAAG,KAAK,OAAO,CAAC,CAAC,QAAQ,GAAG,OAAO;AAC5C;;;;;;AAOA,SAAgB,YAAY,MAAc,SAAkC;CAC1E,MAAM,EAAE,SAAS,KAAK,OAAO;CAC7B,MAAM,UAAU,YAAY;CAC5B,IAAI,OAAO;CACX,KAAK,MAAM,OAAO,MAAM;EACtB,IAAI,OAAO;EACX,IAAI,SAAS;GACX,MAAM,OAAO,IAAI,YAAY,CAAC;GAC9B,IAAI,QAAQ,SAAU,QAAQ,OAAQ,OAAO,OAAO,aAAa,OAAO,QAAS,EAAE;EACrF;EACA,IAAI,KAAK,KAAK,IAAI,GAAG,QAAQ;CAC/B;CACA,OAAO;AACT;;AAGA,SAAgB,cAAc,MAAc,SAA0B,QAAwB;CAC5F,OAAO,YAAY,MAAM,OAAO,CAAC,CAAC,MAAM,GAAG,KAAK,IAAI,GAAG,MAAM,CAAC;AAChE;;;;;;;;;;;;;;;;;AAqDA,SAAgB,UAAU,OAA8B;CACtD,MAAM,EAAE,QAAQ,KAAK,OAAO,WAAW,SAAS,WA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KAEZ,OAAO,KAAK,MAAM,GAAG,KAAK,IAAI,OAAO,KAAK,MAAM,GAAG;MAC9C;EAEL,IAAI,KAAK,UAAU,UAAU,SAAS,QAAQ,QAAQ,SAAS;EAE/D,OAAO,KAAK,MAAM,GAAG,KAAK,IAAI,OAAO,KAAK,MAAM,QAAQ,KAAK,MAAM;CACrE;CACA,OAAO,KAAK,MAAM,GAAG,MAAM;CAC3B,OAAO;EACL,OAAO;EACP,OAAO,KAAK,IAAI,QAAQ,KAAK,QAAQ,MAAM;EAC3C,MAAM,KAAK;EACX,WAAW;CACb;AACF;;AAGA,SAAgB,YACd,SACA,gBACA,cACA,QACqC;CACrC,IAAI,CAAC,WAAW,mBAAmB,QAAQ,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC;CACpE,MAAM,QAAQ,KAAK,IAAI,gBAAgB,gBAAgB,cAAc;CACrE,MAAM,MAAM,KAAK,IAAI,gBAAgB,gBAAgB,cAAc;CACnE,IAAI,UAAU,KAAK;EACjB,MAAM,OAAO,KAAK,IAAI,OAAO,SAAS,CAAC;EACvC,OAAO,CAAC,MAAM,OAAO,CAAC;CACxB;CACA,OAAO,CAAC,KAAK,IAAI,OAAO,MAAM,GAAG,KAAK,IAAI,KAAK,MAAM,CAAC;AACxD"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { OTPFieldState } from "./otp-field-root.mjs";
|
|
3
|
+
import { CSSProperties, ComponentPropsWithRef } from "react";
|
|
4
|
+
//#region src/otp-field/otp-field-input.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Props of `OTPField.Input`.
|
|
7
|
+
*
|
|
8
|
+
* @see https://ui.funstack.work/components/otp-field
|
|
9
|
+
*/
|
|
10
|
+
export interface OTPFieldInputProps extends Omit<ComponentPropsWithRef<"input">, "type" | "value" | "defaultValue" | "maxLength" | "minLength" | "pattern" | "name" | "form" | "required" | "disabled" | "readOnly" | "onChange" | "className" | "style" | "children"> {
|
|
11
|
+
/** Class name, or a function of the state that returns one. */
|
|
12
|
+
className?: StateValue<string | undefined, OTPFieldState>;
|
|
13
|
+
/**
|
|
14
|
+
* Inline style, or a function of the state that returns one. The library's own inline style
|
|
15
|
+
* (the Input covers the Root with transparent text) wins over these keys.
|
|
16
|
+
*/
|
|
17
|
+
style?: StateValue<CSSProperties | undefined, OTPFieldState>;
|
|
18
|
+
/** Must render an `<input>`. */
|
|
19
|
+
render?: RenderProp<ComponentPropsWithRef<"input">, OTPFieldState>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The one text input of an OTPField (design/components/otp-field.md 1, 4): `type="text"`,
|
|
23
|
+
* `autocomplete="one-time-code"`, `inputmode` and `pattern` from the Root's `charset`, covering
|
|
24
|
+
* the Root with transparent text. The only focusable and the only element assistive technology
|
|
25
|
+
* sees. Registers with an enclosing `Field.Root` as `Field.Control` does.
|
|
26
|
+
*
|
|
27
|
+
* Owned keys (conventions 1.2): `type`, `value`, `pattern`, `name`, `form`, `required`,
|
|
28
|
+
* `disabled`, `readOnly`, `onChange` (and the library's native `beforeinput`, composition,
|
|
29
|
+
* selection and click listeners), the inline style above, the Field's `id`, `aria-describedby`
|
|
30
|
+
* (token-merged) and `aria-invalid`, `data-complete`, `data-pending`, `data-disabled`,
|
|
31
|
+
* `data-invalid`. Defaults the consumer's own prop replaces: `autoComplete="one-time-code"`,
|
|
32
|
+
* `inputMode`, `autoCapitalize="off"`, `autoCorrect="off"`, `spellCheck={false}`.
|
|
33
|
+
*
|
|
34
|
+
* @see https://ui.funstack.work/components/otp-field
|
|
35
|
+
*/
|
|
36
|
+
export declare function OTPFieldInput({ className, style, render, ...rest }: OTPFieldInputProps): import("react").ReactNode;
|
|
37
|
+
export declare namespace OTPFieldInput {
|
|
38
|
+
var displayName: string;
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
//# sourceMappingURL=otp-field-input.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp-field-input.d.mts","names":[],"sources":["../../src/otp-field/otp-field-input.tsx"],"mappings":";;;;;;;;;iBAuBiB,2BAA2B,KAC1C;;EAkBA,YAAY,+BAA+B;;;;;EAK3C,QAAQ,WAAW,2BAA2B;;EAE9C,SAAS,WAAW,gCAAgC;;;;;;;;;;;;;;;;;wBAyCtC,gBAAgB,WAAW,OAAO,WAAW,QAAQ,qCAAkB;yBAAvE"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { dataAttr } from "../internal/data-attr.mjs";
|
|
3
|
+
import { useRender } from "../internal/use-render.mjs";
|
|
4
|
+
import { useLatest } from "../internal/use-latest.mjs";
|
|
5
|
+
import { useElementCheck } from "../internal/use-element-check.mjs";
|
|
6
|
+
import { useMergedRefs } from "../internal/use-merged-refs.mjs";
|
|
7
|
+
import { useFieldControl } from "../internal/form/use-field-control.mjs";
|
|
8
|
+
import { useFormOwnerEvent } from "../internal/form/use-form-owner-event.mjs";
|
|
9
|
+
import { useOTPFieldContext } from "./otp-field-context.mjs";
|
|
10
|
+
import { charsetInputMode, charsetPattern } from "./otp-field-filter.mjs";
|
|
11
|
+
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
|
12
|
+
//#region src/otp-field/otp-field-input.tsx
|
|
13
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
14
|
+
/**
|
|
15
|
+
* The Input covers the Root with transparent text and caret (design 6). Functional, owned; the
|
|
16
|
+
* outline is left to the consumer. Provisional until the device probes are done (changelog).
|
|
17
|
+
*/
|
|
18
|
+
const INPUT_STYLE = {
|
|
19
|
+
position: "absolute",
|
|
20
|
+
inset: 0,
|
|
21
|
+
inlineSize: "100%",
|
|
22
|
+
blockSize: "100%",
|
|
23
|
+
margin: 0,
|
|
24
|
+
padding: 0,
|
|
25
|
+
border: 0,
|
|
26
|
+
background: "transparent",
|
|
27
|
+
color: "transparent",
|
|
28
|
+
caretColor: "transparent",
|
|
29
|
+
forcedColorAdjust: "none"
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The one text input of an OTPField (design/components/otp-field.md 1, 4): `type="text"`,
|
|
33
|
+
* `autocomplete="one-time-code"`, `inputmode` and `pattern` from the Root's `charset`, covering
|
|
34
|
+
* the Root with transparent text. The only focusable and the only element assistive technology
|
|
35
|
+
* sees. Registers with an enclosing `Field.Root` as `Field.Control` does.
|
|
36
|
+
*
|
|
37
|
+
* Owned keys (conventions 1.2): `type`, `value`, `pattern`, `name`, `form`, `required`,
|
|
38
|
+
* `disabled`, `readOnly`, `onChange` (and the library's native `beforeinput`, composition,
|
|
39
|
+
* selection and click listeners), the inline style above, the Field's `id`, `aria-describedby`
|
|
40
|
+
* (token-merged) and `aria-invalid`, `data-complete`, `data-pending`, `data-disabled`,
|
|
41
|
+
* `data-invalid`. Defaults the consumer's own prop replaces: `autoComplete="one-time-code"`,
|
|
42
|
+
* `inputMode`, `autoCapitalize="off"`, `autoCorrect="off"`, `spellCheck={false}`.
|
|
43
|
+
*
|
|
44
|
+
* @see https://ui.funstack.work/components/otp-field
|
|
45
|
+
*/
|
|
46
|
+
function OTPFieldInput({ className, style, render, ...rest }) {
|
|
47
|
+
const ctx = useOTPFieldContext("OTPField.Input");
|
|
48
|
+
const { state, inputRef, experimentalWebOTP, webOTPAbort } = ctx;
|
|
49
|
+
const field = useFieldControl({
|
|
50
|
+
labelable: true,
|
|
51
|
+
name: ctx.name,
|
|
52
|
+
disabled: state.disabled,
|
|
53
|
+
required: ctx.required,
|
|
54
|
+
id: rest.id,
|
|
55
|
+
component: "OTPField.Input"
|
|
56
|
+
});
|
|
57
|
+
const checkElement = useElementCheck(isDev && ((element) => element.tagName !== "INPUT" ? `OTPField.Input: rendered <${element.tagName.toLowerCase()}>. Render an <input>: the field's text, autofill and form value live in it.` : null));
|
|
58
|
+
const libraryRef = useCallback((element) => {
|
|
59
|
+
checkElement?.(element);
|
|
60
|
+
inputRef.current = element;
|
|
61
|
+
return () => {
|
|
62
|
+
if (inputRef.current === element) inputRef.current = null;
|
|
63
|
+
};
|
|
64
|
+
}, [checkElement, inputRef]);
|
|
65
|
+
const ref = useMergedRefs(libraryRef, field.ref);
|
|
66
|
+
const { beforeInput, compositionStart, compositionEnd, selectionChanged, blurred, click, inputMounted, webOTPCode } = ctx;
|
|
67
|
+
useLayoutEffect(() => {
|
|
68
|
+
const input = inputRef.current;
|
|
69
|
+
if (input === null) return;
|
|
70
|
+
const listeners = [
|
|
71
|
+
["beforeinput", beforeInput],
|
|
72
|
+
["compositionstart", compositionStart],
|
|
73
|
+
["compositionend", compositionEnd],
|
|
74
|
+
["selectionchange", selectionChanged],
|
|
75
|
+
["select", selectionChanged],
|
|
76
|
+
["focus", selectionChanged],
|
|
77
|
+
["keyup", selectionChanged],
|
|
78
|
+
["blur", blurred],
|
|
79
|
+
["click", click]
|
|
80
|
+
];
|
|
81
|
+
for (const [type, listener] of listeners) input.addEventListener(type, listener);
|
|
82
|
+
selectionChanged();
|
|
83
|
+
return () => {
|
|
84
|
+
for (const [type, listener] of listeners) input.removeEventListener(type, listener);
|
|
85
|
+
};
|
|
86
|
+
}, [
|
|
87
|
+
inputRef,
|
|
88
|
+
beforeInput,
|
|
89
|
+
compositionStart,
|
|
90
|
+
compositionEnd,
|
|
91
|
+
selectionChanged,
|
|
92
|
+
blurred,
|
|
93
|
+
click
|
|
94
|
+
]);
|
|
95
|
+
const mounted = useRef(false);
|
|
96
|
+
useLayoutEffect(() => {
|
|
97
|
+
if (mounted.current) return;
|
|
98
|
+
mounted.current = true;
|
|
99
|
+
inputMounted();
|
|
100
|
+
}, [inputMounted]);
|
|
101
|
+
const webOTPCodeRef = useLatest(webOTPCode);
|
|
102
|
+
const { webOTPDone } = ctx;
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (!experimentalWebOTP || !("OTPCredential" in window) || webOTPDone.current) return;
|
|
105
|
+
const controller = new AbortController();
|
|
106
|
+
webOTPAbort.current = controller;
|
|
107
|
+
const request = {
|
|
108
|
+
otp: { transport: ["sms"] },
|
|
109
|
+
signal: controller.signal
|
|
110
|
+
};
|
|
111
|
+
navigator.credentials.get(request).then((credential) => {
|
|
112
|
+
if (controller.signal.aborted) return;
|
|
113
|
+
const code = credential?.code;
|
|
114
|
+
if (typeof code === "string") webOTPCodeRef.current(code);
|
|
115
|
+
}, () => {});
|
|
116
|
+
return () => {
|
|
117
|
+
controller.abort();
|
|
118
|
+
if (webOTPAbort.current === controller) webOTPAbort.current = null;
|
|
119
|
+
};
|
|
120
|
+
}, [
|
|
121
|
+
experimentalWebOTP,
|
|
122
|
+
webOTPAbort,
|
|
123
|
+
webOTPDone,
|
|
124
|
+
webOTPCodeRef
|
|
125
|
+
]);
|
|
126
|
+
useFormOwnerEvent(inputRef, "submit", () => {
|
|
127
|
+
webOTPAbort.current?.abort();
|
|
128
|
+
webOTPAbort.current = null;
|
|
129
|
+
});
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
if (!isDev) return;
|
|
132
|
+
const input = inputRef.current;
|
|
133
|
+
if (input === null) return;
|
|
134
|
+
if (!((input.labels?.length ?? 0) > 0 || (input.getAttribute("aria-label") ?? "").trim() !== "" || (input.getAttribute("aria-labelledby") ?? "").trim() !== "")) console.error("OTPField.Input: the input has no accessible name. Associate a <label for>, use Field.Label, or give it aria-label / aria-labelledby.");
|
|
135
|
+
}, []);
|
|
136
|
+
const dataInvalid = ctx.fieldInvalid === void 0 ? {} : { "data-invalid": dataAttr(ctx.fieldInvalid) };
|
|
137
|
+
const libraryProps = {
|
|
138
|
+
...field.attributes,
|
|
139
|
+
type: "text",
|
|
140
|
+
value: ctx.inputText,
|
|
141
|
+
pattern: charsetPattern(ctx.charset, state.length),
|
|
142
|
+
name: field.name,
|
|
143
|
+
form: ctx.form,
|
|
144
|
+
required: field.required,
|
|
145
|
+
disabled: field.disabled,
|
|
146
|
+
readOnly: state.readOnly,
|
|
147
|
+
onChange: ctx.change,
|
|
148
|
+
style: INPUT_STYLE,
|
|
149
|
+
"data-complete": dataAttr(state.complete),
|
|
150
|
+
"data-pending": dataAttr(state.pending),
|
|
151
|
+
"data-disabled": dataAttr(state.disabled),
|
|
152
|
+
...dataInvalid
|
|
153
|
+
};
|
|
154
|
+
return useRender({
|
|
155
|
+
defaultElement: "input",
|
|
156
|
+
render,
|
|
157
|
+
state,
|
|
158
|
+
className,
|
|
159
|
+
style,
|
|
160
|
+
children: void 0,
|
|
161
|
+
props: {
|
|
162
|
+
autoComplete: "one-time-code",
|
|
163
|
+
inputMode: charsetInputMode(ctx.charset),
|
|
164
|
+
autoCapitalize: "off",
|
|
165
|
+
autoCorrect: "off",
|
|
166
|
+
spellCheck: false,
|
|
167
|
+
...rest
|
|
168
|
+
},
|
|
169
|
+
libraryProps,
|
|
170
|
+
ref
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
OTPFieldInput.displayName = "OTPFieldInput";
|
|
174
|
+
//#endregion
|
|
175
|
+
export { OTPFieldInput };
|
|
176
|
+
|
|
177
|
+
//# sourceMappingURL=otp-field-input.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp-field-input.mjs","names":[],"sources":["../../src/otp-field/otp-field-input.tsx"],"sourcesContent":["\"use client\";\nimport { useCallback, useEffect, useLayoutEffect, useRef } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties } from \"react\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport { useFieldControl } from \"../internal/form/use-field-control.js\";\nimport { useFormOwnerEvent } from \"../internal/form/use-form-owner-event.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { useElementCheck } from \"../internal/use-element-check.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useMergedRefs } from \"../internal/use-merged-refs.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport type { DataAttributes } from \"../internal/use-render.js\";\nimport { useOTPFieldContext } from \"./otp-field-context.js\";\nimport { charsetInputMode, charsetPattern } from \"./otp-field-filter.js\";\nimport type { OTPFieldState } from \"./otp-field-root.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/**\n * Props of `OTPField.Input`.\n *\n * @see https://ui.funstack.work/components/otp-field\n */\nexport interface OTPFieldInputProps extends Omit<\n ComponentPropsWithRef<\"input\">,\n | \"type\"\n | \"value\"\n | \"defaultValue\"\n | \"maxLength\"\n | \"minLength\"\n | \"pattern\"\n | \"name\"\n | \"form\"\n | \"required\"\n | \"disabled\"\n | \"readOnly\"\n | \"onChange\"\n | \"className\"\n | \"style\"\n | \"children\"\n> {\n /** Class name, or a function of the state that returns one. */\n className?: StateValue<string | undefined, OTPFieldState>;\n /**\n * Inline style, or a function of the state that returns one. The library's own inline style\n * (the Input covers the Root with transparent text) wins over these keys.\n */\n style?: StateValue<CSSProperties | undefined, OTPFieldState>;\n /** Must render an `<input>`. */\n render?: RenderProp<ComponentPropsWithRef<\"input\">, OTPFieldState>;\n}\n\n/**\n * The Input covers the Root with transparent text and caret (design 6). Functional, owned; the\n * outline is left to the consumer. Provisional until the device probes are done (changelog).\n */\nconst INPUT_STYLE: CSSProperties = {\n position: \"absolute\",\n inset: 0,\n inlineSize: \"100%\",\n blockSize: \"100%\",\n margin: 0,\n padding: 0,\n border: 0,\n background: \"transparent\",\n color: \"transparent\",\n caretColor: \"transparent\",\n forcedColorAdjust: \"none\",\n};\n\n/** The WebOTP request (design 8); not in the DOM typings yet. */\ninterface OTPCredentialRequest extends CredentialRequestOptions {\n otp: { transport: string[] };\n}\n\n/**\n * The one text input of an OTPField (design/components/otp-field.md 1, 4): `type=\"text\"`,\n * `autocomplete=\"one-time-code\"`, `inputmode` and `pattern` from the Root's `charset`, covering\n * the Root with transparent text. The only focusable and the only element assistive technology\n * sees. Registers with an enclosing `Field.Root` as `Field.Control` does.\n *\n * Owned keys (conventions 1.2): `type`, `value`, `pattern`, `name`, `form`, `required`,\n * `disabled`, `readOnly`, `onChange` (and the library's native `beforeinput`, composition,\n * selection and click listeners), the inline style above, the Field's `id`, `aria-describedby`\n * (token-merged) and `aria-invalid`, `data-complete`, `data-pending`, `data-disabled`,\n * `data-invalid`. Defaults the consumer's own prop replaces: `autoComplete=\"one-time-code\"`,\n * `inputMode`, `autoCapitalize=\"off\"`, `autoCorrect=\"off\"`, `spellCheck={false}`.\n *\n * @see https://ui.funstack.work/components/otp-field\n */\nexport function OTPFieldInput({ className, style, render, ...rest }: OTPFieldInputProps) {\n const ctx = useOTPFieldContext(\"OTPField.Input\");\n const { state, inputRef, experimentalWebOTP, webOTPAbort } = ctx;\n\n const field = useFieldControl({\n labelable: true,\n name: ctx.name,\n disabled: state.disabled,\n required: ctx.required,\n id: rest.id,\n component: \"OTPField.Input\",\n });\n\n const checkElement = useElementCheck(\n isDev &&\n ((element) =>\n element.tagName !== \"INPUT\"\n ? `OTPField.Input: rendered <${element.tagName.toLowerCase()}>. Render an <input>: ` +\n \"the field's text, autofill and form value live in it.\"\n : null),\n );\n const libraryRef = useCallback(\n (element: HTMLElement) => {\n checkElement?.(element);\n inputRef.current = element as HTMLInputElement;\n return () => {\n if (inputRef.current === element) inputRef.current = null;\n };\n },\n [checkElement, inputRef],\n );\n const ref = useMergedRefs<HTMLElement>(libraryRef, field.ref);\n\n // --- Native listeners (design 4): React has no `beforeinput` and no `selectionchange`. -------\n\n const {\n beforeInput,\n compositionStart,\n compositionEnd,\n selectionChanged,\n blurred,\n click,\n inputMounted,\n webOTPCode,\n } = ctx;\n useLayoutEffect(() => {\n const input = inputRef.current;\n if (input === null) return;\n const listeners: [string, (event: never) => void][] = [\n [\"beforeinput\", beforeInput],\n [\"compositionstart\", compositionStart],\n [\"compositionend\", compositionEnd],\n [\"selectionchange\", selectionChanged],\n [\"select\", selectionChanged],\n [\"focus\", selectionChanged],\n [\"keyup\", selectionChanged],\n [\"blur\", blurred],\n [\"click\", click],\n ];\n for (const [type, listener] of listeners) {\n input.addEventListener(type, listener as EventListener);\n }\n // Focused before hydration (or by `autoFocus`): no `focus` event will tell the Root.\n selectionChanged();\n return () => {\n for (const [type, listener] of listeners) {\n input.removeEventListener(type, listener as EventListener);\n }\n };\n }, [inputRef, beforeInput, compositionStart, compositionEnd, selectionChanged, blurred, click]);\n\n // Text typed, autofilled or restored before hydration is adopted once (design 9).\n const mounted = useRef(false);\n useLayoutEffect(() => {\n if (mounted.current) return;\n mounted.current = true;\n inputMounted();\n }, [inputMounted]);\n\n // --- WebOTP (design 8) ------------------------------------------------------------------------\n\n // Keyed on the flag alone: the request lives as long as the Input (and the flag), and the code\n // is handed over through the latest callback.\n const webOTPCodeRef = useLatest(webOTPCode);\n const { webOTPDone } = ctx;\n useEffect(() => {\n if (!experimentalWebOTP || !(\"OTPCredential\" in window) || webOTPDone.current) return;\n const controller = new AbortController();\n webOTPAbort.current = controller;\n const request: OTPCredentialRequest = {\n otp: { transport: [\"sms\"] },\n signal: controller.signal,\n };\n navigator.credentials.get(request).then(\n (credential) => {\n if (controller.signal.aborted) return;\n const code = (credential as { code?: unknown } | null)?.code;\n if (typeof code === \"string\") webOTPCodeRef.current(code);\n },\n // Declined, no message, aborted: the normal outcomes.\n () => {},\n );\n return () => {\n controller.abort();\n if (webOTPAbort.current === controller) webOTPAbort.current = null;\n };\n }, [experimentalWebOTP, webOTPAbort, webOTPDone, webOTPCodeRef]);\n useFormOwnerEvent(inputRef, \"submit\", () => {\n webOTPAbort.current?.abort();\n webOTPAbort.current = null;\n });\n\n // --- Development: an unnamed input (design 5) -------------------------------------------------\n\n useEffect(() => {\n if (!isDev) return;\n const input = inputRef.current;\n if (input === null) return;\n const named =\n (input.labels?.length ?? 0) > 0 ||\n (input.getAttribute(\"aria-label\") ?? \"\").trim() !== \"\" ||\n (input.getAttribute(\"aria-labelledby\") ?? \"\").trim() !== \"\";\n if (!named) {\n console.error(\n \"OTPField.Input: the input has no accessible name. Associate a <label for>, use \" +\n \"Field.Label, or give it aria-label / aria-labelledby.\",\n );\n }\n // Mount only.\n // oxlint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n // --- Render -----------------------------------------------------------------------------------\n\n const dataInvalid =\n ctx.fieldInvalid === undefined ? {} : { \"data-invalid\": dataAttr(ctx.fieldInvalid) };\n const libraryProps: ComponentPropsWithRef<\"input\"> & DataAttributes = {\n ...field.attributes,\n type: \"text\",\n value: ctx.inputText,\n pattern: charsetPattern(ctx.charset, state.length),\n name: field.name,\n form: ctx.form,\n required: field.required,\n disabled: field.disabled,\n readOnly: state.readOnly,\n onChange: ctx.change,\n style: INPUT_STYLE,\n \"data-complete\": dataAttr(state.complete),\n \"data-pending\": dataAttr(state.pending),\n \"data-disabled\": dataAttr(state.disabled),\n ...dataInvalid,\n };\n\n return useRender<ComponentPropsWithRef<\"input\">, OTPFieldState>({\n defaultElement: \"input\",\n render,\n state,\n className,\n style,\n children: undefined,\n props: {\n autoComplete: \"one-time-code\",\n inputMode: charsetInputMode(ctx.charset),\n autoCapitalize: \"off\",\n autoCorrect: \"off\",\n spellCheck: false,\n ...rest,\n },\n libraryProps,\n ref,\n });\n}\n\nOTPFieldInput.displayName = \"OTPFieldInput\";\n"],"mappings":";;;;;;;;;;;;AAgBA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;;;;AAwCvC,MAAM,cAA6B;CACjC,UAAU;CACV,OAAO;CACP,YAAY;CACZ,WAAW;CACX,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,YAAY;CACZ,OAAO;CACP,YAAY;CACZ,mBAAmB;AACrB;;;;;;;;;;;;;;;;AAsBA,SAAgB,cAAc,EAAE,WAAW,OAAO,QAAQ,GAAG,QAA4B;CACvF,MAAM,MAAM,mBAAmB,gBAAgB;CAC/C,MAAM,EAAE,OAAO,UAAU,oBAAoB,gBAAgB;CAE7D,MAAM,QAAQ,gBAAgB;EAC5B,WAAW;EACX,MAAM,IAAI;EACV,UAAU,MAAM;EAChB,UAAU,IAAI;EACd,IAAI,KAAK;EACT,WAAW;CACb,CAAC;CAED,MAAM,eAAe,gBACnB,WACI,YACA,QAAQ,YAAY,U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;EACA;EACA,UAAU,KAAA;EACV,OAAO;GACL,cAAc;GACd,WAAW,iBAAiB,IAAI,OAAO;GACvC,gBAAgB;GAChB,aAAa;GACb,YAAY;GACZ,GAAG;EACL;EACA;EACA;CACF,CAAC;AACH;AAEA,cAAc,cAAc"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Action, ActionDetails, ChangeReason, RenderProp, StateValue, UrgentChangeHandler } from "../internal/types.mjs";
|
|
2
|
+
import { OTPFieldCharset } from "./otp-field-filter.mjs";
|
|
3
|
+
import { CSSProperties, ComponentPropsWithRef, ReactNode } from "react";
|
|
4
|
+
//#region src/otp-field/otp-field-root.d.ts
|
|
5
|
+
/** Details passed as the second argument of `onValueChange`. */
|
|
6
|
+
export interface OTPFieldValueChangeDetails extends ActionDetails {
|
|
7
|
+
/**
|
|
8
|
+
* `"activate"`: the user edited the text (`details.event` is the `input` event, or the
|
|
9
|
+
* `compositionend` that ended a composition). `"platform"`: a form reset (the `reset` event),
|
|
10
|
+
* text adopted at hydration or a WebOTP fill (`undefined`).
|
|
11
|
+
*/
|
|
12
|
+
readonly reason: ChangeReason;
|
|
13
|
+
}
|
|
14
|
+
/** Details passed as the second argument of `completeAction`. */
|
|
15
|
+
export interface OTPFieldCompleteDetails extends ActionDetails {
|
|
16
|
+
/**
|
|
17
|
+
* `"activate"`: an edit completed the code (typing, paste, keyboard autofill; `details.event`
|
|
18
|
+
* is the `input` event). `"platform"`: a WebOTP fill (`details.event` is `undefined`).
|
|
19
|
+
*/
|
|
20
|
+
readonly reason: ChangeReason;
|
|
21
|
+
}
|
|
22
|
+
/** The state the Root's and the Input's function props receive. */
|
|
23
|
+
export interface OTPFieldState {
|
|
24
|
+
/** The text shown, already filtered: at most `length` characters of `charset`. */
|
|
25
|
+
readonly value: string;
|
|
26
|
+
/** The number of characters in a complete code. */
|
|
27
|
+
readonly length: number;
|
|
28
|
+
/** `value.length === length`. */
|
|
29
|
+
readonly complete: boolean;
|
|
30
|
+
/** A `completeAction` is in flight. */
|
|
31
|
+
readonly pending: boolean;
|
|
32
|
+
/** The `disabled` prop, or the enclosing Field's. */
|
|
33
|
+
readonly disabled: boolean;
|
|
34
|
+
/** The `readOnly` prop. */
|
|
35
|
+
readonly readOnly: boolean;
|
|
36
|
+
/** Inside a `Field.Root`: an error is shown for the field. `false` outside. */
|
|
37
|
+
readonly invalid: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Props of `OTPField.Root`.
|
|
41
|
+
*
|
|
42
|
+
* @see https://ui.funstack.work/components/otp-field
|
|
43
|
+
*/
|
|
44
|
+
export interface OTPFieldRootProps extends Omit<ComponentPropsWithRef<"div">, "className" | "style" | "children" | "defaultValue"> {
|
|
45
|
+
/** The number of characters in a complete code. */
|
|
46
|
+
length: number;
|
|
47
|
+
/**
|
|
48
|
+
* Which characters a code may contain; also picks the Input's `inputmode` and `pattern`.
|
|
49
|
+
*
|
|
50
|
+
* @defaultValue `"numeric"`
|
|
51
|
+
*/
|
|
52
|
+
charset?: OTPFieldCharset;
|
|
53
|
+
/** Controlled text. Follows the input: it cannot hold an edit back (the urgent lane). */
|
|
54
|
+
value?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Initial text in uncontrolled mode (filtered), and the form-reset target.
|
|
57
|
+
*
|
|
58
|
+
* @defaultValue `""`
|
|
59
|
+
*/
|
|
60
|
+
defaultValue?: string;
|
|
61
|
+
/**
|
|
62
|
+
* The text changed: an edit (`"activate"`), a form reset, text adopted at hydration or a WebOTP
|
|
63
|
+
* fill (`"platform"`). A fact: called synchronously, outside any transition, not awaited.
|
|
64
|
+
* Called before `completeAction` for the same edit.
|
|
65
|
+
*/
|
|
66
|
+
onValueChange?: UrgentChangeHandler<string, OTPFieldValueChangeDetails>;
|
|
67
|
+
/**
|
|
68
|
+
* An edit left the code complete: runs in the Root's transition and is awaited, with
|
|
69
|
+
* `data-pending` on the Root and the Input. Runs for every such edit, also while an earlier run
|
|
70
|
+
* is pending (a different code), and for the same code pasted again. Not at mount, on a reset,
|
|
71
|
+
* for a controlled change, or while disabled or read-only. A rejection reaches the nearest Error
|
|
72
|
+
* Boundary. May be a Server Function.
|
|
73
|
+
*/
|
|
74
|
+
completeAction?: Action<[value: string, details: OTPFieldCompleteDetails]>;
|
|
75
|
+
/**
|
|
76
|
+
* Submit the Input's form (`requestSubmit()`) when an edit leaves the code complete, right
|
|
77
|
+
* after dispatching `completeAction`. A change of context on input (WCAG 3.2.2): say so before
|
|
78
|
+
* the field.
|
|
79
|
+
*
|
|
80
|
+
* @defaultValue `false`
|
|
81
|
+
*/
|
|
82
|
+
submitOnComplete?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Native `name` on the Input.
|
|
85
|
+
*
|
|
86
|
+
* @defaultValue the Field's `name` inside a `Field.Root`
|
|
87
|
+
*/
|
|
88
|
+
name?: string;
|
|
89
|
+
/** Native `form` on the Input. */
|
|
90
|
+
form?: string;
|
|
91
|
+
/**
|
|
92
|
+
* Native `required` on the Input; or the Field's.
|
|
93
|
+
*
|
|
94
|
+
* @defaultValue `false`
|
|
95
|
+
*/
|
|
96
|
+
required?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Native `disabled` on the Input; or the Field's.
|
|
99
|
+
*
|
|
100
|
+
* @defaultValue `false`
|
|
101
|
+
*/
|
|
102
|
+
disabled?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Native `readonly` on the Input: no edits, no completion.
|
|
105
|
+
*
|
|
106
|
+
* @defaultValue `false`
|
|
107
|
+
*/
|
|
108
|
+
readOnly?: boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Ask the browser for the code of an origin-bound SMS (WebOTP, `navigator.credentials.get({
|
|
111
|
+
* otp })`) while the Input is mounted. Chromium and Safari; nothing happens where the API is
|
|
112
|
+
* missing (Firefox).
|
|
113
|
+
*
|
|
114
|
+
* @defaultValue `false`
|
|
115
|
+
*/
|
|
116
|
+
experimental_webOTP?: boolean;
|
|
117
|
+
/** Class name, or a function of the state that returns one. */
|
|
118
|
+
className?: StateValue<string | undefined, OTPFieldState>;
|
|
119
|
+
/** Inline style, or a function of the state that returns one. `position: relative` unless given. */
|
|
120
|
+
style?: StateValue<CSSProperties | undefined, OTPFieldState>;
|
|
121
|
+
/** The parts. The function form receives the state. */
|
|
122
|
+
children?: StateValue<ReactNode, OTPFieldState>;
|
|
123
|
+
/** Replace the default `<div>` (element form) or take over rendering (function form). */
|
|
124
|
+
render?: RenderProp<ComponentPropsWithRef<"div">, OTPFieldState>;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Owns the text of a one-time-code field (design/components/otp-field.md): the urgent value
|
|
128
|
+
* (`value` / `defaultValue` / `onValueChange`), the filter that keeps it a code, the selection the
|
|
129
|
+
* Slots draw, the completion transition (`completeAction`, `submitOnComplete`), form reset,
|
|
130
|
+
* hydration adoption and WebOTP. Renders a `div` with `position: relative`: the box the Input
|
|
131
|
+
* covers.
|
|
132
|
+
*
|
|
133
|
+
* Owned keys (conventions 1.2): `data-complete`, `data-pending`, `data-disabled`,
|
|
134
|
+
* `data-invalid` (inside a Field); `position` in `style` unless the consumer's style sets it.
|
|
135
|
+
*
|
|
136
|
+
* @see https://ui.funstack.work/components/otp-field
|
|
137
|
+
*/
|
|
138
|
+
export declare function OTPFieldRoot(props: OTPFieldRootProps): import("react").JSX.Element;
|
|
139
|
+
export declare namespace OTPFieldRoot {
|
|
140
|
+
var displayName: string;
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
//# sourceMappingURL=otp-field-root.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp-field-root.d.mts","names":[],"sources":["../../src/otp-field/otp-field-root.tsx"],"mappings":";;;;;iBA+CiB,mCAAmC;;;;;;WAMzC,QAAQ;;;iBAIF,gCAAgC;;;;;WAKtC,QAAQ;;;iBAIF;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;;;;;iBAQM,0BAA0B,KACzC;;EAIA;;;;;;EAMA,UAAU;;EAEV;;;;;;EAMA;;;;;;EAMA,gBAAgB,4BAA4B;;;;;;;;EAQ5C,iBAAiB,QAAQ,eAAe,SAAS;;;;;;;;EAQjD;;;;;;EAMA;;EAEA;;;;;;EAMA;;;;;;EAMA;;;;;;EAMA;;;;;;;;EAQA;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,8BAA8B;;;;;;;;;;;;;;wBAiEpC,aAAa,OAAO,oCAAiB,IAAA;yBAArC"}
|