import { html } from '@webjsdev/core';
import { inputClass } from '#components/ui/input.ts';
export default function InputOTPDemo() {
return html`
<ui-input-otp>
<input id="input-otp-demo" name="otp" aria-label="One-time code" value="123456" class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
`;
}
View Code
About
Input OTP is built on one real <input> that you author. Without JavaScript it is an ordinary text field the form submits. With JavaScript the element lays that same input, transparent, over the slots and draws each character in its box, so typing, pasting, SMS autofill and the mobile keyboard all still go to the real input. It uses no library.
Installation
Copy and paste the following code into your project.
/**
* Input OTP: a one-time code typed into a row of boxes. Tier-2. It ENHANCES
* one real `<input>` (D13): you author the input, so without JavaScript the
* page shows an ordinary text field that the form submits, autofills from an
* SMS (`autocomplete="one-time-code"`) and pastes into. With JavaScript the
* element lays that same input, transparent, over shadcn's slots and draws
* each character in its box, so typing, pasting, autofill and the mobile
* keyboard all still go to the real input.
*
* shadcn parity:
* InputOTP maxLength → <ui-input-otp> around <input maxlength="6" ...>
* pattern → the input's `pattern` (a keystroke that would
* make the value stop matching is refused)
* InputOTPGroup → <ui-input-otp-group>
* InputOTPSlot index → <ui-input-otp-slot index="0">
* InputOTPSeparator → <ui-input-otp-separator>
* onChange → `ui-value-change` ({ detail: { value } }); the input fires `input`
* onComplete → `ui-complete` ({ detail: { value } }) once every slot is filled
*
* A11y (handled by the element, plus your part): the real input is the only
* thing assistive technology meets, so it keeps its own name, value and
* focus. The slots, group and separator are drawn decoration, hidden from the
* accessibility tree. An `aria-invalid` on the input reaches the slots for
* the error styling. Your part: a `<label for>` (or `aria-label`) on the input,
* and `inputmode="numeric"` for a numeric code so phones show the number pad.
*
* @example
* ```html
* <label for="otp" class=${labelClass()}>One-time code</label>
* <ui-input-otp>
* <input id="otp" name="otp" class=${inputClass()} maxlength="6" pattern="[0-9]*"
* inputmode="numeric" autocomplete="one-time-code">
* <ui-input-otp-group>
* <ui-input-otp-slot index="0"></ui-input-otp-slot>
* <ui-input-otp-slot index="1"></ui-input-otp-slot>
* <ui-input-otp-slot index="2"></ui-input-otp-slot>
* </ui-input-otp-group>
* <ui-input-otp-separator></ui-input-otp-separator>
* <ui-input-otp-group>
* <ui-input-otp-slot index="3"></ui-input-otp-slot>
* <ui-input-otp-slot index="4"></ui-input-otp-slot>
* <ui-input-otp-slot index="5"></ui-input-otp-slot>
* </ui-input-otp-group>
* </ui-input-otp>
* ```
*/
import { WebComponent, html, signal } from '@webjsdev/core';
import { takeAuthoredClass } from '../lib/utils.ts';
/** The row's classes (the element applies them). */
export const inputOtpClass = (): string =>
'gap-2 relative flex items-center has-disabled:opacity-50';
/** A group's classes (the element applies them). */
export const inputOtpGroupClass = (): string => 'flex items-center';
/** A slot's classes (the element applies them, to the slot's own host). */
export const inputOtpSlotClass = (): string =>
'h-9 w-9 border-y border-e border-input text-sm shadow-xs transition-all outline-none first:rounded-s-md first:border-s last:rounded-e-md aria-invalid:border-destructive data-[active=true]:border-ring data-[active=true]:ring-[3px] data-[active=true]:ring-ring/50 data-[active=true]:aria-invalid:border-destructive data-[active=true]:aria-invalid:ring-destructive/20 dark:bg-input/30 dark:data-[active=true]:aria-invalid:ring-destructive/40 relative flex items-center justify-center data-[active=true]:z-10';
/** The fake caret's box. */
export const inputOtpCaretClass = (): string =>
'pointer-events-none absolute inset-0 flex items-center justify-center';
/** The fake caret's line. */
export const inputOtpCaretLineClass = (): string => 'h-4 w-px animate-caret-blink bg-foreground duration-1000';
/** The separator's classes (the element applies them). */
export const inputOtpSeparatorClass = (): string => 'flex items-center';
// The real input, laid transparent over the slots so it takes every click,
// keystroke, paste and autofill while the slots do the drawing.
const OVERLAY =
'position:absolute;inset:0;width:100%;height:100%;margin:0;padding:0;border:0;outline:0;box-shadow:none;background:transparent;color:transparent;-webkit-text-fill-color:transparent;caret-color:transparent;font-size:1px;letter-spacing:-0.5em;z-index:20;';
const ROOT = 'ui-input-otp';
export class UiInputOtp extends WebComponent {
readonly #enhanced = signal(false);
_observer: MutationObserver | null = null;
_waiting: MutationObserver | null = null;
_form: HTMLFormElement | null = null;
_savedStyle: string | null = null;
_last = '';
_focused = false;
_disabled: boolean | null = null;
constructor() {
super();
}
/** The real input this element enhances. */
get input(): HTMLInputElement | null {
// The server renders the element with no DOM queries available.
return typeof this.querySelector === 'function' ? this.querySelector('input') : null;
}
/** The code typed so far. */
get value(): string {
return this.input?.value ?? '';
}
/** How many characters the code has: the input's maxlength, else the slots. */
get maxLength(): number {
const m = this.input?.maxLength ?? -1;
return m > 0 ? m : this._slots().length;
}
connectedCallback(): void {
super.connectedCallback?.();
queueMicrotask(() => {
if (this._enhance() || this._waiting) return;
this._waiting = new MutationObserver(() => {
if (this._enhance()) { this._waiting?.disconnect(); this._waiting = null; }
});
this._waiting.observe(this, { childList: true, subtree: true });
});
}
disconnectedCallback(): void {
this._waiting?.disconnect();
this._observer?.disconnect();
const input = this.input;
if (input) {
input.removeEventListener('input', this._onInput);
input.removeEventListener('focus', this._onFocus);
input.removeEventListener('blur', this._onBlur);
input.removeEventListener('keyup', this._sync);
input.removeEventListener('click', this._sync);
input.removeEventListener('select', this._sync);
if (this._savedStyle !== null) input.setAttribute('style', this._savedStyle);
else input.removeAttribute('style');
}
document.removeEventListener('selectionchange', this._sync);
this._form?.removeEventListener('reset', this._onReset);
super.disconnectedCallback?.();
}
/** Take over from the plain input. True once it has (or already had). */
_enhance(): boolean {
if (this.#enhanced.get()) return true;
const input = this.input;
if (!input) return false;
this._savedStyle = input.getAttribute('style');
input.setAttribute('style', (this._savedStyle ?? '') + OVERLAY);
input.addEventListener('input', this._onInput);
input.addEventListener('focus', this._onFocus);
input.addEventListener('blur', this._onBlur);
input.addEventListener('keyup', this._sync);
input.addEventListener('click', this._sync);
input.addEventListener('select', this._sync);
this._form = input.form;
this._form?.addEventListener('reset', this._onReset);
this._observer = new MutationObserver(() => this._sync());
this._observer.observe(input, { attributes: true });
this._last = input.value;
this.#enhanced.set(true);
this._sync();
return true;
}
// A part connected after the input (a client render appends children in
// order) asks to be drawn.
_partConnected(): void {
if (this.#enhanced.get()) queueMicrotask(() => this._sync());
}
render() {
const disabled = !!this.input?.disabled;
return html`<div data-slot="input-otp" ?data-disabled=${disabled} class=${inputOtpClass()}><slot></slot></div>`;
}
_slots(): UiInputOtpSlot[] {
return Array.from(this.querySelectorAll<UiInputOtpSlot>(`${ROOT}-slot`));
}
_pattern(): RegExp | null {
const p = this.input?.getAttribute('pattern');
if (!p) return null;
// HTML compiles `pattern` with the v flag; an engine without it gets u.
for (const flags of ['v', 'u']) {
try { return new RegExp(`^(?:${p})$`, flags); } catch { /* next */ }
}
return null;
}
// Refuse an edit that breaks the pattern or the length, as input-otp does.
_onInput = (): void => {
const input = this.input;
if (!input) return;
const re = this._pattern();
const v = input.value;
if ((re && v && !re.test(v)) || v.length > this.maxLength) {
const at = Math.max(0, (input.selectionStart ?? this._last.length) - (v.length - this._last.length));
input.value = this._last;
input.setSelectionRange(at, at);
this._sync();
return;
}
if (v !== this._last) {
this._last = v;
this.dispatchEvent(new CustomEvent('ui-value-change', { bubbles: true, detail: { value: v } }));
if (v.length === this.maxLength) {
this.dispatchEvent(new CustomEvent('ui-complete', { bubbles: true, detail: { value: v } }));
}
}
this._sync();
};
_onFocus = (): void => {
this._focused = true;
document.addEventListener('selectionchange', this._sync);
// Typing continues the code, so the caret starts at its end.
const input = this.input;
if (input) {
queueMicrotask(() => {
const end = input.value.length;
input.setSelectionRange(end, end);
this._sync();
});
}
this._sync();
};
_onBlur = (): void => {
this._focused = false;
document.removeEventListener('selectionchange', this._sync);
this._sync();
};
_onReset = (): void => {
queueMicrotask(() => { this._last = this.input?.value ?? ''; this._sync(); });
};
// Push the value, the caret and the state into every drawn part. It runs
// several times per keystroke (input, keyup, each selectionchange), so it
// hands a part new state only when something it draws changed: every
// attribute write re-checks the page's :has() rules, and a page carrying
// the kit's styles has hundreds of them, which made each keystroke wait on
// style recalculation.
_sync = (): void => {
const input = this.input;
if (!input || !this.#enhanced.get()) return;
const value = input.value;
const max = this.maxLength;
const start = Math.min(input.selectionStart ?? value.length, max - 1);
const end = input.selectionEnd ?? start;
const invalid = input.getAttribute('aria-invalid') === 'true';
this._slots().forEach((slot) => {
const i = Number(slot.index);
const char = value[i] ?? '';
const active = this._focused && (end > start ? i >= start && i < end : i === start);
const next = { shown: true, char, active, caret: active && !char, invalid };
// A slot not upgraded yet has no state signal; the next sync reaches it.
const s = slot.state?.get();
if (s && (s.shown !== next.shown || s.char !== next.char || s.active !== next.active || s.caret !== next.caret || s.invalid !== next.invalid)) {
slot.state?.set(next);
}
});
this.querySelectorAll<UiInputOtpGroup | UiInputOtpSeparator>(`${ROOT}-group, ${ROOT}-separator`).forEach((part) => {
if (!part.shown?.get()) part.shown?.set(true);
});
const disabled = !!input.disabled;
if (disabled !== this._disabled) {
this._disabled = disabled;
this.requestUpdate();
}
};
}
UiInputOtp.register('ui-input-otp');
// --------------------------------------------------------------------------
// <ui-input-otp-group>
// --------------------------------------------------------------------------
export class UiInputOtpGroup extends WebComponent {
readonly shown = signal(false);
constructor() {
super();
}
connectedCallback(): void {
super.connectedCallback?.();
(this.closest(ROOT) as UiInputOtp | null)?._partConnected();
}
render() {
return html`<div data-slot="input-otp-group" ?hidden=${!this.shown.get()} class=${inputOtpGroupClass()}><slot></slot></div>`;
}
}
UiInputOtpGroup.register('ui-input-otp-group');
// --------------------------------------------------------------------------
// <ui-input-otp-slot>
// --------------------------------------------------------------------------
type SlotState = { shown: boolean; char: string; active: boolean; caret: boolean; invalid: boolean };
export class UiInputOtpSlot extends WebComponent({
index: Number,
}) {
readonly state = signal<SlotState>({ shown: false, char: '', active: false, caret: false, invalid: false });
_authored: string | null = null;
constructor() {
super();
this.index = 0;
}
connectedCallback(): void {
// The author's own classes, read once: later the host carries ours too.
this._authored = takeAuthoredClass(this);
super.connectedCallback?.();
(this.closest(ROOT) as UiInputOtp | null)?._partConnected();
}
// shadcn's slot classes use first: / last: for the joined corners, which
// only work on the elements that are siblings in the group: the slot hosts
// themselves, not a box inside each. So the slot styles its own host.
// Each write is guarded: an attribute set to the value it already has still
// invalidates style, and the page re-checks its :has() rules for it.
updated(changedProperties: Map<string, unknown>): void {
super.updated?.(changedProperties);
const s = this.state.get();
const set = (name: string, value: string | null) => {
if (this.getAttribute(name) === value) return;
if (value === null) this.removeAttribute(name);
else this.setAttribute(name, value);
};
set('class', [inputOtpSlotClass(), this._authored].filter(Boolean).join(' '));
set('data-slot', 'input-otp-slot');
set('data-active', s.active ? 'true' : 'false');
set('aria-hidden', 'true');
set('aria-invalid', s.invalid ? 'true' : null);
if (this.hidden !== !s.shown) this.hidden = !s.shown;
}
// The caret is always in the slot and only its class changes. Inserting or
// removing an element makes the browser re-check every :has() rule on the
// page, which on a page carrying the kit's styles took longer than a frame
// on each keystroke; a class change on a leaf is nearly free.
render() {
const s = this.state.get();
return html`${s.char}<div class=${s.caret ? inputOtpCaretClass() : 'hidden'}><div class=${inputOtpCaretLineClass()}></div></div>`;
}
}
UiInputOtpSlot.register('ui-input-otp-slot');
// --------------------------------------------------------------------------
// <ui-input-otp-separator>
// --------------------------------------------------------------------------
export class UiInputOtpSeparator extends WebComponent {
readonly shown = signal(false);
constructor() {
super();
}
connectedCallback(): void {
super.connectedCallback?.();
(this.closest(ROOT) as UiInputOtp | null)?._partConnected();
}
render() {
return html`<div data-slot="input-otp-separator" role="separator" aria-hidden="true" ?hidden=${!this.shown.get()} class=${inputOtpSeparatorClass()}><svg
xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"
><path d="M5 12h14"></path></svg></div>`;
}
}
UiInputOtpSeparator.register('ui-input-otp-separator');
Update the import paths to match your project setup.
Usage
import { inputClass } from '#components/ui/input.ts';
import '#components/ui/input-otp.ts';
<ui-input-otp>
<input id="otp" name="otp" aria-label="One-time code" class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator></ui-input-otp-separator>
<ui-input-otp-group>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
Composition
Use the following composition to build an input OTP:
<ui-input-otp>
├── <input>
├── <ui-input-otp-group>
│ ├── <ui-input-otp-slot>
│ ├── <ui-input-otp-slot>
│ └── <ui-input-otp-slot>
├── <ui-input-otp-separator>
├── <ui-input-otp-group>
│ ├── <ui-input-otp-slot>
│ ├── <ui-input-otp-slot>
│ └── <ui-input-otp-slot>
├── <ui-input-otp-separator>
└── <ui-input-otp-group>
├── <ui-input-otp-slot>
└── <ui-input-otp-slot>
Pattern
Use the input's own pattern attribute to define a custom pattern for the OTP input. A keystroke that would make the value stop matching it is refused.
<ui-input-otp>
<input class=${inputClass()} maxlength="6" pattern="[a-zA-Z0-9]*" autocomplete="one-time-code">
...
</ui-input-otp>
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { fieldClass, fieldLabelClass } from '#components/ui/field.ts';
import { inputClass } from '#components/ui/input.ts';
// input-otp's REGEXP_ONLY_DIGITS, as the input's own pattern attribute.
const REGEXP_ONLY_DIGITS = '[0-9]*';
export default function InputOTPPattern() {
return html`
<div role="group" data-slot="field" data-orientation="vertical" class=${cn(fieldClass(), 'w-fit')}>
<label data-slot="field-label" for="digits-only" class=${fieldLabelClass()}>Digits Only</label>
<ui-input-otp>
<input id="digits-only" class=${inputClass()} maxlength="6" pattern=${REGEXP_ONLY_DIGITS} inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
</div>
`;
}
View Code
Separator
Use the <ui-input-otp-separator> element to add a separator between input groups.
import { html } from '@webjsdev/core';
import { inputClass } from '#components/ui/input.ts';
export default function InputOTPWithSeparator() {
return html`
<ui-input-otp>
<input id="input-otp-separator" aria-label="One-time code" class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator></ui-input-otp-separator>
<ui-input-otp-group>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator></ui-input-otp-separator>
<ui-input-otp-group>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
`;
}
View Code
Disabled
Use the disabled attribute on the input to disable it.
import { html } from '@webjsdev/core';
import { inputClass } from '#components/ui/input.ts';
export default function InputOTPDisabled() {
return html`
<ui-input-otp>
<input aria-label="One-time code" value="123456" disabled class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator></ui-input-otp-separator>
<ui-input-otp-group>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
`;
}
View Code
Controlled
The input holds the value, so set its value attribute for the starting code and read input.value (or the element's value) for the current one. The element fires ui-value-change with { detail: { value } } on every change, and the input fires its native input event.
import { html } from '@webjsdev/core';
import { inputClass } from '#components/ui/input.ts';
// shadcn's demo keeps the code in React state to echo it below the slots.
// The element reports every change as a ui-value-change event carrying the
// value, so a listener on it can write the line below. Shown here at rest.
export default function InputOTPControlled() {
const value = '';
return html`
<div class="space-y-2">
<ui-input-otp>
<input id="input-otp-controlled" aria-label="One-time password" value=${value} class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
<div class="text-center text-sm">
${value === '' ? 'Enter your one-time password.' : html`You entered: ${value}`}
</div>
</div>
`;
}
View Code
Invalid
Use aria-invalid="true" on the input to show an error state. The element passes it on to every slot.
import { html } from '@webjsdev/core';
import { inputClass } from '#components/ui/input.ts';
// aria-invalid goes on the real input; the element passes it to every slot.
export default function InputOTPInvalid() {
return html`
<ui-input-otp>
<input id="input-otp-invalid" aria-label="One-time code" aria-invalid="true" value="000000" class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator></ui-input-otp-separator>
<ui-input-otp-group>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator></ui-input-otp-separator>
<ui-input-otp-group>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
`;
}
View Code
Four Digits
A common pattern for PIN codes. This uses maxlength="4" with a digits-only pattern="[0-9]*".
import { html } from '@webjsdev/core';
import { inputClass } from '#components/ui/input.ts';
// input-otp's REGEXP_ONLY_DIGITS, as the input's own pattern attribute.
const REGEXP_ONLY_DIGITS = '[0-9]*';
export default function InputOTPFourDigits() {
return html`
<ui-input-otp>
<input id="input-otp-four-digits" aria-label="PIN" class=${inputClass()} maxlength="4" pattern=${REGEXP_ONLY_DIGITS} inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
`;
}
View Code
Alphanumeric
Use pattern="[a-zA-Z0-9]*" to accept both letters and numbers.
import { html } from '@webjsdev/core';
import { inputClass } from '#components/ui/input.ts';
// input-otp's REGEXP_ONLY_DIGITS_AND_CHARS, as the input's own pattern attribute.
const REGEXP_ONLY_DIGITS_AND_CHARS = '[a-zA-Z0-9]*';
export default function InputOTPAlphanumeric() {
return html`
<ui-input-otp>
<input id="input-otp-alphanumeric" aria-label="One-time code" class=${inputClass()} maxlength="6" pattern=${REGEXP_ONLY_DIGITS_AND_CHARS} autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator></ui-input-otp-separator>
<ui-input-otp-group>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
`;
}
View Code
Form
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
cardClass, cardHeaderClass, cardTitleClass, cardDescriptionClass,
cardContentClass, cardFooterClass,
} from '#components/ui/card.ts';
import { fieldClass, fieldLabelClass, fieldDescriptionClass } from '#components/ui/field.ts';
import { inputClass } from '#components/ui/input.ts';
import { RefreshCwIcon } from '#lib/ui/lucide.ts';
// The slot hosts sit inside the group's rendered box, so the size rule
// reaches them with ** (any descendant) where shadcn writes *.
const GROUP = '**:data-[slot=input-otp-slot]:h-12 **:data-[slot=input-otp-slot]:w-11 **:data-[slot=input-otp-slot]:text-xl';
export default function InputOTPForm() {
return html`
<div data-slot="card" data-size="default" class=${cn(cardClass(), 'mx-auto max-w-md')}>
<div data-slot="card-header" class=${cardHeaderClass()}>
<div data-slot="card-title" class=${cardTitleClass()}>Verify your login</div>
<div data-slot="card-description" class=${cardDescriptionClass()}>
Enter the verification code we sent to your email address:${' '}
<span class="font-medium">[email protected]</span>.
</div>
</div>
<div data-slot="card-content" class=${cardContentClass()}>
<div role="group" data-slot="field" data-orientation="vertical" class=${fieldClass()}>
<div class="flex items-center justify-between">
<label data-slot="field-label" for="otp-verification" class=${fieldLabelClass()}>
Verification code
</label>
<button type="button" data-slot="button" data-variant="outline" data-size="xs" class=${buttonClass({ variant: 'outline', size: 'xs' })}>
${RefreshCwIcon()}
Resend Code
</button>
</div>
<ui-input-otp>
<input id="otp-verification" name="otp" required class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group class=${GROUP}>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
</ui-input-otp-group>
<ui-input-otp-separator class="mx-2"></ui-input-otp-separator>
<ui-input-otp-group class=${GROUP}>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
<p data-slot="field-description" class=${fieldDescriptionClass()}>
<a href="#">I no longer have access to this email address.</a>
</p>
</div>
</div>
<div data-slot="card-footer" class=${cardFooterClass()}>
<div role="group" data-slot="field" data-orientation="vertical" class=${fieldClass()}>
<button type="submit" data-slot="button" data-variant="default" data-size="default" class=${cn(buttonClass(), 'w-full')}>
Verify
</button>
<div class="text-sm text-muted-foreground">
Having trouble signing in?${' '}
<a href="#" class="underline underline-offset-4 transition-colors hover:text-primary">
Contact support
</a>
</div>
</div>
</div>
</div>
`;
}
View Code
RTL
To enable RTL support, see the RTL configuration guide.
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { fieldClass, fieldLabelClass } from '#components/ui/field.ts';
import { inputClass } from '#components/ui/input.ts';
const translations = {
en: {
dir: 'ltr',
values: {
verificationCode: 'Verification code',
},
},
ar: {
dir: 'rtl',
values: {
verificationCode: 'رمز التحقق',
},
},
he: {
dir: 'rtl',
values: {
verificationCode: 'קוד אימות',
},
},
};
export default function InputOTPRtl() {
const lang = 'ar';
const { dir, values: t } = translations[lang];
return html`
<div role="group" data-slot="field" data-orientation="vertical" dir=${dir} lang=${lang} class=${cn(fieldClass(), 'mx-auto max-w-xs')}>
<label data-slot="field-label" for="input-otp-rtl" class=${fieldLabelClass()}>${t.verificationCode}</label>
<ui-input-otp dir=${dir}>
<input id="input-otp-rtl" value="123456" class=${inputClass()} maxlength="6" inputmode="numeric" autocomplete="one-time-code">
<ui-input-otp-group>
<ui-input-otp-slot index="0"></ui-input-otp-slot>
<ui-input-otp-slot index="1"></ui-input-otp-slot>
<ui-input-otp-slot index="2"></ui-input-otp-slot>
<ui-input-otp-slot index="3"></ui-input-otp-slot>
<ui-input-otp-slot index="4"></ui-input-otp-slot>
<ui-input-otp-slot index="5"></ui-input-otp-slot>
</ui-input-otp-group>
</ui-input-otp>
</div>
`;
}
View Code
API Reference
The kit does not use the input-otp library. <ui-input-otp> enhances one real <input> that you author: without JavaScript the page shows an ordinary text field the form submits. With JavaScript the element lays that same input, transparent, over the slots, so typing, pasting, SMS autofill and the mobile keyboard all still go to the real input.
ui-input-otp
The root. It wraps the input and the groups, and is configured through the input's own attributes.
The element also exposes value, maxLength and input (the enhanced input) as properties.
ui-input-otp-group
Groups slots into one joined row. A class written on it stays on the host.
ui-input-otp-slot
Draws the character at index and the blinking caret. The slot styles its own host, so a class written on it is kept, and it carries data-slot="input-otp-slot" and data-active.
ui-input-otp-separator
A dash between groups.
The slots, groups and separator are decoration hidden from assistive technology. The real input is what a screen reader meets, so give it a <label for> or an aria-label.