|
1 | | -import { useState } from "react"; |
2 | | -import { I18nManager, Platform, Pressable, View } from "react-native"; |
3 | | -import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated"; |
4 | | - |
5 | | -import { |
6 | | - flattenThemeColor, |
7 | | - themeColorWithAlpha, |
8 | | - themeColorWithContrast, |
9 | | -} from "../../lib/mobileTheme"; |
10 | | -import { AppText } from "./AppText"; |
11 | | -import { useControlTheme } from "./useControlTheme"; |
12 | | -import type { SegmentedControlProps } from "./SegmentedControl.types"; |
13 | | - |
14 | | -export type { SegmentedControlProps } from "./SegmentedControl.types"; |
15 | | - |
16 | | -export function SegmentedControl<Value extends string | number>( |
17 | | - props: SegmentedControlProps<Value>, |
18 | | -) { |
19 | | - const { colorScheme, colors } = useControlTheme(); |
20 | | - const [layout, setLayout] = useState({ width: 0, height: 0 }); |
21 | | - const { width } = layout; |
22 | | - const reduceMotion = useReducedMotion(); |
23 | | - const selectedIndex = props.options.findIndex((option) => option.value === props.selected); |
24 | | - const segmentWidth = props.options.length ? Math.max(0, width - 6) / props.options.length : 0; |
25 | | - const selectionOffset = selectedIndex * segmentWidth * (I18nManager.isRTL ? -1 : 1); |
26 | | - const selectionStyle = useAnimatedStyle(() => ({ |
27 | | - width: segmentWidth, |
28 | | - transform: [{ translateX: withTiming(selectionOffset, { duration: reduceMotion ? 0 : 160 }) }], |
29 | | - })); |
30 | | - const dark = colorScheme === "dark"; |
31 | | - const trackColor = flattenThemeColor( |
32 | | - themeColorWithAlpha(colors["--color-prominent-action"], dark ? 0.18 : 0.07), |
33 | | - colors["--color-screen"], |
34 | | - ); |
35 | | - const selectedColor = colors[dark ? "--color-prominent-action" : "--color-secondary"]; |
36 | | - const selectedTextColor = |
37 | | - colors[dark ? "--color-prominent-action-foreground" : "--color-secondary-foreground"]; |
38 | | - const mutedColor = themeColorWithContrast( |
39 | | - colors["--color-foreground-muted"], |
40 | | - trackColor, |
41 | | - dark ? 7 : 4.5, |
42 | | - ); |
43 | | - const minHeight = props.size === "compact" ? 32 : Platform.OS === "android" ? 40 : 36; |
44 | | - |
45 | | - return ( |
46 | | - <View collapsable={false} className={props.className}> |
47 | | - <View |
48 | | - onLayout={(event) => setLayout(event.nativeEvent.layout)} |
49 | | - className="flex-row rounded-full p-[3px]" |
50 | | - style={{ |
51 | | - minHeight, |
52 | | - backgroundColor: trackColor, |
53 | | - }} |
54 | | - > |
55 | | - {selectedIndex >= 0 && width > 0 ? ( |
56 | | - <Animated.View |
57 | | - pointerEvents="none" |
58 | | - className="absolute bottom-[3px] start-[3px] top-[3px] rounded-full" |
59 | | - style={[ |
60 | | - { |
61 | | - backgroundColor: selectedColor, |
62 | | - }, |
63 | | - selectionStyle, |
64 | | - ]} |
65 | | - /> |
66 | | - ) : null} |
67 | | - {props.options.map((option) => { |
68 | | - const selected = option.value === props.selected; |
69 | | - return ( |
70 | | - <Pressable |
71 | | - key={`${typeof option.value}:${option.value}`} |
72 | | - accessibilityRole={props.role ?? "button"} |
73 | | - accessibilityLabel={option.accessibilityLabel ?? option.label} |
74 | | - accessibilityState={{ selected }} |
75 | | - onPress={() => props.onSelect(option.value)} |
76 | | - className="flex-1 items-center justify-center rounded-full px-2 py-1 active:opacity-70" |
77 | | - style={{ minHeight: minHeight - 6 }} |
78 | | - > |
79 | | - <AppText |
80 | | - textRole="footnote" |
81 | | - className={selected ? "font-t3-bold" : "font-t3-medium"} |
82 | | - style={{ |
83 | | - color: selected ? selectedTextColor : mutedColor, |
84 | | - }} |
85 | | - > |
86 | | - {option.label} |
87 | | - </AppText> |
88 | | - </Pressable> |
89 | | - ); |
90 | | - })} |
91 | | - </View> |
92 | | - </View> |
93 | | - ); |
94 | | -} |
| 1 | +export { SegmentedControl, type SegmentedControlProps } from "./SegmentedControl.shared"; |
0 commit comments