-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add animation for switch children #55478
Changes from all commits
20b38b2
8736bbd
91e739a
6ac60f3
760362c
b61a2fd
6d0bc8b
88998d1
107731c
aa3e3b7
9839df6
e84be2a
2f73c2f
bd90b09
44a9e2c
a278e11
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type {ReactNode} from 'react'; | ||
import React from 'react'; | ||
import type {StyleProp, ViewStyle} from 'react-native'; | ||
import {View} from 'react-native'; | ||
import type {SharedValue} from 'react-native-reanimated'; | ||
import Animated, {Easing, useAnimatedStyle, useDerivedValue, useSharedValue, withTiming} from 'react-native-reanimated'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
||
type AccordionProps = { | ||
/** Giving information whether the component is open */ | ||
isExpanded: SharedValue<boolean>; | ||
|
||
/** Element that is inside Accordion */ | ||
children: ReactNode; | ||
|
||
/** Duration of expansion animation */ | ||
duration?: number; | ||
|
||
/** Additional external style */ | ||
style?: StyleProp<ViewStyle>; | ||
|
||
/** Was toggle triggered */ | ||
isToggleTriggered: SharedValue<boolean>; | ||
}; | ||
|
||
function Accordion({isExpanded, children, duration = 300, isToggleTriggered, style}: AccordionProps) { | ||
const height = useSharedValue(0); | ||
const styles = useThemeStyles(); | ||
|
||
const derivedHeight = useDerivedValue(() => { | ||
if (!isToggleTriggered.get()) { | ||
return isExpanded.get() ? height.get() : 0; | ||
} | ||
|
||
return withTiming(height.get() * Number(isExpanded.get()), { | ||
duration, | ||
easing: Easing.inOut(Easing.quad), | ||
}); | ||
}); | ||
|
||
const derivedOpacity = useDerivedValue(() => { | ||
if (!isToggleTriggered.get()) { | ||
return isExpanded.get() ? 1 : 0; | ||
} | ||
|
||
return withTiming(isExpanded.get() ? 1 : 0, { | ||
duration, | ||
easing: Easing.inOut(Easing.quad), | ||
}); | ||
}); | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
if (!isToggleTriggered.get() && !isExpanded.get()) { | ||
return { | ||
height: 0, | ||
opacity: 0, | ||
}; | ||
} | ||
return { | ||
height: !isToggleTriggered.get() ? height.get() : derivedHeight.get(), | ||
opacity: derivedOpacity.get(), | ||
}; | ||
}); | ||
|
||
return ( | ||
<Animated.View style={[animatedStyle, style]}> | ||
<View | ||
onLayout={(e) => { | ||
height.set(e.nativeEvent.layout.height); | ||
}} | ||
style={[styles.pAbsolute, styles.l0, styles.r0, styles.t0]} | ||
> | ||
{children} | ||
</View> | ||
</Animated.View> | ||
); | ||
} | ||
|
||
Accordion.displayName = 'Accordion'; | ||
|
||
export default Accordion; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type {ReactNode} from 'react'; | ||
import React from 'react'; | ||
import type {StyleProp, ViewStyle} from 'react-native'; | ||
import {View} from 'react-native'; | ||
import type {SharedValue} from 'react-native-reanimated'; | ||
import Animated, {Easing, useAnimatedStyle, useDerivedValue, useSharedValue, withTiming} from 'react-native-reanimated'; | ||
|
||
type AccordionProps = { | ||
/** Giving information whether the component is open */ | ||
isExpanded: SharedValue<boolean>; | ||
|
||
/** Element that is inside Accordion */ | ||
children: ReactNode; | ||
|
||
/** Duration of expansion animation */ | ||
duration?: number; | ||
|
||
/** Additional external style */ | ||
style?: StyleProp<ViewStyle>; | ||
|
||
/** Was toggle triggered */ | ||
isToggleTriggered: SharedValue<boolean>; | ||
}; | ||
|
||
function Accordion({isExpanded, children, duration = 300, isToggleTriggered, style}: AccordionProps) { | ||
const height = useSharedValue(0); | ||
|
||
const derivedHeight = useDerivedValue(() => { | ||
if (!isToggleTriggered.get()) { | ||
return isExpanded.get() ? height.get() : 0; | ||
} | ||
|
||
return withTiming(height.get() * Number(isExpanded.get()), { | ||
duration, | ||
easing: Easing.inOut(Easing.quad), | ||
}); | ||
}); | ||
|
||
const derivedOpacity = useDerivedValue(() => { | ||
if (!isToggleTriggered.get()) { | ||
return isExpanded.get() ? 1 : 0; | ||
} | ||
|
||
return withTiming(isExpanded.get() ? 1 : 0, { | ||
duration, | ||
easing: Easing.inOut(Easing.quad), | ||
}); | ||
}); | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
if (!isToggleTriggered.get() && !isExpanded.get()) { | ||
return { | ||
height: 0, | ||
opacity: 0, | ||
}; | ||
} | ||
|
||
return { | ||
height: !isToggleTriggered.get() ? undefined : derivedHeight.get(), | ||
opacity: derivedOpacity.get(), | ||
}; | ||
}); | ||
|
||
return ( | ||
<Animated.View style={[animatedStyle, style]}> | ||
<View | ||
onLayout={(e) => { | ||
height.set(e.nativeEvent.layout.height); | ||
}} | ||
> | ||
{children} | ||
</View> | ||
</Animated.View> | ||
); | ||
} | ||
Accordion.displayName = 'Accordion'; | ||
|
||
export default Accordion; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1063,9 +1063,9 @@ function removePendingFieldsFromCustomUnit(customUnit: CustomUnit): CustomUnit { | |
return cleanedCustomUnit; | ||
} | ||
|
||
function navigateWhenEnableFeature(policyID: string) { | ||
function goBackWhenEnableFeature(policyID: string) { | ||
setTimeout(() => { | ||
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'm a bit concerned about using setTimeout, we try to avoid it unless absolutely necessary. cc: @mountiny 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. is exactly the same solution as it was before in the navigate function:
which we replacing here 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. Ah, my bad. Let's not make any changes here then. |
||
Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID)); | ||
Navigation.goBack(ROUTES.WORKSPACE_INITIAL.getRoute(policyID)); | ||
}, CONST.WORKSPACE_ENABLE_FEATURE_REDIRECT_DELAY); | ||
} | ||
|
||
|
@@ -1355,7 +1355,7 @@ export { | |
getDistanceRateCustomUnitRate, | ||
sortWorkspacesBySelected, | ||
removePendingFieldsFromCustomUnit, | ||
navigateWhenEnableFeature, | ||
goBackWhenEnableFeature, | ||
getIntegrationLastSuccessfulDate, | ||
getCurrentConnectionName, | ||
getCustomersOrJobsLabelNetSuite, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this line was missing 1 extra check which led to the causation of this issue:
which was addressed in PR: