-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Merged
mountiny
merged 16 commits into
Expensify:main
from
software-mansion-labs:feature/kuba_nowakowski/add_animation_for_switch_childreans
Jan 29, 2025
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
20b38b2
fix problem in WorkspaceInitialPage when adding new feature
sumo-slonik 8736bbd
Merge branch 'main' into feature/kuba_nowakowski/add_animation_for_sw…
sumo-slonik 91e739a
add accordion component
sumo-slonik 6ac60f3
fix imports
sumo-slonik 760362c
Merge branch 'main' into feature/kuba_nowakowski/add_animation_for_sw…
sumo-slonik b61a2fd
Merge branch 'main' into feature/kuba_nowakowski/add_animation_for_sw…
sumo-slonik 6d0bc8b
fix entering animation bug
sumo-slonik 88998d1
rename function
sumo-slonik 107731c
Merge branch 'main' into feature/kuba_nowakowski/add_animation_for_sw…
sumo-slonik aa3e3b7
rename function
sumo-slonik 9839df6
better naming for function parameters
sumo-slonik e84be2a
fix import
sumo-slonik 2f73c2f
remove unused function
sumo-slonik bd90b09
Merge branch 'main' into feature/kuba_nowakowski/add_animation_for_sw…
sumo-slonik 44a9e2c
resolve conflicts
sumo-slonik a278e11
fix linter
sumo-slonik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: