-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[RNMobile] Use Reanimated in bottom sheet height animation #52563
Merged
fluiddot
merged 10 commits into
rnmobile/upgrade/react-native-0.71.8
from
rnmobile/fix/bottom-sheet-height-layout-animation
Jul 14, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3f5a220
Expose max height properties in `BottomSheetProvider`
fluiddot 634b2fd
Animate bottom sheet's height with Reanimated
fluiddot 00adbb4
Use pixel value when setting fullscreen height
fluiddot 406366b
Rename `heightRef` to `maxHeight`
fluiddot ed95231
Re-enable `exhaustive-deps` lint rule in `BottomSheetNavigationContai…
fluiddot 4dcc3d4
Avoid setting height using debounce
fluiddot c8fe621
Add test ID to navigation container component
fluiddot 4e85db6
Mock Reanimated's `now` function
fluiddot e4eafc2
Update test cases related to bottom sheet height animation
fluiddot f7dc1fa
Update test snapshots
fluiddot 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
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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { View, Easing } from 'react-native'; | ||
import { NavigationContainer, DefaultTheme } from '@react-navigation/native'; | ||
import { createStackNavigator } from '@react-navigation/stack'; | ||
import Animated, { | ||
Easing, | ||
useAnimatedStyle, | ||
useSharedValue, | ||
withTiming, | ||
} from 'react-native-reanimated'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
useState, | ||
useContext, | ||
useMemo, | ||
useCallback, | ||
|
@@ -23,11 +27,11 @@ import { usePreferredColorSchemeStyle } from '@wordpress/compose'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { performLayoutAnimation } from '../../layout-animation'; | ||
import { | ||
BottomSheetNavigationContext, | ||
BottomSheetNavigationProvider, | ||
} from './bottom-sheet-navigation-context'; | ||
import { BottomSheetContext } from '../bottom-sheet-context'; | ||
|
||
import styles from './styles.scss'; | ||
|
||
|
@@ -57,67 +61,73 @@ const options = { | |
cardStyleInterpolator: fadeConfig, | ||
}; | ||
|
||
const ANIMATION_DURATION = 190; | ||
const HEIGHT_ANIMATION_DURATION = 300; | ||
const DEFAULT_HEIGHT = 1; | ||
dcalhoun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function BottomSheetNavigationContainer( { | ||
children, | ||
animate, | ||
main, | ||
theme, | ||
style, | ||
testID, | ||
} ) { | ||
const Stack = useRef( createStackNavigator() ).current; | ||
const context = useContext( BottomSheetNavigationContext ); | ||
const [ currentHeight, setCurrentHeight ] = useState( | ||
context.currentHeight || 1 | ||
const navigationContext = useContext( BottomSheetNavigationContext ); | ||
const { maxHeight: sheetMaxHeight, isMaxHeightSet: isSheetMaxHeightSet } = | ||
useContext( BottomSheetContext ); | ||
dcalhoun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const currentHeight = useSharedValue( | ||
navigationContext.currentHeight?.value || DEFAULT_HEIGHT | ||
); | ||
|
||
const backgroundStyle = usePreferredColorSchemeStyle( | ||
styles.background, | ||
styles.backgroundDark | ||
); | ||
|
||
const _theme = theme || { | ||
...DefaultTheme, | ||
colors: { | ||
...DefaultTheme.colors, | ||
background: backgroundStyle.backgroundColor, | ||
}, | ||
}; | ||
const defaultTheme = useMemo( | ||
() => ( { | ||
...DefaultTheme, | ||
colors: { | ||
...DefaultTheme.colors, | ||
background: backgroundStyle.backgroundColor, | ||
}, | ||
} ), | ||
[ backgroundStyle.backgroundColor ] | ||
); | ||
Comment on lines
+88
to
+97
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. This is memoized to avoid potentially generating a new object on every render in |
||
const _theme = theme || defaultTheme; | ||
|
||
const setHeight = useCallback( | ||
( height ) => { | ||
// The screen is fullHeight. | ||
if ( | ||
typeof height === 'string' && | ||
typeof height !== typeof currentHeight | ||
height > DEFAULT_HEIGHT && | ||
Math.round( height ) !== Math.round( currentHeight.value ) | ||
) { | ||
performLayoutAnimation( ANIMATION_DURATION ); | ||
setCurrentHeight( height ); | ||
|
||
return; | ||
} | ||
|
||
if ( | ||
height > 1 && | ||
Math.round( height ) !== Math.round( currentHeight ) | ||
) { | ||
if ( currentHeight === 1 ) { | ||
setCurrentHeight( height ); | ||
} else if ( animate ) { | ||
performLayoutAnimation( ANIMATION_DURATION ); | ||
setCurrentHeight( height ); | ||
// If max height is set in the bottom sheet, we clamp | ||
// the new height using that value. | ||
const newHeight = isSheetMaxHeightSet | ||
? Math.min( sheetMaxHeight, height ) | ||
: height; | ||
const shouldAnimate = | ||
animate && currentHeight.value !== DEFAULT_HEIGHT; | ||
|
||
if ( shouldAnimate ) { | ||
currentHeight.value = withTiming( newHeight, { | ||
duration: HEIGHT_ANIMATION_DURATION, | ||
easing: Easing.out( Easing.cubic ), | ||
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. You can check the easing function here. |
||
} ); | ||
} else { | ||
setCurrentHeight( height ); | ||
currentHeight.value = newHeight; | ||
} | ||
} | ||
}, | ||
// Disable reason: deferring this refactor to the native team. | ||
// see https://github.com/WordPress/gutenberg/pull/41166 | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[ currentHeight ] | ||
[ animate, currentHeight, isSheetMaxHeightSet, sheetMaxHeight ] | ||
); | ||
|
||
const animatedStyles = useAnimatedStyle( () => ( { | ||
height: currentHeight.value, | ||
} ) ); | ||
|
||
const screens = useMemo( () => { | ||
return Children.map( children, ( child ) => { | ||
let screen = child; | ||
|
@@ -136,19 +146,16 @@ function BottomSheetNavigationContainer( { | |
/> | ||
); | ||
} ); | ||
// Disable reason: deferring this refactor to the native team. | ||
// see https://github.com/WordPress/gutenberg/pull/41166 | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ children ] ); | ||
}, [ children, main ] ); | ||
|
||
return useMemo( () => { | ||
return ( | ||
<View style={ [ style, { height: currentHeight } ] }> | ||
<Animated.View | ||
style={ [ style, animatedStyles ] } | ||
testID={ testID } | ||
> | ||
<BottomSheetNavigationProvider | ||
value={ { | ||
setHeight, | ||
currentHeight, | ||
} } | ||
value={ { setHeight, currentHeight } } | ||
> | ||
{ main ? ( | ||
<NavigationContainer theme={ _theme }> | ||
|
@@ -168,12 +175,18 @@ function BottomSheetNavigationContainer( { | |
</Stack.Navigator> | ||
) } | ||
</BottomSheetNavigationProvider> | ||
</View> | ||
</Animated.View> | ||
); | ||
// Disable reason: deferring this refactor to the native team. | ||
// see https://github.com/WordPress/gutenberg/pull/41166 | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ currentHeight, _theme ] ); | ||
}, [ | ||
_theme, | ||
animatedStyles, | ||
currentHeight, | ||
main, | ||
screens, | ||
setHeight, | ||
style, | ||
testID, | ||
] ); | ||
} | ||
|
||
export default BottomSheetNavigationContainer; |
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.
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.
This structure matches Reanimated's shared value. I changed the default value to
0
as I feel it's a more appropriate default value.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.
I agree that
0
seems like a more reasonable default.That said, my gut tells me moving to
0
may introduce a bug. I was unable to find a direct answer (indirectly not answered), but my hunch is that using1
as the default may be a cryptic way of ensuring something occurs when first setting the height, but not if the height is set to0
in the future. I.e. that "something" should not occur if the height comes through as0
, which more likely to organically occur than1
.Does that make sense?
My fear may be irrational and not worth following, particularly if we do not observe any bugs while testing now.
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.
These default values will only be used when using the provider without setting the
value
prop. AFAIK this is not our case (reference), so similarly to setting an empty function forsetHeight
below, I used0
as the empty value forcurrentHeight
.Regarding the default value to use for the height animation, I agree it's a bit cryptic why we use
1
. I haven't checked this but my gut feeling is that, since we need to calculate the layout of the content to set the final height, using a0
value would prevent the layout calculations to happen.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.
The impact — or lack thereof — of the default Context value makes sense. Thank you for noting that.