From 139027ac5f843c65aae5ad824d049aae6dae9f79 Mon Sep 17 00:00:00 2001 From: Ansh Nanda Date: Tue, 16 May 2023 17:27:46 -0700 Subject: [PATCH] bad fix to the tab bar animation --- src/view/com/pager/FeedsTabBarMobile.tsx | 1 + src/view/com/pager/TabBar.tsx | 154 ++++++++++++----------- 2 files changed, 82 insertions(+), 73 deletions(-) diff --git a/src/view/com/pager/FeedsTabBarMobile.tsx b/src/view/com/pager/FeedsTabBarMobile.tsx index 0952cb14..0fcf19f1 100644 --- a/src/view/com/pager/FeedsTabBarMobile.tsx +++ b/src/view/com/pager/FeedsTabBarMobile.tsx @@ -53,6 +53,7 @@ export const FeedsTabBar = observer( ( - // items.map(() => ({x: 0, width: 0})), - // ) + const [itemLayouts, setItemLayouts] = useState( + items.map(() => ({x: 0, width: 0})), + ) const itemRefs = useMemo( () => Array.from({length: items.length}).map(() => createRef()), [items.length], ) - // const panX = Animated.add(position, offset) + const panX = Animated.add(position, offset) const containerRef = useRef(null) + const [scrollX, setScrollX] = useState(0) - // const indicatorStyle = { - // backgroundColor: indicatorColor || pal.colors.link, - // bottom: - // indicatorPosition === 'bottom' ? (isDesktopWeb ? 0 : -1) : undefined, - // top: indicatorPosition === 'top' ? (isDesktopWeb ? 0 : -1) : undefined, - // transform: [ - // { - // translateX: panX.interpolate({ - // inputRange: items.map((_item, i) => i), - // outputRange: itemLayouts.map(l => l.x + l.width / 2), - // }), - // }, - // { - // scaleX: panX.interpolate({ - // inputRange: items.map((_item, i) => i), - // outputRange: itemLayouts.map(l => l.width), - // }), - // }, - // ], - // } + const indicatorStyle = useMemo( + () => ({ + backgroundColor: indicatorColor || pal.colors.link, + bottom: + indicatorPosition === 'bottom' ? (isDesktopWeb ? 0 : -1) : undefined, + top: indicatorPosition === 'top' ? (isDesktopWeb ? 0 : -1) : undefined, + transform: [ + { + translateX: panX.interpolate({ + inputRange: items.map((_item, i) => i), + outputRange: itemLayouts.map(l => l.x + l.width / 2 - scrollX), + }), + }, + { + scaleX: panX.interpolate({ + inputRange: items.map((_item, i) => i), + outputRange: itemLayouts.map(l => l.width), + }), + }, + ], + }), + [ + indicatorColor, + indicatorPosition, + itemLayouts, + items, + panX, + pal.colors.link, + scrollX, + ], + ) - // const onLayout = () => { - // const promises = [] - // for (let i = 0; i < items.length; i++) { - // promises.push( - // new Promise(resolve => { - // if (!containerRef.current || !itemRefs[i].current) { - // return resolve({x: 0, width: 0}) - // } + const onLayout = () => { + const promises = [] + for (let i = 0; i < items.length; i++) { + promises.push( + new Promise(resolve => { + if (!containerRef.current || !itemRefs[i].current) { + return resolve({x: 0, width: 0}) + } - // itemRefs[i].current?.measureLayout( - // containerRef.current, - // (x: number, _y: number, width: number) => { - // resolve({x, width}) - // }, - // ) - // }), - // ) - // } - // Promise.all(promises).then((layouts: Layout[]) => { - // setItemLayouts(layouts) - // }) - // } + itemRefs[i].current?.measureLayout( + containerRef.current, + (x: number, _y: number, width: number) => { + resolve({x, width}) + }, + ) + }), + ) + } + Promise.all(promises).then((layouts: Layout[]) => { + setItemLayouts(layouts) + }) + } const onPressItem = (index: number) => { onSelect?.(index) @@ -100,31 +111,33 @@ export function TabBar({ - - + + { + setScrollX(nativeEvent.contentOffset.x) + }}> {items.map((item, i) => { const selected = i === selectedPage return ( - - onPressItem(i)}> - - {item} - - - + onPressItem(i)}> + + {item} + + ) })} @@ -155,7 +168,6 @@ const styles = isDesktopWeb height: 3, zIndex: 1, }, - active: {}, }) : StyleSheet.create({ outer: { @@ -178,8 +190,4 @@ const styles = isDesktopWeb width: 1, height: 3, }, - active: { - borderBottomColor: 'blue', - borderBottomWidth: 3, - }, })