ochreui 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +153 -2
- package/cli/ochreui.mjs +601 -0
- package/mcp/core.mjs +126 -0
- package/mcp/docs.json +906 -0
- package/mcp/server.mjs +23 -0
- package/package.json +393 -3
- package/registry/agent-team.ts +350 -0
- package/registry/avatar.ts +300 -0
- package/registry/badge.ts +207 -0
- package/registry/breadcrumb.ts +435 -0
- package/registry/button.ts +238 -0
- package/registry/card.ts +221 -0
- package/registry/change-summary.ts +200 -0
- package/registry/checkbox.ts +204 -0
- package/registry/code-block.ts +332 -0
- package/registry/color-picker.ts +720 -0
- package/registry/comment-thread.ts +503 -0
- package/registry/context-menu.ts +281 -0
- package/registry/cover-flow.ts +623 -0
- package/registry/date-picker.ts +799 -0
- package/registry/decision-inbox.ts +340 -0
- package/registry/dialog.ts +577 -0
- package/registry/dropdown-menu.ts +864 -0
- package/registry/email-draft.ts +916 -0
- package/registry/empty-calendar.ts +199 -0
- package/registry/empty-state.ts +164 -0
- package/registry/enrichment-orbit.ts +164 -0
- package/registry/filter-menu.ts +541 -0
- package/registry/form-field.ts +308 -0
- package/registry/frost-cloud.ts +381 -0
- package/registry/ghosted-chat.ts +210 -0
- package/registry/glow-cloud.ts +402 -0
- package/registry/grid-background.ts +164 -0
- package/registry/hover-card.ts +401 -0
- package/registry/icon-button.ts +133 -0
- package/registry/index.json +2124 -0
- package/registry/integration-beam.ts +588 -0
- package/registry/integration-grid.ts +209 -0
- package/registry/kbd.ts +176 -0
- package/registry/layer-stack.ts +351 -0
- package/registry/lead-sourcing.ts +285 -0
- package/registry/logo-cluster.ts +239 -0
- package/registry/logo-marquee.ts +505 -0
- package/registry/message-actions.ts +833 -0
- package/registry/model-orbit.ts +583 -0
- package/registry/model-wheel.ts +372 -0
- package/registry/notification-stack.ts +301 -0
- package/registry/number-input.ts +552 -0
- package/registry/number-ticker.ts +261 -0
- package/registry/ochre.css +917 -0
- package/registry/otp-input.ts +704 -0
- package/registry/overlay.ts +312 -0
- package/registry/pagination.ts +362 -0
- package/registry/popover.ts +296 -0
- package/registry/profile-menu.ts +332 -0
- package/registry/progress-tracker.ts +291 -0
- package/registry/rating.ts +461 -0
- package/registry/reaction-bar.ts +322 -0
- package/registry/region-map.ts +537 -0
- package/registry/resizable.ts +489 -0
- package/registry/scroll-area.ts +271 -0
- package/registry/segmented-control.ts +433 -0
- package/registry/select.ts +769 -0
- package/registry/separator.ts +97 -0
- package/registry/shimmer-loader.ts +211 -0
- package/registry/side-folder.ts +152 -0
- package/registry/skeleton.ts +136 -0
- package/registry/skill/INSTRUCTIONS.md +11 -0
- package/registry/skill/SKILL.md +65 -0
- package/registry/skill/checklist.md +45 -0
- package/registry/skill/references/agent-team.md +105 -0
- package/registry/skill/references/avatar.md +88 -0
- package/registry/skill/references/badge.md +92 -0
- package/registry/skill/references/breadcrumb.md +99 -0
- package/registry/skill/references/button.md +82 -0
- package/registry/skill/references/card.md +111 -0
- package/registry/skill/references/catalog.md +114 -0
- package/registry/skill/references/change-summary.md +103 -0
- package/registry/skill/references/checkbox.md +105 -0
- package/registry/skill/references/code-block.md +109 -0
- package/registry/skill/references/color-picker.md +138 -0
- package/registry/skill/references/comment-thread.md +164 -0
- package/registry/skill/references/context-menu.md +205 -0
- package/registry/skill/references/cover-flow.md +128 -0
- package/registry/skill/references/date-picker.md +154 -0
- package/registry/skill/references/decision-inbox.md +126 -0
- package/registry/skill/references/dialog.md +155 -0
- package/registry/skill/references/dropdown-menu.md +144 -0
- package/registry/skill/references/email-draft.md +182 -0
- package/registry/skill/references/empty-calendar.md +74 -0
- package/registry/skill/references/empty-state.md +118 -0
- package/registry/skill/references/enrichment-orbit.md +71 -0
- package/registry/skill/references/filter-menu.md +143 -0
- package/registry/skill/references/form-field.md +118 -0
- package/registry/skill/references/frost-cloud.md +80 -0
- package/registry/skill/references/ghosted-chat.md +75 -0
- package/registry/skill/references/glow-cloud.md +84 -0
- package/registry/skill/references/grid-background.md +76 -0
- package/registry/skill/references/hover-card.md +125 -0
- package/registry/skill/references/icon-button.md +99 -0
- package/registry/skill/references/integration-beam.md +107 -0
- package/registry/skill/references/integration-grid.md +100 -0
- package/registry/skill/references/kbd.md +65 -0
- package/registry/skill/references/layer-stack.md +115 -0
- package/registry/skill/references/lead-sourcing.md +92 -0
- package/registry/skill/references/logo-cluster.md +92 -0
- package/registry/skill/references/logo-marquee.md +123 -0
- package/registry/skill/references/message-actions.md +146 -0
- package/registry/skill/references/model-orbit.md +106 -0
- package/registry/skill/references/model-wheel.md +84 -0
- package/registry/skill/references/notification-stack.md +138 -0
- package/registry/skill/references/number-input.md +117 -0
- package/registry/skill/references/number-ticker.md +73 -0
- package/registry/skill/references/otp-input.md +142 -0
- package/registry/skill/references/pagination.md +112 -0
- package/registry/skill/references/popover.md +143 -0
- package/registry/skill/references/profile-menu.md +123 -0
- package/registry/skill/references/progress-tracker.md +122 -0
- package/registry/skill/references/rating.md +111 -0
- package/registry/skill/references/reaction-bar.md +128 -0
- package/registry/skill/references/region-map.md +109 -0
- package/registry/skill/references/resizable.md +127 -0
- package/registry/skill/references/scroll-area.md +113 -0
- package/registry/skill/references/segmented-control.md +96 -0
- package/registry/skill/references/select.md +122 -0
- package/registry/skill/references/separator.md +69 -0
- package/registry/skill/references/shimmer-loader.md +83 -0
- package/registry/skill/references/side-folder.md +75 -0
- package/registry/skill/references/skeleton.md +95 -0
- package/registry/skill/references/smart-recommendation.md +132 -0
- package/registry/skill/references/spinner.md +84 -0
- package/registry/skill/references/switch.md +79 -0
- package/registry/skill/references/tab-overload.md +70 -0
- package/registry/skill/references/tag-input.md +126 -0
- package/registry/skill/references/team-invite.md +94 -0
- package/registry/skill/references/testimonial-wall.md +110 -0
- package/registry/skill/references/text-area.md +115 -0
- package/registry/skill/references/text-field.md +126 -0
- package/registry/skill/references/text-reveal.md +71 -0
- package/registry/skill/references/thinking-loader.md +87 -0
- package/registry/skill/references/time-picker.md +122 -0
- package/registry/skill/references/tips-panel.md +115 -0
- package/registry/skill/references/toggle-group.md +98 -0
- package/registry/skill/references/tool-call-card.md +151 -0
- package/registry/skill/references/tooltip.md +94 -0
- package/registry/skill/references/tree-view.md +172 -0
- package/registry/skill/theming.md +57 -0
- package/registry/smart-recommendation.ts +415 -0
- package/registry/spinner.ts +67 -0
- package/registry/switch.ts +146 -0
- package/registry/tab-overload.ts +211 -0
- package/registry/tag-input.ts +620 -0
- package/registry/team-invite.ts +248 -0
- package/registry/testimonial-wall.ts +521 -0
- package/registry/text-area.ts +432 -0
- package/registry/text-field.ts +415 -0
- package/registry/text-reveal.ts +175 -0
- package/registry/theme.ts +68 -0
- package/registry/thinking-loader.ts +302 -0
- package/registry/time-picker.ts +828 -0
- package/registry/tips-panel.ts +312 -0
- package/registry/toggle-group.ts +357 -0
- package/registry/tool-call-card.ts +578 -0
- package/registry/tooltip.ts +486 -0
- package/registry/tree-view.ts +454 -0
- package/styles/base.css +500 -0
- package/styles/tokens.css +415 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* OTP Input, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (the --ui-* tokens and shared keyframes), which `npx ochreui init` adds once.
|
|
5
|
+
*
|
|
6
|
+
* <ui-otp-input separator [status]="status()" [resendIn]="30" (completed)="verify($event)" (resend)="send()">
|
|
7
|
+
* <input uiOtp [formField]="form.code" />
|
|
8
|
+
* </ui-otp-input>
|
|
9
|
+
* One real <input> holds the code, so autofill, paste, password managers and Angular forms work; the boxes only draw it.
|
|
10
|
+
* `completed` fires when the last box fills; your app then sets `status` to `verifying`, `error` or `success`.
|
|
11
|
+
*
|
|
12
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
13
|
+
* --ui-otp-input-box widest box (default 40px; Classic 48px); boxes shrink to fit
|
|
14
|
+
* --ui-otp-input-radius corners (default a fifth of the box width)
|
|
15
|
+
* --ui-otp-input-ring focus ring and caret (default: the focus colour)
|
|
16
|
+
* --ui-text-field-edge resting edge, shared with Text Field
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
Component,
|
|
20
|
+
DOCUMENT,
|
|
21
|
+
DestroyRef,
|
|
22
|
+
Directive,
|
|
23
|
+
ElementRef,
|
|
24
|
+
afterNextRender,
|
|
25
|
+
afterRenderEffect,
|
|
26
|
+
booleanAttribute,
|
|
27
|
+
computed,
|
|
28
|
+
contentChild,
|
|
29
|
+
effect,
|
|
30
|
+
inject,
|
|
31
|
+
input,
|
|
32
|
+
linkedSignal,
|
|
33
|
+
numberAttribute,
|
|
34
|
+
output,
|
|
35
|
+
signal,
|
|
36
|
+
untracked,
|
|
37
|
+
} from '@angular/core';
|
|
38
|
+
|
|
39
|
+
export type OtpStatus = 'idle' | 'verifying' | 'error' | 'success';
|
|
40
|
+
export type OtpMode = 'numeric' | 'alphanumeric';
|
|
41
|
+
export type OtpAlign = 'start' | 'center';
|
|
42
|
+
|
|
43
|
+
export interface OtpInputLabels {
|
|
44
|
+
name: string;
|
|
45
|
+
digits: (n: number) => string;
|
|
46
|
+
characters: (n: number) => string;
|
|
47
|
+
verifying: string;
|
|
48
|
+
verified: string;
|
|
49
|
+
error: string;
|
|
50
|
+
noCode: string;
|
|
51
|
+
resendIn: string;
|
|
52
|
+
resend: string;
|
|
53
|
+
canResend: string;
|
|
54
|
+
resent: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const LABELS: OtpInputLabels = {
|
|
58
|
+
name: 'Verification code',
|
|
59
|
+
digits: (n) => `${n} digits`,
|
|
60
|
+
characters: (n) => `${n} characters`,
|
|
61
|
+
verifying: 'Verifying code',
|
|
62
|
+
verified: 'Verified',
|
|
63
|
+
error: "That code didn't work. Try again.",
|
|
64
|
+
noCode: "Didn't get a code?",
|
|
65
|
+
resendIn: 'Resend in',
|
|
66
|
+
resend: 'Resend code',
|
|
67
|
+
canResend: 'You can resend the code now',
|
|
68
|
+
resent: 'A new code is on its way',
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
let nextId = 0;
|
|
72
|
+
|
|
73
|
+
const ascii = (s: string) => s.replace(/[\uff01-\uff5e]/g, (c) => String.fromCharCode(c.charCodeAt(0) - 0xfee0));
|
|
74
|
+
|
|
75
|
+
/** Put it on the `<input>` inside a `<ui-otp-input>`. It owns the editing: one character per box, paste, autofill and arrows. */
|
|
76
|
+
@Directive({
|
|
77
|
+
selector: 'input[uiOtp]',
|
|
78
|
+
host: {
|
|
79
|
+
type: 'text',
|
|
80
|
+
autocomplete: 'one-time-code',
|
|
81
|
+
autocorrect: 'off',
|
|
82
|
+
spellcheck: 'false',
|
|
83
|
+
enterkeyhint: 'done',
|
|
84
|
+
dir: 'ltr',
|
|
85
|
+
style:
|
|
86
|
+
'position: absolute; inset: 0; z-index: 1; box-sizing: border-box; inline-size: 100%; block-size: 100%; margin: 0; padding: 0; ' +
|
|
87
|
+
'border: 0; outline: 0; background: none; color: transparent; caret-color: transparent; font-size: 16px; letter-spacing: -1em; ' +
|
|
88
|
+
'cursor: text; appearance: none; forced-color-adjust: none',
|
|
89
|
+
'[id]': 'id()',
|
|
90
|
+
'[attr.inputmode]': 'field.mode() === "numeric" ? "numeric" : "text"',
|
|
91
|
+
'[attr.pattern]': 'field.mode() === "numeric" ? "[0-9]*" : null',
|
|
92
|
+
'[attr.autocapitalize]': 'field.mode() === "numeric" ? "off" : "characters"',
|
|
93
|
+
'[attr.aria-label]': 'name()',
|
|
94
|
+
'[attr.aria-invalid]': 'field.showError() || null',
|
|
95
|
+
'[attr.aria-describedby]': 'describedBy()',
|
|
96
|
+
'(beforeinput)': 'edit($event)',
|
|
97
|
+
'(input)': 'typed($event)',
|
|
98
|
+
'(compositionend)': 'typed()',
|
|
99
|
+
'(keydown)': 'key($event)',
|
|
100
|
+
'(focus)': 'go(value().length)',
|
|
101
|
+
'(document:reset)': 'reset($event)',
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
export class UiOtp {
|
|
105
|
+
readonly id = input(`ui-otp-input-${nextId++}`);
|
|
106
|
+
/** Your own name for the field. Without it, the field's label or "Verification code, 6 digits". */
|
|
107
|
+
readonly ariaLabel = input<string>(undefined, { alias: 'aria-label' });
|
|
108
|
+
/** Your own descriptions, kept alongside the field's. Set it as `aria-describedby="…"`, not `[attr.aria-describedby]`. */
|
|
109
|
+
readonly ariaDescribedby = input<string>(undefined, { alias: 'aria-describedby' });
|
|
110
|
+
|
|
111
|
+
readonly el = inject<ElementRef<HTMLInputElement>>(ElementRef).nativeElement;
|
|
112
|
+
protected readonly field = inject(UiOtpInput);
|
|
113
|
+
private readonly destroy = inject(DestroyRef);
|
|
114
|
+
readonly value = signal('');
|
|
115
|
+
readonly disabled = signal(false);
|
|
116
|
+
private readonly at = signal(0);
|
|
117
|
+
/** The active box: never past the first empty one. */
|
|
118
|
+
readonly pos = computed(() => Math.min(this.at(), this.value().length, this.field.length() - 1));
|
|
119
|
+
private readonly labelled = this.el.hasAttribute('aria-labelledby');
|
|
120
|
+
private native?: PropertyDescriptor;
|
|
121
|
+
private own = false;
|
|
122
|
+
|
|
123
|
+
protected readonly name = computed(() => {
|
|
124
|
+
if (this.ariaLabel()) return this.ariaLabel();
|
|
125
|
+
if (this.labelled || this.field.label()) return null;
|
|
126
|
+
return [this.field.t().name, this.field.count()].filter(Boolean).join(', ');
|
|
127
|
+
});
|
|
128
|
+
protected readonly describedBy = computed(
|
|
129
|
+
() => [this.ariaDescribedby(), this.field.message() ? this.field.msgId : ''].filter(Boolean).join(' ') || null,
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
constructor() {
|
|
133
|
+
const el = this.el;
|
|
134
|
+
const native = (this.native = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), 'value'));
|
|
135
|
+
if (native?.set && native.get) {
|
|
136
|
+
Object.defineProperty(el, 'value', {
|
|
137
|
+
configurable: true,
|
|
138
|
+
get: () => native.get!.call(el),
|
|
139
|
+
set: (v: string) => {
|
|
140
|
+
native.set!.call(el, v);
|
|
141
|
+
queueMicrotask(() => this.read());
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
afterNextRender(() => {
|
|
146
|
+
this.read();
|
|
147
|
+
if (el.hasAttribute('autofocus')) el.focus({ preventScroll: true });
|
|
148
|
+
const view = el.ownerDocument.defaultView;
|
|
149
|
+
if (!view?.MutationObserver) return;
|
|
150
|
+
const watch = new view.MutationObserver(() => this.read());
|
|
151
|
+
watch.observe(el, { attributes: true, attributeFilter: ['disabled'] });
|
|
152
|
+
this.destroy.onDestroy(() => watch.disconnect());
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
read() {
|
|
157
|
+
this.value.set(this.el.value);
|
|
158
|
+
this.disabled.set(this.el.disabled);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private clean(text: string) {
|
|
162
|
+
text = ascii(text);
|
|
163
|
+
return this.field.mode() === 'numeric' ? text.replace(/\D/g, '') : text.replace(/[^a-z0-9]/gi, '').toUpperCase();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Moves the active box (clamped to the first empty one) and selects its character, so typing replaces it. */
|
|
167
|
+
go(i: number) {
|
|
168
|
+
this.at.set(Math.max(0, i));
|
|
169
|
+
const a = this.pos();
|
|
170
|
+
if (this.el.ownerDocument.activeElement === this.el) this.el.setSelectionRange(a, a + (a < this.value().length ? 1 : 0));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Writes a value the way typing would, so every kind of form sees it. */
|
|
174
|
+
write(next: string, at: number, inputType = 'insertText') {
|
|
175
|
+
if (this.native?.set) this.native.set.call(this.el, next);
|
|
176
|
+
else this.el.value = next;
|
|
177
|
+
this.value.set(next);
|
|
178
|
+
this.go(at);
|
|
179
|
+
this.own = true;
|
|
180
|
+
this.el.dispatchEvent(new InputEvent('input', { bubbles: true, inputType }));
|
|
181
|
+
this.own = false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
protected edit(e: InputEvent) {
|
|
185
|
+
if (e.isComposing) return;
|
|
186
|
+
e.preventDefault();
|
|
187
|
+
const el = this.el;
|
|
188
|
+
if (el.readOnly || el.disabled) return;
|
|
189
|
+
const n = this.field.length();
|
|
190
|
+
const prev = this.value();
|
|
191
|
+
const all = prev.length > 1 && el.selectionStart === 0 && el.selectionEnd === prev.length;
|
|
192
|
+
const base = all ? '' : prev;
|
|
193
|
+
let a = all ? 0 : this.pos();
|
|
194
|
+
let next: string;
|
|
195
|
+
const type = e.inputType;
|
|
196
|
+
if (type.startsWith('insert')) {
|
|
197
|
+
const clean = this.clean(e.data ?? e.dataTransfer?.getData('text') ?? '');
|
|
198
|
+
if (!clean) return;
|
|
199
|
+
if (clean.length >= n) [next, a] = [clean.slice(0, n), 0];
|
|
200
|
+
else next = (base.slice(0, a) + clean + base.slice(a + clean.length)).slice(0, n);
|
|
201
|
+
this.field.burst.set(clean.length > 1 ? a : -1);
|
|
202
|
+
a += clean.length;
|
|
203
|
+
} else if (type.startsWith('delete')) {
|
|
204
|
+
this.field.burst.set(-1);
|
|
205
|
+
if (all) next = '';
|
|
206
|
+
else if (type.includes('Backward') && !base[a]) {
|
|
207
|
+
if (!a) return;
|
|
208
|
+
next = base.slice(0, --a) + base.slice(a + 1);
|
|
209
|
+
} else next = base.slice(0, a) + base.slice(a + 1);
|
|
210
|
+
} else return;
|
|
211
|
+
this.write(next, a, type);
|
|
212
|
+
this.field.edited(prev, next);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
protected typed(e?: Event) {
|
|
216
|
+
if (this.own || (e as InputEvent | undefined)?.isComposing) return;
|
|
217
|
+
const prev = this.value();
|
|
218
|
+
const next = this.clean(this.el.value).slice(0, this.field.length());
|
|
219
|
+
if (next !== this.el.value) this.write(next, next.length);
|
|
220
|
+
else {
|
|
221
|
+
this.value.set(next);
|
|
222
|
+
this.go(next.length);
|
|
223
|
+
}
|
|
224
|
+
this.field.edited(prev, next);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
protected reset(e: Event) {
|
|
228
|
+
if (e.target !== this.el.form) return;
|
|
229
|
+
setTimeout(() => {
|
|
230
|
+
this.read();
|
|
231
|
+
this.go(this.value().length);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
protected key(e: KeyboardEvent) {
|
|
236
|
+
if (e.isComposing || e.altKey || e.ctrlKey || e.metaKey) return;
|
|
237
|
+
const a = this.pos();
|
|
238
|
+
const to = { ArrowLeft: a - 1, ArrowRight: a + 1, Home: 0, End: this.value().length }[e.key];
|
|
239
|
+
if (to === undefined) return;
|
|
240
|
+
e.preventDefault();
|
|
241
|
+
this.go(to);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** The drawn row: boxes, separator, focus ring and the Verified chip. Screen readers skip it; they hear the input. */
|
|
246
|
+
@Component({
|
|
247
|
+
selector: 'ui-otp-boxes',
|
|
248
|
+
host: {
|
|
249
|
+
'aria-hidden': 'true',
|
|
250
|
+
dir: 'ltr',
|
|
251
|
+
'[attr.data-nudge]': 'f.nudge() || null',
|
|
252
|
+
'[attr.data-fade]': 'f.fading() || null',
|
|
253
|
+
'[style.--a]': 'pos()',
|
|
254
|
+
'[style.--p]': 'f.sepAt() > 0 && pos() >= f.sepAt() ? 1 : 0',
|
|
255
|
+
'(animationend)': '$event.target === $event.currentTarget && f.nudge.set(false)',
|
|
256
|
+
'(animationcancel)': '$event.target === $event.currentTarget && f.nudge.set(false)',
|
|
257
|
+
},
|
|
258
|
+
template: `
|
|
259
|
+
@for (i of f.slots(); track i) {
|
|
260
|
+
@if (i === f.sepAt()) {<span class="sep"></span>}
|
|
261
|
+
<span class="box" [class.on]="!!f.chars()[i]" [style.--i]="i" [style.--d]="f.burst() >= 0 && i >= f.burst() ? i - f.burst() : 0">
|
|
262
|
+
@if (f.chars()[i]; as c) {<span class="ch">{{ c }}</span>}
|
|
263
|
+
</span>
|
|
264
|
+
}
|
|
265
|
+
<span class="ring">@if (!f.chars()[pos()]) {<span class="caret"></span>}</span>
|
|
266
|
+
<span class="box on chip">
|
|
267
|
+
@if (f.status() === 'success') {
|
|
268
|
+
<svg class="ui-icon" viewBox="0 0 16 16"><path class="ui-draw" pathLength="1" d="M3.5 8.5l3 3 6-7" /></svg>
|
|
269
|
+
<span>{{ f.t().verified }}</span>
|
|
270
|
+
}
|
|
271
|
+
</span>
|
|
272
|
+
`,
|
|
273
|
+
styles: `
|
|
274
|
+
:host {
|
|
275
|
+
position: relative;
|
|
276
|
+
display: flex;
|
|
277
|
+
gap: var(--_g);
|
|
278
|
+
}
|
|
279
|
+
:host([data-nudge]) { animation: ui-nudge var(--ui-duration-5) var(--ui-ease-out); }
|
|
280
|
+
.box {
|
|
281
|
+
position: relative;
|
|
282
|
+
display: grid;
|
|
283
|
+
place-items: center;
|
|
284
|
+
flex: none;
|
|
285
|
+
box-sizing: border-box;
|
|
286
|
+
inline-size: var(--_w);
|
|
287
|
+
block-size: var(--_h);
|
|
288
|
+
overflow: hidden;
|
|
289
|
+
border: var(--_bw) solid transparent;
|
|
290
|
+
border-radius: var(--_rad);
|
|
291
|
+
background: color-mix(in srgb, var(--ui-bg-subtle) calc(var(--_t) * 55%), var(--_fill));
|
|
292
|
+
box-shadow: 0 0 0 var(--_ew) var(--_eb, var(--_e)), 0 1px 1px -0.5px var(--_sh), 0 1px 3px var(--_sh);
|
|
293
|
+
color: var(--_ink);
|
|
294
|
+
font: var(--ui-weight-medium) max(12px, var(--_w) * (0.375 + 0.085 * var(--_t))) / 1 var(--ui-font-sans);
|
|
295
|
+
font-variant-numeric: tabular-nums;
|
|
296
|
+
opacity: var(--_on);
|
|
297
|
+
scale: calc(0.7 + 0.3 * var(--_on));
|
|
298
|
+
transition: var(--ui-duration-3) var(--ui-ease-out);
|
|
299
|
+
transition-property: background, color, box-shadow, opacity, scale;
|
|
300
|
+
}
|
|
301
|
+
.on { background: var(--_fill); --_eb: color-mix(in srgb, var(--ui-text) calc(var(--_t) * 9%), var(--_e)); }
|
|
302
|
+
.box::after {
|
|
303
|
+
content: '';
|
|
304
|
+
display: var(--_band, none);
|
|
305
|
+
position: absolute;
|
|
306
|
+
inset: 0;
|
|
307
|
+
background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--ui-bg-placeholder-sheen) 80%, transparent), transparent);
|
|
308
|
+
animation: ui-sheen calc(1.3s * var(--_m)) calc(var(--i) * 70ms) infinite both;
|
|
309
|
+
}
|
|
310
|
+
.ch { animation: ui-blur-in calc(280ms * var(--_m)) calc(var(--d) * var(--ui-stagger)) var(--ui-ease-out) both; transition: opacity 130ms; }
|
|
311
|
+
:host([data-fade]) .ch { opacity: 0; }
|
|
312
|
+
.sep { align-self: center; inline-size: 8px; border-top: calc(1px + 1px * var(--_t)) solid var(--ui-border-strong); border-radius: 1px; opacity: var(--_on); transition: opacity var(--ui-duration-3); }
|
|
313
|
+
.ring {
|
|
314
|
+
position: absolute;
|
|
315
|
+
inset: 0 auto 0 0;
|
|
316
|
+
display: grid;
|
|
317
|
+
place-items: center;
|
|
318
|
+
inline-size: var(--_w);
|
|
319
|
+
border-radius: var(--_rad);
|
|
320
|
+
outline: 2px solid transparent;
|
|
321
|
+
box-shadow: 0 0 0 calc(1px - var(--_t) * 1px) var(--_r), inset 0 0 0 calc(var(--_t) * 1.5px) var(--_r),
|
|
322
|
+
0 0 0 calc(3px + var(--_t) * 1px) color-mix(in srgb, var(--_r) calc(30% - 20% * var(--_t)), transparent);
|
|
323
|
+
opacity: calc(var(--_ro) * var(--_on));
|
|
324
|
+
translate: calc(var(--a) * (var(--_w) + var(--_g)) + var(--p) * (8px + var(--_g)));
|
|
325
|
+
transition: translate calc(260ms * var(--_m)) var(--ui-ease-out), opacity var(--ui-duration-3);
|
|
326
|
+
}
|
|
327
|
+
.caret {
|
|
328
|
+
block-size: calc(var(--_h) * 0.41);
|
|
329
|
+
border-inline-start: calc(1px + 0.5px * var(--_t)) solid color-mix(in srgb, var(--_r) calc(100% - 60% * var(--_t)), transparent);
|
|
330
|
+
animation: ui-wave calc(1.1s * var(--_m)) infinite;
|
|
331
|
+
}
|
|
332
|
+
.chip {
|
|
333
|
+
position: absolute;
|
|
334
|
+
top: 50%;
|
|
335
|
+
left: 50%;
|
|
336
|
+
grid-auto-flow: column;
|
|
337
|
+
gap: var(--ui-space-1-5);
|
|
338
|
+
inline-size: var(--_cw);
|
|
339
|
+
block-size: calc(var(--_h) - var(--_t) * (var(--_h) - 36px));
|
|
340
|
+
padding-inline: calc(14px + 2px * var(--_t));
|
|
341
|
+
border-radius: calc(var(--_rad) + var(--_t) * 99px);
|
|
342
|
+
--_fill: color-mix(in srgb, var(--ui-bg-surface) calc(var(--_t) * 100%), var(--ui-success-soft));
|
|
343
|
+
--_ink: var(--ui-text);
|
|
344
|
+
font: var(--ui-weight-medium) calc(var(--ui-text-base) + 1px * var(--_t)) / var(--ui-leading-base) var(--ui-font-sans);
|
|
345
|
+
opacity: calc(1 - var(--_on));
|
|
346
|
+
scale: 1;
|
|
347
|
+
translate: -50% -50%;
|
|
348
|
+
interpolate-size: allow-keywords;
|
|
349
|
+
transition: opacity var(--ui-duration-2) calc(120ms * var(--_m)), inline-size calc(460ms * var(--_m)) calc(220ms * var(--_m)) var(--ui-ease-out);
|
|
350
|
+
}
|
|
351
|
+
svg { inline-size: var(--ui-icon-md); stroke-width: calc(2 + 0.4 * var(--_t)); color: var(--ui-success); }
|
|
352
|
+
path { animation-delay: calc(560ms * var(--_m)); }
|
|
353
|
+
.chip span { animation: ui-fade-in var(--ui-duration-3) calc(460ms * var(--_m)) both; }
|
|
354
|
+
`,
|
|
355
|
+
})
|
|
356
|
+
export class UiOtpBoxes {
|
|
357
|
+
protected readonly f = inject(UiOtpInput);
|
|
358
|
+
protected readonly pos = computed(() => this.f.ctl()?.pos() ?? 0);
|
|
359
|
+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
360
|
+
|
|
361
|
+
constructor() {
|
|
362
|
+
let was: string[] = [];
|
|
363
|
+
afterRenderEffect(() => {
|
|
364
|
+
const now = this.f.chars();
|
|
365
|
+
const chars = this.host.querySelectorAll('.box:not(.chip)');
|
|
366
|
+
now.forEach((c, i) => {
|
|
367
|
+
if (!was[i] || was[i] === c) return;
|
|
368
|
+
for (const a of chars[i]?.querySelector('.ch')?.getAnimations?.() ?? []) {
|
|
369
|
+
a.currentTime = 0;
|
|
370
|
+
a.play();
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
was = now;
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** The lines under the boxes: the error or hint, and the resend countdown. */
|
|
379
|
+
@Component({
|
|
380
|
+
selector: 'ui-otp-foot',
|
|
381
|
+
template: `
|
|
382
|
+
<div class="msg" [style.grid-template-rows]="f.message() ? '1fr' : '0fr'">
|
|
383
|
+
<div>
|
|
384
|
+
<p [id]="f.msgId" dir="auto" [style.opacity]="f.message() ? 1 : 0">
|
|
385
|
+
@if (f.showError()) {<svg class="ui-icon" viewBox="0 0 16 16" aria-hidden="true"><circle cx="8" cy="8" r="6.25" /><path d="M8 5v3.5M8 11h0" /></svg>}
|
|
386
|
+
{{ f.text() }}
|
|
387
|
+
</p>
|
|
388
|
+
</div>
|
|
389
|
+
</div>
|
|
390
|
+
@if (f.resendIn() !== undefined) {
|
|
391
|
+
<div class="re">
|
|
392
|
+
{{ f.t().noCode }}
|
|
393
|
+
@if (f.left()) {
|
|
394
|
+
<span>{{ f.t().resendIn }} <b>{{ f.clock() }}</b></span>
|
|
395
|
+
} @else {
|
|
396
|
+
<button type="button" class="ui-btn-xs" [disabled]="f.ctl()?.disabled() || f.locked()" (click)="f.resendNow()">{{ f.t().resend }}</button>
|
|
397
|
+
}
|
|
398
|
+
</div>
|
|
399
|
+
}
|
|
400
|
+
`,
|
|
401
|
+
styles: `
|
|
402
|
+
:host { display: contents; }
|
|
403
|
+
.msg { display: grid; max-inline-size: 100%; transition: grid-template-rows var(--ui-duration-4) var(--ui-ease-out); }
|
|
404
|
+
.msg > div { overflow: hidden; }
|
|
405
|
+
p, .re { margin: 0; color: var(--ui-text-muted); font: calc(var(--ui-text-sm) + 0.5px * var(--_t)) / calc(var(--ui-leading-xs) + 1px * var(--_t)) var(--ui-font-sans); }
|
|
406
|
+
p { display: flex; gap: var(--ui-space-1-5); padding: calc(var(--ui-space-2) + 2px * var(--_t)) calc(2px * var(--_t)) 0; transition: opacity var(--ui-duration-3); }
|
|
407
|
+
p svg { inline-size: calc(var(--ui-icon-sm) + 1px * var(--_t)); block-size: 1lh; color: var(--ui-danger); }
|
|
408
|
+
.re {
|
|
409
|
+
display: flex;
|
|
410
|
+
flex-wrap: wrap;
|
|
411
|
+
align-items: center;
|
|
412
|
+
justify-content: var(--_jc, flex-start);
|
|
413
|
+
gap: var(--ui-space-1) var(--ui-space-2);
|
|
414
|
+
min-block-size: calc(var(--ui-control-h-sm) - 9px * var(--_t));
|
|
415
|
+
margin-block-start: calc(var(--ui-space-3) + 2px * var(--_t));
|
|
416
|
+
padding-inline: calc(2px * var(--_t));
|
|
417
|
+
opacity: var(--_on);
|
|
418
|
+
visibility: var(--_vis, visible);
|
|
419
|
+
transition: var(--ui-duration-3);
|
|
420
|
+
transition-property: opacity, visibility;
|
|
421
|
+
}
|
|
422
|
+
.re > span { color: color-mix(in srgb, var(--ui-text) 50%, var(--ui-text-muted)); }
|
|
423
|
+
b { display: inline-block; color: var(--ui-text); font-weight: inherit; font-variant-numeric: tabular-nums; }
|
|
424
|
+
button {
|
|
425
|
+
height: calc(var(--ui-control-h-sm) - 9px * var(--_t));
|
|
426
|
+
padding: 0 calc(10px - 10px * var(--_t));
|
|
427
|
+
background: none;
|
|
428
|
+
box-shadow: inset 0 0 0 calc(0.5px - 0.5px * var(--_t)) var(--ui-hairline);
|
|
429
|
+
color: color-mix(in srgb, var(--ui-text-link) calc(var(--_t) * 100%), var(--ui-text-muted));
|
|
430
|
+
font: var(--ui-weight-medium) calc(var(--ui-text-base) - 0.5px * var(--_t)) / var(--ui-leading-base) var(--ui-font-sans);
|
|
431
|
+
transition: var(--ui-duration-2);
|
|
432
|
+
}
|
|
433
|
+
@media (hover: hover) {
|
|
434
|
+
button:hover:enabled {
|
|
435
|
+
background: color-mix(in srgb, var(--ui-bg-subtle) calc(100% - var(--_t) * 100%), transparent);
|
|
436
|
+
color: color-mix(in srgb, var(--ui-text-link) calc(var(--_t) * 100%), var(--ui-text));
|
|
437
|
+
text-decoration: underline color-mix(in srgb, currentColor calc(var(--_t) * 100%), transparent);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
`,
|
|
441
|
+
})
|
|
442
|
+
class UiOtpFoot {
|
|
443
|
+
protected readonly f = inject(UiOtpInput);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
@Component({
|
|
447
|
+
selector: 'ui-otp-input',
|
|
448
|
+
imports: [UiOtpBoxes, UiOtpFoot],
|
|
449
|
+
host: {
|
|
450
|
+
'[attr.data-status]': 'status()',
|
|
451
|
+
'[attr.data-align]': 'align()',
|
|
452
|
+
'[attr.data-error]': 'showError() || null',
|
|
453
|
+
'[style.--_n]': 'length()',
|
|
454
|
+
'[style.--_k]': 'sepAt() > 0 ? 1 : 0',
|
|
455
|
+
},
|
|
456
|
+
template: `
|
|
457
|
+
<div class="q">
|
|
458
|
+
@if (label()) {
|
|
459
|
+
<label [for]="ctl()?.id()">{{ label() }}<span class="ui-sr">, {{ count() }}</span></label>
|
|
460
|
+
}
|
|
461
|
+
<div class="row" (pointerdown)="press($event)" (click)="point($event)">
|
|
462
|
+
<ui-otp-boxes />
|
|
463
|
+
<ng-content select="input" />
|
|
464
|
+
</div>
|
|
465
|
+
<ui-otp-foot />
|
|
466
|
+
</div>
|
|
467
|
+
<span class="ui-sr" aria-live="polite">{{ live() }}</span>
|
|
468
|
+
`,
|
|
469
|
+
styles: `
|
|
470
|
+
:host {
|
|
471
|
+
--_t: var(--ui-look-tray, 0);
|
|
472
|
+
--_g: clamp(var(--ui-space-1-5), (100cqi - 335px) * 99, var(--ui-space-2) - 2px * var(--_t));
|
|
473
|
+
--_w: min(var(--ui-otp-input-box, 40px + 8px * var(--_t)), (100cqi - (var(--_n) - 1 + 2 * var(--_k)) * var(--_g) - var(--_k) * 8px) / var(--_n));
|
|
474
|
+
--_h: calc(var(--_w) * (1.1 + 0.08 * var(--_t)));
|
|
475
|
+
--_rad: var(--ui-otp-input-radius, calc(var(--_w) * (0.2 + 0.04 * var(--_t))));
|
|
476
|
+
--_fill: var(--ui-bg-surface);
|
|
477
|
+
--_ink: var(--ui-text);
|
|
478
|
+
--_e0: var(--ui-text-field-edge, var(--ui-hairline));
|
|
479
|
+
--_e: var(--_e0);
|
|
480
|
+
--_ew: calc(0.5px + 0.5px * var(--_t));
|
|
481
|
+
--_sh: color-mix(in srgb, var(--ui-gray-1000) calc(7% - 7% * var(--_t)), transparent);
|
|
482
|
+
--_r0: var(--ui-otp-input-ring, color-mix(in srgb, var(--ui-text) calc(var(--_t) * 100%), var(--ui-focus-ring)));
|
|
483
|
+
--_r: var(--_r0);
|
|
484
|
+
--_m: var(--ui-motion-scale);
|
|
485
|
+
--_on: 1;
|
|
486
|
+
--_bw: 1px;
|
|
487
|
+
--_ro: 0;
|
|
488
|
+
--_cw: 100%;
|
|
489
|
+
display: block;
|
|
490
|
+
align-items: flex-start;
|
|
491
|
+
color: var(--ui-text);
|
|
492
|
+
font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans);
|
|
493
|
+
}
|
|
494
|
+
:host::before { content: 'x x x x x x x x' / ''; display: block; block-size: 0; overflow: hidden; font-size: 0; word-spacing: calc(max(336px, var(--_n) * (var(--ui-otp-input-box, 40px + 8px * var(--_t)) + 8px) + 24px * var(--_k)) / 7); }
|
|
495
|
+
.q { display: flex; flex-direction: column; align-items: inherit; container-type: inline-size; }
|
|
496
|
+
:host([data-align='center']) { --_jc: center; align-items: center; text-align: center; }
|
|
497
|
+
:host(:has(input:disabled)) { opacity: var(--ui-disabled-opacity); }
|
|
498
|
+
:host([data-status='verifying']) { --_fill: var(--ui-bg-subtle); --_ink: var(--ui-text-muted); --_sh: transparent; --_band: block; }
|
|
499
|
+
:host([data-status='success']) { --_on: 0; --_cw: fit-content; --_vis: hidden; }
|
|
500
|
+
:host([data-error]) {
|
|
501
|
+
--_fill: color-mix(in srgb, var(--ui-bg-surface) calc(var(--_t) * 100%), var(--ui-danger-soft));
|
|
502
|
+
--_ink: color-mix(in srgb, var(--ui-text) calc(var(--_t) * 100%), var(--ui-danger));
|
|
503
|
+
--_e: color-mix(in srgb, var(--ui-danger) calc(var(--_t) * 50%), var(--_e0));
|
|
504
|
+
--_r: color-mix(in srgb, var(--ui-danger) calc(var(--_t) * 100%), var(--_r0));
|
|
505
|
+
--_bw: 2px;
|
|
506
|
+
}
|
|
507
|
+
label { margin-block-end: calc(3px + 4px * var(--_t)); padding-inline: calc(2px * var(--_t)); font-weight: var(--ui-weight-medium); }
|
|
508
|
+
.row { position: relative; padding-block: calc(3px - 3px * var(--_t)); }
|
|
509
|
+
.row:has(:read-write:focus) { --_ro: 1; }
|
|
510
|
+
.row ::ng-deep input::selection { background: transparent; }
|
|
511
|
+
`,
|
|
512
|
+
})
|
|
513
|
+
export class UiOtpInput {
|
|
514
|
+
/** How many boxes. */
|
|
515
|
+
readonly length = input(6, { transform: numberAttribute });
|
|
516
|
+
/** Digits only, with the number pad on phones; or `alphanumeric`, letters and digits stored in capitals. */
|
|
517
|
+
readonly mode = input<OtpMode>('numeric');
|
|
518
|
+
/** A short dash between two groups: bare `separator` puts it in the middle, a number after that many boxes. */
|
|
519
|
+
readonly separator = input<boolean | number, unknown>(false, {
|
|
520
|
+
transform: (v: unknown) => (v === '' || v === true || v === 'true' ? true : v === false || v == null || v === 'false' ? false : Number(v)),
|
|
521
|
+
});
|
|
522
|
+
/** Set by your app: `verifying` locks the field and shimmers, `error` shakes and shows the message, `success` turns into the Verified chip. */
|
|
523
|
+
readonly status = input<OtpStatus>('idle');
|
|
524
|
+
/** The message shown while `status` is `error`. Default: `labels.error`. */
|
|
525
|
+
readonly error = input<string | null | undefined>();
|
|
526
|
+
/** Empties the boxes about a second after an error, ready for the next try. */
|
|
527
|
+
readonly clearOnError = input(true, { transform: booleanAttribute });
|
|
528
|
+
/** Seconds before the code can be sent again. Set it to show the resend line. */
|
|
529
|
+
readonly resendIn = input<number | undefined, unknown>(undefined, { transform: (v: unknown) => (v == null || v === '' ? undefined : numberAttribute(v)) });
|
|
530
|
+
/** Where the boxes and the lines under them sit. */
|
|
531
|
+
readonly align = input<OtpAlign>('start');
|
|
532
|
+
/** Shown above the boxes. Without it, screen readers hear "Verification code, 6 digits". */
|
|
533
|
+
readonly label = input<string>();
|
|
534
|
+
/** A quiet line under the boxes. The error takes its place while there is one. */
|
|
535
|
+
readonly hint = input<string>();
|
|
536
|
+
/** Words for the name, the messages and the resend line, to translate them. */
|
|
537
|
+
readonly labels = input<Partial<OtpInputLabels>>({});
|
|
538
|
+
/** The last box was filled by the user: start verifying. Not emitted for values written by code. */
|
|
539
|
+
readonly completed = output<string>();
|
|
540
|
+
/** The Resend button was pressed. The countdown has already restarted. */
|
|
541
|
+
readonly resend = output<void>();
|
|
542
|
+
|
|
543
|
+
readonly ctl = contentChild(UiOtp);
|
|
544
|
+
readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
545
|
+
readonly msgId = `ui-otp-input-msg-${nextId++}`;
|
|
546
|
+
readonly burst = signal(-1);
|
|
547
|
+
readonly nudge = signal(false);
|
|
548
|
+
readonly fading = signal(false);
|
|
549
|
+
protected readonly live = signal('');
|
|
550
|
+
readonly left = signal(0);
|
|
551
|
+
private readonly dismissed = signal(false);
|
|
552
|
+
|
|
553
|
+
readonly count = computed(() => (this.mode() === 'numeric' ? this.t().digits : this.t().characters)(this.length()));
|
|
554
|
+
readonly slots = computed(() => Array.from({ length: this.length() }, (_, i) => i));
|
|
555
|
+
readonly chars = computed(() => [...(this.ctl()?.value() ?? '').slice(0, this.length())]);
|
|
556
|
+
readonly sepAt = computed(() => {
|
|
557
|
+
const [s, n] = [this.separator(), this.length()];
|
|
558
|
+
const k = s === true ? Math.floor(n / 2) : s === false ? 0 : s;
|
|
559
|
+
return k > 0 && k < n ? k : -1;
|
|
560
|
+
});
|
|
561
|
+
readonly locked = computed(() => this.status() === 'verifying' || this.status() === 'success');
|
|
562
|
+
readonly showError = computed(() => this.status() === 'error' && !this.dismissed());
|
|
563
|
+
readonly message = computed(() => (this.showError() ? this.error() || this.t().error : this.hint() || ''));
|
|
564
|
+
readonly text = linkedSignal<string, string>({ source: this.message, computation: (m, prev) => m || prev?.value || '' });
|
|
565
|
+
readonly clock = computed(() => `${Math.floor(this.left() / 60)}:${String(this.left() % 60).padStart(2, '0')}`);
|
|
566
|
+
|
|
567
|
+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
568
|
+
private readonly doc = inject(DOCUMENT);
|
|
569
|
+
private timers: ReturnType<typeof setTimeout>[] = [];
|
|
570
|
+
private tick?: ReturnType<typeof setTimeout>;
|
|
571
|
+
private end = 0;
|
|
572
|
+
/** On screen: the countdown digits only redraw then, and catch up when it comes back. */
|
|
573
|
+
private readonly seen = signal(true);
|
|
574
|
+
private ownLock = false;
|
|
575
|
+
|
|
576
|
+
constructor() {
|
|
577
|
+
let was: string | undefined;
|
|
578
|
+
effect(() => {
|
|
579
|
+
const s = this.status();
|
|
580
|
+
const key = s === 'error' ? `error:${this.error()}` : s;
|
|
581
|
+
untracked(() => {
|
|
582
|
+
if (was !== undefined && key !== was) this.enter(s);
|
|
583
|
+
was = key;
|
|
584
|
+
});
|
|
585
|
+
});
|
|
586
|
+
effect(() => {
|
|
587
|
+
const lock = this.locked();
|
|
588
|
+
const el = this.ctl()?.el;
|
|
589
|
+
if (!el) return;
|
|
590
|
+
if (lock && !el.readOnly) el.readOnly = this.ownLock = true;
|
|
591
|
+
else if (!lock && this.ownLock) el.readOnly = this.ownLock = false;
|
|
592
|
+
});
|
|
593
|
+
afterRenderEffect(() => {
|
|
594
|
+
this.resendIn();
|
|
595
|
+
untracked(() => this.restartCountdown(false));
|
|
596
|
+
});
|
|
597
|
+
effect(() => {
|
|
598
|
+
if (this.seen() && this.end) untracked(() => this.left.set(this.remaining()));
|
|
599
|
+
});
|
|
600
|
+
let io: IntersectionObserver | undefined;
|
|
601
|
+
afterNextRender(() => {
|
|
602
|
+
const W = this.doc.defaultView?.IntersectionObserver;
|
|
603
|
+
if (!W) return;
|
|
604
|
+
io = new W((e) => this.seen.set(e[e.length - 1].isIntersecting));
|
|
605
|
+
io.observe(this.host);
|
|
606
|
+
});
|
|
607
|
+
inject(DestroyRef).onDestroy(() => {
|
|
608
|
+
this.stop();
|
|
609
|
+
clearTimeout(this.tick);
|
|
610
|
+
io?.disconnect();
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
private enter(s: OtpStatus) {
|
|
615
|
+
this.stop();
|
|
616
|
+
if (s === 'error') {
|
|
617
|
+
this.dismissed.set(false);
|
|
618
|
+
this.nudge.set(true);
|
|
619
|
+
this.say(this.message());
|
|
620
|
+
if (this.clearOnError()) {
|
|
621
|
+
this.timers.push(
|
|
622
|
+
setTimeout(() => {
|
|
623
|
+
this.fading.set(true);
|
|
624
|
+
this.timers.push(setTimeout(() => this.clear(), 130));
|
|
625
|
+
}, 870),
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
} else this.say(s === 'verifying' ? this.t().verifying : s === 'success' ? this.t().verified : '');
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
private stop() {
|
|
632
|
+
this.timers.forEach(clearTimeout);
|
|
633
|
+
this.timers = [];
|
|
634
|
+
this.fading.set(false);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
private clear() {
|
|
638
|
+
this.fading.set(false);
|
|
639
|
+
const c = this.ctl();
|
|
640
|
+
if (c?.value()) c.write('', 0, 'deleteContent');
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
private say(text: string) {
|
|
644
|
+
this.live.set(text && text === this.live() ? `${text} ` : text);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/** Called by the input after each change the user made. */
|
|
648
|
+
edited(prev: string, next: string) {
|
|
649
|
+
this.stop();
|
|
650
|
+
if (this.status() === 'error') this.dismissed.set(true);
|
|
651
|
+
const n = this.length();
|
|
652
|
+
if (next.length === n && prev.length < n) this.completed.emit(next);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** Starts the resend countdown again, for apps that send a new code from somewhere else. */
|
|
656
|
+
restartCountdown(announce = true) {
|
|
657
|
+
const s = this.resendIn();
|
|
658
|
+
clearTimeout(this.tick);
|
|
659
|
+
if (s === undefined) return;
|
|
660
|
+
const end = (this.end = Date.now() + s * 1000);
|
|
661
|
+
this.left.set(s);
|
|
662
|
+
if (announce) this.say(this.t().resent);
|
|
663
|
+
if (s <= 0) return;
|
|
664
|
+
const next = () => {
|
|
665
|
+
const ms = end - Date.now();
|
|
666
|
+
this.tick = setTimeout(() => {
|
|
667
|
+
const left = this.remaining();
|
|
668
|
+
if (this.seen() || !left) this.left.set(left);
|
|
669
|
+
if (left) next();
|
|
670
|
+
else this.say(this.t().canResend);
|
|
671
|
+
}, Math.max(0, ms % 1000 || 1000));
|
|
672
|
+
};
|
|
673
|
+
next();
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
private remaining() {
|
|
677
|
+
return Math.max(0, Math.ceil((this.end - Date.now()) / 1000));
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
resendNow() {
|
|
681
|
+
this.resend.emit();
|
|
682
|
+
this.restartCountdown();
|
|
683
|
+
this.ctl()?.el.focus({ preventScroll: true });
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
protected press(e: PointerEvent) {
|
|
687
|
+
const el = this.ctl()?.el;
|
|
688
|
+
if (!el || e.pointerType !== 'mouse' || e.button || el.disabled) return;
|
|
689
|
+
e.preventDefault();
|
|
690
|
+
el.focus({ preventScroll: true });
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
protected point(e: MouseEvent) {
|
|
694
|
+
const c = this.ctl();
|
|
695
|
+
const boxes = this.host.querySelectorAll('ui-otp-boxes .box:not(.chip)');
|
|
696
|
+
if (!c || !boxes?.length || this.doc.activeElement !== c.el) return;
|
|
697
|
+
let best = 0;
|
|
698
|
+
boxes.forEach((b, i) => {
|
|
699
|
+
const r = b.getBoundingClientRect();
|
|
700
|
+
if (e.clientX >= r.left - 4) best = i;
|
|
701
|
+
});
|
|
702
|
+
c.go(best);
|
|
703
|
+
}
|
|
704
|
+
}
|