Input OTP
A one-time code field shown as separate boxes, backed by one real input that autofills from SMS and accepts paste.
Installation
bash
npx bootcn add input-otpbash
pnpm dlx bootcn add input-otpbash
yarn dlx bootcn add input-otpbash
bunx --bun bootcn add input-otpThe CLI also installs the design tokens and these components: label. It prints the <link> and <script> tags to add to your layout.
Usage
Group the slots any way you like (two groups of three here) as long as the number of slots matches maxlength. Paste and the phone's SMS code suggestion fill every slot at once, because there is only one input.
JavaScript API
data-bootcn-otpon the wrapper; the input'smaxlengthsets the length,inputmode="numeric"keeps digits only.- Event
bootcn:otp:complete(bubbles),detail.value, once all slots are filled; use it to submit the form. Bootcn.inputOtp.render(root)after setting the value from script;init(root)/destroy(root).
Accessibility
- One labelled input, so screen readers announce one field with its help text, and type, paste and autofill work as usual (
autocomplete="one-time-code"). - The boxes are visual only (
aria-hidden). - Without JavaScript it is a normal text input.
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.- The
labelcomponent.
Source
The exact files the CLI installs (input-otp@1.0.1). View on GitHub · Report an issue
html
<div class="bootcn-field">
<label class="bootcn-label" for="bootcn-otp-code">Verification code</label>
<div class="bootcn-otp" data-bootcn-otp>
<input
class="bootcn-otp__input"
id="bootcn-otp-code"
name="code"
type="text"
inputmode="numeric"
autocomplete="one-time-code"
pattern="[0-9]*"
maxlength="6"
aria-describedby="bootcn-otp-code-help"
/>
<div class="bootcn-otp__slots" aria-hidden="true">
<div class="bootcn-otp__group">
<span class="bootcn-otp__slot"></span>
<span class="bootcn-otp__slot"></span>
<span class="bootcn-otp__slot"></span>
</div>
<span class="bootcn-otp__separator">–</span>
<div class="bootcn-otp__group">
<span class="bootcn-otp__slot"></span>
<span class="bootcn-otp__slot"></span>
<span class="bootcn-otp__slot"></span>
</div>
</div>
</div>
<p class="bootcn-field__help" id="bootcn-otp-code-help">
Enter the 6-digit code we sent to your phone.
</p>
</div>css
.bootcn-otp {
position: relative;
display: inline-flex;
}
/* Without JavaScript: a normal input with spaced digits. */
.bootcn-otp__input {
width: 12rem;
height: var(--bootcn-control-height);
padding: 0 0.75rem;
border: var(--bootcn-border-width) solid var(--bootcn-input);
border-radius: var(--bootcn-radius);
background: transparent;
color: var(--bootcn-foreground);
font-family: var(--bs-font-monospace);
font-size: 1rem;
letter-spacing: 0.5em;
}
.bootcn-otp__slots {
display: none;
}
/* With input-otp.js: the input stays on top (for typing, paste and SMS
autofill) but is transparent; the slots show the digits. */
.bootcn-otp.is-enhanced .bootcn-otp__input {
position: absolute;
z-index: 1;
inset: 0;
width: 100%;
height: 100%;
border: 0;
background: transparent;
color: transparent;
caret-color: transparent;
letter-spacing: normal;
outline: 0;
opacity: 0;
}
.bootcn-otp.is-enhanced .bootcn-otp__slots {
display: flex;
align-items: center;
gap: 0.5rem;
}
.bootcn-otp__group {
display: flex;
border-radius: var(--bootcn-radius);
box-shadow: var(--bootcn-shadow-sm);
}
.bootcn-otp__slot {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 2.25rem;
height: 2.25rem;
border: var(--bootcn-border-width) solid var(--bootcn-input);
background-color: var(--bootcn-background);
color: var(--bootcn-foreground);
font-size: 0.875rem;
font-weight: 500;
transition:
border-color var(--bootcn-transition),
box-shadow var(--bootcn-transition);
}
.bootcn-otp__slot + .bootcn-otp__slot {
border-left: 0;
}
.bootcn-otp__slot:first-child {
border-radius: var(--bootcn-radius) 0 0 var(--bootcn-radius);
}
.bootcn-otp__slot:last-child {
border-radius: 0 var(--bootcn-radius) var(--bootcn-radius) 0;
}
.bootcn-otp__separator {
color: var(--bootcn-muted-foreground);
}
/* The slot where the next digit goes, while the input has focus. */
.bootcn-otp:focus-within .bootcn-otp__slot.is-active {
z-index: 1;
border-color: var(--bootcn-ring);
box-shadow: 0 0 0 3px var(--bs-focus-ring-color);
}
.bootcn-otp:focus-within .bootcn-otp__slot.is-active:empty::after {
content: '';
width: 1px;
height: 1rem;
background-color: var(--bootcn-foreground);
animation: bootcn-otp-caret 1s step-end infinite;
}
@keyframes bootcn-otp-caret {
50% {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.bootcn-otp:focus-within .bootcn-otp__slot.is-active:empty::after {
animation: none;
}
}
.bootcn-otp__input[aria-invalid='true'] + .bootcn-otp__slots .bootcn-otp__slot {
border-color: var(--bootcn-destructive);
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.inputOtp) return; // already loaded: idempotent
const ROOT = '[data-bootcn-otp]';
let listening = false;
function render(root) {
const input = root.querySelector('.bootcn-otp__input');
const slots = Array.from(root.querySelectorAll('.bootcn-otp__slot'));
if (!input || slots.length === 0) return;
const max = Number(input.maxLength) > 0 ? input.maxLength : slots.length;
const numeric = (input.getAttribute('inputmode') || '') === 'numeric';
let value = numeric ? input.value.replace(/\D/g, '') : input.value.replace(/\s/g, '');
value = value.slice(0, max);
if (input.value !== value) input.value = value;
slots.forEach((slot, index) => {
slot.textContent = value[index] || '';
slot.classList.toggle('is-active', index === Math.min(value.length, slots.length - 1));
});
if (value.length === max && root.getAttribute('data-bootcn-otp-last') !== value) {
root.setAttribute('data-bootcn-otp-last', value);
root.dispatchEvent(
new CustomEvent('bootcn:otp:complete', { bubbles: true, detail: { value: value } }),
);
} else if (value.length < max) {
root.removeAttribute('data-bootcn-otp-last');
}
}
function onInput(event) {
const root = event.target.closest && event.target.closest(ROOT);
if (root) render(root);
}
// Clicking a slot puts the caret at the end, so typing fills the next slot.
// A text selection (select all, then paste or type) is left alone, so it
// replaces the code.
function onPlaceCaret(event) {
const input = event.target;
if (!(input instanceof HTMLInputElement) || !input.closest(ROOT)) return;
const end = input.value.length;
if (input.selectionStart === input.selectionEnd && input.selectionStart !== end) {
input.setSelectionRange(end, end);
}
}
function init(root = document) {
root.querySelectorAll(ROOT).forEach((element) => {
element.classList.add('is-enhanced');
render(element);
});
if (!listening) {
document.addEventListener('input', onInput);
document.addEventListener('click', onPlaceCaret, true);
document.addEventListener('focus', onPlaceCaret, true);
listening = true;
}
}
function destroy(root = document) {
root.querySelectorAll(ROOT).forEach((element) => element.classList.remove('is-enhanced'));
if (root === document && listening) {
document.removeEventListener('input', onInput);
document.removeEventListener('click', onPlaceCaret, true);
document.removeEventListener('focus', onPlaceCaret, true);
listening = false;
}
}
ns.inputOtp = { init: init, destroy: destroy, render: render };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => init());
} else {
init();
}
})();
