Zubra UI
Code for
GitHub

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

Compiled (any project)
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
One file, no dependencies (3.5 KB gzipped)
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
.zb source (to edit it with Zubra)
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
No build: a script tag, straight from this site
HTML
<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

index.html
<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>
Example.jsx
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>
  );
}
Example.vue
<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('-') } } }) -->
Example.svelte
<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>
example.component.ts
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

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
valuestring""
lengthnumber6
numericbooleantrue
labelstring"Verification code"
namestring""
Events
EventWhen
valuechangeThe code changed; detail is the text so far.
completeEvery 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.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour