# Animated OTP Input for UI SDK (/docs/components/otp-input)

One ring glides from slot to slot, digits pop into place and the slots pulse when the code is complete.

```tsx title="example.tsx"
import * as React from "react"

import { Field, FieldError, FieldLabel } from "@/components/keyframery/field"
import { OTPInput, OTPInputGroup, OTPInputSeparator, OTPInputSlot } from "@/components/keyframery/otp-input"

const code = '[data-slot="otp-input"] input'

/* Types a code one digit at a time. */
function typeOut(text: string, step = 260) {
  return Array.from(text, (_, i) => ({ type: text.slice(0, i + 1), into: code, wait: step }))
}

/* The code the "server" accepts. Anything else is turned away. */
const sent = "246810"

export default function OTPInputDemo() {
  const [wrong, setWrong] = React.useState(false)

  return (
    <div className="flex w-[340px] flex-col gap-5">
      <div>
        <h3 className="text-[15px] font-semibold tracking-tight">Check your phone</h3>
        <p className="mt-1 text-[13px] text-muted-foreground">
          We sent a 6-digit code to <span className="text-foreground tabular-nums">+44 7700 900 418</span>.
        </p>
      </div>
      <Field invalid={wrong || undefined} className="gap-2.5">
        <FieldLabel className="sr-only">Verification code</FieldLabel>
        <OTPInput
          maxLength={6}
          pattern="digits"
          inputMode="numeric"
          onValueChange={() => setWrong(false)}
          onValueCommit={(code) => setWrong(code !== sent)}
        >
          <OTPInputGroup>
            <OTPInputSlot index={0} />
            <OTPInputSlot index={1} />
            <OTPInputSlot index={2} />
          </OTPInputGroup>
          <OTPInputSeparator />
          <OTPInputGroup>
            <OTPInputSlot index={3} />
            <OTPInputSlot index={4} />
            <OTPInputSlot index={5} />
          </OTPInputGroup>
        </OTPInput>
        <FieldError>That code didn't match. Check the latest message.</FieldError>
      </Field>
      <p className="text-[12px] text-muted-foreground">
        Didn't get it?{" "}
        <button
          type="button"
          className="cursor-pointer rounded-[4px] font-medium text-foreground underline decoration-foreground/25 underline-offset-[3px] outline-none transition-[text-decoration-color] duration-150 hover:decoration-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background motion-reduce:transition-none"
        >
          Send a new code
        </button>
      </p>
    </div>
  )
}
```

## Installation [#installation]

```bash
npx shadcn@latest add @keyframery/otp-input
```

This adds `components/keyframery/otp-input.tsx` and its primitive, `components/keyframery/primitives/otp-input.tsx`, to your project, and installs `@ui-sdk/react`. The motion tokens are added to your global CSS the first time you install a component.

## Usage [#usage]

```tsx
import {
  OTPInput,
  OTPInputGroup,
  OTPInputSeparator,
  OTPInputSlot,
} from "@/components/keyframery/otp-input"
```

The parts and props are the same as UI SDK's OTP Input. For every prop, see the [UI SDK otp input reference](https://ui-sdk.dev/docs/components/otp-input).

## What moves [#what-moves]

| Move        | What you see                                                        |
| ----------- | ------------------------------------------------------------------- |
| **Glide**   | One ring glides from slot to slot as you type.                      |
| **Pop**     | Each digit pops up into place.                                      |
| **Cascade** | When the code is complete, the slots pulse once from left to right. |

Timing and springiness follow the [motion style](/docs/motion-styles) you set, so this component moves like the rest of your app.

## Swap the import [#swap-the-import]

Already using UI SDK's otp input with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.

```tsx
// Before
import { … } from "@ui-sdk/react/otp-input"

// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/otp-input"
```

## Parts [#parts]

The finished component exports 4 parts:

`OTPInput`, `OTPInputGroup`, `OTPInputSeparator`, `OTPInputSlot`

## Accessibility [#accessibility]

Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the otp input moves: changes happen at once or with a short fade. More in [Accessibility](/docs/accessibility).

## Source [#source]

The code that lands in your project. It is yours to change.

**components/keyframery/otp-input.tsx**

```tsx
"use client"

/* Styled OTP input: the animated primitive plus the Keyframery look. */

import * as React from "react"

import {
  OTPInput as OTPInputPrimitive,
  OTPInputGroup as OTPInputGroupPart,
  OTPInputSeparator as OTPInputSeparatorPart,
  OTPInputSlot as OTPInputSlotPrimitive,
  createOTPInputController,
} from "@/components/keyframery/primitives/otp-input"
import { cn } from "@/lib/utils"

/* One invisible native input spans the root and takes every key, paste and autofill.
   The slots only draw it. The focus ring is the primitive's gliding ring, given the
   field's ring look here; it turns destructive while the code is invalid. */
function OTPInput({ className, ...props }: React.ComponentProps<typeof OTPInputPrimitive>) {
  return (
    <OTPInputPrimitive
      className={cn(
        "group/otp flex w-fit items-center gap-2",
        "before:rounded-[10px] before:shadow-[0_0_0_1px_var(--ring),0_0_0_4px_color-mix(in_oklch,var(--ring)_25%,transparent)]",
        "has-aria-invalid:before:shadow-[0_0_0_1px_var(--destructive),0_0_0_4px_color-mix(in_oklch,var(--destructive)_20%,transparent)]",
        "data-disabled:cursor-not-allowed data-disabled:opacity-50",
        className,
      )}
      {...props}
    />
  )
}

function OTPInputGroup({ className, ...props }: React.ComponentProps<typeof OTPInputGroupPart>) {
  return <OTPInputGroupPart className={cn("flex items-center gap-1.5", className)} {...props} />
}

/* Each slot is a small well; the active one brightens under the gliding ring, and an empty
   active slot shows a soft neutral caret. */
function OTPInputSlot({ className, ...props }: React.ComponentProps<typeof OTPInputSlotPrimitive>) {
  return (
    <OTPInputSlotPrimitive
      className={cn(
        "relative grid h-11 w-10 place-items-center rounded-[10px] bg-muted text-[17px] leading-none font-semibold text-foreground tabular-nums",
        "[--well-line:rgb(0_0_0/0.035)] dark:[--well-line:rgb(255_255_255/0.04)]",
        "shadow-[inset_0_1px_2px_rgb(0_0_0/0.07),inset_0_0_0_1px_var(--well-line)] dark:shadow-[inset_0_1px_2px_rgb(0_0_0/0.5),inset_0_0_0_1px_var(--well-line)]",
        "transition-[box-shadow,background-color] duration-[calc(150ms*var(--kf-speed,1))] ease-house motion-reduce:transition-none!",
        "group-hover/otp:[--well-line:rgb(0_0_0/0.09)] dark:group-hover/otp:[--well-line:rgb(255_255_255/0.09)]",
        "group-has-aria-invalid/otp:bg-destructive/[0.06] group-has-aria-invalid/otp:[--well-line:var(--destructive)] dark:group-has-aria-invalid/otp:[--well-line:var(--destructive)]",
        "data-active:bg-background dark:data-active:bg-background",
        "data-disabled:bg-muted",
        "after:pointer-events-none after:absolute after:top-1/2 after:left-1/2 after:h-[18px] after:w-[1.5px] after:-translate-1/2 after:rounded-full after:bg-foreground/45 after:opacity-0",
        "data-fake-caret:after:animate-pulse data-fake-caret:after:opacity-100 data-fake-caret:after:[animation-duration:1.2s] motion-reduce:after:animate-none!",
        className,
      )}
      {...props}
    />
  )
}

function OTPInputSeparator({
  className,
  children,
  ...props
}: React.ComponentProps<typeof OTPInputSeparatorPart>) {
  return (
    <OTPInputSeparatorPart
      className={cn("grid place-items-center text-muted-foreground/60 [&_svg]:size-3.5", className)}
      {...props}
    >
      {children ?? (
        <svg
          viewBox="0 0 16 16"
          fill="none"
          stroke="currentColor"
          strokeWidth="1.5"
          strokeLinecap="round"
          strokeLinejoin="round"
          aria-hidden="true"
        >
          <path d="M5 8h6" />
        </svg>
      )}
    </OTPInputSeparatorPart>
  )
}

export { OTPInput, OTPInputGroup, OTPInputSeparator, OTPInputSlot, createOTPInputController }
```

**components/keyframery/primitives/otp-input.tsx**

```tsx
"use client"

/* Animated drop-in for `@ui-sdk/react/otp-input`: same parts, same props. Swap the import and keep
   your code. This file adds motion only. Colours, radii, shadows and spacing stay yours. */

import { OTPInput as OTPInputRoot, OTPInputSlot as OTPInputSlotPart } from "@ui-sdk/react/otp-input"
import * as React from "react"

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

export * from "@ui-sdk/react/otp-input"

const keyframes =
  "@keyframes kf-otp-wave{0%{translate:0 0;scale:1}35%{translate:0 -5px;scale:1.07}100%{translate:0 0;scale:1}}"

/* Glide: one ring, drawn by the root, glides from slot to slot under CSS anchors instead of
   each slot lighting up. Its default look is neutral and follows the text colour; restyle it
   with `before:` classes (radius, shadow). It fades out when the input loses focus. */
function OTPInput({ className, ...props }: React.ComponentProps<typeof OTPInputRoot>) {
  return (
    <>
      <style href="kf-otp-wave" precedence="default">
        {keyframes}
      </style>
      <OTPInputRoot
        className={cn(
          "group/kf-otp isolate [anchor-scope:--kf-otp]",
          "before:pointer-events-none before:absolute before:z-10 before:[position-anchor:--kf-otp]",
          "before:top-[anchor(top)] before:right-[anchor(right)] before:bottom-[anchor(bottom)] before:left-[anchor(left)]",
          "before:rounded-[10px] before:shadow-[0_0_0_2px_color-mix(in_oklch,currentColor_40%,transparent)]",
          "before:scale-75 before:opacity-0 has-data-active:before:scale-100 has-data-active:before:opacity-100",
          "before:[transition:top_calc(340ms*var(--kf-speed,1))_var(--ease-spring),right_calc(340ms*var(--kf-speed,1))_var(--ease-spring),bottom_calc(340ms*var(--kf-speed,1))_var(--ease-spring),left_calc(340ms*var(--kf-speed,1))_var(--ease-spring),scale_calc(360ms*var(--kf-speed,1))_var(--ease-pop),opacity_calc(200ms*var(--kf-speed,1))_var(--ease-house)]",
          "motion-reduce:before:[transition:none]!",
          className,
        )}
        {...props}
      />
    </>
  )
}

/* Each typed character rises 12px into place from half size on `ease-pop`. When the code is
   complete, the slots pulse once, left to right. */
function OTPInputSlot({
  className,
  children,
  index,
  style,
  ...props
}: React.ComponentProps<typeof OTPInputSlotPart>) {
  const order = { "--kf-i": index } as React.CSSProperties
  return (
    <OTPInputSlotPart
      index={index}
      style={typeof style === "function" ? (state) => ({ ...order, ...style(state) }) : { ...order, ...style }}
      className={cn(
        "data-active:[anchor-name:--kf-otp]",
        "group-data-complete/kf-otp:animate-[kf-otp-wave_calc(560ms*var(--kf-speed,1))_var(--ease-house)_calc(var(--kf-i)*calc(60ms*var(--kf-speed,1)))_both]",
        "motion-reduce:animate-none!",
        className,
      )}
      {...props}
    >
      {children ??
        ((state) =>
          state.char ? (
            <span
              key={state.char}
              className={cn(
                "inline-block [transition:translate_calc(460ms*var(--kf-speed,1))_var(--ease-pop),scale_calc(460ms*var(--kf-speed,1))_var(--ease-pop),opacity_calc(220ms*var(--kf-speed,1))_var(--ease-house),filter_calc(260ms*var(--kf-speed,1))_var(--ease-house)]",
                "starting:translate-y-3 starting:scale-50 starting:opacity-0 starting:blur-[3px] motion-reduce:[transition:none]!",
              )}
            >
              {state.char}
            </span>
          ) : null)}
    </OTPInputSlotPart>
  )
}

export { OTPInput, OTPInputSlot }
```

## Questions

### How does the animated OTP Input for UI SDK move?

Glide: One ring glides from slot to slot as you type. Pop: Each digit pops up into place. Cascade: When the code is complete, the slots pulse once from left to right.

### Do I need Motion or Framer Motion for the animated OTP Input?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

### Can I keep my own styling and only take the motion?

Yes. Import from `@/components/keyframery/primitives/otp-input` instead. It has the same parts and props as `@ui-sdk/react/otp-input` and adds motion only.

### What happens to the OTP Input when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.
