Extract TabBarItem

This commit is contained in:
Dan Abramov
2024-11-29 21:46:17 +00:00
parent 020d71a741
commit 6d134229d6
2 changed files with 46 additions and 30 deletions
-3
View File
@@ -1,7 +1,6 @@
import React from 'react'
import {useNavigation} from '@react-navigation/native'
import {usePalette} from '#/lib/hooks/usePalette'
import {NavigationProp} from '#/lib/routes/types'
import {FeedSourceInfo} from '#/state/queries/feed'
import {useSession} from '#/state/session'
@@ -19,7 +18,6 @@ export function HomeHeader(
const {feeds} = props
const {hasSession} = useSession()
const navigation = useNavigation<NavigationProp>()
const pal = usePalette('default')
const hasPinnedCustom = React.useMemo<boolean>(() => {
if (!hasSession) return false
@@ -61,7 +59,6 @@ export function HomeHeader(
onSelect={onSelect}
testID={props.testID}
items={items}
indicatorColor={pal.colors.link}
dragGesture={props.dragGesture}
/>
</HomeHeaderLayout>
+46 -27
View File
@@ -16,7 +16,6 @@ export interface TabBarProps {
testID?: string
selectedPage: number
items: string[]
indicatorColor?: string
onSelect?: (index: number) => void
onPressSelected?: (index: number) => void
}
@@ -25,17 +24,12 @@ export function TabBar({
testID,
selectedPage,
items,
indicatorColor,
onSelect,
onPressSelected,
dragGesture,
}: TabBarProps) {
const pal = usePalette('default')
const scrollElRef = useAnimatedRef()
const indicatorStyle = useMemo(
() => ({borderBottomColor: indicatorColor || pal.colors.link}),
[indicatorColor, pal],
)
const onPressItem = useCallback(
(index: number) => {
@@ -96,28 +90,15 @@ export function TabBar({
},
]}>
{items.map((item, i) => {
const selected = i === selectedPage
return (
<PressableWithHover
testID={`${testID}-selector-${i}`}
key={`${item}-${i}`}
style={styles.item}
hoverStyle={pal.viewLight}
onPress={() => onPressItem(i)}
accessibilityRole="tab">
<View style={[styles.itemInner, selected && indicatorStyle]}>
<Text
emoji
type="lg-bold"
testID={testID ? `${testID}-${item}` : undefined}
style={[
selected ? pal.text : pal.textLight,
{lineHeight: 20},
]}>
{item}
</Text>
</View>
</PressableWithHover>
<TabBarItem
key={i}
index={i}
testID={testID}
selected={i === selectedPage}
item={item}
onPressItem={onPressItem}
/>
)
})}
</Animated.View>
@@ -127,6 +108,44 @@ export function TabBar({
)
}
function TabBarItem({
index,
testID,
selected,
item,
onPressItem,
}: {
index: number
testID: string | undefined
selected: boolean
item: string
onPressItem: (index: number) => void
}) {
const pal = usePalette('default')
const indicatorStyle = useMemo(
() => ({borderBottomColor: pal.colors.link}),
[pal],
)
return (
<PressableWithHover
testID={`${testID}-selector-${index}`}
style={styles.item}
hoverStyle={pal.viewLight}
onPress={() => onPressItem(index)}
accessibilityRole="tab">
<View style={[styles.itemInner, selected && indicatorStyle]}>
<Text
emoji
type="lg-bold"
testID={testID ? `${testID}-${item}` : undefined}
style={[selected ? pal.text : pal.textLight, {lineHeight: 20}]}>
{item}
</Text>
</View>
</PressableWithHover>
)
}
const styles = StyleSheet.create({
outer: {
flexDirection: 'row',