Skip to content

Dashed

Make sure Nativewind is installed and configured in your project.

dashed-nativewind.tsx
import { View, Text, Alert } from 'react-native';
import { OTPInput, type SlotProps } from 'input-otp-native';
import type { OTPInputRef } from 'input-otp-native';
import { useRef } from 'react';
import Animated, {
useAnimatedStyle,
withRepeat,
withTiming,
withSequence,
useSharedValue,
} from 'react-native-reanimated';
import { useEffect } from 'react';
import { cn } from './utils';
export default function DashedOTPInput() {
const ref = useRef<OTPInputRef>(null);
const onComplete = (code: string) => {
Alert.alert('Completed with code:', code);
ref.current?.clear();
};
return (
<View>
<OTPInput
ref={ref}
onComplete={onComplete}
maxLength={5}
render={({ slots }) => (
<View className="flex-row items-center justify-center my-4">
{slots.map((slot, idx) => (
<Slot key={idx} {...slot} />
))}
</View>
)}
/>
</View>
);
}
function Slot({ char, isActive, hasFakeCaret }: SlotProps) {
return (
<View className="w-12 h-12 mx-2 items-center justify-center">
{char !== null && (
<Text className="text-3xl font-medium text-gray-900">{char}</Text>
)}
{hasFakeCaret && <FakeCaret />}
<View
className={cn('absolute bottom-0 w-full h-[1px] bg-gray-200', {
'bg-gray-900 h-0.5': isActive,
})}
/>
</View>
);
}
function FakeCaret() {
const opacity = useSharedValue(1);
useEffect(() => {
opacity.value = withRepeat(
withSequence(
withTiming(0, { duration: 500 }),
withTiming(1, { duration: 500 })
),
-1,
true
);
}, [opacity]);
const animatedStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
}));
const baseStyle = {
width: 2,
height: 24,
backgroundColor: '#111827',
borderRadius: 1,
};
return (
<View className="absolute w-full h-full items-center justify-center">
<Animated.View style={[baseStyle, animatedStyle]} />
</View>
);
}
dashed.tsx
import { View, Text, StyleSheet, type ViewStyle, Alert } from 'react-native';
import { OTPInput, type SlotProps } from 'input-otp-native';
import type { OTPInputRef } from 'input-otp-native';
import { useRef } from 'react';
import Animated, {
useAnimatedStyle,
withRepeat,
withTiming,
withSequence,
useSharedValue,
} from 'react-native-reanimated';
import { useEffect } from 'react';
export default function DashedOTPInput() {
const ref = useRef<OTPInputRef>(null);
const onComplete = (code: string) => {
Alert.alert('Completed with code:', code);
ref.current?.clear();
};
return (
<View>
<OTPInput
ref={ref}
onComplete={onComplete}
containerStyle={styles.container}
maxLength={5}
render={({ slots }) => (
<View style={styles.slotsContainer}>
{slots.map((slot, idx) => (
<Slot key={idx} {...slot} />
))}
</View>
)}
/>
</View>
);
}
function Slot({ char, isActive, hasFakeCaret }: SlotProps) {
return (
<View style={styles.slot}>
{char !== null && <Text style={styles.char}>{char}</Text>}
{hasFakeCaret && <FakeCaret />}
<View style={[styles.underline, isActive && styles.activeUnderline]} />
</View>
);
}
function FakeCaret({ style }: { style?: ViewStyle }) {
const opacity = useSharedValue(1);
useEffect(() => {
opacity.value = withRepeat(
withSequence(
withTiming(0, { duration: 500 }),
withTiming(1, { duration: 500 })
),
-1,
true
);
}, [opacity]);
const animatedStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
}));
return (
<View style={styles.fakeCaretContainer}>
<Animated.View style={[styles.fakeCaret, style, animatedStyle]} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
slotsContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
slot: {
width: 40,
height: 40,
marginHorizontal: 8,
alignItems: 'center',
justifyContent: 'center',
},
char: {
fontSize: 24,
fontWeight: '500',
color: '#111827',
},
underline: {
position: 'absolute',
bottom: 0,
width: '100%',
height: 1,
backgroundColor: '#E5E7EB',
},
activeUnderline: {
backgroundColor: '#111827',
height: 2,
},
fakeCaretContainer: {
position: 'absolute',
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
},
fakeCaret: {
width: 2,
height: 24,
backgroundColor: '#111827',
borderRadius: 1,
},
});