input-otp-native

repository·main·Indexed 43 days ago

https://github.com/yjose/input-otp-native

An unstyled and fully customizable one-time passcode (OTP) input component for React Native and Expo. Version 0.6.0 provides high flexibility through render props and OTPInputRef for imperative control, allowing developers to implement various styles such as Apple, Stripe, Revolt, or Dashed using standard StyleSheet or NativeWind.

Tokens
7.1K
Snippets
25
Records
30
Agent score
54%

What's inside input-otp-native

  1. Overview of Input OTP Native

    main

    Input OTP Native is a one-time passcode (OTP) input component designed specifically for React Native and Expo applications. It is unstyled and fully customizable, allowing developers to implement secure code entry fields with high flexibility.

    Key Features

    • React Native/Expo Support: Built natively for the React Native ecosystem.
    • Production Ready: Maintains 100% test coverage for reliability.
    • Customizable Styling: Uses render props for complete control over the UI, including support for NativeWind.
    • Pre-made Styles: Offers four copy-pasteable styles: Apple, Stripe, Revolt, and Dashed.
    • Animation Friendly: Easily integrates with react-native-reanimated.
    • Web Support: Works on the web via integration with otp-input.
  2. How to handle slot interaction with slot.focus()

    main

    Each slot exposes a focus() method. You can use this to allow users to tap a specific slot (e.g., via a Pressable) to resume typing from that position without needing a component ref. This is particularly useful for suppressing default iOS clear behavior when tapping slots.

    <OTPInput
      maxLength={6}
      render={({ slots }) => (
        <View style={{ flexDirection: 'row', gap: 8 }}>
          {slots.map((slot, index) => (
            <Pressable key={index} onPress={slot.focus}>
              <Slot {...slot} />
            </Pressable>
          ))}
        </View>
      )}
    />
  3. Understand the Starlight project structure

    main

    The project follows a standard Astro + Starlight structure:

    • src/content/docs/: Place your .md or .mdx files here. Each file is automatically exposed as a route based on its filename.
    • src/assets/: Store images here to embed them in Markdown using relative links.
    • public/: Store static assets like favicons.
    • tailwind.config.mjs: Configuration for Tailwind CSS.
    • astro.config.mjs: Configuration for the Astro framework.
  4. How to render OTP slots with RenderProps and SlotProps

    main

    To customize the appearance of the input, use the render prop. This function receives RenderProps, which contains an array of slots. Each slot is a SlotProps object representing an individual digit position.

    Each slot provides a focus() method. You can use this to allow users to tap a specific slot (e.g., via a Pressable) to resume typing from that position without needing a manual ref.

    <OTPInput
      maxLength={6}
      render={({ slots }) => (
        <View style={{ flexDirection: 'row', gap: 8 }}>
          {slots.map((slot, index) => (
            <Pressable key={index} onPress={slot.focus}>
              <Slot {...slot} />
            </Pressable>
          ))}
        </View>
      )}
    />
  5. Style a Dashed OTPInput using StyleSheet

    main

    For projects not using Nativewind, you can achieve a dashed style by using standard React Native StyleSheet. Apply styles to the input slots to define the borders and dashed properties for each OTP segment.

    // Example implementation using StyleSheet
    import { StyleSheet } from 'react-native';
    import { OTPInput } from 'input-otp-native';
    
    // Implementation logic provided in dashed.tsx
  6. Style a Dashed OTPInput using Nativewind

    main

    To create a dashed OTP input style using Nativewind, use the OTPInput component and apply Tailwind-style utility classes via the slotProps or renderProps (depending on your specific implementation needs) to the individual input slots. Ensure Nativewind is correctly installed and configured in your React Native project before attempting this.

    // Example implementation using Nativewind
    // Note: The exact component structure depends on the OTPInput API
    import { OTPInput } from 'input-otp-native';
    
    // Implementation logic provided in dashed-nativewind.tsx
  7. Implement an animated Stripe-style OTP input

    main

    You can create a Stripe-style OTP input with fade-in animations for entered characters by combining input-otp-native with react-native-reanimated and NativeWind. This pattern uses Reanimated to handle the entry animations of each character slot and NativeWind for styling the input components.

    // Note: This example requires react-native-reanimated and Nativewind to be installed and configured.
    import React from 'react';
    import { OTPInput } from 'input-otp-native';
    import Animated, { FadeIn } from 'react-native-reanimated';
    import { styled } from 'nativewind';
    
    // Example implementation pattern using NativeWind and Reanimated
    const StyledSlot = styled(Animated.View);
    
    export function AnimatedStripeOTP() {
      return (
        <OTPInput
          // ... other props
          renderSlot={({ char, isFocused }) => (
            <StyledSlot
              entering={FadeIn}
              className={isFocused ? 'border-blue-500' : 'border-gray-300'}
            >
              <Text>{char}</Text>
            </StyledSlot>
          )}
        />
      );
    }
  8. Implement Apple-style OTPInput with StyleSheet

    main

    For projects not using Nativewind, you can implement the Apple-style OTP input using standard React Native StyleSheet. This approach uses traditional style objects to define the appearance of the input cells and container.

    // Example implementation using StyleSheet
    import { StyleSheet } from 'react-native';
    import { OTPInput } from 'input-otp-native';
    
    // The actual implementation is provided in the apple.tsx example
  9. Implement Revolt style using Nativewind

    main

    To implement the Revolt style using Nativewind, ensure that Nativewind is properly installed and configured in your React Native project. You can then use Tailwind classes via the className prop on the components provided by input-otp-native to achieve the Revolt aesthetic.

    // revolt-nativewind.tsx
    // Note: This is a conceptual representation of the example provided in the documentation
    import { OTPInput } from 'input-otp-native';
    
    export function RevoltNativeWind() {
      return (
        <OTPInput
          // Use Nativewind/Tailwind classes for styling
          className="flex-row gap-2"
          // ... other props
        />
      );
    }