From 4172b9c61ea4e47929b564498422c9805fc15c5b Mon Sep 17 00:00:00 2001 From: lukewalczak Date: Fri, 20 Nov 2026 11:34:28 +0100 Subject: [PATCH 1/5] refactor(button): share the shape morph between Button and IconButton Move the corner morph into a shared useShapeMorph hook. IconButton now caps the spring curve against the inner corner, as Button does, so the inner corner of an outlined button can't overshoot below 0. --- src/components/Button/Button.tsx | 54 ++++++---------- src/components/Button/useShapeMorph.ts | 66 ++++++++++++++++++++ src/components/IconButton/IconButton.tsx | 64 +++++-------------- src/components/__tests__/Button.test.tsx | 50 +++++++++++++++ src/components/__tests__/IconButton.test.tsx | 59 +++++++++++++++++ 5 files changed, 209 insertions(+), 84 deletions(-) create mode 100644 src/components/Button/useShapeMorph.ts diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 0e7365f953..8cd85e3640 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -11,8 +11,9 @@ import type { ViewStyle, } from 'react-native'; -import Reanimated, { linear } from 'react-native-reanimated'; +import Reanimated from 'react-native-reanimated'; +import { useShapeMorph } from './useShapeMorph'; import { createIsMode, getButtonColors, @@ -20,7 +21,6 @@ import { getButtonHitSlop, getButtonPressedRadius, getButtonRippleColor, - getButtonShapeCurvePoints, getButtonShapeRadius, getButtonSizeStyle, getButtonTransitionDuration, @@ -29,7 +29,6 @@ import { import { getDefaultDirection, useLocale } from '../../core/locale'; import { useInternalTheme } from '../../core/theming'; import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; -import { toSpringCurve } from '../../theme/tokens/sys/motion'; import type { ThemeProp } from '../../theme/types'; import hasTouchHandler from '../../utils/hasTouchHandler'; import { useFocusRing } from '../../utils/useFocusRing'; @@ -346,12 +345,6 @@ const Button = ({ animatesShape && pressed ? getButtonPressedRadius({ size, theme }) : restingRadius; - const shapeSpring = React.useMemo( - () => toSpringCurve(theme.motion.spring.fast.spatial), - [theme.motion.spring.fast.spatial] - ); - const shapeTransitionDuration = animatesShape ? shapeSpring.duration : 0; - const tracksPress = animatesShape || isElevationEntitled; const handlePressIn = React.useCallback( @@ -427,23 +420,22 @@ const Button = ({ previousContainerColor: committedContainerColor, }); - const innerRadius = Math.max(0, radius - borderWidth); - const largestRadius = Math.max( - sizeStyle.minHeight / 2, - getButtonShapeRadius({ size, shape: 'square', theme }), - getButtonPressedRadius({ size, theme }) - ); - const shapeTimingFunction = React.useMemo( - () => - linear( - ...getButtonShapeCurvePoints({ - points: shapeSpring.points, - radius: innerRadius, - largestRadius: Math.max(0, largestRadius - borderWidth), - }) - ), - [shapeSpring.points, innerRadius, largestRadius, borderWidth] - ); + const { + duration: shapeTransitionDuration, + timingFunction: shapeTimingFunction, + outerStyle: clipStyle, + innerStyle: innerClipStyle, + } = useShapeMorph({ + theme, + radius, + largestRadius: Math.max( + sizeStyle.minHeight / 2, + getButtonShapeRadius({ size, shape: 'square', theme }), + getButtonPressedRadius({ size, theme }) + ), + borderWidth, + animate: animatesShape, + }); // The clip carries the same radius transition as the `Surface`, so the // ripple and the disabled overlay follow the morph. @@ -460,16 +452,6 @@ const Button = ({ 'outward', 'within' ); - const shapeTransitionStyle = { - transitionProperty: 'borderRadius', - transitionDuration: shapeTransitionDuration, - transitionTimingFunction: shapeTimingFunction, - } as const; - - const clipStyle = { ...shapeTransitionStyle, borderRadius: radius }; - - const innerClipStyle = { ...shapeTransitionStyle, borderRadius: innerRadius }; - const outlineStyle = React.useMemo( () => ({ borderColor, borderWidth }), [borderColor, borderWidth] diff --git a/src/components/Button/useShapeMorph.ts b/src/components/Button/useShapeMorph.ts new file mode 100644 index 0000000000..b7f9a78080 --- /dev/null +++ b/src/components/Button/useShapeMorph.ts @@ -0,0 +1,66 @@ +import * as React from 'react'; +import type { ViewStyle } from 'react-native'; + +import { linear, type CSSStyle } from 'react-native-reanimated'; + +import { getButtonShapeCurvePoints } from './utils'; +import { toSpringCurve } from '../../theme/tokens/sys/motion'; +import type { InternalTheme } from '../../theme/types'; + +/** + * Corner morph shared by Button and IconButton: a CSS transition shaped like + * the MD3 fast spatial spring. + * + * The curve is capped against the inner corner (inside the outline), so a + * bouncy spring can't push that corner below 0. The outer corner takes the + * same curve, so the two stay `borderWidth` apart throughout. + */ +export const useShapeMorph = ({ + theme, + radius, + largestRadius, + borderWidth, + animate, +}: { + theme: InternalTheme; + radius: number; + largestRadius: number; + borderWidth: number; + animate: boolean; +}) => { + const spring = React.useMemo( + () => toSpringCurve(theme.motion.spring.fast.spatial), + [theme.motion.spring.fast.spatial] + ); + + const innerRadius = Math.max(0, radius - borderWidth); + const largestInnerRadius = Math.max(0, largestRadius - borderWidth); + + const timingFunction = React.useMemo( + () => + linear( + ...getButtonShapeCurvePoints({ + points: spring.points, + radius: innerRadius, + largestRadius: largestInnerRadius, + }) + ), + [spring.points, innerRadius, largestInnerRadius] + ); + + const duration = animate ? spring.duration : 0; + + const transitionStyle: CSSStyle = { + transitionProperty: 'borderRadius', + transitionDuration: duration, + transitionTimingFunction: timingFunction, + }; + + return { + duration, + timingFunction, + innerRadius, + outerStyle: { ...transitionStyle, borderRadius: radius }, + innerStyle: { ...transitionStyle, borderRadius: innerRadius }, + }; +}; diff --git a/src/components/IconButton/IconButton.tsx b/src/components/IconButton/IconButton.tsx index 82730ced44..04a54e6274 100644 --- a/src/components/IconButton/IconButton.tsx +++ b/src/components/IconButton/IconButton.tsx @@ -9,17 +9,12 @@ import type { ViewStyle, } from 'react-native'; -import Animated, { - type AnimatedStyle, - linear, - type CSSStyle, -} from 'react-native-reanimated'; +import Animated, { type AnimatedStyle } from 'react-native-reanimated'; import type { Mode, Shape, Size, Width } from './tokens'; import { getDimensions, getIconButtonColor } from './utils'; import { useInternalTheme } from '../../core/theming'; import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; -import { toSpringCurve } from '../../theme/tokens/sys/motion'; import type { ThemeProp } from '../../theme/types'; import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSlop'; import hasTouchHandler, { @@ -27,10 +22,8 @@ import hasTouchHandler, { } from '../../utils/hasTouchHandler'; import { useFocusRing } from '../../utils/useFocusRing'; import ActivityIndicator from '../ActivityIndicator'; -import { - getButtonRippleColor, - getButtonShapeCurvePoints, -} from '../Button/utils'; +import { useShapeMorph } from '../Button/useShapeMorph'; +import { getButtonRippleColor } from '../Button/utils'; import CrossFadeIcon from '../CrossFadeIcon'; import Icon from '../Icon'; import type { IconSource } from '../Icon'; @@ -242,35 +235,18 @@ const IconButton = ({ const borderRadius = isPressed ? dimensions.pressedRadius : dimensions.restingRadius; - // The morph follows the MD3 fast spatial spring, as on Button. The curve is - // capped so a bouncy spring can't push the corner below 0 on its way back - // from the largest radius (the round one). - const shapeSpring = React.useMemo( - () => toSpringCurve(theme.motion.spring.fast.spatial), - [theme.motion.spring.fast.spatial] - ); - const largestRadius = Math.max( - dimensions.height / 2, - dimensions.restingRadius, - dimensions.pressedRadius - ); - const shapeTimingFunction = React.useMemo( - () => - linear( - ...getButtonShapeCurvePoints({ - points: shapeSpring.points, - radius: borderRadius, - largestRadius, - }) + const { outerStyle: radiusStyle, innerStyle: innerRadiusStyle } = + useShapeMorph({ + theme, + radius: borderRadius, + largestRadius: Math.max( + dimensions.height / 2, + dimensions.restingRadius, + dimensions.pressedRadius ), - [shapeSpring.points, borderRadius, largestRadius] - ); - const radiusStyle: CSSStyle = { - borderRadius, - transitionProperty: ['borderRadius'], - transitionDuration: reduceMotion ? 0 : shapeSpring.duration, - transitionTimingFunction: shapeTimingFunction, - }; + borderWidth, + animate: !reduceMotion, + }); // The state layer takes the icon color, as Button's takes its label color. const rippleColor = getButtonRippleColor({ labelColor: iconColor }); @@ -345,16 +321,6 @@ const IconButton = ({ !hasPassedTouchHandler && !carriesToggleState({ ...rest, ...toggleStateProps }); - // The touchable and the disabled layer sit inside the outline, so their - // corner is the container's less the outline, and they morph with it. The - // touchable clips its own ripple to it, though not on web, where - // `overflow: 'hidden'` would also clip the hit slop target; there the ripple - // container follows the touchable's corner. - const innerRadiusStyle: CSSStyle = { - ...radiusStyle, - borderRadius: Math.max(0, borderRadius - borderWidth), - }; - return ( { expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20 }); }); + // Reanimated's Jest setup keeps CSS transitions out of `style`. + const transitionOf = (element: ReturnType) => { + const { transitionDuration, transitionTimingFunction } = StyleSheet.flatten( + // eslint-disable-next-line no-restricted-syntax -- see the comment above. + element.props.jestInlineStyle + ); + return { transitionDuration, transitionTimingFunction }; + }; + + it('morphs the ripple inside the outline in step with the outline', async () => { + await render( + + ); + const touchable = screen.getByTestId('button'); + const clip = parentOf(touchable, 'the touchable'); + + expect(transitionOf(touchable)).toEqual(transitionOf(clip)); + + await fireEvent(touchable, 'onPressIn'); + + expect(transitionOf(touchable)).toEqual(transitionOf(clip)); + expect(clip).toHaveStyle({ borderRadius: LightTheme.shapes.corner.large }); + expect(touchable).toHaveStyle({ + borderRadius: + LightTheme.shapes.corner.large - + Tokens.sizes.large.outlinedOutlineWidth, + }); + }); + + it('snaps the corner when animateShape is false', async () => { + await render( + + ); + const touchable = screen.getByTestId('button'); + + expect(transitionOf(touchable).transitionDuration).toBe(0); + expect( + transitionOf(parentOf(touchable, 'the touchable')).transitionDuration + ).toBe(0); + }); + it('still changes the shape when animateShape is false and selected flips', async () => { await render(