OTP Field

A row of one-character slots that edit a single passcode.

EXAMPLEwasm · webgpu

This demo runs the real GPUI component on WebGPU, which this browser does not expose.

Try Chrome or Edge 113+, or Safari 26+. In Firefox, enable dom.webgpu.enabled.

You can also run it natively: cargo run -p showcase otp-field

The demo failed to start in this browser.

You can run it natively instead: cargo run -p showcase otp-field

Live examples are not supported on most mobile browsers yet — open this page on a desktop or tablet to run the demo.

Recent phones with WebGPU may manage it (downloads ~18 MB):

#Anatomy

Import the component's parts and compose them under its root:

use base_gpui::otp_field::{
    OTPFieldInput,
    OTPFieldRoot,
};

OTPFieldRoot::new()
    .child(
        OTPFieldInput::new(),
    );
The anatomy is a structural overview. Parts with mutually exclusive modes may need separate instances; each part's section below documents its configuration.

#OTPFieldRoot

Coordinates the component's state and supplies context to its child parts.

Source

use base_gpui::otp_field::OTPFieldRoot;

OTPFieldRoot::new()
    .with_field_context(/* context: FieldContext */)
    .id("example-id")
    .name("name")
    .aria_label("aria label")
    .length(0)
    .default_value(/* default_value: impl Into<SharedString> */)
    .value(/* value: impl Into<SharedString> */)
    .validation_type("example-id")
    .normalize_value(|/* callback arguments */| { /* handle change */ })
    .mask(true)
    .auto_submit(true)
    .disabled(true)
    .read_only(true)
    .required(true)
    .on_value_change(|/* callback arguments */| { /* handle change */ })
    .on_value_complete(|/* callback arguments */| { /* handle change */ })
    .on_value_invalid("example-id")
    .style_with_state(|/* callback arguments */| { /* handle change */ });

#Builders

BuilderSignature & description
.newpub fn new() -> Self

Creates a OTPFieldRoot with its default configuration.

.with_field_contextpub fn with_field_context(mut self, context: FieldContext) -> Self
  • context: FieldContext

Sets the with field context configuration for this part.

.childpub fn child(mut self, child: impl Into<OTPFieldChild>) -> Self
  • child: impl Into<OTPFieldChild>

Adds one typed child to this part.

.childrenpub fn children(mut self, children: impl IntoIterator<Item = impl Into<OTPFieldChild>>,) -> Self
  • children: impl IntoIterator<Item = impl Into<OTPFieldChild>>

Adds multiple typed children in iteration order.

.child_anypub fn child_any(mut self, child: impl IntoElement) -> Self
  • child: impl IntoElement

Adds one arbitrary renderable child to this part.

.idpub fn id(mut self, id: impl Into<ElementId>) -> Self
  • id: impl Into<ElementId>

Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view.

.namepub fn name(mut self, name: impl Into<SharedString>) -> Self
  • name: impl Into<SharedString>

Sets the form field name used when serializing or submitting the value.

.aria_labelpub fn aria_label(mut self, aria_label: impl Into<SharedString>) -> Self
  • aria_label: impl Into<SharedString>

Accessible name for the OTP group. Mirror the visible FieldLabel text here manually: relationship props (aria-labelledby) do not exist in this gpui revision.

.lengthpub fn length(mut self, length: usize) -> Self
  • length: usize

Sets the length configuration for this part.

.default_valuepub fn default_value(mut self, default_value: impl Into<SharedString>) -> Self
  • default_value: impl Into<SharedString>

Sets the initial value for uncontrolled state. Later user changes are retained by the component.

.valuepub fn value(mut self, value: impl Into<SharedString>) -> Self
  • value: impl Into<SharedString>

Sets the current controlled value or the value represented by this part, depending on the part's role.

.validation_typepub fn validation_type(mut self, validation_type: OTPFieldValidationType) -> Self
  • validation_type: OTPFieldValidationType

Sets the validation type configuration for this part.

.normalize_valuepub fn normalize_value(mut self, normalize_value: impl Fn(String) -> String + 'static) -> Self
  • normalize_value: impl Fn(String) -> String + 'static

Sets the normalize value configuration for this part.

.maskpub fn mask(mut self, mask: bool) -> Self
  • mask: bool

Sets the mask configuration for this part.

.auto_submitpub fn auto_submit(mut self, auto_submit: bool) -> Self
  • auto_submit: bool

Controls whether auto submit behavior is enabled.

.disabledpub fn disabled(mut self, disabled: bool) -> Self
  • disabled: bool

When true, prevents user interaction with this part.

.read_onlypub fn read_only(mut self, read_only: bool) -> Self
  • read_only: bool

When true, allows presentation and focus behavior without permitting value changes.

.requiredpub fn required(mut self, required: bool) -> Self
  • required: bool

Marks the control as required for validation and accessibility state.

.on_value_changepub fn on_value_change(mut self, on_value_change: impl Fn(SharedString, OTPFieldChangeDetails, &mut Window, &mut App) + 'static,) -> Self
  • on_value_change: impl Fn(SharedString, OTPFieldChangeDetails, &mut Window, &mut App) + 'static

Registers a callback invoked when value change occurs.

.on_value_completepub fn on_value_complete(mut self, on_value_complete: impl Fn(SharedString, OTPFieldChangeDetails, &mut Window, &mut App) + 'static,) -> Self
  • on_value_complete: impl Fn(SharedString, OTPFieldChangeDetails, &mut Window, &mut App) + 'static

Registers a callback invoked when value complete occurs.

.on_value_invalidpub fn on_value_invalid(mut self, on_value_invalid: impl Fn(SharedString, OTPFieldChangeDetails, &mut Window, &mut App) + 'static,) -> Self
  • on_value_invalid: impl Fn(SharedString, OTPFieldChangeDetails, &mut Window, &mut App) + 'static

Registers a callback invoked when value invalid occurs.

.style_with_statepub fn style_with_state(mut self, style: impl Fn(OTPFieldRootStyleState, Div) -> Div + 'static,) -> Self
  • style: impl Fn(OTPFieldRootStyleState, Div) -> Div + 'static,

Styles the part from its current behavioral state while preserving separation between behavior and visual design.

OTPFieldRoot also supports the GPUI traits implemented in its source, such as standard styling or child composition.

#OTPFieldInput

Text input integrated with the component's state and behavior.

Source

use base_gpui::otp_field::OTPFieldInput;

OTPFieldInput::new()
    .with_otp_field_context(/* context: OTPFieldContext */)
    .with_slot_index(0)
    .id("example-id")
    .style_with_state(|/* callback arguments */| { /* handle change */ });

#Builders

BuilderSignature & description
.newpub fn new() -> Self

Creates a OTPFieldInput with its default configuration.

.with_otp_field_contextpub fn with_otp_field_context(mut self, context: OTPFieldContext) -> Self
  • context: OTPFieldContext

Sets the with otp field context configuration for this part.

.with_slot_indexpub fn with_slot_index(mut self, index: usize) -> Self
  • index: usize

Sets the with slot index configuration for this part.

.idpub fn id(mut self, id: impl Into<ElementId>) -> Self
  • id: impl Into<ElementId>

Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view.

.style_with_statepub fn style_with_state(mut self, style: impl Fn(OTPFieldInputStyleState, Div) -> Div + 'static,) -> Self
  • style: impl Fn(OTPFieldInputStyleState, Div) -> Div + 'static,

Styles the part from its current behavioral state while preserving separation between behavior and visual design.

OTPFieldInput also supports the GPUI traits implemented in its source, such as standard styling or child composition.

#Accessibility

Keyboard interaction and accessibility semantics are implemented by the component, independently of visual styling. Known limitations caused by missing GPUI accessibility primitives are documented in the module source and are not silently approximated.

#Stability

Base GPUI is pre-1.0. Builder names and state types may evolve as GPUI and this port mature.