Input OTP
The InputOtp component allows users to enter one-time passcodes. It provides a series of input boxes that automatically handle focus and input.
Installation
$ cbui-cli install inputotpcbui-cli globally and authenticated your account. This will ensure you can access all the necessary features.Import
1import { InputOtp } from '@/crossbuildui/inputotp';glass variant for the InputOtp component uses expo-blur. Ensure this library is installed and linked if you plan to use this feature.InputOtp Component
Basic Usage
Specify the length of the OTP.
1<InputOtp length={6} />Variants
InputOtp supports multiple visual variant options for the input boxes: flat, bordered (default), faded, underlined, and glass.
1<View style={{gap: 16}}>
2 <InputOtp length={4} variant="flat" />
3 <InputOtp length={4} variant="bordered" /> {/* Default */}
4 <InputOtp length={4} variant="faded" />
5 <InputOtp length={4} variant="underlined" />
6 <InputOtp length={4} variant="glass" />
7</View>Sizes
Adjust input box size with sm, md (default), or lg.
1<View style={{gap: 16, alignItems: 'center'}}>
2 <InputOtp length={4} size="sm" />
3 <InputOtp length={4} size="md" /> {/* Default */}
4 <InputOtp length={4} size="lg" />
5</View>Radius
Control the border radius of input boxes with none, sm, md (default), lg, or full.
1<View style={{gap: 16, alignItems: 'center'}}>
2 <InputOtp length={4} radius="none" />
3 <InputOtp length={4} radius="sm" />
4 <InputOtp length={4} radius="md" /> {/* Default */}
5 <InputOtp length={4} radius="lg" />
6 <InputOtp length={4} radius="full" />
7</View>Colors
The color prop influences the input box appearance on focus or when invalid. Supported colors include primary, secondary, success, warning, danger, and default.
1<View style={{gap: 16, alignItems: 'center'}}>
2 <InputOtp length={4} color="primary" placeholder="P" />
3 <InputOtp length={4} color="secondary" placeholder="S" />
4 <InputOtp length={4} color="success" placeholder="S" />
5 <InputOtp length={4} color="warning" placeholder="W" />
6 <InputOtp length={4} color="danger" isInvalid errorMessage="Invalid OTP" />
7 <InputOtp length={4} color="default" placeholder="D" />
8</View>Glass Variant
The glass variant applies a frosted glass effect to each OTP input box background using expo-blur. Customize its appearance with glassIntensity (0-100) and glassTint ('light', 'dark', or 'default'). Text and placeholder colors for this variant typically adapt to the theme's foreground color for better contrast. The border color also adapts based on focus, invalid state, and glass tint.
1<View style={{gap: 16}}>
2 <InputOtp
3 length={4}
4 variant="glass"
5 glassIntensity={70}
6 glassTint="light"
7 placeholder="LGT"
8 />
9 <InputOtp
10 length={6}
11 variant="glass"
12 glassIntensity={40}
13 glassTint="dark"
14 placeholder="DRK"
15 />
16</View>Description and Error Message
Provide helpful context with description or display validation feedback using isInvalid and errorMessage.
1<View style={{gap: 16}}>
2 <InputOtp
3 length={6}
4 description="Enter the 6-digit code sent to your phone."
5 />
6 <InputOtp
7 length={4}
8 isInvalid
9 errorMessage="The OTP entered is incorrect. Please try again."
10 value="123" // Example value to show error state
11 />
12</View>Controlled and Uncontrolled
Manage OTP state externally using value and onValueChange for a controlled component, or let it manage its own state (uncontrolled).
1// Controlled InputOtp
2function ControlledInputOtpExample() {
3 const [otp, setOtp] = React.useState('');
4
5 return (
6 <View style={{gap: 8}}>
7 <InputOtp
8 length={4}
9 value={otp}
10 onValueChange={setOtp}
11 placeholder="C"
12 />
13 <Text>Current OTP: {otp}</Text>
14 </View>
15 );
16}
17
18// Uncontrolled InputOtp (uses internal state)
19// <InputOtp length={4} placeholder="U" />
20Disabled State
Disable the OTP input using the disabled prop.
1<InputOtp length={4} disabled value="1234" />Placeholder
Set a placeholder character for empty input boxes and customize its color with placeholderTextColor.
1<View style={{gap: 16}}>
2 <InputOtp length={4} placeholder="0" />
3 <InputOtp length={4} placeholder="*" placeholderTextColor="blue" />
4</View>Custom TextInput Props
Pass additional props to the underlying TextInput components using textInputProps. This is useful for settings like keyboardType, autoFocus on the first input, etc.
1<InputOtp
2 length={6}
3 textInputProps={{
4 keyboardType: 'default', // Default is 'number-pad'
5 // You can pass other TextInput props like autoFocus on the first input, etc.
6 // Note: some props like 'value', 'onChangeText', 'maxLength' are controlled by InputOtp
7 }}
8 description="Uses 'default' keyboard type."
9/>Props Overview
| PROP | TYPE | DEFAULT | DESCRIPTION |
|---|---|---|---|
length | number | - | Number of OTP input boxes. Required. |
variant | InputOtpVariant | 'bordered' | Visual style. |
color | InputOtpColor | 'default' | Thematic color on focus/invalid. |
size | InputOtpSize | 'md' | Size of input boxes. |
radius | InputOtpRadius | 'md' | Border radius of boxes. |
value | string | - | Controlled value of the OTP. |
onValueChange | (otp: string) => void | - | Callback when OTP value changes. |
description | ReactNode | - | Helper text below the OTP input. |
errorMessage | ReactNode | - | Error message when isInvalid. |
isInvalid | boolean | false | Marks input as invalid. |
styles | InputOtpSlotsStyles | - | Custom styles for slots (base, otpInputWrapper, etc.). |
style | StyleProp<ViewStyle> | - | Style for the outermost container (styles.base equivalent). |
disabled | boolean | false | Disable the input. |
placeholder | string | - | Placeholder character for each box. |
placeholderTextColor | ColorValue | - | Custom color for placeholder text. |
textInputProps | Omit<TextInputProps, ...> | - | Props for underlying TextInput components. |
glassTint | 'default' | 'light' | 'dark' | Theme-derived | Tint for the 'glass' variant blur effect on each input box. |
glassIntensity | number | 30 | Intensity (0-100) for the 'glass' variant blur effect. |
Styling
Customize the InputOtp appearance using the style prop for the main wrapper or the styles prop for more granular control over internal slots:
style: Applied to the outermost container (View) of the entire component. Equivalent tostyles.base.styles.base: Styles for the outermost container.styles.otpInputWrapper: Styles for theViewthat wraps all the individual OTP input boxes.styles.otpInput: Styles for each individual OTPTextInputbox.styles.description: Styles for the descriptionTextcomponent.- For the
glassvariant, eachotpInputbox background is rendered byBlurViewfromexpo-blur, controlled byglassIntensityandglassTint. Thestyles.otpInput.backgroundColorwill be overridden for this variant. styles.errorMessage: Styles for the error messageTextcomponent.
1<InputOtp
2 length={4}
3 value="STYL"
4 style={{ marginVertical: 20, padding: 10, backgroundColor: '#e6e6fa' }} // Styles the outermost 'base' container
5 styles={{
6 base: { borderWidth: 1, borderColor: 'purple' },
7 otpInputWrapper: { columnGap: 20 }, // Increased gap between boxes
8 otpInput: {
9 backgroundColor: 'lightcyan',
10 borderColor: 'teal',
11 borderWidth: 2,
12 color: 'darkgreen',
13 fontWeight: 'bold',
14 },
15 description: { color: 'navy', fontStyle: 'italic', marginTop: 10 },
16 errorMessage: { color: 'darkred', fontWeight: 'bold', marginTop: 10 }
17 }}
18 description="This OTP input has custom slot styling."
19/>Accessibility
The InputOtp component includes features to aid accessibility:
- Each input box has
textContentType="oneTimeCode"(iOS) andautoComplete="sms-otp"(Android) /"one-time-code"(web) to help with autofill from SMS. - Keyboard type defaults to
"number-pad"for easier numeric input. - Consider providing an overall
accessibilityLabelfor the component if the context isn't clear from surrounding elements, especially if there's no visible description.