<fieldsetclass="otp"aria-label="Verification code"aria-describedby="otp-error"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 1"aria-invalid="true"value="4"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 2"aria-invalid="true"value="2"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 3"aria-invalid="true"value="0"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 4"aria-invalid="true"value="9"></fieldset><pclass="m(0)text-xs fg-danger"id="otp-error">That code has expired. Request a new one.</p>
<><fieldsetclassName="otp"aria-label="Verification code"aria-describedby="otp-error"><inputclassName="otp-slot"inputMode="numeric"maxLength={1}aria-label="Digit 1"aria-invalid="true"defaultValue="4"/><inputclassName="otp-slot"inputMode="numeric"maxLength={1}aria-label="Digit 2"aria-invalid="true"defaultValue="2"/><inputclassName="otp-slot"inputMode="numeric"maxLength={1}aria-label="Digit 3"aria-invalid="true"defaultValue="0"/><inputclassName="otp-slot"inputMode="numeric"maxLength={1}aria-label="Digit 4"aria-invalid="true"defaultValue="9"/></fieldset><pclassName="m(0)text-xs fg-danger"id="otp-error">That code has expired. Request a new one.</p></>
<fieldsetclass="otp"aria-label="Verification code"aria-describedby="otp-error"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 1"aria-invalid="true"value="4"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 2"aria-invalid="true"value="2"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 3"aria-invalid="true"value="0"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 4"aria-invalid="true"value="9"></fieldset><pclass="m(0)text-xs fg-danger"id="otp-error">That code has expired. Request a new one.</p>
<template><fieldsetclass="otp"aria-label="Verification code"aria-describedby="otp-error"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 1"aria-invalid="true"value="4"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 2"aria-invalid="true"value="2"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 3"aria-invalid="true"value="0"><inputclass="otp-slot"inputmode="numeric"maxlength="1"aria-label="Digit 4"aria-invalid="true"value="9"></fieldset><pclass="m(0)text-xs fg-danger"id="otp-error">That code has expired. Request a new one.</p></template>
'use client'import{ useState,type ComponentProps }from'react'import{ otp }from'@master/ui/behaviors/otp.js'import{ useBehavior }from'./use-behavior'exportinterfaceOtpPropsextendsOmit<ComponentProps<'fieldset'>,'defaultValue'|'onChange'|'ref'>{/** How many characters the code has. */length?:number/** The characters typed so far. */value?:stringdefaultValue?:stringonValueChange?:(value:string)=>void/** Called with the code once every slot holds a character. */onComplete?:(code:string)=>void/** What one character must match, as the `pattern` of each slot. A digit by default. */pattern?:stringinputMode?:'numeric'|'text'/** The name of a slot, which has no visible label. */slotLabel?:(index:number)=>stringinvalid?:boolean}/** A one-time code. Name the group with `aria-label` or `aria-labelledby`; `disabled` disables every slot. *//** The behavior reads the slots and what a slot takes as it attaches, so it attaches again when either changes. */constslotted=(root:HTMLElement, _read?:{length:number,pattern:string})=>otp(root)exportfunctionOtp({ length =6, value, defaultValue, onValueChange, onComplete, pattern ='[0-9]', inputMode ='numeric', slotLabel = index =>`Digit ${index +1}`, invalid, className,...root }:OtpProps){const ref =useBehavior(slotted,{ length, pattern },{ value, defaultValue, onValueChange,// The root's own change is the behavior's: every slot is filled. A slot's change bubbles through it too.on:{change:(event)=>{if(event.target=== event.currentTarget)onComplete?.((event asCustomEvent<{value:string}>).detail.value)}},})const[first]=useState(value ?? defaultValue ??'')return(<fieldsetclassName={className ?`otp ${className}`:'otp'}ref={ref}{...root}>{Array.from({ length },(_, index)=>(<inputclassName="otp-slot"key={index}inputMode={inputMode}pattern={pattern}maxLength={1}autoComplete={index ===0?'one-time-code':'off'}aria-label={slotLabel(index)}aria-invalid={invalid ||undefined}defaultValue={first[index]??''}/>))}</fieldset>)}
ui/Otp.svelte
<scriptlang="ts"module>importtype{ HTMLFieldsetAttributes }from'svelte/elements'exportinterfaceOtpPropsextendsOmit<HTMLFieldsetAttributes,'children'|'inputmode'>{/** How many characters the code has. */length?:number/** The characters typed so far. Bind it, or leave it out. */value?:stringonValueChange?:(value:string)=>void/** Called with the code once every slot holds a character. */onComplete?:(code:string)=>void/** What one character must match, as the `pattern` of each slot. A digit by default. */pattern?:stringinputmode?:'numeric'|'text'/** The name of a slot, which has no visible label. */slotLabel?:(index:number)=>stringinvalid?:boolean}</script><scriptlang="ts">import{ untrack }from'svelte'import{ otp }from'@master/ui/behaviors/otp.js'import{ behavior }from'./behavior.svelte.js'let{ length =6, value =$bindable(), onValueChange, onComplete, pattern ='[0-9]', inputmode ='numeric', slotLabel = index =>`Digit ${index +1}`, invalid,class: className,...root }:OtpProps=$props()const first =untrack(()=> value)??''// The behavior reads the slots and what a slot takes as it attaches, so it attaches again when either changes.constslotted=(root:HTMLElement, _read?:{length:number,pattern:string})=>otp(root)const attach =behavior(slotted,()=>({ length, pattern }),{value:()=> value,change:(next)=>{ value = next;onValueChange?.(next)},// The root's own change is the behavior's: every slot is filled. A slot's change bubbles through it too.on:{change:(event)=>{if(event.target=== event.currentTarget)onComplete?.((event asCustomEvent<{value:string}>).detail.value)}},})</script><!--@component A one-time code. Name the group with `aria-label` or `aria-labelledby`; `disabled` disables every slot.--><fieldsetclass={['otp', className]}{@attach attach}{...root}>{#each{ length }, index}<inputclass="otp-slot"{inputmode}{pattern}maxlength={1}autocomplete={index ===0?'one-time-code':'off'}aria-label={slotLabel(index)}aria-invalid={invalid ||undefined}value={first[index]??''}/>{/each}</fieldset>
ui/Otp.vue
<scriptsetuplang="ts">import{ useTemplateRef }from'vue'import{ otp }from'@master/ui/behaviors/otp.js'import{ useBehavior }from'./use-behavior'defineOptions({inheritAttrs:false})const{ length =6, pattern ='[0-9]', inputMode ='numeric', slotLabel =(index:number)=>`Digit ${index +1}`}=defineProps<{/** How many characters the code has. */length?:number/** What one character must match, as the `pattern` of each slot. A digit by default. */pattern?:stringinputMode?:'numeric'|'text'/** The name of a slot, which has no visible label. */slotLabel?:(index:number)=>stringinvalid?:boolean}>()/** The characters typed so far: `v-model`. */const model =defineModel<string>()const emit =defineEmits<{/** The code, once every slot holds a character. */complete:[code:string]}>()/* A one-time code. Name the group with `aria-label` or `aria-labelledby`; `disabled` disables every slot. */const first = model.value??''const root =useTemplateRef<HTMLFieldSetElement>('root')// The behavior reads the slots and what a slot takes as it attaches, so it attaches again when either changes.constslotted=(element:HTMLElement, _read?:{length:number,pattern:string})=>otp(element)useBehavior(root, slotted,()=>({ length, pattern }), model,{// The root's own change is the behavior's: every slot is filled. A slot's change bubbles through it too.change:(event)=>{if(event.target=== event.currentTarget)emit('complete',(event asCustomEvent<{value:string}>).detail.value)},})</script><template><fieldsetclass="otp"ref="root"v-bind="$attrs"><inputv-for="index in length":key="index"class="otp-slot":inputmode="inputMode":pattern="pattern"maxlength="1":autocomplete="index ===1?'one-time-code':'off'":aria-label="slotLabel(index -1)":aria-invalid="invalid ||undefined":value.attr="first[index -1]"></fieldset></template>
/* * OTP: a short code typed one character to a slot. * Each slot is a native input drawn as a small well, the same material as a text field, so a slot rises * to the sheet with an ink contour while it has focus. Moving on after a character and pasting a whole code * belong to a script; without one, every slot is still an input the keys can reach. * Geometry: a scale of its own, each slot 40px wide and 44px tall with a control's corner, 8px apart. */@layer components.ui {.otp{--otp-width:2.5rem;--otp-height:2.75rem;display:inline-flex;align-items:center;gap:.5rem;min-width:0;margin:0;padding:0;border:0;}.otp-slot{width:var(--otp-width);height:var(--otp-height);margin:0;padding:0;border:0;border-radius:calc(var(--radius-control)*var(--corner-scale));corner-shape:var(--corner-shape);background:var(--color-surface-inset);color:var(--color-text-strong);box-shadow:var(--shadow-pressed);font:var(--font-weight-control)1.125rem/1var(--font-family-sans);font-feature-settings:inherit;font-variant-numeric:tabular-nums;text-align:center;caret-color:var(--color-text-strong);appearance: textfield;transition-property: box-shadow, background-color;transition-duration:var(--duration-quick);transition-timing-function:var(--easing-glide);/* A slot holds one character: a number field's steppers have nothing to step. */&::-webkit-inner-spin-button,&::-webkit-outer-spin-button{display:none;}&::placeholder{color:var(--color-text-disabled);opacity:1;}@media(hover:hover){&:hover:not(:focus,:disabled,[readonly]){background:var(--color-surface-inset-hover);}}/* Read only: the character is shown, not typed, so the slot rests flat and the caret is gone, as a field's does. */&:where([readonly]:not(:disabled)){background:transparent;box-shadow:var(--shadow-flat);caret-color:transparent;}&:focus{outline:none;background:var(--color-surface-raised);box-shadow:inset000.5pxvar(--color-line-mark),000.5pxvar(--color-line-mark),0003.5pxvar(--color-focus-halo);}/* * Filled in by the browser, as the first slot is when a code is offered from a message: the engine paints its * own background, so the well is laid over it as an inset shadow with the faint tint a text field takes. */&:autofill{box-shadow:var(--shadow-pressed),inset000100vmaxcolor-mix(in oklab,var(--color-info)7%,var(--color-surface-inset));&:focus{box-shadow:inset000.5pxvar(--color-line-mark),000.5pxvar(--color-line-mark),0003.5pxvar(--color-focus-halo),inset000100vmaxcolor-mix(in oklab,var(--color-info)7%,var(--color-surface-raised));}}/* * After the browser's fill, as a text field's is, so a filled code that is wrong still shows that it is. * :user-invalid reports the slot's own constraints (required, pattern) once the reader has tried, without script. */&:is([aria-invalid="true"],:user-invalid){box-shadow:var(--shadow-pressed),inset000.5pxvar(--color-danger),000.5pxcolor-mix(in oklab,var(--color-danger)45%,transparent);&:focus{box-shadow:inset000.5pxvar(--color-danger),000.5pxvar(--color-danger),0003.5pxcolor-mix(in oklab,var(--color-danger)15%,transparent);}}&:disabled{color:var(--color-text-disabled);-webkit-text-fill-color:currentColor;opacity:1;cursor:not-allowed;}@media(forced-colors:active){outline:1pxsolid FieldText;outline-offset:-1px;&:focus{outline:2pxsolid Highlight;outline-offset:-2px;}/* The danger edge is a shadow, which forced colors drop: the outline says it instead, as a field's does. */&:is([aria-invalid="true"],:user-invalid){outline-color: Mark;}&:disabled{outline-color: GrayText;}}}/* A short dash between groups of slots, such as three and three. */.otp-separator{flex:none;width:.5rem;height:1.5px;border-radius:999px;background:var(--color-line-mark);@media(forced-colors:active){background: CanvasText;}}}