Repository navigation
feat(motion): adopt spring tokens and add shared CSS-transition helper #5112
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
base: main
Are you sure you want to change the base?
Changes from all commits
1d75730
b72a8d3
ece44d4
dc639e3
cea214b
6c5697b
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 |
|---|---|---|
|
|
@@ -22,19 +22,14 @@ import { | |
| } from './utils'; | ||
| import { useInternalTheme } from '../../core/theming'; | ||
| import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; | ||
| import { toRawSpring } from '../../theme/tokens/sys/motion'; | ||
| import type { ThemeProp } from '../../theme/types'; | ||
| import type { IconSource } from '../Icon'; | ||
| import Icon from '../Icon'; | ||
| import TouchableRipple from '../TouchableRipple/TouchableRipple'; | ||
| import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; | ||
| import Text from '../Typography/Text'; | ||
|
|
||
| const CHECK_SPRING_CONFIG = { | ||
| stiffness: 230.2, | ||
| damping: 22, | ||
| mass: 1, | ||
| }; | ||
|
|
||
| export type Props = { | ||
| /** | ||
| * Whether the segmented button is checked | ||
|
|
@@ -139,16 +134,21 @@ const SegmentedButtonItem = ({ | |
|
|
||
| const checkScale = useSharedValue(0); | ||
|
|
||
| const checkSpringConfig = React.useMemo( | ||
| () => ({ | ||
| ...toRawSpring(theme.motion.spring.fast.spatial), | ||
| reduceMotion: reduceMotion ? ReduceMotion.Always : ReduceMotion.Never, | ||
| }), | ||
| [theme.motion.spring.fast.spatial, reduceMotion] | ||
| ); | ||
|
|
||
| React.useEffect(() => { | ||
| if (!showSelectedCheck) { | ||
| return; | ||
| } | ||
|
|
||
| checkScale.value = withSpring(checked ? 1 : 0, { | ||
| ...CHECK_SPRING_CONFIG, | ||
| reduceMotion: reduceMotion ? ReduceMotion.Always : ReduceMotion.Never, | ||
| }); | ||
| }, [checked, checkScale, reduceMotion, showSelectedCheck]); | ||
| checkScale.value = withSpring(checked ? 1 : 0, checkSpringConfig); | ||
|
Member
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. The check animation runs on a shared value with
Author
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'd rather keep this one as withSpring. The checkmark gets toggled by quick taps, so it's common for the animation to get interrupted mid flight and reverse. withSpring picks up from the current position and velocity when that happens, so the reversal looks physically continuous. A linear() curve sampled from the spring only matches one specific start to end run, if it gets interrupted and restarted the sampled points no longer match the remaining distance, so the motion would snap or look off on rapid re-taps. That's basically why the original task treats MD3 spring motion as an exception to "CSS by default", it's for motion that's genuinely spring shaped and needs to handle interruption correctly, which this is, even though the trigger is a prop rather than a gesture. Happy to revisit if you feel strongly, but wanted to explain the reasoning first. |
||
| }, [checked, checkScale, checkSpringConfig, showSelectedCheck]); | ||
|
|
||
| const checkAnimatedStyle = useAnimatedStyle(() => ({ | ||
| transform: [{ scale: checkScale.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.
Have you considered typing this style as
CSSStyle<ViewStyle>like inCheckbox.tsx?Uh oh!
There was an error while loading. Please reload this page.
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.
Good call, done! Badge renders
Animated.Text, so I've updated it toCSSStyle<TextStyle>.