From a5a48b165435f27bdb9af8effbd9a114aa4e38e8 Mon Sep 17 00:00:00 2001 From: Doberjohn Date: Mon, 23 Nov 2026 14:53:51 +0200 Subject: [PATCH] revert(button): undo the shape morph refactor merged early This reverts commit 343f2cf52725e25caa9d4a55f881642f1e0dacb2. That commit is #5264, "refactor(button): share the shape morph between Button and IconButton". It was merged by mistake, and its squash message took the title and body meant for #5278, a docs change that is still open: the commit changes no docs. The refactor waits until after 6.0.0-alpha.4, so the release matches the code it was tested on. It will come back in a new pull request with its own title. --- src/components/Button/Button.tsx | 58 +++++++---- src/components/Button/utils.tsx | 49 ++++++++++ src/components/IconButton/IconButton.tsx | 66 +++++++++---- src/components/TouchableRipple/utils.ts | 30 ------ src/components/__tests__/Button.test.tsx | 96 +++++++++--------- src/components/__tests__/Chip.test.tsx | 11 ++- src/components/__tests__/IconButton.test.tsx | 62 +----------- .../__tests__/TouchableRippleUtils.test.tsx | 25 ----- .../reduceMotionThemeOverride.test.tsx | 24 +++-- src/test-utils.tsx | 20 +--- src/utils/__tests__/useShapeMorph.test.ts | 64 ------------ src/utils/useShapeMorph.ts | 97 ------------------- 12 files changed, 216 insertions(+), 386 deletions(-) delete mode 100644 src/components/__tests__/TouchableRippleUtils.test.tsx delete mode 100644 src/utils/__tests__/useShapeMorph.test.ts delete mode 100644 src/utils/useShapeMorph.ts diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index dd39a768e7..0e7365f953 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -11,7 +11,7 @@ import type { ViewStyle, } from 'react-native'; -import Reanimated from 'react-native-reanimated'; +import Reanimated, { linear } from 'react-native-reanimated'; import { createIsMode, @@ -19,6 +19,8 @@ import { getButtonContentBoxStyle, getButtonHitSlop, getButtonPressedRadius, + getButtonRippleColor, + getButtonShapeCurvePoints, getButtonShapeRadius, getButtonSizeStyle, getButtonTransitionDuration, @@ -27,10 +29,10 @@ 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'; -import { useShapeMorph } from '../../utils/useShapeMorph'; import ActivityIndicator from '../ActivityIndicator'; import Icon from '../Icon'; import type { IconSource } from '../Icon'; @@ -38,7 +40,6 @@ import Surface from '../Surface'; import type { SurfaceStyle } from '../Surface'; import TouchableRipple from '../TouchableRipple/TouchableRipple'; import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; -import { getRippleColorFromContent } from '../TouchableRipple/utils'; import Text from '../Typography/Text'; // Elevation levels (MD3) used by the `elevated` mode: level 1 at rest, @@ -345,6 +346,12 @@ 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( @@ -398,7 +405,7 @@ const Button = ({ ); const rippleColor = React.useMemo( - () => getRippleColorFromContent({ contentColor: labelColor }), + () => getButtonRippleColor({ labelColor }), [labelColor] ); @@ -420,22 +427,23 @@ const Button = ({ previousContainerColor: committedContainerColor, }); - 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, - }); + 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] + ); // The clip carries the same radius transition as the `Surface`, so the // ripple and the disabled overlay follow the morph. @@ -452,6 +460,16 @@ 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/utils.tsx b/src/components/Button/utils.tsx index f784225a04..1844654478 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -1,5 +1,7 @@ import type { ColorValue, Insets } from 'react-native'; +import color from 'color'; + import { Tokens } from './tokens'; import type { ButtonToggleMode } from './tokens'; import { black, white } from '../../theme/colors'; @@ -149,6 +151,28 @@ export const getButtonTransitionDuration = ({ return theme.motion.duration[pressed ? 'short4' : 'short3']; }; +/** + * Caps the spring curve so the corner can't overshoot below 0 on its way to + * `radius`, from any start up to `largestRadius`. + */ +export const getButtonShapeCurvePoints = ({ + points, + radius, + largestRadius, +}: { + points: number[]; + radius: number; + largestRadius: number; +}): number[] => { + if (largestRadius <= radius) { + return points; + } + + const maxProgress = largestRadius / (largestRadius - radius); + + return points.map((point) => Math.min(point, maxProgress)); +}; + /** Corner the container morphs to while pressed. */ export const getButtonPressedRadius = ({ size, @@ -425,3 +449,28 @@ export const getButtonColors = ({ backgroundOpacity, }; }; + +/** + * Returns the color used for the button's ripple / state layer: the label + * color at the pressed-state opacity (per Material Design 3). + * + * When the label color is not a plain string (e.g. an Android Material You + * `PlatformColor`), or is a string `color` can't parse (e.g. `var(--x)` or + * `currentColor` on web), `undefined` is returned so `TouchableRipple` falls + * back to its own default state-layer color. + */ +export const getButtonRippleColor = ({ + labelColor, +}: { + labelColor: ColorValue; +}): ColorValue | undefined => { + if (typeof labelColor !== 'string') { + return undefined; + } + + try { + return color(labelColor).alpha(stateOpacity.pressed).rgb().string(); + } catch { + return undefined; + } +}; diff --git a/src/components/IconButton/IconButton.tsx b/src/components/IconButton/IconButton.tsx index a3553503c1..82730ced44 100644 --- a/src/components/IconButton/IconButton.tsx +++ b/src/components/IconButton/IconButton.tsx @@ -9,26 +9,33 @@ import type { ViewStyle, } from 'react-native'; -import Animated, { type AnimatedStyle } from 'react-native-reanimated'; +import Animated, { + type AnimatedStyle, + linear, + type CSSStyle, +} 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, { carriesToggleState, } from '../../utils/hasTouchHandler'; import { useFocusRing } from '../../utils/useFocusRing'; -import { useShapeMorph } from '../../utils/useShapeMorph'; import ActivityIndicator from '../ActivityIndicator'; +import { + getButtonRippleColor, + getButtonShapeCurvePoints, +} from '../Button/utils'; import CrossFadeIcon from '../CrossFadeIcon'; import Icon from '../Icon'; import type { IconSource } from '../Icon'; import TouchableRipple from '../TouchableRipple/TouchableRipple'; import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; -import { getRippleColorFromContent } from '../TouchableRipple/utils'; const AnimatedTouchableRipple = Animated.createAnimatedComponent(TouchableRipple); @@ -235,21 +242,38 @@ const IconButton = ({ const borderRadius = isPressed ? dimensions.pressedRadius : dimensions.restingRadius; - const { outerStyle: radiusStyle, innerStyle: innerRadiusStyle } = - useShapeMorph({ - theme, - radius: borderRadius, - largestRadius: Math.max( - dimensions.height / 2, - dimensions.restingRadius, - dimensions.pressedRadius + // 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, + }) ), - borderWidth, - animate: !reduceMotion, - }); + [shapeSpring.points, borderRadius, largestRadius] + ); + const radiusStyle: CSSStyle = { + borderRadius, + transitionProperty: ['borderRadius'], + transitionDuration: reduceMotion ? 0 : shapeSpring.duration, + transitionTimingFunction: shapeTimingFunction, + }; // The state layer takes the icon color, as Button's takes its label color. - const rippleColor = getRippleColorFromContent({ contentColor: iconColor }); + const rippleColor = getButtonRippleColor({ labelColor: iconColor }); const handlePressIn = (e: GestureResponderEvent) => { setPressed(true); @@ -321,6 +345,16 @@ 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 ( (typeof children === 'function' ? children(state) : children); - -/** - * Returns the ripple / state layer color for a component's content: the - * content color at the pressed-state opacity (per Material Design 3). - * - * When the content color is not a plain string (e.g. an Android Material You - * `PlatformColor`), or is a string `color` can't parse (e.g. `var(--x)` or - * `currentColor` on web), `undefined` is returned so `TouchableRipple` falls - * back to its own default state-layer color. - */ -export const getRippleColorFromContent = ({ - contentColor, -}: { - contentColor: ColorValue; -}): ColorValue | undefined => { - if (typeof contentColor !== 'string') { - return undefined; - } - - try { - return color(contentColor).alpha(stateOpacity.pressed).rgb().string(); - } catch { - return undefined; - } -}; diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 6295f2eba9..61b23afe0e 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -2,10 +2,11 @@ import { Platform, PlatformColor, StyleSheet } from 'react-native'; import { afterEach, describe, expect, it, jest } from '@jest/globals'; import { fireEvent } from '@testing-library/react-native'; +import color from 'color'; import { LocaleProvider } from '../../core/locale'; import PaperProvider from '../../core/PaperProvider'; -import { render, screen, transitionOf } from '../../test-utils'; +import { render, screen } from '../../test-utils'; import { ReduceMotionContext } from '../../theme/accessibility/ReduceMotionContext'; import { pink500, white } from '../../theme/colors'; import { LightTheme } from '../../theme/schemes'; @@ -19,6 +20,8 @@ import { getButtonContentBoxStyle, getButtonHitSlop, getButtonPressedRadius, + getButtonRippleColor, + getButtonShapeCurvePoints, getButtonShapeRadius, getButtonSizeStyle, getButtonTransitionDuration, @@ -627,6 +630,20 @@ describe('getButtonColors - border width', () => { ); }); +describe('getButtonRippleColor', () => { + it('defaults to the label color at the pressed-state opacity', () => { + expect(getButtonRippleColor({ labelColor: '#123456' })).toBe( + color('#123456').alpha(stateOpacity.pressed).rgb().string() + ); + }); + + it('returns undefined when the label color is not a plain string', () => { + expect( + getButtonRippleColor({ labelColor: PlatformColor('?attr/colorPrimary') }) + ).toBeUndefined(); + }); +}); + it('renders with a label color the ripple cannot parse', 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(