Fix first render flash

This commit is contained in:
Dan Abramov
2023-10-17 17:52:06 +01:00
parent caa55cba7a
commit 05d92799ff
+16 -7
View File
@@ -38,8 +38,13 @@ export function TabBar({
const scrollX = useSharedValue(0) const scrollX = useSharedValue(0)
const didScroll = useSharedValue(false) const didScroll = useSharedValue(false)
const didLayout = (
layouts.length === items.length &&
layouts.every(l => l !== undefined)
);
const indicatorStyle = useAnimatedStyle(() => { const indicatorStyle = useAnimatedStyle(() => {
if (layouts.length < items.length - 1 || layouts.some(l => l === undefined)) { if (!didLayout) {
return {} return {}
} }
return { return {
@@ -113,11 +118,15 @@ export function TabBar({
}} }}
scrollEventThrottle={16}> scrollEventThrottle={16}>
{items.map((item, i) => { {items.map((item, i) => {
const isSelected = i === selectedPage
return ( return (
<PressableWithHover <PressableWithHover
key={item} key={item}
onLayout={e => onItemLayout(e, i)} onLayout={e => onItemLayout(e, i)}
style={[styles.item]} style={[styles.item, {
borderBottomColor: (!didLayout && isSelected) ? indicatorColor : 'transparent',
borderBottomWidth: 3
}]}
hoverStyle={pal.viewLight} hoverStyle={pal.viewLight}
onPress={() => onPressItem(i)}> onPress={() => onPressItem(i)}>
<MaybeHighlightedText <MaybeHighlightedText
@@ -130,21 +139,21 @@ export function TabBar({
</PressableWithHover> </PressableWithHover>
) )
})} })}
<Animated.View {didLayout && <Animated.View
style={[{ style={[{
position: 'absolute', position: 'absolute',
bottom: 0, bottom: 0,
height: 3, height: 3,
backgroundColor: indicatorColor || pal.colors.link, backgroundColor: indicatorColor,
}, indicatorStyle]} }, indicatorStyle]}
/> />}
</DraggableScrollView> </DraggableScrollView>
</View> </View>
) )
} }
export function MaybeHighlightedText({ approxIndex, index, style, type, ...rest }) { export function MaybeHighlightedText({ approxIndex, index, type, ...rest }) {
const pal = usePalette('default') const pal = usePalette('default')
const theme = useTheme() const theme = useTheme()
const typography = theme.typography[type] const typography = theme.typography[type]
@@ -152,7 +161,7 @@ export function MaybeHighlightedText({ approxIndex, index, style, type, ...rest
color: interpolateColor(Math.min(Math.abs(approxIndex.value - index), 1), [0, 1], [pal.text.color, pal.textLight.color]) color: interpolateColor(Math.min(Math.abs(approxIndex.value - index), 1), [0, 1], [pal.text.color, pal.textLight.color])
})) }))
return ( return (
<Animated.Text {...rest} style={[style, typography, animatedStyle]} /> <Animated.Text {...rest} style={[typography, animatedStyle]} />
) )
} }