$ git show slide-confirm
Slide to confirm
Drag the knob along the track to confirm. The fill follows your finger, and past the threshold it locks in, otherwise it springs back.
import { scheduleOnRN } from 'react-native-worklets';
// Drag-driven, so Reanimated 4 still uses a shared value; scheduleOnRN replaces runOnJS
const TRACK = 280, KNOB = 48, PAD = 4;
const MAX = TRACK - KNOB - PAD * 2; // how far the knob can travel
const x = useSharedValue(0);
const done = useSharedValue(false);
const pan = Gesture.Pan()
.enabled(!confirmed)
.onChange((e) => {
x.value = Math.min(Math.max(x.value + e.changeX, 0), MAX);
})
.onEnd((e) => {
if (x.value > MAX * 0.85 || e.velocityX > 1200) {
x.value = withSpring(MAX, { damping: 18, stiffness: 200 });
if (!done.value) {
done.value = true;
scheduleOnRN(onConfirm);
}
} else {
x.value = withSpring(0, { damping: 14, stiffness: 180 }); // not far enough, snap back
}
});
const knob = useAnimatedStyle(() => ({ transform: [{ translateX: x.value }] }));
const fill = useAnimatedStyle(() => ({ width: x.value + KNOB + PAD * 2 }));
const label = useAnimatedStyle(() => ({
opacity: interpolate(x.value, [0, MAX * 0.5], [1, 0], Extrapolation.CLAMP),
}));
// Call to reset, e.g. after the request fails
const reset = () => { done.value = false; x.value = withSpring(0); };
<View style={[styles.track, { width: TRACK, height: KNOB + PAD * 2 }]}> {/* borderRadius 999, light gray, overflow hidden */}
<Animated.View style={[styles.fill, fill]} /> {/* absolute left/top/bottom 0, accent, borderRadius 999 */}
<Animated.Text style={[styles.label, label]}>Slide to confirm</Animated.Text>
<GestureDetector gesture={pan}>
{/* absolute, top/left PAD, KNOB x KNOB circle, white, small shadow, chevron icon */}
<Animated.View style={[styles.knob, knob]}><Chevron /></Animated.View>
</GestureDetector>
</View>