Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 4x 4x 2x 4x 12x 2x 1x 1x | /**
* CategoryTabs - 分类标签组件
*
* 水平滚动的分类选择器
* 设计风格:胶囊形状,带有平滑动画和微交互
*/
import React, { memo, useCallback, useRef } from 'react';
import { ScrollView, Pressable } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
interpolateColor,
useDerivedValue,
} from 'react-native-reanimated';
import { styled, XStack, Text, Stack, useTheme } from 'tamagui';
export interface CategoryItem {
id: string;
label: string;
icon?: string;
}
export interface CategoryTabsProps {
categories: CategoryItem[];
activeId: string;
onSelect: (id: string) => void;
}
const TabButton = styled(Stack, {
name: 'CategoryTab',
paddingHorizontal: 16,
paddingVertical: 10,
borderRadius: 20,
alignItems: 'center',
justifyContent: 'center',
});
const TabContent = styled(XStack, {
name: 'TabContent',
alignItems: 'center',
gap: 6,
});
const TabText = styled(Text, {
name: 'CategoryTabText',
fontSize: 14,
fontWeight: '600',
letterSpacing: -0.2,
});
const TabIcon = styled(Text, {
name: 'TabIcon',
fontSize: 14,
});
const AnimatedTabButton = Animated.createAnimatedComponent(TabButton);
const AnimatedTabText = Animated.createAnimatedComponent(TabText);
interface TabItemProps {
item: CategoryItem;
isActive: boolean;
onPress: () => void;
}
function TabItem({ item, isActive, onPress }: TabItemProps) {
const theme = useTheme();
const scale = useSharedValue(1);
const activeProgress = useDerivedValue(() => {
return withSpring(isActive ? 1 : 0, { damping: 20, stiffness: 300 });
}, [isActive]);
const containerStyle = useAnimatedStyle(() => ({
transform: [{ scale: scale.value }],
backgroundColor: interpolateColor(
activeProgress.value,
[0, 1],
['transparent', theme.primary?.val || '#7FB093']
),
}));
const textStyle = useAnimatedStyle(() => ({
color: interpolateColor(
activeProgress.value,
[0, 1],
[theme.colorMuted?.val || '#6C6A66', '#FFFFFF']
),
}));
const handlePressIn = () => {
scale.value = withSpring(0.92, { damping: 15, stiffness: 400 });
};
const handlePressOut = () => {
scale.value = withSpring(1, { damping: 12, stiffness: 300 });
};
return (
<Pressable onPressIn={handlePressIn} onPressOut={handlePressOut} onPress={onPress}>
<AnimatedTabButton style={containerStyle}>
<TabContent>
{item.icon && <TabIcon>{item.icon}</TabIcon>}
<AnimatedTabText style={textStyle}>{item.label}</AnimatedTabText>
</TabContent>
</AnimatedTabButton>
</Pressable>
);
}
function CategoryTabsComponent({ categories, activeId, onSelect }: CategoryTabsProps) {
const scrollRef = useRef<ScrollView>(null);
const handleSelect = useCallback(
(id: string) => {
onSelect(id);
},
[onSelect]
);
return (
<ScrollView
ref={scrollRef}
horizontal
showsHorizontalScrollIndicator={false}
contentContainerStyle={{
paddingHorizontal: 16,
paddingVertical: 8,
gap: 8,
}}
>
{categories.map((item) => (
<TabItem
key={item.id}
item={item}
isActive={activeId === item.id}
onPress={() => handleSelect(item.id)}
/>
))}
</ScrollView>
);
}
export const CategoryTabs = memo(CategoryTabsComponent);
// 默认分类
export const DEFAULT_CATEGORIES: CategoryItem[] = [
{ id: 'recommend', label: '推荐', icon: '✨' },
{ id: 'cats', label: '猫咪', icon: '🐱' },
{ id: 'dogs', label: '狗狗', icon: '🐕' },
{ id: 'nutrition', label: '营养', icon: '🥗' },
{ id: 'health', label: '健康', icon: '💊' },
{ id: 'funny', label: '搞笑', icon: '😂' },
];
|