Simplify the calculation

This commit is contained in:
Dan Abramov
2023-10-16 19:15:08 +01:00
parent 493b40a28f
commit 828e0b0d22
2 changed files with 8 additions and 11 deletions
+5 -6
View File
@@ -34,14 +34,13 @@ export function TabBar({
const {isDesktop, isTablet} = useWebMediaQueries()
const [layouts, setLayouts] = useState([])
const approxIndex = useDerivedValue(() => dragProgress.value * (items.length - 1))
const indicatorStyle = useAnimatedStyle(() => {
if (layouts.length < items.length - 1 || layouts.some(l => l === undefined)) {
return {}
}
return {
width: interpolate(approxIndex.value, layouts.map((l, i) => i), layouts.map(l => l.width)),
left: interpolate(approxIndex.value, layouts.map((l, i) => i), layouts.map(l => l.x)),
width: interpolate(dragProgress.value, layouts.map((l, i) => i), layouts.map(l => l.width)),
left: interpolate(dragProgress.value, layouts.map((l, i) => i), layouts.map(l => l.x)),
}
})
@@ -56,12 +55,12 @@ export function TabBar({
)
useAnimatedReaction(() => {
return dragProgress.value * (contentSize.value - SCREEN.width)
return (dragProgress.value / (items.length - 1)) * (contentSize.value - SCREEN.width)
}, (nextX, prevX) => {
if (prevX !== nextX) {
scrollTo(scrollElRef, nextX, 0, false);
}
}, [dragProgress, contentSize])
})
const onItemLayout = (e: LayoutChangeEvent, index: number) => {
const l = e.nativeEvent.layout
@@ -97,7 +96,7 @@ export function TabBar({
<MaybeHighlightedText
type={isDesktop || isTablet ? 'xl-bold' : 'lg-bold'}
testID={testID ? `${testID}-${item}` : undefined}
approxIndex={approxIndex}
approxIndex={dragProgress}
index={i}>
{item}
</MaybeHighlightedText>
+3 -5
View File
@@ -84,12 +84,10 @@ export const HomeScreen = withAuthRequired(
const onPageScroll = React.useCallback(
e => {
'worklet'
if (customFeeds.length > 0) {
const progress = (e.offset + e.position) / customFeeds.length
dragProgress.value = progress
}
const progress = e.offset + e.position
dragProgress.value = progress
},
[customFeeds, dragProgress],
[dragProgress],
)
const onPressSelected = React.useCallback(() => {