---
title: "Input OTP"
description: "Input OTP with accessible presentation and local state."
---

# Input OTP

Input OTP with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use InputOTP from "@rightui/ui"
```

## Usage

Import InputOTP from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, InputOTP from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state otp = ""
  state pin = ""
  action editPIN(value: string)
    pin = value
  action editOTP(value: string)
    otp = value
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      InputOTP(id="controls-otp", label="Verification code", value=otp, length=6, disabled=false, change=editOTP)
      text "Four digits and disabled" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      InputOTP(id="otp-four", label="Four digit PIN", value=pin, length=4, disabled=false, change=editPIN)
      InputOTP(id="otp-disabled", label="Disabled code", value="123456", length=6, disabled=true, change=editOTP)
      text ("Entered code: " + otp) testId="otp-controlled-result" role="status" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```

## API Reference

| Property | Type |
| --- | --- |
| InputOTP.id | string |
| InputOTP.label | string |
| InputOTP.value | string |
| InputOTP.length | number |
| InputOTP.disabled | bool |
| InputOTP.change | action |

## Source

File: packages/ui/input-otp.contract

```contract
use FieldFocus from "./styles.contract"
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
component InputOTP
  props
    id: string
    label: string
    value: string
    length: number
    disabled: bool
    change: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state cellWidth = 36
  state glyphWidth = 9
  action measureCell(w: number, h: number)
    cellWidth = w
  action measureGlyph(w: number, h: number)
    glyphWidth = w
  derive size = max(1, min(8, length))
  derive digits = value.split("").filter(c => "0123456789".includes(c)).join("").slice(0, size)
  action edit(v: string)
    change(v.split("").filter(c => "0123456789".includes(c)).join("").slice(0, size))
  view
    column gap="ui.space-8" min-width="0px"
      text label id=(id + "-label") font-size="ui.text-sm" font-weight=uiWeights.medium color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      view position="relative" align-self="flex-start" min-width="0px" max-width="100%"
        row aria-hidden=true gap="0px" pointer-events="none"
          for i in [0, 1, 2, 3, 4, 5, 6, 7].slice(0, size) key=i
            view width="ui.space-36" height="ui.space-36" position="relative" resize=measureCell
              view position="absolute" inset=0 display="flex" align-items="center" justify-content="center" border-width="ui.border-width" border-style="solid" border-color="ui.border" background-color="ui.surface" color="ui.foreground" font-size="ui.text-sm"
                border-top-left-radius=(i == 0 ? "ui.radius-control" : "0px") border-bottom-left-radius=(i == 0 ? "ui.radius-control" : "0px")
                border-top-right-radius=(i == size - 1 ? "ui.radius-control" : "0px") border-bottom-right-radius=(i == size - 1 ? "ui.radius-control" : "0px")
                text digits.slice(i, i + 1) font-family=uiFontFamily(uiFont) data-rightui-font="body" font-variant-numeric="tabular-nums"
        text "0" position="absolute" left=0 top=0 opacity=0 aria-hidden=true pointer-events="none" font-family=uiFontFamily(uiFont) data-rightui-font="body" font-size="ui.text-sm" font-variant-numeric="tabular-nums" resize=measureGlyph
        input class=FieldFocus id=id testId=id value=digits input=edit disabled=disabled inputmode="numeric" autocomplete="one-time-code" aria-labelledby=(id + "-label") font-family=uiFontFamily(uiFont) data-rightui-font="body"
          position="absolute" inset=0 width="100%" height="100%" box-sizing="border-box" appearance="none" border="0" border-radius="ui.radius-control" background-color="transparent" color="transparent" caret-color="ui.foreground" font-size="ui.text-sm" letter-spacing=max(0, cellWidth - glyphWidth) padding-top=0 padding-bottom=0 padding-left=max(0, (cellWidth - glyphWidth) / 2) padding-right=max(0, (cellWidth - glyphWidth) / 2) font-variant-numeric="tabular-nums"

```
