code-input
A one-time code (from an email or text message), shown as boxes but typed into one native field: phones offer the code from the message (autocomplete="one-time-code"), pasting the whole code works, and Backspace goes back. length is the number of characters; numeric keeps it to digits. value is two-way.
Try it: events appear here (valuechange, complete).
Source: code-input.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/code-input.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/code-input.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/code-input.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/code-input.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/code-input.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/code-input.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/code-input.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/code-input.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/code-input.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/code-input.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/code-input.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/code-input.json
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/code-input.js"></script>The compiled install writes src/components/zubra/code-input.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.
Usage
<code-input label="Enter the 6-digit code we sent you" name="code"></code-input>
<script type="module">
import './components/zubra/code-input.js';
const element = document.querySelector('code-input');
element.addEventListener('valuechange', (event) => console.log('valuechange', event.detail));
element.addEventListener('complete', (event) => console.log('complete', event.detail));
</script>import './components/zubra/code-input.js';
export function CodeInputExample() {
return (
<code-input
label="Enter the 6-digit code we sent you"
name="code"
onvaluechange={(event) => console.log('valuechange', event.detail)}
oncomplete={(event) => console.log('complete', event.detail)}
></code-input>
);
}<script setup>
import './components/zubra/code-input.js';
const onValuechange = (event) => console.log('valuechange', event.detail);
const onComplete = (event) => console.log('complete', event.detail);
</script>
<template>
<code-input
label="Enter the 6-digit code we sent you"
name="code"
@valuechange="onValuechange"
@complete="onComplete"
></code-input>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/code-input.js';
</script>
<code-input
label="Enter the 6-digit code we sent you"
name="code"
onvaluechange={(event) => console.log('valuechange', event.detail)}
oncomplete={(event) => console.log('complete', event.detail)}
></code-input>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/code-input.js';
@Component({
selector: 'app-code-input-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<code-input
label="Enter the 6-digit code we sent you"
name="code"
(valuechange)="onValuechange($event)"
(complete)="onComplete($event)"
></code-input>
`,
})
export class CodeInputExample {
onValuechange(event: Event) {
console.log('valuechange', (event as CustomEvent).detail);
}
onComplete(event: Event) {
console.log('complete', (event as CustomEvent).detail);
}
}API
| Property | Type | Default |
|---|---|---|
value | string | "" |
length | number | 6 |
numeric | boolean | true |
label | string | "Verification code" |
name | string | "" |
| Event | When |
|---|---|
valuechange | The code changed; detail is the text so far. |
complete | Every box is filled; detail is the code. |
Size
1.3 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 18.0 KB.