-
Notifications
You must be signed in to change notification settings - Fork 734
Infra/ migrate SegmentedControl to reanimated v2 #1567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2d001b1
ea1a38d
7a63e46
1c61712
f9b68f5
452f7ba
1026d56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
import _ from 'lodash'; | ||
import React, {useRef, useState, useCallback, useMemo} from 'react'; | ||
import React, {useRef, useState, useCallback} from 'react'; | ||
import {StyleSheet, StyleProp, ViewStyle, LayoutChangeEvent} from 'react-native'; | ||
import Reanimated, {EasingNode, Easing as _Easing} from 'react-native-reanimated'; | ||
import Reanimated, { | ||
Easing, | ||
useAnimatedReaction, | ||
useAnimatedStyle, | ||
useSharedValue, | ||
withTiming, | ||
runOnJS | ||
} from 'react-native-reanimated'; | ||
import {Colors, BorderRadiuses, Spacings} from '../../style'; | ||
import {asBaseComponent} from '../../commons/new'; | ||
import View from '../view'; | ||
import Segment, {SegmentedControlItemProps as SegmentProps} from './segment'; | ||
import {Constants} from 'helpers'; | ||
|
||
const {interpolate: _interpolate, interpolateNode} = Reanimated; | ||
const interpolate = interpolateNode || _interpolate; | ||
const Easing = EasingNode || _Easing; | ||
const BORDER_WIDTH = 1; | ||
const TIMING_CONFIG = { | ||
duration: 300, | ||
easing: Easing.bezier(0.33, 1, 0.68, 1) | ||
}; | ||
|
||
export type SegmentedControlItemProps = SegmentProps; | ||
export type SegmentedControlProps = { | ||
|
@@ -59,6 +67,10 @@ export type SegmentedControlProps = { | |
* Should the icon be on right of the label | ||
*/ | ||
iconOnRight?: boolean; | ||
/** | ||
* Disable the trailing delay when changing index | ||
*/ | ||
disableThrottle?: boolean; | ||
/** | ||
* Additional spacing styles for the container | ||
*/ | ||
|
@@ -85,36 +97,39 @@ const SegmentedControl = (props: SegmentedControlProps) => { | |
inactiveColor = Colors.grey20, | ||
outlineColor = activeColor, | ||
outlineWidth = BORDER_WIDTH, | ||
disableThrottle, | ||
testID | ||
} = props; | ||
const [selectedSegment, setSelectedSegment] = useState(-1); | ||
|
||
const animatedSelectedIndex = useSharedValue(selectedSegment); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that you have a shared value for the selectedIndex, you can implement this component a little differently. I'd suggest to try and unify them all to a single source and use only the shareValue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's go over it together.. |
||
const segmentsStyle = useRef([] as {x: number; width: number}[]); | ||
const segmentedControlHeight = useRef(0); | ||
const indexRef = useRef(0); | ||
const segmentsCounter = useRef(0); | ||
const animatedValue = useRef(new Reanimated.Value(initialIndex)); | ||
const delay = disableThrottle ? 0 : 400; | ||
|
||
const changeIndex = useCallback(_.throttle(() => { | ||
onChangeIndex?.(indexRef.current); | ||
}, | ||
400, | ||
delay, | ||
{trailing: true, leading: false}), | ||
[]); | ||
|
||
const onSegmentPress = useCallback((index: number) => { | ||
if (selectedSegment !== index) { | ||
setSelectedSegment(index); | ||
indexRef.current = index; | ||
|
||
Reanimated.timing(animatedValue.current, { | ||
toValue: index, | ||
duration: 300, | ||
easing: Easing.bezier(0.33, 1, 0.68, 1) | ||
}).start(changeIndex); | ||
useAnimatedReaction(() => { | ||
return animatedSelectedIndex.value; | ||
}, | ||
(selected, previous) => { | ||
if (selected !== -1 && selected !== previous) { | ||
onChangeIndex && runOnJS(changeIndex)(); | ||
} | ||
}, | ||
[onChangeIndex, selectedSegment]); | ||
[]); | ||
|
||
const onSegmentPress = useCallback((index: number) => { | ||
setSelectedSegment(index); | ||
indexRef.current = index; | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
animatedSelectedIndex.value = index; | ||
}, []); | ||
|
||
const onLayout = useCallback((index: number, event: LayoutChangeEvent) => { | ||
const {x, width, height} = event.nativeEvent.layout; | ||
|
@@ -126,32 +141,25 @@ const SegmentedControl = (props: SegmentedControlProps) => { | |
}, | ||
[initialIndex, segments?.length]); | ||
|
||
const animatedStyle = useMemo(() => { | ||
const animatedStyle = useAnimatedStyle(() => { | ||
if (segmentsCounter.current === segments?.length) { | ||
const inset = interpolate(animatedValue.current, { | ||
inputRange: _.times(segmentsCounter.current), | ||
outputRange: _.map(segmentsStyle.current, segment => segment.x) | ||
}); | ||
|
||
const width = interpolate(animatedValue.current, { | ||
inputRange: _.times(segmentsCounter.current), | ||
outputRange: _.map(segmentsStyle.current, segment => segment.width - 2 * BORDER_WIDTH) | ||
}); | ||
|
||
return [{width}, Constants.isRTL ? {right: inset} : {left: inset}]; | ||
const inset = withTiming(segmentsStyle.current[selectedSegment].x, TIMING_CONFIG); | ||
const width = withTiming(segmentsStyle.current[selectedSegment].width - 2 * BORDER_WIDTH, TIMING_CONFIG); | ||
return Constants.isRTL ? {width, right: inset} : {width, left: inset}; | ||
} | ||
return undefined; | ||
}, [segmentsCounter.current, segments?.length]); | ||
return {}; | ||
}, [segmentsCounter.current, segments?.length, selectedSegment]); | ||
|
||
const renderSegments = () => | ||
_.map(segments, (_value, index) => { | ||
const isSelected = selectedSegment === index; | ||
return ( | ||
<Segment | ||
key={index} | ||
onLayout={onLayout} | ||
index={index} | ||
onPress={onSegmentPress} | ||
isSelected={selectedSegment === index} | ||
onPress={isSelected ? undefined : onSegmentPress} | ||
isSelected={isSelected} | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
activeColor={activeColor} | ||
inactiveColor={inactiveColor} | ||
{...segments?.[index]} | ||
|
Uh oh!
There was an error while loading. Please reload this page.