Naar inhoud

OTP-invoer

Veldjes voor het invoeren van een eenmalige code voor 2FA, inlogverificatie en pincodes — velden waarin je tekst kunt plakken, met neobrutalistische randen en scherpe schaduwen.

import {
  InputOTP,
  InputOTPGroup,

De input OTP is een gesegmenteerd veld voor eenmalige codes: visuele vakjes gerenderd boven op één native input, zodat plakken en het automatisch invullen van codes op mobiel meteen werken. Beide backend-varianten wikkelen dezelfde input-otp-bibliotheek van @guilherme_rodz in — er zit geen Radix- of Base-primitive onder — met vakjes vormgegeven volgens het neobrutalistische recept: dikke randen, scherpe schaduwen en vette typografie.

Ideaal voor:

  • Tweefactorauthenticatie — de zescijferige TOTP- of sms-code na het inloggen met een wachtwoord.
  • E-mail- en telefoonverificatie — bevestig eigenaarschap tijdens registratie, checkout of accountherstel.
  • Pincode-invoer — viercijferige codes voor schermvergrendeling of betalingsbevestiging (zie het voorbeeld Vier cijfers).

Over

Input OTP is gebouwd bovenop input-otp van @guilherme_rodz.

Installatie

pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/input-otp.json
npx shadcn@latest add https://neobrutalism.com/r/base/input-otp.json
yarn dlx shadcn@latest add https://neobrutalism.com/r/base/input-otp.json
bunx --bun shadcn@latest add https://neobrutalism.com/r/base/input-otp.json

Installeer de volgende afhankelijkheden:

pnpm add input-otp
npm install input-otp
yarn add input-otp
bun add input-otp

Kopieer en plak de volgende code in je project.

components/ui/input-otp.tsx
"use client"

import * as React from "react"
import { OTPInput, OTPInputContext } from "input-otp"
import { MinusIcon } from "lucide-react"

import { cn } from "@/lib/utils"

function InputOTP({
  className,
  containerClassName,
  ...props
}: React.ComponentProps<typeof OTPInput> & {
  containerClassName?: string
}) {
  return (
    <OTPInput
      data-slot="input-otp"
      containerClassName={cn(
        "cn-input-otp flex items-center has-disabled:opacity-50",
        containerClassName
      )}
      spellCheck={false}
      className={cn("disabled:cursor-not-allowed", className)}
      {...props}
    />
  )
}

function InputOTPGroup({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="input-otp-group"
      className={cn(
        "flex items-center rounded has-aria-invalid:border-destructive",
        className
      )}
      {...props}
    />
  )
}

function InputOTPSlot({
  index,
  className,
  ...props
}: React.ComponentProps<"div"> & {
  index: number
}) {
  const inputOTPContext = React.useContext(OTPInputContext)
  const { char, hasFakeCaret, isActive } = inputOTPContext?.slots[index] ?? {}

  return (
    <div
      data-slot="input-otp-slot"
      data-active={isActive}
      className={cn(
        "relative flex size-8 items-center justify-center border-y-2 border-r-2 bg-input text-sm shadow-sm transition-all outline-none first:rounded-l first:border-l-2 last:rounded-r aria-invalid:border-destructive data-[active=true]:z-10 data-[active=true]:outline-2 data-[active=true]:outline-primary data-[active=true]:aria-invalid:border-destructive",
        className
      )}
      {...props}
    >
      {char}
      {hasFakeCaret && (
        <div className="pointer-events-none absolute inset-0 flex items-center justify-center">
          <div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
        </div>
      )}
    </div>
  )
}

function InputOTPSeparator({ ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="input-otp-separator"
      className="flex items-center [&_svg:not([class*='size-'])]:size-4"
      role="separator"
      {...props}
    >
      <MinusIcon />
    </div>
  )
}

export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }

Werk de importpaden bij zodat ze overeenkomen met je projectconfiguratie.

Gebruik

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from "@/components/ui/input-otp"
<InputOTP maxLength={6}>
  <InputOTPGroup>
    <InputOTPSlot index={0} />
    <InputOTPSlot index={1} />
    <InputOTPSlot index={2} />
  </InputOTPGroup>
  <InputOTPSeparator />
  <InputOTPGroup>
    <InputOTPSlot index={3} />
    <InputOTPSlot index={4} />
    <InputOTPSlot index={5} />
  </InputOTPGroup>
</InputOTP>

Compositie

Gebruik de volgende compositie om een InputOTP te bouwen:

InputOTP
├── InputOTPGroup
│   ├── InputOTPSlot
│   ├── InputOTPSlot
│   └── InputOTPSlot
├── InputOTPSeparator
├── InputOTPGroup
│   ├── InputOTPSlot
│   ├── InputOTPSlot
│   └── InputOTPSlot
├── InputOTPSeparator
└── InputOTPGroup
    ├── InputOTPSlot
    └── InputOTPSlot

Pattern

Gebruik de pattern-prop om een aangepast patroon voor het OTP-invoerveld te definiëren.

import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"
 
;<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
  ...
</InputOTP>
"use client"

import { REGEXP_ONLY_DIGITS } from "input-otp"

Voorbeelden

Scheidingsteken

Gebruik het <InputOTPSeparator />-component om een scheidingsteken tussen invoergroepen toe te voegen.

import {
  InputOTP,
  InputOTPGroup,

Uitgeschakeld

Gebruik de disabled-prop om het invoerveld uit te schakelen.

import { Field, FieldLabel } from "@/components/ui/field"
import {
  InputOTP,

Gecontroleerd

Gebruik de props value en onChange om de waarde van het invoerveld te beheren.

"use client"

import * as React from "react"

Ongeldig

Gebruik aria-invalid op de slots om een foutstatus te tonen.

"use client"

import * as React from "react"

Vier cijfers

Een veelvoorkomend patroon voor pincodes. Dit gebruikt de pattern={REGEXP_ONLY_DIGITS}-prop.

"use client"

import { REGEXP_ONLY_DIGITS } from "input-otp"

Alfanumeriek

Gebruik REGEXP_ONLY_DIGITS_AND_CHARS om zowel letters als cijfers te accepteren.

"use client"

import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"

Formulier

import { RefreshCwIcon } from "lucide-react"

import { Button } from "@/components/ui/button"

RTL

Om RTL-ondersteuning in Neobrutalism in te schakelen, zie de RTL-configuratiegids.

"use client"

import * as React from "react"

Toegankelijkheid

Er is geen WAI-ARIA APG-patroon voor OTP-velden, en dat is ook niet nodig: input-otp rendert één echte <input> met autocomplete="one-time-code" achter de visuele vakjes, dus ondersteunende technologie kondigt één tekstveld aan en mobiele toetsenborden kunnen de binnenkomende code voorstellen. De vakjes zijn presentationeel — geef het veld zelf een label (een FormLabel of aria-label) en geef aria-invalid door aan de vakjes voor de foutstyling.

De toetsenbordinteracties volgen het native gedrag van een tekstveld en zijn identiek in de Radix- en de Base-variant:

ToetsActie
Tab / Shift + TabVerplaatst de focus het veld in / uit (één tabstop)
ArrowLeft / ArrowRightVerplaatst de cursor tussen de vakjes
Home / EndSpringt naar het eerste / laatste vakje
Backspace / DeleteVerwijdert het vorige / volgende teken
Ctrl/Cmd + VPlakt — de code vult de vakjes als hij aan pattern voldoet

API-referentie

Zie de input-otp-documentatie voor meer informatie.