From 4457a08625d75778749eba15bb9cf7a031c3059a Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:22:05 +0800 Subject: [PATCH] Import apps/android/src/ui/toggle.tsx --- apps/android/src/ui/toggle.tsx | 79 ++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 apps/android/src/ui/toggle.tsx diff --git a/apps/android/src/ui/toggle.tsx b/apps/android/src/ui/toggle.tsx new file mode 100644 index 0000000..b189813 --- /dev/null +++ b/apps/android/src/ui/toggle.tsx @@ -0,0 +1,79 @@ +import { useEffect, useRef } from 'react' +import { Animated, Easing, Pressable, StyleSheet } from 'react-native' +import { useTheme, type ThemeColors } from './theme-context' +import { useThemedStyles } from './use-themed-styles' + +const TRACK_WIDTH = 56 +const TRACK_HEIGHT = 30 +const INSET = 2 +const THUMB_WIDTH = 34 +const THUMB_HEIGHT = TRACK_HEIGHT - INSET * 2 +const TRAVEL = TRACK_WIDTH - INSET * 2 - THUMB_WIDTH + +/** + * Pill-shaped on/off control replacing the platform Switch: a blue track when + * on, a grey track when off, and a wide white thumb outlined in the track color. + * `trackColor` / `thumbColor` are accepted for drop-in parity with Switch and ignored. + */ +export function Toggle({ value, onValueChange, disabled = false, accessibilityLabel }: { + value: boolean + onValueChange?: (value: boolean) => void + disabled?: boolean + accessibilityLabel?: string + trackColor?: unknown + thumbColor?: unknown +}) { + const { colors } = useTheme() + const styles = useThemedStyles(createStyles) + const progress = useRef(new Animated.Value(value ? 1 : 0)).current + + useEffect(() => { + Animated.timing(progress, { + toValue: value ? 1 : 0, + duration: 180, + easing: Easing.out(Easing.cubic), + useNativeDriver: false, + }).start() + }, [progress, value]) + + const trackColor = progress.interpolate({ inputRange: [0, 1], outputRange: [colors.disabled, colors.primary] }) + const translateX = progress.interpolate({ inputRange: [0, 1], outputRange: [0, TRAVEL] }) + + return ( + { + e?.stopPropagation?.() + onValueChange?.(!value) + }} + style={disabled ? styles.disabled : undefined} + > + + + + + ) +} + +function createStyles(colors: ThemeColors) { + return StyleSheet.create({ + track: { width: TRACK_WIDTH, height: TRACK_HEIGHT, borderRadius: TRACK_HEIGHT / 2, padding: INSET, justifyContent: 'center' }, + thumb: { + width: THUMB_WIDTH, + height: THUMB_HEIGHT, + borderRadius: THUMB_HEIGHT / 2, + backgroundColor: colors.white, + borderWidth: 1.5, + shadowColor: colors.shadow, + shadowOffset: { width: 0, height: 1 }, + shadowOpacity: 0.12, + shadowRadius: 2, + elevation: 1, + }, + disabled: { opacity: 0.5 }, + }) +}