-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Animate settlement button when pay and trigger a haptic feedback #48615
Merged
roryabraham
merged 18 commits into
Expensify:main
from
bernhardoj:fix/48036-animate-pay-button
Sep 13, 2024
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
eaf39ea
animate settlement button when pay and trigger a haptic feedback
bernhardoj f3e6b99
lint
bernhardoj f535234
lint
bernhardoj a78fe85
Merge branch 'main' into fix/48036-animate-pay-button
bernhardoj bf5317e
missing type from conflict
bernhardoj a6061bd
undo unintentional change
bernhardoj a37a6e4
undo unintentional change
bernhardoj 05e4818
Merge branch 'main' into fix/48036-animate-pay-button
bernhardoj eb8e753
Merge branch 'main' into fix/48036-animate-pay-button
bernhardoj e9e48ab
move the state to start the animation to report preview
bernhardoj 1a8b847
disable the button while animating
bernhardoj 763821f
Merge branch 'main' into fix/48036-animate-pay-button
bernhardoj 77f814f
move value to const
bernhardoj 741ff5a
Merge branch 'main' into fix/48036-animate-pay-button
bernhardoj 55e6eb8
Merge branch 'main' into fix/48036-animate-pay-button
bernhardoj 45798dd
renaming
bernhardoj 327da6f
replace withOnyx with useOnyx
bernhardoj eb00272
simplify the code and update useEffect deps
bernhardoj 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
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
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
93 changes: 93 additions & 0 deletions
93
src/components/SettlementButton/AnimatedSettlementButton.tsx
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,93 @@ | ||
import React, {useCallback, useEffect} from 'react'; | ||
import Animated, {runOnJS, useAnimatedStyle, useSharedValue, withDelay, withTiming} from 'react-native-reanimated'; | ||
import Text from '@components/Text'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import variables from '@styles/variables'; | ||
import CONST from '@src/CONST'; | ||
import SettlementButton from '.'; | ||
import type SettlementButtonProps from './types'; | ||
|
||
type AnimatedSettlementButtonProps = SettlementButtonProps & { | ||
shouldStartPaidAnimation: boolean; | ||
onAnimationFinish: () => void; | ||
}; | ||
|
||
function AnimatedSettlementButton({shouldStartPaidAnimation, onAnimationFinish, isDisabled, ...settlementButtonProps}: AnimatedSettlementButtonProps) { | ||
const styles = useThemeStyles(); | ||
const buttonScale = useSharedValue(1); | ||
const buttonOpacity = useSharedValue(1); | ||
const paymentCompleteTextScale = useSharedValue(0); | ||
const paymentCompleteTextOpacity = useSharedValue(1); | ||
const height = useSharedValue<number>(variables.componentSizeNormal); | ||
const buttonStyles = useAnimatedStyle(() => ({ | ||
transform: [{scale: buttonScale.value}], | ||
opacity: buttonOpacity.value, | ||
})); | ||
const paymentCompleteTextStyles = useAnimatedStyle(() => ({ | ||
transform: [{scale: paymentCompleteTextScale.value}], | ||
opacity: paymentCompleteTextOpacity.value, | ||
position: 'absolute', | ||
alignSelf: 'center', | ||
})); | ||
const containerStyles = useAnimatedStyle(() => ({ | ||
height: height.value, | ||
justifyContent: 'center', | ||
overflow: 'hidden', | ||
})); | ||
const buttonDisabledStyle = shouldStartPaidAnimation | ||
? { | ||
opacity: 1, | ||
...styles.cursorDefault, | ||
} | ||
: undefined; | ||
|
||
const resetAnimation = useCallback(() => { | ||
// eslint-disable-next-line react-compiler/react-compiler | ||
buttonScale.value = 1; | ||
buttonOpacity.value = 1; | ||
paymentCompleteTextScale.value = 0; | ||
paymentCompleteTextOpacity.value = 1; | ||
height.value = variables.componentSizeNormal; | ||
}, [buttonScale, buttonOpacity, paymentCompleteTextScale, paymentCompleteTextOpacity, height]); | ||
|
||
useEffect(() => { | ||
if (!shouldStartPaidAnimation) { | ||
resetAnimation(); | ||
return; | ||
} | ||
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 reset the animation here in case of a pay error. error.mp4 |
||
// eslint-disable-next-line react-compiler/react-compiler | ||
buttonScale.value = withTiming(0, {duration: CONST.ANIMATION_PAY_BUTTON_DURATION}); | ||
buttonOpacity.value = withTiming(0, {duration: CONST.ANIMATION_PAY_BUTTON_DURATION}); | ||
paymentCompleteTextScale.value = withTiming(1, {duration: CONST.ANIMATION_PAY_BUTTON_DURATION}); | ||
|
||
// Wait for the above animation + 1s delay before hiding the component | ||
const totalDelay = CONST.ANIMATION_PAY_BUTTON_DURATION + CONST.ANIMATION_PAY_BUTTON_HIDE_DELAY; | ||
height.value = withDelay( | ||
totalDelay, | ||
withTiming(0, {duration: CONST.ANIMATION_PAY_BUTTON_DURATION}, () => runOnJS(onAnimationFinish)()), | ||
); | ||
paymentCompleteTextOpacity.value = withDelay(totalDelay, withTiming(0, {duration: CONST.ANIMATION_PAY_BUTTON_DURATION})); | ||
}, [shouldStartPaidAnimation, onAnimationFinish, buttonOpacity, buttonScale, height, paymentCompleteTextOpacity, paymentCompleteTextScale, resetAnimation]); | ||
|
||
return ( | ||
<Animated.View style={containerStyles}> | ||
{shouldStartPaidAnimation && ( | ||
<Animated.View style={paymentCompleteTextStyles}> | ||
<Text style={[styles.buttonMediumText]}>Payment complete</Text> | ||
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. We should've translated this |
||
</Animated.View> | ||
)} | ||
<Animated.View style={buttonStyles}> | ||
<SettlementButton | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...settlementButtonProps} | ||
isDisabled={shouldStartPaidAnimation || isDisabled} | ||
disabledStyle={buttonDisabledStyle} | ||
/> | ||
</Animated.View> | ||
</Animated.View> | ||
); | ||
} | ||
|
||
AnimatedSettlementButton.displayName = 'AnimatedSettlementButton'; | ||
|
||
export default AnimatedSettlementButton; |
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.
Changes look good !
The only thing I would prefer
Is to pass only ref and use the state inside AnimatedSettlementButton since stopAnimation looks redundant when we pass this function
But it's optional
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 don't think using ref is a good idea since it won't trigger a re-render.