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 React from 'react'
import {useNavigation} from '@react-navigation/native' import {useNavigation} from '@react-navigation/native'
import {usePalette} from '#/lib/hooks/usePalette'
import {NavigationProp} from '#/lib/routes/types' import {NavigationProp} from '#/lib/routes/types'
import {FeedSourceInfo} from '#/state/queries/feed' import {FeedSourceInfo} from '#/state/queries/feed'
import {useSession} from '#/state/session' import {useSession} from '#/state/session'
@@ -19,7 +18,6 @@ export function HomeHeader(
const {feeds} = props const {feeds} = props
const {hasSession} = useSession() const {hasSession} = useSession()
const navigation = useNavigation<NavigationProp>() const navigation = useNavigation<NavigationProp>()
const pal = usePalette('default')
const hasPinnedCustom = React.useMemo<boolean>(() => { const hasPinnedCustom = React.useMemo<boolean>(() => {
if (!hasSession) return false if (!hasSession) return false
@@ -61,7 +59,6 @@ export function HomeHeader(
onSelect={onSelect} onSelect={onSelect}
testID={props.testID} testID={props.testID}
items={items} items={items}
indicatorColor={pal.colors.link}
dragGesture={props.dragGesture} dragGesture={props.dragGesture}
/> />
</HomeHeaderLayout> </HomeHeaderLayout>
+46 -27
View File
@@ -16,7 +16,6 @@ export interface TabBarProps {
testID?: string testID?: string
selectedPage: number selectedPage: number
items: string[] items: string[]
indicatorColor?: string
onSelect?: (index: number) => void onSelect?: (index: number) => void
onPressSelected?: (index: number) => void onPressSelected?: (index: number) => void
} }
@@ -25,17 +24,12 @@ export function TabBar({
testID, testID,
selectedPage, selectedPage,
items, items,
indicatorColor,
onSelect, onSelect,
onPressSelected, onPressSelected,
dragGesture, dragGesture,
}: TabBarProps) { }: TabBarProps) {
const pal = usePalette('default') const pal = usePalette('default')
const scrollElRef = useAnimatedRef() const scrollElRef = useAnimatedRef()
const indicatorStyle = useMemo(
() => ({borderBottomColor: indicatorColor || pal.colors.link}),
[indicatorColor, pal],
)
const onPressItem = useCallback( const onPressItem = useCallback(
(index: number) => { (index: number) => {
@@ -96,28 +90,15 @@ export function TabBar({
}, },
]}> ]}>
{items.map((item, i) => { {items.map((item, i) => {
const selected = i === selectedPage
return ( return (
<PressableWithHover <TabBarItem
testID={`${testID}-selector-${i}`} key={i}
key={`${item}-${i}`} index={i}
style={styles.item} testID={testID}
hoverStyle={pal.viewLight} selected={i === selectedPage}
onPress={() => onPressItem(i)} item={item}
accessibilityRole="tab"> onPressItem={onPressItem}
<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>
) )
})} })}
</Animated.View> </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({ const styles = StyleSheet.create({
outer: { outer: {
flexDirection: 'row', flexDirection: 'row',