|
| 1 | +import { GlassView } from "expo-glass-effect"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { Pressable, View } from "react-native"; |
| 4 | +import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated"; |
| 5 | +import { withUniwind } from "uniwind"; |
| 6 | + |
| 7 | +import { cn } from "../../lib/cn"; |
| 8 | +import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass"; |
| 9 | +import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider"; |
| 10 | +import { AppText } from "./AppText"; |
| 11 | + |
| 12 | +const TabGlass = withUniwind(GlassView, { style: { fromClassName: "className" } }); |
| 13 | + |
| 14 | +export interface TabsProps<Value extends string> { |
| 15 | + readonly options: readonly { |
| 16 | + readonly value: Value; |
| 17 | + readonly label: string; |
| 18 | + readonly count?: number; |
| 19 | + }[]; |
| 20 | + readonly selected: Value; |
| 21 | + readonly onSelect: (value: Value) => void; |
| 22 | + readonly className?: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** Section navigation. Value pickers use SegmentedControl. */ |
| 26 | +export function Tabs<Value extends string>(props: TabsProps<Value>) { |
| 27 | + const { themeAppearance } = useAppearancePreferences(); |
| 28 | + const reduceMotion = useReducedMotion(); |
| 29 | + const [frames, setFrames] = useState<Partial<Record<Value, { x: number; width: number }>>>({}); |
| 30 | + const frame = frames[props.selected]; |
| 31 | + const duration = reduceMotion ? 0 : 220; |
| 32 | + const indicatorStyle = useAnimatedStyle(() => ({ |
| 33 | + width: withTiming(frame?.width ?? 0, { duration }), |
| 34 | + transform: [{ translateX: withTiming(frame?.x ?? 0, { duration }) }], |
| 35 | + })); |
| 36 | + |
| 37 | + return ( |
| 38 | + <View accessibilityRole="tablist" className={cn("flex-row", props.className)}> |
| 39 | + {frame ? ( |
| 40 | + <Animated.View |
| 41 | + pointerEvents="none" |
| 42 | + className="absolute bottom-0 left-0 top-0 rounded-full border border-border-subtle" |
| 43 | + style={indicatorStyle} |
| 44 | + > |
| 45 | + {NATIVE_LIQUID_GLASS_SUPPORTED ? ( |
| 46 | + <TabGlass |
| 47 | + className="flex-1 rounded-full" |
| 48 | + colorScheme={themeAppearance === "dark" ? "dark" : "light"} |
| 49 | + /> |
| 50 | + ) : ( |
| 51 | + <View className="flex-1 rounded-full bg-grouped-card" /> |
| 52 | + )} |
| 53 | + </Animated.View> |
| 54 | + ) : null} |
| 55 | + {props.options.map((option) => { |
| 56 | + const selected = option.value === props.selected; |
| 57 | + return ( |
| 58 | + <Pressable |
| 59 | + key={option.value} |
| 60 | + accessibilityRole="tab" |
| 61 | + accessibilityLabel={option.count ? `${option.label}, ${option.count}` : option.label} |
| 62 | + accessibilityState={{ selected }} |
| 63 | + onPress={() => props.onSelect(option.value)} |
| 64 | + onLayout={({ nativeEvent: { layout } }) => |
| 65 | + setFrames((current) => |
| 66 | + current[option.value]?.x === layout.x && |
| 67 | + current[option.value]?.width === layout.width |
| 68 | + ? current |
| 69 | + : { ...current, [option.value]: { x: layout.x, width: layout.width } }, |
| 70 | + ) |
| 71 | + } |
| 72 | + className="min-h-9 flex-row items-center gap-1.5 rounded-full px-3.5 active:opacity-70" |
| 73 | + > |
| 74 | + <AppText |
| 75 | + textRole="footnote" |
| 76 | + className={cn( |
| 77 | + "font-t3-semibold", |
| 78 | + selected ? "text-foreground" : "text-foreground-muted", |
| 79 | + )} |
| 80 | + > |
| 81 | + {option.label} |
| 82 | + </AppText> |
| 83 | + {option.count ? ( |
| 84 | + <AppText textRole="label" className="font-t3-medium text-foreground-muted"> |
| 85 | + {option.count} |
| 86 | + </AppText> |
| 87 | + ) : null} |
| 88 | + </Pressable> |
| 89 | + ); |
| 90 | + })} |
| 91 | + </View> |
| 92 | + ); |
| 93 | +} |
0 commit comments