From d0c3945634af49c72e26f137a088c4c5760c73dd Mon Sep 17 00:00:00 2001 From: Aziz Becha Date: Tue, 12 May 2026 20:47:01 +0100 Subject: [PATCH 01/66] refactor: add Button label prop and deprecate children Introduce a `label?: string` prop as the primary way to set the button text. The `children` prop keeps working as a deprecated fallback (when both are set, `label` wins) and emits a dev-only warning. This decouples the button layout from arbitrary child structures and makes `uppercase` work reliably, since the label is always a string. --- src/components/Button/Button.tsx | 34 ++++- src/components/__tests__/Button.test.tsx | 167 +++++++++++++++-------- 2 files changed, 137 insertions(+), 64 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 17d40fb035..1e4493f1db 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -31,7 +31,10 @@ import TouchableRipple, { } from '../TouchableRipple/TouchableRipple'; import Text from '../Typography/Text'; -export type Props = $Omit, 'mode'> & { +export type Props = $Omit< + React.ComponentProps, + 'mode' | 'children' +> & { /** * Mode of the button. You can change the mode to adjust the styling to give it desired emphasis. * - `text` - flat button without background or outline, used for the lowest priority actions, especially when presenting multiple options. @@ -74,9 +77,14 @@ export type Props = $Omit, 'mode'> & { /** * Label text of the button. */ - children: React.ReactNode; + label?: string; /** - * Make the label text uppercased. Note that this won't work if you pass React elements as children. + * @deprecated Use `label` instead. When both `label` and `children` are set, `label` is used. + * Label text of the button. + */ + children?: React.ReactNode; + /** + * Make the label text uppercased. */ uppercase?: boolean; /** @@ -157,9 +165,12 @@ export type Props = $Omit, 'mode'> & { * import { Button } from 'react-native-paper'; * * const MyComponent = () => ( - * + * ).toJSON(); + const tree = render().toJSON(); + const tree = render( + + ).toJSON(); + const tree = render( + ).toJSON(); + const tree = render().toJSON(); + const tree = render( + + + + + + + + + label="Custom radius" + /> ); expect(getByTestId('custom-radius-container')).toHaveStyle( @@ -186,9 +192,11 @@ it('renders outlined button with custom border radius', () => { it('renders button without border radius', () => { const { getByTestId } = render( - + + + + ); + + expect(getByTestId('button-text')).toHaveTextContent('From label'); + }); +}); + +describe('deprecated children prop', () => { + it('still renders the children as the label', () => { + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); + const { getByTestId } = render( + + ); + + expect(getByTestId('button-text')).toHaveTextContent('Legacy label'); + warn.mockRestore(); + }); + + it('warns about the deprecation', () => { + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); + render(); + + expect(warn).toHaveBeenCalledWith( + expect.stringContaining('`children` prop is deprecated') + ); + warn.mockRestore(); + }); +}); + describe('button text styles', () => { it('applies uppercase styles if uppercase prop is truthy', () => { const { getByTestId } = render( - + + + + + + + label="Compact button" + /> ); expect(getByTestId('button-container-outer-layer')).toHaveStyle({ transform: [{ scale: 1 }], From ef3f3ad7e5247f59bcc9e9bd837fb19e7817344f Mon Sep 17 00:00:00 2001 From: Aziz Becha Date: Tue, 12 May 2026 20:53:10 +0100 Subject: [PATCH 02/66] refactor: migrate internal Button usages to label Update the components that compose Button (Banner, Snackbar, DataTablePagination) to pass the new `label` prop instead of children, and update the `## Usage` / `@example` JSDoc blocks (and the test files) accordingly so nothing relies on the deprecated `children` prop. --- src/components/Banner.tsx | 5 ++-- src/components/Card/Card.tsx | 4 +-- src/components/Card/CardActions.tsx | 4 +-- .../DataTable/DataTablePagination.tsx | 5 ++-- src/components/Dialog/Dialog.tsx | 4 +-- src/components/Dialog/DialogActions.tsx | 4 +-- src/components/Menu/Menu.tsx | 2 +- src/components/Modal.tsx | 4 +-- src/components/Snackbar.tsx | 7 +++-- src/components/__tests__/Card/Card.test.tsx | 7 +++-- src/components/__tests__/Dialog.test.tsx | 12 ++++----- src/components/__tests__/Menu.test.tsx | 26 +++++++------------ 12 files changed, 35 insertions(+), 49 deletions(-) diff --git a/src/components/Banner.tsx b/src/components/Banner.tsx index b1a00b4ce4..428a225d37 100644 --- a/src/components/Banner.tsx +++ b/src/components/Banner.tsx @@ -246,10 +246,9 @@ const Banner = ({ style={styles.button} textColor={colors.primary} theme={theme} + label={label} {...others} - > - {label} - + /> ))} diff --git a/src/components/Card/Card.tsx b/src/components/Card/Card.tsx index 6dc433322a..1e1677c5d2 100644 --- a/src/components/Card/Card.tsx +++ b/src/components/Card/Card.tsx @@ -127,8 +127,8 @@ export type Props = $Omit, 'mode'> & { * * * - * - * + * - * + * + label={`${numberOfItemsPerPage}`} + /> } > {numberOfItemsPerPageList?.map((option) => ( diff --git a/src/components/Dialog/Dialog.tsx b/src/components/Dialog/Dialog.tsx index 717445aed7..e2f9ec2491 100644 --- a/src/components/Dialog/Dialog.tsx +++ b/src/components/Dialog/Dialog.tsx @@ -73,7 +73,7 @@ const DIALOG_ELEVATION: number = 24; * return ( * * - * + * + * - * + * }> + * anchor={ + * + * + /> ) : null} {isIconButton ? ( { const { getByTestId } = render( - + + label="Agree" + /> ); diff --git a/src/components/__tests__/Dialog.test.tsx b/src/components/__tests__/Dialog.test.tsx index 742f44b941..47a6409c58 100644 --- a/src/components/__tests__/Dialog.test.tsx +++ b/src/components/__tests__/Dialog.test.tsx @@ -110,8 +110,8 @@ describe('DialogActions', () => { it('should render passed children', () => { const { getByTestId } = render( - - + - + - + } + anchor={} + anchor={} + anchor={} + anchor={ - } + anchor={ - } + anchor={} + anchor={} + anchor={} + anchor={ + - - - - + + label="Icon" + /> + + label="Default" + /> + label="Custom" + /> + label="Disabled" + /> + label="Icon" + /> + label="Loading" + /> + iconPosition="trailing" + label="Icon right" + /> - + + label="Custom" + /> + label="Disabled" + /> + label="Icon" + /> + label="Loading" + /> + iconPosition="trailing" + label="Icon right" + /> - + + label="Custom" + /> + label="Disabled" + /> + label="Icon" + /> + label="Loading" + /> + iconPosition="trailing" + label="Icon right" + /> - + + label="Custom" + /> + label="Disabled" + /> + label="Icon" + /> + label="Loading" + /> + iconPosition="trailing" + label="Icon right" + /> @@ -244,17 +241,15 @@ const ButtonExample = () => { }} onPress={() => {}} style={styles.button} - > - Remote image - + label="Remote image" + /> + label="Required asset" + /> + label="Custom component" + /> - + label="Custom Font" + /> + - + label="Custom radius" + /> + + label="Custom radius and padding" + /> @@ -306,18 +304,16 @@ const ButtonExample = () => { mode="contained" onPress={() => {}} style={styles.flexGrow1Button} - > - flex-grow: 1 - + label="flex-grow: 1" + /> + label="width: 100%" + /> @@ -339,9 +335,8 @@ const ButtonExample = () => { onPress={() => {}} style={styles.button} icon="camera" - > - Compact {mode} - + label={`Compact ${mode}`} + /> ); })} @@ -363,9 +358,6 @@ const styles = StyleSheet.create({ button: { margin: 4, }, - flexReverse: { - flexDirection: 'row-reverse', - }, md3FontStyles: { lineHeight: 32, }, diff --git a/example/src/Examples/CardExample.tsx b/example/src/Examples/CardExample.tsx index ad3451e8b0..9a7973a8de 100644 --- a/example/src/Examples/CardExample.tsx +++ b/example/src/Examples/CardExample.tsx @@ -75,8 +75,8 @@ const CardExample = () => { - - + - + + label="Long text" + /> + label="Radio buttons" + /> + label="Progress indicator" + /> + label="Undismissable Dialog" + /> + label="Custom colors" + /> + label="With icon" + /> {Platform.OS === 'android' && ( + label="Dismissable back button" + /> )} - + - + - + + - + - + + + + - - + + label={showSnackbar ? 'Hide' : 'Show'} + /> { - - + - + + + label="Press me" +/> ``` Local image: ```js - + + label="Press me" +/> ``` ### 4. Use custom icons @@ -131,15 +129,14 @@ Example for using an image source: }, direction: 'rtl', }} -> - Press me - + label="Press me" +/> ``` Example for using an icon name: ```js - + + label="Press me" +/> ``` diff --git a/docs/docs/guides/09-react-navigation.md b/docs/docs/guides/09-react-navigation.md index 5ea5d556fa..c37bf2bad9 100644 --- a/docs/docs/guides/09-react-navigation.md +++ b/docs/docs/guides/09-react-navigation.md @@ -86,9 +86,11 @@ function HomeScreen({ navigation }) { return ( Home Screen - + + onPress={() => console.log('Pressed')} + label="Press me" +/> ``` ## Disable ripple effect in all components diff --git a/docs/src/components/BannerExample.tsx b/docs/src/components/BannerExample.tsx index 59ec60634a..0bae591bac 100644 --- a/docs/src/components/BannerExample.tsx +++ b/docs/src/components/BannerExample.tsx @@ -74,15 +74,19 @@ const BannerExample = () => { > - - - + + + label="Try on Snack" + /> ); }; From 487eea45854b89eb6bb0de2e55b185a7e8b2288d Mon Sep 17 00:00:00 2001 From: Aziz Becha Date: Wed, 13 May 2026 13:59:26 +0100 Subject: [PATCH 08/66] feat: add Button size prop (MD3 expressive) Add a `size?: 'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'` prop. When omitted, the Button keeps its current visuals; when set, the per-size MD3 metrics (minHeight, horizontal padding, icon size, icon/label gap, label typescale) are applied via a new `getButtonSizeStyle` helper. --- src/components/Button/Button.tsx | 67 ++++-- src/components/Button/utils.tsx | 67 ++++++ src/components/__tests__/Button.test.tsx | 56 ++++- .../__snapshots__/Button.test.tsx.snap | 192 ++++++++++++++++++ 4 files changed, 366 insertions(+), 16 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index fc71192a2d..075afc2b63 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -15,9 +15,11 @@ import { import { ButtonMode, + ButtonSize, getButtonColors, getButtonIconStyle, getButtonRippleColor, + getButtonSizeStyle, getButtonTouchableRippleStyle, } from './utils'; import { useInternalTheme } from '../../core/theming'; @@ -56,6 +58,15 @@ export type Props = $Omit< * Use a compact look, useful for `text` buttons in a row. */ compact?: boolean; + /** + * Size of the button (Material Design 3 expressive). One of + * `'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'`. + * + * When omitted, the button uses its legacy visuals. When set, the size + * controls the minimum height, horizontal padding, icon size, the gap + * between icon and label, and the label typescale. + */ + size?: ButtonSize; /** * Custom button's background color. */ @@ -202,6 +213,7 @@ const Button = ( disabled, compact, mode = 'text', + size, dark, loading, icon, @@ -388,22 +400,29 @@ const Button = ( [labelStyle] ); + const sizeStyle = React.useMemo( + () => (size ? getButtonSizeStyle(size) : undefined), + [size] + ); + const textStyle = React.useMemo( () => ({ color: textColor, - ...(theme as Theme).fonts.labelLarge, + ...(theme as Theme).fonts[sizeStyle?.labelVariant ?? 'labelLarge'], }), - [textColor, theme] + [textColor, theme, sizeStyle] ); const iconStyle = React.useMemo( () => - getButtonIconStyle({ - mode, - compact, - position: isTrailingIcon ? 'trailing' : 'leading', - }), - [mode, compact, isTrailingIcon] + sizeStyle + ? null + : getButtonIconStyle({ + mode, + compact, + position: isTrailingIcon ? 'trailing' : 'leading', + }), + [mode, compact, isTrailingIcon, sizeStyle] ); return ( @@ -460,15 +479,27 @@ const Button = ( style={[ styles.content, isTrailingIcon && styles.contentReverse, + ...(sizeStyle + ? [ + { + minHeight: sizeStyle.minHeight, + paddingHorizontal: sizeStyle.paddingHorizontal, + gap: sizeStyle.iconGap, + }, + ] + : []), { opacity: textOpacity }, contentStyle, ]} > {icon && loading !== true ? ( - + ) : null} = { + 'extra-small': { + minHeight: 32, + paddingHorizontal: 12, + iconSize: 16, + iconGap: 4, + labelVariant: 'labelLarge', + }, + small: { + minHeight: 40, + paddingHorizontal: 16, + iconSize: 20, + iconGap: 8, + labelVariant: 'labelLarge', + }, + medium: { + minHeight: 56, + paddingHorizontal: 24, + iconSize: 24, + iconGap: 8, + labelVariant: 'titleMedium', + }, + large: { + minHeight: 96, + paddingHorizontal: 48, + iconSize: 32, + iconGap: 12, + labelVariant: 'headlineSmall', + }, + 'extra-large': { + minHeight: 136, + paddingHorizontal: 64, + iconSize: 40, + iconGap: 16, + labelVariant: 'headlineLarge', + }, +}; + +export const getButtonSizeStyle = (size: ButtonSize): ButtonSizeStyle => + BUTTON_SIZE_STYLES[size]; + /** * Returns the margins applied to the button's icon (or loading indicator) * depending on the button mode, density and the position of the icon relative diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 94cf3ab0db..df026b86b3 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -9,7 +9,11 @@ import { render } from '../../test-utils'; import { pink500, white } from '../../theme/colors'; import { tokens } from '../../theme/tokens'; import Button from '../Button/Button'; -import { getButtonColors, getButtonRippleColor } from '../Button/utils'; +import { + getButtonColors, + getButtonRippleColor, + getButtonSizeStyle, +} from '../Button/utils'; const { stateOpacity } = tokens.md.ref; @@ -825,6 +829,56 @@ describe('getButtonRippleColor', () => { }); }); +describe('getButtonSizeStyle', () => { + it.each([ + ['extra-small', 32, 12, 16, 4, 'labelLarge'], + ['small', 40, 16, 20, 8, 'labelLarge'], + ['medium', 56, 24, 24, 8, 'titleMedium'], + ['large', 96, 48, 32, 12, 'headlineSmall'], + ['extra-large', 136, 64, 40, 16, 'headlineLarge'], + ] as const)( + 'returns expected metrics for %s', + (size, minHeight, paddingHorizontal, iconSize, iconGap, labelVariant) => { + expect(getButtonSizeStyle(size)).toEqual({ + minHeight, + paddingHorizontal, + iconSize, + iconGap, + labelVariant, + }); + } + ); +}); + +describe('size prop', () => { + it('renders a button with per-size metrics', () => { + const tree = render( + ``` :::note @@ -63,14 +63,15 @@ Remote image: ```js ``` Local image: ```js - ``` ### 3. A render function @@ -87,8 +88,9 @@ Example: style={{ width: size, height: size, tintColor: color }} /> )} - label="Press me" -/> +> + Press me + ``` ### 4. Use custom icons @@ -129,14 +131,15 @@ Example for using an image source: }, direction: 'rtl', }} - label="Press me" -/> +> + Press me + ``` Example for using an icon name: ```js - ``` You can also use a render function. Along with `size` and `color`, you have access to `direction` which will either be `'rtl'` or `'ltr'`. You can then decide how to render your icon component accordingly. @@ -160,6 +163,7 @@ Example of using a render function: ]} /> )} - label="Press me" -/> +> + Press me + ``` diff --git a/docs/6.x/docs/guides/react-navigation.md b/docs/6.x/docs/guides/react-navigation.md index 1e36636d37..33c1fac71b 100644 --- a/docs/6.x/docs/guides/react-navigation.md +++ b/docs/6.x/docs/guides/react-navigation.md @@ -86,11 +86,9 @@ function HomeScreen({ navigation }) { return ( Home Screen - ); } diff --git a/docs/6.x/docs/guides/ripple-effect.md b/docs/6.x/docs/guides/ripple-effect.md index 3b083c0abb..7d5d97725b 100644 --- a/docs/6.x/docs/guides/ripple-effect.md +++ b/docs/6.x/docs/guides/ripple-effect.md @@ -19,9 +19,9 @@ The `rippleColor` prop is available for every pressable component which allows y rippleColor="#FF000020" icon="camera" mode="filled" - onPress={() => console.log('Pressed')} - label="Press me" -/> + onPress={() => console.log('Pressed')}> + Press me + ``` ## Disable ripple effect in all components diff --git a/docs/src/components/BannerExample.tsx b/docs/src/components/BannerExample.tsx index 0638f279e0..0ca2828fd9 100644 --- a/docs/src/components/BannerExample.tsx +++ b/docs/src/components/BannerExample.tsx @@ -94,14 +94,15 @@ const BannerExample = () => { > - + + {}} /> {}} /> {}} /> diff --git a/example/src/DrawerItems.tsx b/example/src/DrawerItems.tsx index 7c9afe1d3d..94afa3136c 100644 --- a/example/src/DrawerItems.tsx +++ b/example/src/DrawerItems.tsx @@ -254,7 +254,7 @@ function DrawerItems() { example directory. - diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index f4b3e8683b..fad8e6068a 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -118,8 +118,9 @@ const ButtonExample = () => { // tokens own spacing once a size is set. compact={size === 'unset' && compact} onPress={() => {}} - label="Play me" - /> + > + Play me + { {MODES.map((m) => ( - ))} - @@ -226,8 +222,9 @@ const ButtonExample = () => { icon="star" onPress={() => {}} style={styles.button} - label={s} - /> + > + {s} + ))} @@ -243,8 +240,9 @@ const ButtonExample = () => { shape={shapeVariant} onPress={() => {}} style={styles.button} - label={`${s} ${shapeVariant}`} - /> + > + {`${s} ${shapeVariant}`} + ))} ))} @@ -265,8 +263,9 @@ const ButtonExample = () => { onPress={() => toggle(key)} style={styles.button} icon={isSelected ? 'check' : 'plus'} - label={m} - /> + > + {m} + ); })} @@ -279,8 +278,9 @@ const ButtonExample = () => { buttonColor={color} onPress={() => {}} style={styles.button} - label="Custom color" - /> + > + Custom color + diff --git a/example/src/Examples/CardExample.tsx b/example/src/Examples/CardExample.tsx index a36def0b76..61c7ebe822 100644 --- a/example/src/Examples/CardExample.tsx +++ b/example/src/Examples/CardExample.tsx @@ -75,8 +75,8 @@ const CardExample = () => { - + @@ -104,8 +104,12 @@ const CardExample = () => { /> - + { mode="outlined" onPress={_toggleDialog('dialog1')} style={styles.button} - label="Long text" - /> + > + Long text + {Platform.OS === 'android' && ( )} - diff --git a/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx b/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx index 76cd8129fe..e9a7189068 100644 --- a/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx +++ b/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx @@ -24,8 +24,10 @@ const DialogWithDismissableBackButton = ({ - + diff --git a/example/src/Examples/Dialogs/DialogWithIcon.tsx b/example/src/Examples/Dialogs/DialogWithIcon.tsx index f3f3b6709f..6281e9bf6c 100644 --- a/example/src/Examples/Dialogs/DialogWithIcon.tsx +++ b/example/src/Examples/Dialogs/DialogWithIcon.tsx @@ -23,12 +23,10 @@ const DialogWithIcon = ({ - + diff --git a/example/src/Examples/Dialogs/DialogWithLongText.tsx b/example/src/Examples/Dialogs/DialogWithLongText.tsx index 097884ef93..eeac1c7d3c 100644 --- a/example/src/Examples/Dialogs/DialogWithLongText.tsx +++ b/example/src/Examples/Dialogs/DialogWithLongText.tsx @@ -62,7 +62,7 @@ const DialogWithLongText = ({ - diff --git a/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx b/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx index 07aca7ecf2..966422369e 100644 --- a/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx +++ b/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx @@ -84,8 +84,8 @@ const DialogWithRadioBtns = ({ visible, close }: Props) => { - + diff --git a/example/src/Examples/Dialogs/UndismissableDialog.tsx b/example/src/Examples/Dialogs/UndismissableDialog.tsx index 67bfcfe8c2..208c809967 100644 --- a/example/src/Examples/Dialogs/UndismissableDialog.tsx +++ b/example/src/Examples/Dialogs/UndismissableDialog.tsx @@ -16,8 +16,10 @@ const UndismissableDialog = ({ This is an undismissable dialog!! - + diff --git a/example/src/Examples/MenuExample.tsx b/example/src/Examples/MenuExample.tsx index cfce767d1b..2a55069090 100644 --- a/example/src/Examples/MenuExample.tsx +++ b/example/src/Examples/MenuExample.tsx @@ -78,11 +78,9 @@ const MenuExample = () => { visible={_getVisible('menu2')} onDismiss={_toggleMenu('menu2')} anchor={ - } > {}} title="Undo" /> @@ -139,11 +137,9 @@ const MenuExample = () => { onDismiss={_toggleMenu('menu5')} anchorPosition="bottom" anchor={ - } > {}} title="Item 1" /> @@ -157,11 +153,9 @@ const MenuExample = () => { visible={_getVisible('menu4')} onDismiss={_toggleMenu('menu4')} anchor={ - } > {}} title="Bottom Item 1" /> diff --git a/example/src/Examples/ProgressBarExample.tsx b/example/src/Examples/ProgressBarExample.tsx index 4e2e3ff2d4..a42474b99d 100644 --- a/example/src/Examples/ProgressBarExample.tsx +++ b/example/src/Examples/ProgressBarExample.tsx @@ -41,12 +41,11 @@ const ProgressBarExample = () => { return ( - + + Default ProgressBar diff --git a/example/src/Examples/SnackbarExample.tsx b/example/src/Examples/SnackbarExample.tsx index 06e65ce077..e31955f18c 100644 --- a/example/src/Examples/SnackbarExample.tsx +++ b/example/src/Examples/SnackbarExample.tsx @@ -90,8 +90,9 @@ const SnackbarExample = () => { onPress={() => setOptions({ ...options, showSnackbar: !showSnackbar }) } - label={showSnackbar ? 'Hide' : 'Show'} - /> + > + {showSnackbar ? 'Hide' : 'Show'} + { - + @@ -110,8 +110,8 @@ const News = () => { - + diff --git a/src/components/Banner.tsx b/src/components/Banner.tsx index 31c35156a2..f994396931 100644 --- a/src/components/Banner.tsx +++ b/src/components/Banner.tsx @@ -248,9 +248,10 @@ const Banner = ({ style={styles.button} textColor={colors.primary} theme={theme} - label={label} {...others} - /> + > + {label} + ))} diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 984ee036af..ff606bc90d 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -33,10 +33,7 @@ import TouchableRipple from '../TouchableRipple/TouchableRipple'; import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; import Text from '../Typography/Text'; -export type Props = $Omit< - React.ComponentProps, - 'mode' | 'children' -> & { +export type Props = $Omit, 'mode'> & { /** * Mode of the button. You can change the mode to adjust the styling to give it desired emphasis. Defaults to `filled`. * - `filled` - button with a background color, used for the most important action, has the most visual impact and high emphasis. (default) @@ -110,14 +107,9 @@ export type Props = $Omit< /** * Label text of the button. */ - label?: string; + children: React.ReactNode; /** - * @deprecated Use `label` instead. When both `label` and `children` are set, `label` is used. - * Label text of the button. - */ - children?: React.ReactNode; - /** - * Make the label text uppercased. + * Make the label text uppercased. Note that this won't work if you pass React elements as children. */ uppercase?: boolean; /** @@ -207,12 +199,9 @@ export type Props = $Omit< * import { Button } from 'react-native-paper'; * * const MyComponent = () => ( - * * ); * * export default MyComponent; @@ -242,7 +231,6 @@ const Button = ({ iconPosition, buttonColor: customButtonColor, textColor: customLabelColor, - label, children, 'aria-label': ariaLabel, accessibilityHint, @@ -274,14 +262,6 @@ const Button = ({ const uppercase = uppercaseProp ?? false; const isWeb = Platform.OS === 'web'; - if (process.env.NODE_ENV !== 'production' && children != null) { - console.warn( - 'Button: the `children` prop is deprecated and will be removed in a future release. Use the `label` prop instead.' - ); - } - - const labelContent = label != null ? label : children; - const flattenedContentStyle = React.useMemo( () => StyleSheet.flatten(contentStyle) as ViewStyle | undefined, [contentStyle] @@ -691,7 +671,7 @@ const Button = ({ ]} maxFontSizeMultiplier={maxFontSizeMultiplier} > - {labelContent} + {children} diff --git a/src/components/Card/Card.tsx b/src/components/Card/Card.tsx index 02b285e8c1..f0fdc89407 100644 --- a/src/components/Card/Card.tsx +++ b/src/components/Card/Card.tsx @@ -112,8 +112,8 @@ export type Props = $Omit, 'mode'> & { * * * - * + * * * * ); diff --git a/src/components/Card/CardActions.tsx b/src/components/Card/CardActions.tsx index be27062162..6559b888fd 100644 --- a/src/components/Card/CardActions.tsx +++ b/src/components/Card/CardActions.tsx @@ -26,8 +26,8 @@ export type Props = ViewProps & { * const MyComponent = () => ( * * - * + * * * * ); diff --git a/src/components/DataTable/DataTablePagination.tsx b/src/components/DataTable/DataTablePagination.tsx index 0cdbf5a048..d30256d518 100644 --- a/src/components/DataTable/DataTablePagination.tsx +++ b/src/components/DataTable/DataTablePagination.tsx @@ -184,8 +184,9 @@ const PaginationDropdown = ({ icon="menu-down" iconPosition="trailing" theme={theme} - label={`${numberOfItemsPerPage}`} - /> + > + {`${numberOfItemsPerPage}`} + } > {numberOfItemsPerPageList?.map((option) => ( diff --git a/src/components/Dialog/Dialog.tsx b/src/components/Dialog/Dialog.tsx index c410cd258d..a7b082fe89 100644 --- a/src/components/Dialog/Dialog.tsx +++ b/src/components/Dialog/Dialog.tsx @@ -68,7 +68,7 @@ const DIALOG_ELEVATION: number = 24; * return ( * * - * * * * Alert @@ -76,7 +76,7 @@ const DIALOG_ELEVATION: number = 24; * This is simple dialog * * - * * * * diff --git a/src/components/Dialog/DialogActions.tsx b/src/components/Dialog/DialogActions.tsx index 83db082dd7..7e3799451e 100644 --- a/src/components/Dialog/DialogActions.tsx +++ b/src/components/Dialog/DialogActions.tsx @@ -35,8 +35,8 @@ export type Props = ViewProps & { * * * - * + * * * * diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 0e1ee9583c..ec4c256dad 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -156,7 +156,7 @@ const isBrowser = () => Platform.OS === 'web' && 'document' in global; * }> + * anchor={}> * {}} title="Item 1" /> * {}} title="Item 2" /> * diff --git a/src/components/Modal.tsx b/src/components/Modal.tsx index 68eb99c9a8..3c66c885b3 100644 --- a/src/components/Modal.tsx +++ b/src/components/Modal.tsx @@ -86,7 +86,9 @@ const AnimatedPressable = Animated.createAnimatedComponent(Pressable); * Example Modal. Click outside this area to dismiss. * * - * * * ); * }; diff --git a/src/components/Snackbar.tsx b/src/components/Snackbar.tsx index d3f3f3813b..05e2b3d6c1 100644 --- a/src/components/Snackbar.tsx +++ b/src/components/Snackbar.tsx @@ -107,7 +107,7 @@ const DURATION_LONG = 10000; * * return ( * - * * + > + {actionLabel} + ) : null} {isIconButton ? ( { - const tree = (await render()).toJSON(); expect(tree).toMatchSnapshot(); }); it('renders text button with mode', async () => { const tree = ( - await render() ).toJSON(); expect(tree).toMatchSnapshot(); @@ -54,7 +54,7 @@ it('renders text button with mode', async () => { it('renders outlined button with mode', async () => { const tree = ( - await render() ).toJSON(); expect(tree).toMatchSnapshot(); @@ -62,7 +62,7 @@ it('renders outlined button with mode', async () => { it('renders filled button with mode', async () => { const tree = ( - await render() ).toJSON(); expect(tree).toMatchSnapshot(); @@ -70,7 +70,7 @@ it('renders filled button with mode', async () => { it('renders button with icon', async () => { const tree = ( - await render() ).toJSON(); expect(tree).toMatchSnapshot(); @@ -79,11 +79,9 @@ it('renders button with icon', async () => { it('renders button with icon in reverse order', async () => { const tree = ( await render( - ) ).toJSON(); @@ -91,7 +89,11 @@ it('renders button with icon in reverse order', async () => { }); it('swaps the icon to the trailing edge under RTL', async () => { - await render( + ); const ltrIconStyle = StyleSheet.flatten( // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. screen.getByTestId('button-icon-container').props.style @@ -99,7 +101,9 @@ it('swaps the icon to the trailing edge under RTL', async () => { await render( - ); const rtlIconStyle = StyleSheet.flatten( @@ -113,34 +117,30 @@ it('swaps the icon to the trailing edge under RTL', async () => { }); it('renders loading button', async () => { - const tree = ( - await render()).toJSON(); expect(tree).toMatchSnapshot(); }); it('renders disabled button', async () => { const tree = ( - await render() ).toJSON(); expect(tree).toMatchSnapshot(); }); it('renders disabled button if there is no touch handler passed', async () => { - await render(); expect(screen.getByTestId('disabled-button')).toBeDisabled(); }); it('renders active button if only onLongPress handler is passed', async () => { await render( - ); expect(screen.getByTestId('active-button')).toBeEnabled(); @@ -148,7 +148,7 @@ it('renders active button if only onLongPress handler is passed', async () => { it('renders button with color', async () => { const tree = ( - await render() ).toJSON(); expect(tree).toMatchSnapshot(); @@ -156,7 +156,7 @@ it('renders button with color', async () => { it('renders button with button color', async () => { const tree = ( - await render() ).toJSON(); expect(tree).toMatchSnapshot(); @@ -165,7 +165,7 @@ it('renders button with button color', async () => { it('renders button with custom testID', async () => { const tree = ( await render( - ) ).toJSON(); @@ -175,10 +175,9 @@ it('renders button with custom testID', async () => { it('renders button with an accessibility label', async () => { const tree = ( await render( - ) ).toJSON(); @@ -188,10 +187,7 @@ it('renders button with an accessibility label', async () => { it('renders button with an accessibility hint', async () => { const tree = ( await render( - ) ).toJSON(); @@ -200,11 +196,9 @@ it('renders button with an accessibility hint', async () => { it('renders button with custom border radius', async () => { await render( - ); expect(screen.getByTestId('custom-radius-container')).toHaveStyle( @@ -219,8 +213,9 @@ it('renders outlined button with custom border radius', async () => { mode={'outlined'} testID="custom-radius" style={styles.customRadius} - label="Custom radius" - /> + > + Custom radius + ); expect(screen.getByTestId('custom-radius-container')).toHaveStyle( @@ -236,11 +231,9 @@ it('renders outlined button with custom border radius', async () => { it('renders button without border radius', async () => { await render( - ); expect(screen.getByTestId('custom-radius-container')).toHaveStyle( @@ -254,7 +247,9 @@ it('should execute onPressIn', async () => { const onPress = jest.fn(); await render( - ); await fireEvent(screen.getByTestId('button'), 'onPressIn'); expect(onPressInMock).toHaveBeenCalledTimes(1); @@ -265,61 +260,33 @@ it('should execute onPressOut', async () => { const onPress = jest.fn(); await render( - ); await fireEvent(screen.getByTestId('button'), 'onPressOut'); expect(onPressOutMock).toHaveBeenCalledTimes(1); }); -describe('label prop', () => { - it('renders the label text', async () => { - await render( ); - expect(screen.getByTestId('button-text')).toHaveTextContent('From label'); - }); -}); - -describe('deprecated children prop', () => { - it('still renders the children as the label', async () => { - const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); - await render(); - - expect(screen.getByTestId('button-text')).toHaveTextContent('Legacy label'); - warn.mockRestore(); - }); - - it('warns about the deprecation', async () => { - const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); - await render(); - - expect(warn).toHaveBeenCalledWith( - expect.stringContaining('`children` prop is deprecated') - ); - warn.mockRestore(); - }); -}); - -describe('button text styles', () => { - it('applies uppercase styles if uppercase prop is truthy', async () => { - await render( + ); expect(screen.getByTestId('button-text')).not.toHaveStyle({ textTransform: 'uppercase', @@ -330,13 +297,9 @@ describe('button text styles', () => { describe('button icon styles', () => { it('should return correct icon styles for compact text button', async () => { await render( - ); expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ marginLeft: 6, @@ -352,8 +315,7 @@ describe('button icon styles', () => { compact icon="camera" testID="compact-button" - label={`Compact ${mode} button`} - /> + >{`Compact ${mode} button`} ); expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ marginLeft: 8, @@ -364,12 +326,9 @@ describe('button icon styles', () => { it('should return correct icon styles for text button', async () => { await render( - ); expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ marginLeft: 12, @@ -384,8 +343,7 @@ describe('button icon styles', () => { mode={mode} icon="camera" testID="compact-button" - label={`${mode} button`} - /> + >{`${mode} button`} ); expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ marginLeft: 16, @@ -398,7 +356,9 @@ describe('button icon styles', () => { describe('icon position', () => { it('places the icon before the label by default', async () => { await render( - ); expect(screen.getByTestId('button-icon-container')).toHaveStyle({ @@ -414,8 +374,9 @@ describe('icon position', () => { mode="outlined" icon="chevron-right" iconPosition="trailing" - label="Next" - /> + > + Next + ); expect(screen.getByTestId('button-icon-container')).toHaveStyle({ @@ -432,8 +393,9 @@ describe('icon position', () => { mode="outlined" icon="chevron-right" contentStyle={styles.flexing} - label="Next" - /> + > + Next + ); expect(screen.getByTestId('button-icon-container')).toHaveStyle({ @@ -914,7 +876,11 @@ describe('getButtonSizeStyle', () => { describe('size prop', () => { it('renders a button with per-size metrics', async () => { const tree = ( - await render( + ) ).toJSON(); expect(tree).toMatchSnapshot(); @@ -930,7 +896,11 @@ describe('size prop', () => { ] as const ).forEach(([size, expectedFontSize]) => it(`applies the ${size} typescale to the label`, async () => { - await render( + ); expect(screen.getByTestId('button-text')).toHaveStyle({ fontSize: expectedFontSize, }); @@ -940,7 +910,11 @@ describe('size prop', () => { describe('accessible touch target', () => { it('expands extra-small buttons to the 48dp minimum target', async () => { - await render( + ); // (48 - 32) / 2 = 8 // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. expect(screen.getByTestId('button').props.hitSlop).toMatchObject({ @@ -950,7 +924,11 @@ describe('accessible touch target', () => { }); it('expands small buttons to the 48dp minimum target', async () => { - await render( + ); // (48 - 40) / 2 = 4 // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. expect(screen.getByTestId('button').props.hitSlop).toMatchObject({ @@ -960,19 +938,20 @@ describe('accessible touch target', () => { }); it('does not add hitSlop for buttons already at least 48dp tall', async () => { - await render( + ); // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. expect(screen.getByTestId('button').props.hitSlop).toBeUndefined(); }); it('keeps a user-supplied hitSlop axis while filling the rest', async () => { await render( - ); // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. expect(screen.getByTestId('button').props.hitSlop).toMatchObject({ @@ -1011,14 +990,22 @@ describe('getButtonShapeRadius', () => { describe('shape prop', () => { it('applies the round (full-pill) radius', async () => { - await render( + ); expect(screen.getByTestId('button-container')).toHaveStyle({ borderRadius: 9999, }); }); it('applies the square radius (default size)', async () => { - await render( + ); expect(screen.getByTestId('button-container')).toHaveStyle({ borderRadius: 12, }); @@ -1026,7 +1013,9 @@ describe('shape prop', () => { it('uses the per-size square radius when both size and shape are set', async () => { await render( - ); expect(screen.getByTestId('button-container')).toHaveStyle({ borderRadius: 28, @@ -1035,12 +1024,9 @@ describe('shape prop', () => { it('lets an explicit borderRadius in `style` override the shape', async () => { await render( - ); expect(screen.getByTestId('button-container')).toHaveStyle({ borderRadius: 4, @@ -1051,7 +1037,9 @@ describe('shape prop', () => { describe('selected prop', () => { it('marks the button as selected for screen readers', async () => { await render( - ); expect(screen.getByTestId('button')).toBeSelected(); @@ -1059,7 +1047,9 @@ describe('selected prop', () => { it('flips a round button into the square radius when selected', async () => { await render( - ); expect(screen.getByTestId('button-container')).toHaveStyle({ @@ -1068,7 +1058,11 @@ describe('selected prop', () => { }); it('flips a square button into the round radius when selected', async () => { - await render( + ); expect(screen.getByTestId('button-container')).toHaveStyle({ borderRadius: 9999, @@ -1125,8 +1119,9 @@ it('animated value changes correctly', async () => { compact icon="camera" style={[{ transform: [{ scale: value }] }]} - label="Compact button" - /> + > + Compact button + ); expect(screen.getByTestId('button-container-outer-layer')).toHaveStyle({ transform: [{ scale: 1 }], @@ -1153,7 +1148,9 @@ describe('shape morph animation', () => { it('springs the corner radius to corner.small on press in', async () => { const spy = jest.spyOn(Animated, 'spring'); await render( - ); spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); @@ -1164,7 +1161,9 @@ describe('shape morph animation', () => { it('springs the corner radius back to the resting pill radius on press out', async () => { const spy = jest.spyOn(Animated, 'spring'); await render( - ); spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressOut'); @@ -1176,7 +1175,9 @@ describe('shape morph animation', () => { it('animates between round and square radii when toggled (no spring on mount)', async () => { const spy = jest.spyOn(Animated, 'spring'); await render( - ); // Mount snaps to the resting radius — no spring. expect(spy).not.toHaveBeenCalled(); @@ -1187,7 +1188,9 @@ describe('shape morph animation', () => { selected onPress={() => {}} testID="button" - /> + > + {null} + ); // selected flips square -> round; large round resting radius = 96 / 2 = 48 expect(lastSpringToValue(spy)).toContain(48); @@ -1196,13 +1199,19 @@ describe('shape morph animation', () => { it('does not morph legacy or size-only buttons', async () => { const spy = jest.spyOn(Animated, 'spring'); - await render( + ); spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); expect(spy).not.toHaveBeenCalled(); await screen.rerender( - ); spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); @@ -1219,7 +1228,9 @@ describe('shape morph animation', () => { style={styles.overrideRadius} onPress={() => {}} testID="button" - /> + > + {null} + ); spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); @@ -1232,7 +1243,9 @@ describe('shape morph animation', () => { it('applies the pressed corner radius to the surface', async () => { await render( - ); await fireEvent(screen.getByTestId('button'), 'onPressIn'); await act(() => { diff --git a/src/components/__tests__/Card/Card.test.tsx b/src/components/__tests__/Card/Card.test.tsx index 41b3c6346e..8d6b8c3b75 100644 --- a/src/components/__tests__/Card/Card.test.tsx +++ b/src/components/__tests__/Card/Card.test.tsx @@ -130,7 +130,7 @@ describe('CardActions', () => { await render( - ); @@ -149,8 +149,9 @@ describe('CardActions', () => { testID="card-actions-button" mode="filled" style={styles.customBorderRadius} - label="Agree" - /> + > + Agree + ); diff --git a/src/components/__tests__/Dialog.test.tsx b/src/components/__tests__/Dialog.test.tsx index bd4fb17b73..32477e5fe1 100644 --- a/src/components/__tests__/Dialog.test.tsx +++ b/src/components/__tests__/Dialog.test.tsx @@ -113,8 +113,8 @@ describe('DialogActions', () => { it('should render passed children', async () => { await render( - + ); @@ -125,8 +125,8 @@ describe('DialogActions', () => { it('should apply default styles', async () => { await render( - + ); @@ -144,8 +144,8 @@ describe('DialogActions', () => { it('should apply custom styles', async () => { await render( - + ); diff --git a/src/components/__tests__/Menu.test.tsx b/src/components/__tests__/Menu.test.tsx index a7dcdca88a..bd14689f70 100644 --- a/src/components/__tests__/Menu.test.tsx +++ b/src/components/__tests__/Menu.test.tsx @@ -24,7 +24,7 @@ it('renders visible menu', async () => { } + anchor={} > @@ -43,7 +43,7 @@ it('renders not visible menu', async () => { } + anchor={} > @@ -62,7 +62,7 @@ it('renders menu with content styles', async () => { } + anchor={} contentStyle={styles.contentStyle} > @@ -86,7 +86,7 @@ elevations.forEach((elevation) => } + anchor={} elevation={elevation} > @@ -118,7 +118,11 @@ it('uses the default anchorPosition of top', async () => { } + anchor={ + + } contentStyle={styles.contentStyle} > @@ -170,7 +174,11 @@ it('respects anchorPosition bottom', async () => { } + anchor={ + + } anchorPosition="bottom" contentStyle={styles.contentStyle} > @@ -209,7 +217,7 @@ it('animated value changes correctly', async () => { } + anchor={} testID="menu" contentStyle={[{ transform: [{ scale: value }] }]} > @@ -241,7 +249,7 @@ it('renders menu with mode "elevated"', async () => { } + anchor={} mode="elevated" > @@ -266,7 +274,7 @@ it('renders menu with mode "flat"', async () => { } + anchor={} mode="flat" > From 06b6bc1714ce268292387ba585fbbf14f6841eae Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Mon, 31 Aug 2026 16:31:04 +0200 Subject: [PATCH 24/66] refactor: default to the small size and drop the unset size --- example/src/Examples/ButtonExample.tsx | 40 +-- src/components/Button/Button.tsx | 143 +++------ src/components/Button/tokens.ts | 7 - src/components/Button/utils.tsx | 54 +--- src/components/__tests__/Button.test.tsx | 119 ++------ .../__snapshots__/Banner.test.tsx.snap | 80 +++-- .../__snapshots__/Button.test.tsx.snap | 277 ++++++++++-------- .../__snapshots__/DataTable.test.tsx.snap | 25 +- .../__snapshots__/Menu.test.tsx.snap | 57 ++-- .../__snapshots__/Snackbar.test.tsx.snap | 18 +- 10 files changed, 348 insertions(+), 472 deletions(-) diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index fad8e6068a..7524a3d182 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -6,26 +6,19 @@ import { Button, Chip, List, Switch, Text, useTheme } from 'react-native-paper'; import ScreenWrapper from '../ScreenWrapper'; type Mode = 'text' | 'outlined' | 'elevated' | 'filled' | 'tonal'; -type SizeOption = - | 'unset' - | 'extra-small' - | 'small' - | 'medium' - | 'large' - | 'extra-large'; -type ShapeOption = 'unset' | 'round' | 'square'; +type Size = 'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'; +type Shape = 'round' | 'square'; type IconPosition = 'leading' | 'trailing'; const MODES: Mode[] = ['filled', 'tonal', 'elevated', 'outlined', 'text']; -const SIZES: SizeOption[] = [ - 'unset', +const SIZES: Size[] = [ 'extra-small', 'small', 'medium', 'large', 'extra-large', ]; -const SHAPES: ShapeOption[] = ['unset', 'round', 'square']; +const SHAPES: Shape[] = ['round', 'square']; const ICON_POSITIONS: IconPosition[] = ['leading', 'trailing']; function OptionRow({ @@ -84,8 +77,8 @@ const ButtonExample = () => { // Playground state. const [mode, setMode] = React.useState('filled'); - const [size, setSize] = React.useState('unset'); - const [shape, setShape] = React.useState('unset'); + const [size, setSize] = React.useState('small'); + const [shape, setShape] = React.useState('round'); const [iconPosition, setIconPosition] = React.useState('leading'); const [showIcon, setShowIcon] = React.useState(false); @@ -107,16 +100,14 @@ const ButtonExample = () => { ); - const ltrIconStyle = StyleSheet.flatten( - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - screen.getByTestId('button-icon-container').props.style - ); + expect(screen.getByTestId('button-content')).toHaveStyle({ + flexDirection: 'row', + }); await render( @@ -106,14 +105,10 @@ it('swaps the icon to the trailing edge under RTL', async () => { ); - const rtlIconStyle = StyleSheet.flatten( - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - screen.getByTestId('button-icon-container').props.style - ); - - // The physical margins swap so a "leading" icon sits on the right in RTL. - expect(rtlIconStyle.marginLeft).toBe(ltrIconStyle.marginRight); - expect(rtlIconStyle.marginRight).toBe(ltrIconStyle.marginLeft); + // The content direction flips, so a "leading" icon sits on the right in RTL. + expect(screen.getByTestId('button-content')).toHaveStyle({ + flexDirection: 'row-reverse', + }); }); it('renders loading button', async () => { @@ -294,65 +289,6 @@ describe('button text styles', () => { }); }); -describe('button icon styles', () => { - it('should return correct icon styles for compact text button', async () => { - await render( - - ); - expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ - marginLeft: 6, - marginRight: 0, - }); - }); - - (['outlined', 'filled', 'tonal', 'elevated'] as const).forEach((mode) => - it(`should return correct icon styles for compact ${mode} button`, async () => { - await render( - - ); - expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ - marginLeft: 8, - marginRight: 0, - }); - }) - ); - - it('should return correct icon styles for text button', async () => { - await render( - - ); - expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ - marginLeft: 12, - marginRight: -8, - }); - }); - - (['outlined', 'filled', 'tonal', 'elevated'] as const).forEach((mode) => - it(`should return correct icon styles for compact ${mode} button`, async () => { - await render( - - ); - expect(screen.getByTestId('compact-button-icon-container')).toHaveStyle({ - marginLeft: 16, - marginRight: -8, - }); - }) - ); -}); - describe('icon position', () => { it('places the icon before the label by default', async () => { await render( @@ -361,9 +297,8 @@ describe('icon position', () => { ); - expect(screen.getByTestId('button-icon-container')).toHaveStyle({ - marginLeft: 16, - marginRight: -8, + expect(screen.getByTestId('button-content')).toHaveStyle({ + flexDirection: 'row', }); }); @@ -379,9 +314,8 @@ describe('icon position', () => { ); - expect(screen.getByTestId('button-icon-container')).toHaveStyle({ - marginLeft: -8, - marginRight: 16, + expect(screen.getByTestId('button-content')).toHaveStyle({ + flexDirection: 'row-reverse', }); }); @@ -398,9 +332,8 @@ describe('icon position', () => { ); - expect(screen.getByTestId('button-icon-container')).toHaveStyle({ - marginLeft: -8, - marginRight: 16, + expect(screen.getByTestId('button-content')).toHaveStyle({ + flexDirection: 'row-reverse', }); expect(warn).toHaveBeenCalledWith( expect.stringContaining('`contentStyle`') @@ -980,12 +913,6 @@ describe('getButtonShapeRadius', () => { ); } ); - - it('falls back to default radii when size is omitted', () => { - const theme = getTheme(); - expect(getButtonShapeRadius({ shape: 'round', theme })).toBe(9999); - expect(getButtonShapeRadius({ shape: 'square', theme })).toBe(12); - }); }); describe('shape prop', () => { @@ -995,8 +922,9 @@ describe('shape prop', () => { X ); + // Half the small container height (40dp) is the real pill radius. expect(screen.getByTestId('button-container')).toHaveStyle({ - borderRadius: 9999, + borderRadius: 20, }); }); @@ -1065,7 +993,7 @@ describe('selected prop', () => { ); expect(screen.getByTestId('button-container')).toHaveStyle({ - borderRadius: 9999, + borderRadius: 20, }); }); @@ -1197,25 +1125,16 @@ describe('shape morph animation', () => { spy.mockRestore(); }); - it('does not morph legacy or size-only buttons', async () => { + it('morphs a default button, with no size or shape passed', async () => { const spy = jest.spyOn(Animated, 'spring'); await render( ); spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); - expect(spy).not.toHaveBeenCalled(); - - await screen.rerender( - - ); - spy.mockClear(); - await fireEvent(screen.getByTestId('button'), 'onPressIn'); - expect(spy).not.toHaveBeenCalled(); + expect(lastSpringToValue(spy)).toContain(getTheme().shapes.corner.small); spy.mockRestore(); }); diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index 3af59e3dea..795fe25f34 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -178,6 +178,12 @@ exports[`render visible banner, with custom theme 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -209,12 +215,18 @@ exports[`render visible banner, with custom theme 1`] = ` "justifyContent": "center", }, false, + { + "gap": 8, + "minHeight": 40, + "paddingHorizontal": 16, + }, { "opacity": 1, }, undefined, ] } + testID="button-content" > @@ -1958,15 +2005,8 @@ exports[`renders loading button 1`] = ` }, [ { - "marginHorizontal": 16, - "marginVertical": 9, "textAlign": "center", }, - { - "marginHorizontal": 16, - "marginVertical": 10, - }, - undefined, false, { "color": "rgba(255, 255, 255, 1)", @@ -2052,6 +2092,12 @@ exports[`renders outlined button with mode 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -2083,12 +2129,18 @@ exports[`renders outlined button with mode 1`] = ` "justifyContent": "center", }, false, + { + "gap": 8, + "minHeight": 40, + "paddingHorizontal": 16, + }, { "opacity": 1, }, undefined, ] } + testID="button-content" > Date: Mon, 31 Aug 2026 16:43:23 +0200 Subject: [PATCH 25/66] refactor: remove uppercase, compact and contentStyle row-reverse --- example/src/Examples/ButtonExample.tsx | 3 -- src/components/Banner.tsx | 1 - src/components/Button/Button.tsx | 44 +--------------- src/components/Card/CardActions.tsx | 2 - src/components/Card/utils.tsx | 1 - src/components/Dialog/DialogActions.tsx | 6 +-- src/components/Dialog/utils.ts | 5 -- src/components/Snackbar.tsx | 1 - src/components/__tests__/Button.test.tsx | 51 +------------------ .../__snapshots__/Banner.test.tsx.snap | 12 ++--- .../__snapshots__/Button.test.tsx.snap | 18 +------ .../__snapshots__/DataTable.test.tsx.snap | 1 - .../__snapshots__/Menu.test.tsx.snap | 3 -- .../__snapshots__/Snackbar.test.tsx.snap | 1 - 14 files changed, 10 insertions(+), 139 deletions(-) diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index 7524a3d182..dd4c1f3272 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -85,7 +85,6 @@ const ButtonExample = () => { const [disabled, setDisabled] = React.useState(false); const [loading, setLoading] = React.useState(false); const [selected, setSelected] = React.useState(false); - const [compact, setCompact] = React.useState(false); // Selected state for the static toggle showcase below. const [selectedToggles, setSelectedToggles] = React.useState< @@ -107,7 +106,6 @@ const ButtonExample = () => { disabled={disabled} loading={loading} selected={selected} - compact={compact} onPress={() => {}} > Play me @@ -157,7 +155,6 @@ const ButtonExample = () => { value={selected} onValueChange={setSelected} /> - diff --git a/src/components/Banner.tsx b/src/components/Banner.tsx index f994396931..9d9c1a912f 100644 --- a/src/components/Banner.tsx +++ b/src/components/Banner.tsx @@ -243,7 +243,6 @@ const Banner = ({ {actions.map(({ label, ...others }, i) => ( - ); - - expect(screen.getByTestId('button-text')).toHaveStyle({ - textTransform: 'uppercase', - }); - }); - - it('does not apply uppercase styles if uppercase prop is falsy', async () => { - await render( - - ); - - expect(screen.getByTestId('button-text')).not.toHaveStyle({ - textTransform: 'uppercase', - }); - }); -}); - describe('icon position', () => { it('places the icon before the label by default', async () => { await render( @@ -318,28 +292,6 @@ describe('icon position', () => { flexDirection: 'row-reverse', }); }); - - it('still flips the icon via the deprecated contentStyle row-reverse and warns', async () => { - const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); - await render( - - ); - - expect(screen.getByTestId('button-content')).toHaveStyle({ - flexDirection: 'row-reverse', - }); - expect(warn).toHaveBeenCalledWith( - expect.stringContaining('`contentStyle`') - ); - warn.mockRestore(); - }); }); describe('getButtonColors - background color', () => { @@ -1044,11 +996,10 @@ it('animated value changes correctly', async () => { await render( ); expect(screen.getByTestId('button-container-outer-layer')).toHaveStyle({ diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index 795fe25f34..dfb6484fc5 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -145,7 +145,7 @@ exports[`render visible banner, with custom theme 1`] = ` "borderStyle": "solid", "borderWidth": 0, "flex": undefined, - "minWidth": "auto", + "minWidth": 64, "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { "height": 0, @@ -252,7 +252,6 @@ exports[`render visible banner, with custom theme 1`] = ` { "textAlign": "center", }, - false, { "color": "#043", "fontFamily": "System", @@ -583,7 +582,7 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` "borderStyle": "solid", "borderWidth": 0, "flex": undefined, - "minWidth": "auto", + "minWidth": 64, "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { "height": 0, @@ -690,7 +689,6 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(103, 80, 164, 1)", "fontFamily": "System", @@ -864,7 +862,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderStyle": "solid", "borderWidth": 0, "flex": undefined, - "minWidth": "auto", + "minWidth": 64, "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { "height": 0, @@ -971,7 +969,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(103, 80, 164, 1)", "fontFamily": "System", @@ -1021,7 +1018,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderStyle": "solid", "borderWidth": 0, "flex": undefined, - "minWidth": "auto", + "minWidth": 64, "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { "height": 0, @@ -1128,7 +1125,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(103, 80, 164, 1)", "fontFamily": "System", diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index 440f58a9e0..14f1be7ba8 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -136,7 +136,6 @@ exports[`renders button with an accessibility hint 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -296,7 +295,6 @@ exports[`renders button with an accessibility label 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -455,7 +453,6 @@ exports[`renders button with button color 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -614,7 +611,6 @@ exports[`renders button with color 1`] = ` { "textAlign": "center", }, - false, { "color": "#e91e63", "fontFamily": "System", @@ -773,7 +769,6 @@ exports[`renders button with custom testID 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -965,7 +960,6 @@ exports[`renders button with icon 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -1086,9 +1080,7 @@ exports[`renders button with icon in reverse order 1`] = ` "flexDirection": "row", "justifyContent": "center", }, - { - "flexDirection": "row-reverse", - }, + false, { "gap": 8, "minHeight": 40, @@ -1161,7 +1153,6 @@ exports[`renders button with icon in reverse order 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -1339,7 +1330,6 @@ exports[`renders disabled button 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(29, 27, 32, 1)", "fontFamily": "System", @@ -1498,7 +1488,6 @@ exports[`renders filled button by default 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -1657,7 +1646,6 @@ exports[`renders filled button with mode 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -2007,7 +1995,6 @@ exports[`renders loading button 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", @@ -2166,7 +2153,6 @@ exports[`renders outlined button with mode 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(73, 69, 79, 1)", "fontFamily": "System", @@ -2325,7 +2311,6 @@ exports[`renders text button with mode 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(103, 80, 164, 1)", "fontFamily": "System", @@ -2511,7 +2496,6 @@ exports[`size prop renders a button with per-size metrics 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(255, 255, 255, 1)", "fontFamily": "System", diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 5e747cea61..68d378fff1 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1852,7 +1852,6 @@ exports[`DataTable.Pagination renders data table pagination with options select { "textAlign": "center", }, - false, { "color": "rgba(73, 69, 79, 1)", "fontFamily": "System", diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index f5f232ff1e..ab34c2d68a 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -148,7 +148,6 @@ exports[`renders menu with content styles 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(73, 69, 79, 1)", "fontFamily": "System", @@ -717,7 +716,6 @@ exports[`renders not visible menu 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(73, 69, 79, 1)", "fontFamily": "System", @@ -891,7 +889,6 @@ exports[`renders visible menu 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(73, 69, 79, 1)", "fontFamily": "System", diff --git a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap index 0d90c3305b..83450c600f 100644 --- a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap @@ -454,7 +454,6 @@ exports[`renders snackbar with action button 1`] = ` { "textAlign": "center", }, - false, { "color": "rgba(208, 188, 255, 1)", "fontFamily": "System", From 5debe50514bba92a69420bf382a4dcec582e348f Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Mon, 31 Aug 2026 16:59:32 +0200 Subject: [PATCH 26/66] refactor: source shape and spacing from component tokens --- src/components/Button/Button.tsx | 31 +++++--- src/components/Button/tokens.ts | 31 +++----- src/components/Button/utils.tsx | 70 +++++++++++++------ src/components/__tests__/Button.test.tsx | 29 +++++--- .../__snapshots__/Banner.test.tsx.snap | 12 ++-- .../__snapshots__/Button.test.tsx.snap | 42 +++++++---- .../__snapshots__/DataTable.test.tsx.snap | 3 +- .../__snapshots__/Menu.test.tsx.snap | 9 ++- .../__snapshots__/Snackbar.test.tsx.snap | 3 +- 9 files changed, 148 insertions(+), 82 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 50d40a5bd7..ce8816484a 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -12,9 +12,11 @@ import type { import { getButtonColors, + getButtonPressedRadius, getButtonRippleColor, getButtonShapeRadius, getButtonSizeStyle, + getEffectiveButtonShape, getButtonTouchableRippleStyle, } from './utils'; import type { ButtonMode, ButtonShape, ButtonSize } from './utils'; @@ -283,15 +285,11 @@ const Button = ({ // When the button is `selected`, flip the requested shape so the // unselected/selected pair contrasts visually (round ↔ square). - const effectiveShape: ButtonShape = selected - ? shape === 'round' - ? 'square' - : 'round' - : shape; + const effectiveShape = getEffectiveButtonShape(shape, selected); const sizeStyle = React.useMemo(() => getButtonSizeStyle(size), [size]); - // Shape morph: animate the corner on press (→ corner.small) and on the - // `selected`/shape toggle. Skipped when the user pins a radius via `style`. + // Shape morph: animate the corner on press (→ the pressed shape token) and on + // the `selected`/shape toggle. Skipped when the user pins a radius via `style`. const hasPinnedRadius = Object.keys(borderRadiusStyles).length > 0; const animateShape = !hasPinnedRadius; // `round` resolves to the `cornerFull` sentinel, so use the real pill radius @@ -300,8 +298,8 @@ const Button = ({ const restingRadius = effectiveShape === 'round' ? sizeStyle.minHeight / 2 - : getButtonShapeRadius({ size, shape: effectiveShape, theme }); - const pressedRadius = theme.shapes.corner.small; + : getButtonShapeRadius({ size, shape, theme, selected }); + const pressedRadius = getButtonPressedRadius({ size, theme }); const { current: animatedRadius } = React.useRef( new Animated.Value(restingRadius) ); @@ -415,11 +413,21 @@ const Button = ({ customLabelColor, theme, mode, + size, disabled, dark, selected, }), - [customButtonColor, customLabelColor, theme, mode, disabled, dark, selected] + [ + customButtonColor, + customLabelColor, + theme, + mode, + size, + disabled, + dark, + selected, + ] ); const rippleColor = React.useMemo( @@ -559,7 +567,8 @@ const Button = ({ isTrailingIcon && styles.contentReverse, { minHeight: sizeStyle.minHeight, - paddingHorizontal: sizeStyle.paddingHorizontal, + paddingStart: sizeStyle.paddingStart, + paddingEnd: sizeStyle.paddingEnd, gap: sizeStyle.iconGap, }, { opacity: labelOpacity }, diff --git a/src/components/Button/tokens.ts b/src/components/Button/tokens.ts index 60aa194ea0..a6338d8e85 100644 --- a/src/components/Button/tokens.ts +++ b/src/components/Button/tokens.ts @@ -1,16 +1,5 @@ import type { ButtonLabelVariant, ButtonSize } from './utils'; - -/** - * Shape keys for the button container corners. `'full'` resolves to the - * full-pill radius (`cornerFull`); every other key resolves against - * `theme.shapes.corner[key]`. Mirrors MD3 `ShapeKeyTokens`. - */ -export type ButtonCornerKey = - | 'full' - | 'small' - | 'medium' - | 'large' - | 'extraLarge'; +import type { ShapeToken } from '../../theme/utils/shape'; /** * Per-size component tokens for the Material Design 3 (expressive) button @@ -18,7 +7,7 @@ export type ButtonCornerKey = * these here keeps every size-specific metric in one place and references the * theme shape tokens instead of magic numbers for the corner radii. */ -export type ButtonSizeTokens = { +type ButtonSizeTokens = { containerHeight: number; iconSize: number; iconLabelSpace: number; @@ -26,15 +15,15 @@ export type ButtonSizeTokens = { trailingSpace: number; outlinedOutlineWidth: number; labelVariant: ButtonLabelVariant; - containerShapeRound: ButtonCornerKey; - containerShapeSquare: ButtonCornerKey; + containerShapeRound: ShapeToken; + containerShapeSquare: ShapeToken; /** Corner the container morphs to while pressed (MD3: always `small`). */ - pressedContainerShape: ButtonCornerKey; - selectedContainerShapeRound: ButtonCornerKey; - selectedContainerShapeSquare: ButtonCornerKey; + pressedContainerShape: ShapeToken; + selectedContainerShapeRound: ShapeToken; + selectedContainerShapeSquare: ShapeToken; }; -export const buttonSizeTokens: Record = { +const sizes = { 'extra-small': { containerHeight: 32, iconSize: 20, @@ -105,4 +94,6 @@ export const buttonSizeTokens: Record = { selectedContainerShapeRound: 'full', selectedContainerShapeSquare: 'extraLarge', }, -}; +} as const satisfies Record; + +export const Tokens = { sizes }; diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 9780657b6f..36f594b3ad 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -2,10 +2,10 @@ import type { ColorValue, ViewStyle } from 'react-native'; import color from 'color'; -import { buttonSizeTokens, type ButtonCornerKey } from './tokens'; +import { Tokens } from './tokens'; import { black, white } from '../../theme/colors'; import { tokens } from '../../theme/tokens'; -import { cornerFull } from '../../theme/tokens/sys/shape'; +import { resolveCornerRadius } from '../../theme/utils/shape'; import type { InternalTheme } from '../../types'; import { splitStyles } from '../../utils/splitStyles'; @@ -30,9 +30,11 @@ export type ButtonLabelVariant = export type ButtonSizeStyle = { minHeight: number; - paddingHorizontal: number; + paddingStart: number; + paddingEnd: number; iconSize: number; iconGap: number; + outlineWidth: number; labelVariant: ButtonLabelVariant; }; @@ -41,12 +43,14 @@ export type ButtonSizeStyle = { * from the component tokens. */ export const getButtonSizeStyle = (size: ButtonSize): ButtonSizeStyle => { - const t = buttonSizeTokens[size]; + const t = Tokens.sizes[size]; return { minHeight: t.containerHeight, - paddingHorizontal: t.leadingSpace, + paddingStart: t.leadingSpace, + paddingEnd: t.trailingSpace, iconSize: t.iconSize, iconGap: t.iconLabelSpace, + outlineWidth: t.outlinedOutlineWidth, labelVariant: t.labelVariant, }; }; @@ -54,35 +58,58 @@ export const getButtonSizeStyle = (size: ButtonSize): ButtonSizeStyle => { export type ButtonShape = 'round' | 'square'; /** - * Resolves a shape key against the theme: `'full'` is the full-pill radius; - * every other key maps to `theme.shapes.corner[key]`. + * A selected toggle contrasts with its unselected state by flipping the shape, + * so a selected `round` button renders square and vice versa. */ -export const resolveButtonCorner = ( - theme: InternalTheme, - key: ButtonCornerKey -): number => (key === 'full' ? cornerFull : theme.shapes.corner[key]); +export const getEffectiveButtonShape = ( + shape: ButtonShape, + selected?: boolean +): ButtonShape => { + if (!selected) { + return shape; + } + return shape === 'round' ? 'square' : 'round'; +}; /** * Corner radius for the requested shape, read from the component tokens and * resolved against the theme shape tokens. `round` is the full-pill radius; - * `square` uses a per-size smaller corner. + * `square` uses a per-size smaller corner. A selected button resolves against + * the `selectedContainerShape*` token pair, after the shape flip above. */ export const getButtonShapeRadius = ({ size, shape, theme, + selected, }: { size: ButtonSize; shape: ButtonShape; theme: InternalTheme; + selected?: boolean; }): number => { - const key = - shape === 'round' - ? buttonSizeTokens[size].containerShapeRound - : buttonSizeTokens[size].containerShapeSquare; - return resolveButtonCorner(theme, key); + const t = Tokens.sizes[size]; + const token = + getEffectiveButtonShape(shape, selected) === 'round' + ? selected + ? t.selectedContainerShapeRound + : t.containerShapeRound + : selected + ? t.selectedContainerShapeSquare + : t.containerShapeSquare; + return resolveCornerRadius(theme, token); }; +/** Corner the container morphs to while pressed. */ +export const getButtonPressedRadius = ({ + size, + theme, +}: { + size: ButtonSize; + theme: InternalTheme; +}): number => + resolveCornerRadius(theme, Tokens.sizes[size].pressedContainerShape); + type BaseProps = { isMode: (mode: ButtonMode) => boolean; theme: InternalTheme; @@ -220,12 +247,13 @@ const getButtonBorderColor = ({ isMode, theme, selected }: BaseProps) => { const getButtonBorderWidth = ({ isMode, selected, -}: Omit) => { + size, +}: Omit & { size: ButtonSize }) => { if (selected && isMode('outlined')) { return 0; } if (isMode('outlined')) { - return 1; + return Tokens.sizes[size].outlinedOutlineWidth; } return 0; @@ -234,6 +262,7 @@ const getButtonBorderWidth = ({ export const getButtonColors = ({ theme, mode, + size = 'small', customButtonColor, customLabelColor, disabled, @@ -242,6 +271,7 @@ export const getButtonColors = ({ }: { theme: InternalTheme; mode: ButtonMode; + size?: ButtonSize; customButtonColor?: ColorValue; customLabelColor?: ColorValue; disabled?: boolean; @@ -272,7 +302,7 @@ export const getButtonColors = ({ const borderColor = getButtonBorderColor({ isMode, theme, selected }); - const borderWidth = getButtonBorderWidth({ isMode, selected }); + const borderWidth = getButtonBorderWidth({ isMode, selected, size }); const labelOpacity = disabled ? stateOpacity.disabled : stateOpacity.enabled; diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 3beca1aff0..d392d9dcb3 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -731,27 +731,40 @@ describe('getButtonRippleColor', () => { const sizeMetrics: [ size: ButtonSize, minHeight: number, - paddingHorizontal: number, + paddingStart: number, + paddingEnd: number, iconSize: number, iconGap: number, + outlineWidth: number, labelVariant: ButtonLabelVariant, ][] = [ - ['extra-small', 32, 12, 20, 4, 'labelLarge'], - ['small', 40, 16, 20, 8, 'labelLarge'], - ['medium', 56, 24, 24, 8, 'titleMedium'], - ['large', 96, 48, 32, 12, 'headlineSmall'], - ['extra-large', 136, 64, 40, 16, 'headlineLarge'], + ['extra-small', 32, 12, 12, 20, 4, 1, 'labelLarge'], + ['small', 40, 16, 16, 20, 8, 1, 'labelLarge'], + ['medium', 56, 24, 24, 24, 8, 1, 'titleMedium'], + ['large', 96, 48, 48, 32, 12, 1, 'headlineSmall'], + ['extra-large', 136, 64, 64, 40, 16, 1, 'headlineLarge'], ]; describe('getButtonSizeStyle', () => { it.each(sizeMetrics)( 'returns expected metrics for %s', - (size, minHeight, paddingHorizontal, iconSize, iconGap, labelVariant) => { + ( + size, + minHeight, + paddingStart, + paddingEnd, + iconSize, + iconGap, + outlineWidth, + labelVariant + ) => { expect(getButtonSizeStyle(size)).toEqual({ minHeight, - paddingHorizontal, + paddingStart, + paddingEnd, iconSize, iconGap, + outlineWidth, labelVariant, }); } diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index dfb6484fc5..2ecdd41c1f 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -218,7 +218,8 @@ exports[`render visible banner, with custom theme 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -655,7 +656,8 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -935,7 +937,8 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -1091,7 +1094,8 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index 14f1be7ba8..ea92bdc461 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -102,7 +102,8 @@ exports[`renders button with an accessibility hint 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -261,7 +262,8 @@ exports[`renders button with an accessibility label 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -419,7 +421,8 @@ exports[`renders button with button color 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -577,7 +580,8 @@ exports[`renders button with color 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -735,7 +739,8 @@ exports[`renders button with custom testID 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -893,7 +898,8 @@ exports[`renders button with icon 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -1084,7 +1090,8 @@ exports[`renders button with icon in reverse order 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -1296,7 +1303,8 @@ exports[`renders disabled button 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 0.38, @@ -1454,7 +1462,8 @@ exports[`renders filled button by default 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -1612,7 +1621,8 @@ exports[`renders filled button with mode 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -1770,7 +1780,8 @@ exports[`renders loading button 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -2119,7 +2130,8 @@ exports[`renders outlined button with mode 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -2277,7 +2289,8 @@ exports[`renders text button with mode 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -2429,7 +2442,8 @@ exports[`size prop renders a button with per-size metrics 1`] = ` { "gap": 8, "minHeight": 56, - "paddingHorizontal": 24, + "paddingEnd": 24, + "paddingStart": 24, }, { "opacity": 1, diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 68d378fff1..a4271d39b1 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1785,7 +1785,8 @@ exports[`DataTable.Pagination renders data table pagination with options select { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index ab34c2d68a..559efc7571 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -114,7 +114,8 @@ exports[`renders menu with content styles 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -682,7 +683,8 @@ exports[`renders not visible menu 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, @@ -855,7 +857,8 @@ exports[`renders visible menu 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, diff --git a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap index 83450c600f..f7bb00a0f0 100644 --- a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap @@ -420,7 +420,8 @@ exports[`renders snackbar with action button 1`] = ` { "gap": 8, "minHeight": 40, - "paddingHorizontal": 16, + "paddingEnd": 16, + "paddingStart": 16, }, { "opacity": 1, From cbb4172bf762738a5e9d3242ed12fe5ce6c1b8be Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Tue, 1 Sep 2026 09:47:19 +0200 Subject: [PATCH 27/66] feat(button): add MD3 toggle colour sets --- example/src/Examples/ButtonExample.tsx | 2 +- src/components/Button/Button.tsx | 17 ++- src/components/Button/tokens.ts | 49 ++++++- src/components/Button/utils.tsx | 62 ++++++--- src/components/__tests__/Button.test.tsx | 120 ++++++++++++++---- .../__snapshots__/Button.test.tsx.snap | 2 +- .../__snapshots__/DataTable.test.tsx.snap | 2 +- .../__snapshots__/Menu.test.tsx.snap | 6 +- 8 files changed, 207 insertions(+), 53 deletions(-) diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index dd4c1f3272..e1c1cb5f01 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -229,7 +229,7 @@ const ButtonExample = () => { - {(['outlined', 'text', 'tonal'] as const).map((m) => { + {MODES.map((m) => { const key = `toggle-${m}`; const isSelected = !!selectedToggles[key]; return ( diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index ce8816484a..a12169d1f3 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -63,15 +63,18 @@ export type Props = $Omit, 'mode'> & { */ shape?: ButtonShape; /** - * Whether this button is in the selected state (Material Design 3 - * expressive toggle). When `true`: + * Turns the button into a Material Design 3 expressive toggle and sets its + * state. Leave it **undefined** for a plain button — a toggle that is merely + * unselected is a different state, and MD3 gives the two different colours. * - * - The `shape` is flipped: `'round'` becomes `'square'` and vice versa. - * - For `outlined` and `text` modes, the button adopts a filled - * `secondaryContainer` appearance (matches `tonal`). - * - `aria-selected` is set so screen readers announce the toggle state. + * When defined: * - * Other modes only flip the shape. + * - The button takes its container and label colours from the toggle set for + * its `mode`, which differ between `false` and `true`. `text` is the one + * exception: MD3 defines no text toggle, so it keeps its plain colours. + * - When `true` the `shape` is flipped: `'round'` renders square and vice + * versa, and an `outlined` button drops its outline. + * - `aria-selected` is set so screen readers announce the toggle state. */ selected?: boolean; /** diff --git a/src/components/Button/tokens.ts b/src/components/Button/tokens.ts index a6338d8e85..81cee33e6b 100644 --- a/src/components/Button/tokens.ts +++ b/src/components/Button/tokens.ts @@ -1,4 +1,5 @@ -import type { ButtonLabelVariant, ButtonSize } from './utils'; +import type { ButtonLabelVariant, ButtonMode, ButtonSize } from './utils'; +import type { ColorRole } from '../../theme/types'; import type { ShapeToken } from '../../theme/utils/shape'; /** @@ -96,4 +97,48 @@ const sizes = { }, } as const satisfies Record; -export const Tokens = { sizes }; +/** + * Modes that have a toggle variant. MD3 defines `ToggleButton`, + * `ElevatedToggleButton`, `TonalToggleButton` and `OutlinedToggleButton` — there + * is no text/plain toggle, so `text` keeps its plain colours when used as one. + */ +export type ButtonToggleMode = Exclude; + +type ToggleColorSpec = { + /** `'transparent'` where the spec leaves the container unfilled. */ + container: ColorRole | 'transparent'; + label: ColorRole; +}; + +/** + * Selected/unselected colour roles for the toggle variant, taken from the MD3 + * `{Filled,Elevated,Tonal,Outlined}ButtonTokens` `Unselected*` / `Selected*` + * sets. A toggle's unselected colours differ from the same mode's plain + * colours, which is why this table is separate from `getButtonColors`' defaults. + */ +const toggle = { + filled: { + unselected: { container: 'surfaceContainer', label: 'onSurfaceVariant' }, + selected: { container: 'primary', label: 'onPrimary' }, + }, + tonal: { + unselected: { + container: 'secondaryContainer', + label: 'onSecondaryContainer', + }, + selected: { container: 'secondary', label: 'onSecondary' }, + }, + elevated: { + unselected: { container: 'surfaceContainerLow', label: 'primary' }, + selected: { container: 'primary', label: 'onPrimary' }, + }, + outlined: { + unselected: { container: 'transparent', label: 'onSurfaceVariant' }, + selected: { container: 'inverseSurface', label: 'inverseOnSurface' }, + }, +} as const satisfies Record< + ButtonToggleMode, + { unselected: ToggleColorSpec; selected: ToggleColorSpec } +>; + +export const Tokens = { sizes, toggle }; diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 36f594b3ad..17254922de 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -3,6 +3,7 @@ import type { ColorValue, ViewStyle } from 'react-native'; import color from 'color'; import { Tokens } from './tokens'; +import type { ButtonToggleMode } from './tokens'; import { black, white } from '../../theme/colors'; import { tokens } from '../../theme/tokens'; import { resolveCornerRadius } from '../../theme/utils/shape'; @@ -117,6 +118,31 @@ type BaseProps = { selected?: boolean; }; +type ToggleColors = (typeof Tokens.toggle)[ButtonToggleMode][ + | 'selected' + | 'unselected']; + +const isToggleMode = (mode: ButtonMode): mode is ButtonToggleMode => + mode !== 'text'; + +/** + * The toggle colour pair for a mode, or `undefined` when the button is not a + * toggle (`selected` omitted) or the mode has no toggle colours of its own + * (`text`, which MD3 gives no toggle tokens). + */ +const getToggleColors = ({ + mode, + selected, +}: { + mode: ButtonMode; + selected?: boolean; +}) => { + if (!isToggleMode(mode) || selected === undefined) { + return undefined; + } + return Tokens.toggle[mode][selected ? 'selected' : 'unselected']; +}; + const isDark = ({ dark, backgroundColor, @@ -140,9 +166,10 @@ const getButtonBackgroundColor = ({ theme, disabled, customButtonColor, - selected, -}: BaseProps & { + toggleColors, +}: Omit & { customButtonColor?: ColorValue; + toggleColors?: ToggleColors; }) => { const { colors } = theme; if (customButtonColor && !disabled) { @@ -156,10 +183,9 @@ const getButtonBackgroundColor = ({ return colors.onSurface; } - // Selected toggle (only outlined/text adopt a filled "tonal-selected" look; - // filled / tonal / elevated already render filled). - if (selected && (isMode('outlined') || isMode('text'))) { - return colors.secondaryContainer; + if (toggleColors) { + const { container } = toggleColors; + return container === 'transparent' ? 'transparent' : colors[container]; } if (isMode('elevated')) { @@ -184,11 +210,12 @@ const getButtonLabelColor = ({ customLabelColor, backgroundColor, dark, - selected, -}: BaseProps & { + toggleColors, +}: Omit & { customLabelColor?: ColorValue; backgroundColor: ColorValue; dark?: boolean; + toggleColors?: ToggleColors; }) => { const { colors } = theme; if (customLabelColor && !disabled) { @@ -199,9 +226,8 @@ const getButtonLabelColor = ({ return theme.colors.onSurface; } - // Selected toggle for outlined/text mirrors the tonal label color. - if (selected && (isMode('outlined') || isMode('text'))) { - return colors.onSecondaryContainer; + if (toggleColors) { + return colors[toggleColors.label]; } if (typeof dark === 'boolean') { @@ -232,13 +258,13 @@ const getButtonLabelColor = ({ }; const getButtonBorderColor = ({ isMode, theme, selected }: BaseProps) => { - // A selected outlined toggle drops its outline (the filled background takes - // over as the visual affordance). + // A selected outlined toggle drops its outline (the filled inverse-surface + // background takes over as the visual affordance). if (selected && isMode('outlined')) { return 'transparent'; } if (isMode('outlined')) { - return theme.colors.outline; + return theme.colors.outlineVariant; } return 'transparent'; @@ -282,12 +308,16 @@ export const getButtonColors = ({ return mode === modeToCompare; }; + const toggleColors = disabled + ? undefined + : getToggleColors({ mode, selected }); + const backgroundColor = getButtonBackgroundColor({ isMode, theme, disabled, customButtonColor, - selected, + toggleColors, }); const labelColor = getButtonLabelColor({ @@ -297,7 +327,7 @@ export const getButtonColors = ({ customLabelColor, backgroundColor, dark, - selected, + toggleColors, }); const borderColor = getButtonBorderColor({ isMode, theme, selected }); diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index d392d9dcb3..88c8830ac2 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -617,7 +617,7 @@ describe('getButtonColors - border color', () => { mode: 'outlined', }) ).toMatchObject({ - borderColor: getTheme().colors.outline, + borderColor: getTheme().colors.outlineVariant, }); }); @@ -629,7 +629,7 @@ describe('getButtonColors - border color', () => { mode: 'outlined', }) ).toMatchObject({ - borderColor: getTheme(true).colors.outline, + borderColor: getTheme(true).colors.outlineVariant, }); }); @@ -640,7 +640,7 @@ describe('getButtonColors - border color', () => { mode: 'outlined', }) ).toMatchObject({ - borderColor: getTheme().colors.outline, + borderColor: getTheme().colors.outlineVariant, }); }); @@ -651,7 +651,7 @@ describe('getButtonColors - border color', () => { mode: 'outlined', }) ).toMatchObject({ - borderColor: getTheme(true).colors.outline, + borderColor: getTheme(true).colors.outlineVariant, }); }); @@ -962,7 +962,7 @@ describe('selected prop', () => { }); }); - it('gives an outlined button the tonal-selected appearance', () => { + it('drops the outline when an outlined toggle is selected', () => { expect( getButtonColors({ theme: getTheme(), @@ -970,33 +970,36 @@ describe('selected prop', () => { selected: true, }) ).toMatchObject({ - backgroundColor: getTheme().colors.secondaryContainer, - labelColor: getTheme().colors.onSecondaryContainer, borderColor: 'transparent', borderWidth: 0, }); }); - it('gives a text-mode button the tonal-selected appearance', () => { + it('keeps a text button on its plain colors, having no toggle colours', async () => { + const plain = getButtonColors({ theme: getTheme(), mode: 'text' }); + expect( - getButtonColors({ - theme: getTheme(), - mode: 'text', - selected: true, - }) - ).toMatchObject({ - backgroundColor: getTheme().colors.secondaryContainer, - labelColor: getTheme().colors.onSecondaryContainer, + getButtonColors({ theme: getTheme(), mode: 'text', selected: false }) + ).toMatchObject(plain); + expect( + getButtonColors({ theme: getTheme(), mode: 'text', selected: true }) + ).toMatchObject(plain); + + // `selected` is still honoured for the shape flip and for screen readers. + await render( + + ); + expect(screen.getByTestId('button-container')).toHaveStyle({ + borderRadius: 12, }); + expect(screen.getByTestId('button')).toBeSelected(); }); - it('does not change filled colors when selected', () => { + it('leaves a plain button untouched when `selected` is omitted', () => { expect( - getButtonColors({ - theme: getTheme(), - mode: 'filled', - selected: true, - }) + getButtonColors({ theme: getTheme(), mode: 'filled' }) ).toMatchObject({ backgroundColor: getTheme().colors.primary, labelColor: getTheme().colors.onPrimary, @@ -1004,6 +1007,79 @@ describe('selected prop', () => { }); }); +describe('toggle colors', () => { + // From the MD3 {Filled,Elevated,Tonal,Outlined}ButtonTokens Unselected*/ + // Selected* sets. + type Role = keyof ReturnType['colors']; + // `null` = the spec leaves the container unfilled. + const toggleColors: [ + mode: 'filled' | 'tonal' | 'elevated' | 'outlined', + unselectedContainer: Role | null, + unselectedLabel: Role, + selectedContainer: Role, + selectedLabel: Role, + ][] = [ + ['filled', 'surfaceContainer', 'onSurfaceVariant', 'primary', 'onPrimary'], + [ + 'tonal', + 'secondaryContainer', + 'onSecondaryContainer', + 'secondary', + 'onSecondary', + ], + ['elevated', 'surfaceContainerLow', 'primary', 'primary', 'onPrimary'], + [ + 'outlined', + null, + 'onSurfaceVariant', + 'inverseSurface', + 'inverseOnSurface', + ], + ]; + + it.each(toggleColors)( + '%s toggle uses the spec roles for both states', + (mode, uContainer, uLabel, sContainer, sLabel) => { + const theme = getTheme(); + + expect(getButtonColors({ theme, mode, selected: false })).toMatchObject({ + backgroundColor: + uContainer === null ? 'transparent' : theme.colors[uContainer], + labelColor: theme.colors[uLabel], + }); + + expect(getButtonColors({ theme, mode, selected: true })).toMatchObject({ + backgroundColor: theme.colors[sContainer], + labelColor: theme.colors[sLabel], + }); + } + ); + + it('an unselected toggle differs from the same mode as a plain button', () => { + const theme = getTheme(); + const plain = getButtonColors({ theme, mode: 'filled' }); + const unselected = getButtonColors({ + theme, + mode: 'filled', + selected: false, + }); + + expect(unselected.backgroundColor).not.toBe(plain.backgroundColor); + expect(unselected.labelColor).not.toBe(plain.labelColor); + }); + + it('ignores the toggle table when disabled', () => { + const theme = getTheme(); + + expect( + getButtonColors({ theme, mode: 'filled', selected: true, disabled: true }) + ).toMatchObject({ + backgroundColor: theme.colors.onSurface, + labelColor: theme.colors.onSurface, + }); + }); +}); + it('animated value changes correctly', async () => { const value = new Animated.Value(1); await render( diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index ea92bdc461..7930431cda 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -2052,7 +2052,7 @@ exports[`renders outlined button with mode 1`] = ` style={ { "backgroundColor": "transparent", - "borderColor": "rgba(121, 116, 126, 1)", + "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, "borderStyle": "solid", "borderWidth": 1, diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index a4271d39b1..bbaf4b972f 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1704,7 +1704,7 @@ exports[`DataTable.Pagination renders data table pagination with options select style={ { "backgroundColor": "transparent", - "borderColor": "rgba(121, 116, 126, 1)", + "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, "borderStyle": "solid", "borderWidth": 1, diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index 559efc7571..467692f7bd 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -36,7 +36,7 @@ exports[`renders menu with content styles 1`] = ` style={ { "backgroundColor": "transparent", - "borderColor": "rgba(121, 116, 126, 1)", + "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, "borderStyle": "solid", "borderWidth": 1, @@ -605,7 +605,7 @@ exports[`renders not visible menu 1`] = ` style={ { "backgroundColor": "transparent", - "borderColor": "rgba(121, 116, 126, 1)", + "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, "borderStyle": "solid", "borderWidth": 1, @@ -779,7 +779,7 @@ exports[`renders visible menu 1`] = ` style={ { "backgroundColor": "transparent", - "borderColor": "rgba(121, 116, 126, 1)", + "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, "borderStyle": "solid", "borderWidth": 1, From 84cf1bff59898afa815cb2549fb4986be383a1ae Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Tue, 1 Sep 2026 11:50:36 +0200 Subject: [PATCH 28/66] refactor: move animations to Reanimated --- src/components/Button/Button.tsx | 397 +++++---- src/components/Button/utils.tsx | 44 +- src/components/__tests__/Button.test.tsx | 128 +-- src/components/__tests__/Card/Card.test.tsx | 5 +- .../__snapshots__/Banner.test.tsx.snap | 240 +++--- .../__snapshots__/Button.test.tsx.snap | 801 +++++++++--------- .../__snapshots__/DataTable.test.tsx.snap | 60 +- .../__snapshots__/Menu.test.tsx.snap | 171 ++-- .../__snapshots__/Snackbar.test.tsx.snap | 65 +- 9 files changed, 981 insertions(+), 930 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index a12169d1f3..bede7c7811 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { Animated, Platform, StyleSheet, View } from 'react-native'; +import { Platform, StyleSheet, View } from 'react-native'; import type { ColorValue, GestureResponderEvent, @@ -7,9 +7,19 @@ import type { Role, StyleProp, TextStyle, + ViewProps, ViewStyle, } from 'react-native'; +import Reanimated, { + Easing, + ReduceMotion, + useAnimatedStyle, + useSharedValue, + withSpring, + withTiming, +} from 'react-native-reanimated'; + import { getButtonColors, getButtonPressedRadius, @@ -17,11 +27,16 @@ import { getButtonShapeRadius, getButtonSizeStyle, getEffectiveButtonShape, - getButtonTouchableRippleStyle, } from './utils'; import type { ButtonMode, ButtonShape, ButtonSize } from './utils'; import { getDefaultDirection, useLocale } from '../../core/locale'; import { useInternalTheme } from '../../core/theming'; +import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; +import { + androidElevationLevels, + shadow, + shadowLayers, +} from '../../theme/tokens/sys/elevation'; import { toRawSpring } from '../../theme/tokens/sys/motion'; import type { $Omit, ThemeProp } from '../../types'; import hasTouchHandler from '../../utils/hasTouchHandler'; @@ -29,12 +44,11 @@ import { splitStyles } from '../../utils/splitStyles'; import ActivityIndicator from '../ActivityIndicator'; import Icon from '../Icon'; import type { IconSource } from '../Icon'; -import Surface from '../Surface'; import TouchableRipple from '../TouchableRipple/TouchableRipple'; import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; import Text from '../Typography/Text'; -export type Props = $Omit, 'mode'> & { +export type Props = $Omit & { /** * Mode of the button. You can change the mode to adjust the styling to give it desired emphasis. Defaults to `filled`. * - `filled` - button with a background color, used for the most important action, has the most visual impact and high emphasis. (default) @@ -160,7 +174,7 @@ export type Props = $Omit, 'mode'> & { * Sets additional distance outside of element in which a press can be detected. */ hitSlop?: TouchableRippleProps['hitSlop']; - style?: Animated.WithAnimatedValue>; + style?: StyleProp; /** * Style for the button text. */ @@ -244,7 +258,6 @@ const Button = ({ const theme = useInternalTheme(themeOverrides); const { direction } = useLocale(); const isMode = (modeToCompare: ButtonMode) => mode === modeToCompare; - const { animation } = theme; const isWeb = Platform.OS === 'web'; const requestedTrailingIcon = iconPosition === 'trailing'; @@ -260,25 +273,29 @@ const Button = ({ onLongPress, }); + const reduceMotion = useReduceMotion(); + const reanimatedReduceMotion = reduceMotion + ? ReduceMotion.Always + : ReduceMotion.Never; + const isElevationEntitled = !disabled && isMode('elevated'); - const { current: elevation } = React.useRef( - new Animated.Value(isElevationEntitled ? initialElevation : 0) - ); + // 0 = resting, 1 = pressed. The two ends are resolved from the elevation + // tokens further down, so a `disabled` / `mode` change moves the shadow on the + // next render. + const pressProgress = useSharedValue(0); - React.useEffect(() => { - // Workaround not to call setValue on Animated.Value, because it breaks styles. - // https://github.com/callstack/react-native-paper/issues/4559 - Animated.timing(elevation, { - toValue: isElevationEntitled ? initialElevation : 0, - duration: 0, - useNativeDriver: true, - }); - }, [isElevationEntitled, elevation]); + const elevationTimingConfig = React.useMemo( + () => ({ + duration: theme.motion.duration.short4, + easing: Easing.bezier(...theme.motion.easing.standard), + reduceMotion: reanimatedReduceMotion, + }), + [theme.motion, reanimatedReduceMotion] + ); const borderRadiusStyles = React.useMemo(() => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - const flattenedStyles = (StyleSheet.flatten(style) || {}) as ViewStyle; + const flattenedStyles = StyleSheet.flatten(style) || {}; const [, radiusStyles] = splitStyles( flattenedStyles, (key) => key.startsWith('border') && key.endsWith('Radius') @@ -303,21 +320,25 @@ const Button = ({ ? sizeStyle.minHeight / 2 : getButtonShapeRadius({ size, shape, theme, selected }); const pressedRadius = getButtonPressedRadius({ size, theme }); - const { current: animatedRadius } = React.useRef( - new Animated.Value(restingRadius) - ); + const animatedRadius = useSharedValue(restingRadius); const restingRadiusRef = React.useRef(restingRadius); const isRadiusMountedRef = React.useRef(false); + // The morph stays imperative: a press can interrupt it mid-flight, which a + // duration-based transition can't express. + const radiusSpringConfig = React.useMemo( + () => ({ + ...toRawSpring(theme.motion.spring.fast.spatial), + reduceMotion: reanimatedReduceMotion, + }), + [theme.motion.spring.fast.spatial, reanimatedReduceMotion] + ); + const springRadiusTo = React.useCallback( (toValue: number) => { - Animated.spring(animatedRadius, { - toValue, - ...toRawSpring(theme.motion.spring.fast.spatial), - useNativeDriver: false, - }).start(); + animatedRadius.value = withSpring(toValue, radiusSpringConfig); }, - [animatedRadius, theme] + [animatedRadius, radiusSpringConfig] ); const handlePressIn = React.useCallback( @@ -326,14 +347,8 @@ const Button = ({ if (animateShape) { springRadiusTo(pressedRadius); } - if (mode === 'elevated') { - const { scale } = animation; - Animated.timing(elevation, { - toValue: activeElevation, - duration: 200 * scale, - useNativeDriver: - isWeb || Platform.constants.reactNativeVersion.minor <= 72, - }).start(); + if (isElevationEntitled) { + pressProgress.value = withTiming(1, elevationTimingConfig); } }, [ @@ -341,10 +356,9 @@ const Button = ({ animateShape, springRadiusTo, pressedRadius, - mode, - animation, - elevation, - isWeb, + isElevationEntitled, + pressProgress, + elevationTimingConfig, ] ); @@ -354,24 +368,17 @@ const Button = ({ if (animateShape) { springRadiusTo(restingRadiusRef.current); } - if (mode === 'elevated') { - const { scale } = animation; - Animated.timing(elevation, { - toValue: initialElevation, - duration: 150 * scale, - useNativeDriver: - isWeb || Platform.constants.reactNativeVersion.minor <= 72, - }).start(); + if (isElevationEntitled) { + pressProgress.value = withTiming(0, elevationTimingConfig); } }, [ onPressOut, animateShape, springRadiusTo, - mode, - animation, - elevation, - isWeb, + isElevationEntitled, + pressProgress, + elevationTimingConfig, ] ); @@ -385,23 +392,10 @@ const Button = ({ if (animateShape) { springRadiusTo(restingRadius); } else { - animatedRadius.setValue(restingRadius); + animatedRadius.value = restingRadius; } }, [restingRadius, animateShape, animatedRadius, springRadiusTo]); - // Clamp so a spring overshoot can never render a negative radius. - const surfaceRadius = React.useMemo( - () => - animateShape - ? animatedRadius.interpolate({ - inputRange: [0, 1], - outputRange: [0, 1], - extrapolateLeft: 'clamp', - }) - : restingRadius, - [animateShape, animatedRadius, restingRadius] - ); - const { backgroundColor, borderColor, @@ -438,40 +432,89 @@ const Button = ({ [labelColor, customRippleColor] ); - const touchableStyle = React.useMemo( + // A radius pinned via `style` wins over the animated one, and disables the + // morph entirely (see `animateShape`). + const pinnedRadius = borderRadiusStyles.borderRadius; + const containerColor = + backgroundOpacity < 1 ? 'transparent' : backgroundColor; + + const outerStyle = useAnimatedStyle( () => ({ - ...borderRadiusStyles, - borderRadius: borderRadiusStyles.borderRadius ?? restingRadius, + borderRadius: pinnedRadius ?? Math.max(0, animatedRadius.value), + backgroundColor: containerColor, }), - [borderRadiusStyles, restingRadius] + [animatedRadius, pinnedRadius, containerColor] ); - const buttonStyle = React.useMemo( + // The clip carries the same animated radius, so the ripple and the disabled + // overlay follow the morph. + const clipStyle = useAnimatedStyle( () => ({ - backgroundColor: backgroundOpacity < 1 ? 'transparent' : backgroundColor, + borderRadius: pinnedRadius ?? Math.max(0, animatedRadius.value), + backgroundColor: containerColor, borderColor, borderWidth, - ...touchableStyle, - borderRadius: surfaceRadius, // animated; ripple clip + overlay stay static }), - [ - backgroundOpacity, - backgroundColor, - borderColor, - borderWidth, - touchableStyle, - surfaceRadius, - ] + [animatedRadius, pinnedRadius, containerColor, borderColor, borderWidth] ); - const touchableRippleStyle = React.useMemo( - () => - // Web can't animate the inner radius — use a rect clipped by the Surface. - isWeb && animateShape - ? { borderRadius: 0 } - : getButtonTouchableRippleStyle(touchableStyle, borderWidth), - [isWeb, animateShape, touchableStyle, borderWidth] - ); + // TODO: move this back to `Surface` once #5078 lands. + // Button builds its own shadow only because today's `Surface` can't host a + // Reanimated style: it accepts `Elevation | Animated.Value` for `elevation` + // and `StyleSheet.flatten`s `style`, which strips the metadata + // `useAnimatedStyle` needs. https://github.com/callstack/react-native-paper/pull/5078 + // reworks `Surface` for Reanimated — single view, `backgroundColor` and + // `borderRadius` as (shared-value capable) props, ambient layer absolutely + // positioned behind the content — and already migrates Button and FAB to it. + // When it merges, drop this block and the outer `Reanimated.View` in favour of + // ``. + // + // Until then Button renders only the spot shadow layer, mirroring + // `FAB/useVisibility.ts`; `Surface`'s second, ambient layer is missing, so an + // elevated button's shadow is slightly flatter than it was. + const [spotLayer] = shadowLayers; + const shadowColor = theme.colors.shadow; + const restLevel = isElevationEntitled ? initialElevation : 0; + const pressedLevel = isElevationEntitled ? activeElevation : 0; + const webShadow = + isWeb && isElevationEntitled ? shadow(restLevel, shadowColor) : null; + + const androidRest = androidElevationLevels[restLevel]; + const androidPressed = androidElevationLevels[pressedLevel]; + const heightRest = spotLayer.height[restLevel]; + const heightPressed = spotLayer.height[pressedLevel]; + const radiusRest = spotLayer.shadowRadius[restLevel]; + const radiusPressed = spotLayer.shadowRadius[pressedLevel]; + const spotOpacity = isElevationEntitled ? spotLayer.shadowOpacity : 0; + + const shadowStyle = useAnimatedStyle(() => { + const blend = (from: number, to: number) => + from + (to - from) * pressProgress.value; + + if (Platform.OS === 'android') { + return { elevation: blend(androidRest, androidPressed) }; + } + if (Platform.OS === 'web') { + return webShadow ?? {}; + } + return { + shadowColor, + shadowOpacity: spotOpacity, + shadowOffset: { width: 0, height: blend(heightRest, heightPressed) }, + shadowRadius: blend(radiusRest, radiusPressed), + }; + }, [ + pressProgress, + androidRest, + androidPressed, + heightRest, + heightPressed, + radiusRest, + radiusPressed, + shadowColor, + spotOpacity, + webShadow, + ]); const { color: labelStyleColor } = React.useMemo( () => StyleSheet.flatten(labelStyle) || {}, @@ -512,76 +555,76 @@ const Button = ({ ); return ( - > - } - elevation={elevation} - container + testID={`${testID}-container-outer-layer`} + style={[styles.button, shadowStyle, outerStyle, style]} > - {backgroundOpacity < 1 && ( - - )} - - + )} + - {icon && loading !== true ? ( - - + {icon && loading !== true ? ( + + + + ) : null} + {loading ? ( + - - ) : null} - {loading ? ( - - ) : null} - - {children} - - - - + ) : null} + + {children} + + + + + ); }; const styles = StyleSheet.create({ button: { minWidth: 64, - borderStyle: 'solid', }, clip: { + borderStyle: 'solid', overflow: 'hidden', }, content: { diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 17254922de..3177d01a39 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -1,4 +1,4 @@ -import type { ColorValue, ViewStyle } from 'react-native'; +import type { ColorValue } from 'react-native'; import color from 'color'; @@ -8,7 +8,6 @@ import { black, white } from '../../theme/colors'; import { tokens } from '../../theme/tokens'; import { resolveCornerRadius } from '../../theme/utils/shape'; import type { InternalTheme } from '../../types'; -import { splitStyles } from '../../utils/splitStyles'; const stateOpacity = tokens.md.sys.state.opacity; @@ -377,44 +376,3 @@ export const getButtonRippleColor = ({ return color(labelColor).alpha(stateOpacity.pressed).rgb().string(); }; - -type ViewStyleBorderRadiusStyles = Partial< - Pick< - ViewStyle, - | 'borderBottomEndRadius' - | 'borderBottomLeftRadius' - | 'borderBottomRightRadius' - | 'borderBottomStartRadius' - | 'borderTopEndRadius' - | 'borderTopLeftRadius' - | 'borderTopRightRadius' - | 'borderTopStartRadius' - | 'borderRadius' - > ->; -export const getButtonTouchableRippleStyle = ( - style?: ViewStyle, - borderWidth: number = 0 -): ViewStyleBorderRadiusStyles => { - if (!style) return {}; - const touchableRippleStyle: ViewStyleBorderRadiusStyles = {}; - - const [, borderRadiusStyles] = splitStyles( - style, - (style) => style.startsWith('border') && style.endsWith('Radius') - ); - - const borderRadiusKeys = - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - Object.keys(borderRadiusStyles) as Array; - - borderRadiusKeys.forEach((key) => { - const value = style[key]; - if (typeof value === 'number') { - // Only subtract borderWidth if value is greater than 0 - const radius = value > 0 ? value - borderWidth : 0; - touchableRippleStyle[key] = radius; - } - }); - return touchableRippleStyle; -}; diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 88c8830ac2..da1ad8f5e8 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -1,14 +1,16 @@ -import { Animated, PlatformColor, StyleSheet } from 'react-native'; +import { PlatformColor, StyleSheet } from 'react-native'; import { describe, expect, it, jest } from '@jest/globals'; -import { act, fireEvent } from '@testing-library/react-native'; +import { fireEvent } from '@testing-library/react-native'; import color from 'color'; +import * as Reanimated from 'react-native-reanimated'; import { LocaleProvider } from '../../core/locale'; import { getTheme } from '../../core/theming'; import { render, screen } from '../../test-utils'; import { pink500, white } from '../../theme/colors'; import { tokens } from '../../theme/tokens'; +import { shadowLayers } from '../../theme/tokens/sys/elevation'; import Button from '../Button/Button'; import { getButtonColors, @@ -36,6 +38,9 @@ const styles = StyleSheet.create({ overrideRadius: { borderRadius: 4, }, + scaled: { + transform: [{ scale: 1.5 }], + }, }); it('renders filled button by default', async () => { @@ -196,10 +201,14 @@ it('renders button with custom border radius', async () => { ); + // The shadow host and the clip both take the pinned radius so the shadow and + // the ripple share one shape. + expect(screen.getByTestId('custom-radius-container-outer-layer')).toHaveStyle( + styles.customRadius + ); expect(screen.getByTestId('custom-radius-container')).toHaveStyle( styles.customRadius ); - expect(screen.getByTestId('custom-radius')).toHaveStyle(styles.customRadius); }); it('renders outlined button with custom border radius', async () => { @@ -213,14 +222,11 @@ it('renders outlined button with custom border radius', async () => { ); - expect(screen.getByTestId('custom-radius-container')).toHaveStyle( - styles.customRadius - ); - expect(screen.getByTestId('custom-radius')).toHaveStyle({ - borderTopLeftRadius: 15, // styles.customRadius - 1px outline - borderTopRightRadius: 0, - borderBottomLeftRadius: 0, - borderBottomRightRadius: 15, // styles.customRadius - 1px outline + // The outline is drawn on the clip itself, so its radius is no longer + // inset by the outline width. + expect(screen.getByTestId('custom-radius-container')).toHaveStyle({ + ...styles.customRadius, + borderWidth: 1, }); }); @@ -231,10 +237,12 @@ it('renders button without border radius', async () => { ); + expect(screen.getByTestId('custom-radius-container-outer-layer')).toHaveStyle( + styles.noRadius + ); expect(screen.getByTestId('custom-radius-container')).toHaveStyle( styles.noRadius ); - expect(screen.getByTestId('custom-radius')).toHaveStyle(styles.noRadius); }); it('should execute onPressIn', async () => { @@ -1080,41 +1088,60 @@ describe('toggle colors', () => { }); }); -it('animated value changes correctly', async () => { - const value = new Animated.Value(1); +it('gives an elevated button a resting shadow, and other modes none', async () => { + const [spotLayer] = shadowLayers; + await render( - ); - expect(screen.getByTestId('button-container-outer-layer')).toHaveStyle({ - transform: [{ scale: 1 }], + expect(screen.getByTestId('elevated-container-outer-layer')).toHaveStyle({ + shadowOpacity: spotLayer.shadowOpacity, + shadowRadius: spotLayer.shadowRadius[1], + shadowOffset: { width: 0, height: spotLayer.height[1] }, }); - Animated.timing(value, { - toValue: 1.5, - useNativeDriver: false, - duration: 200, - }).start(); - - await act(() => { - jest.advanceTimersByTime(200); + await render( + + ); + expect(screen.getByTestId('filled-container-outer-layer')).toHaveStyle({ + shadowOpacity: 0, }); - expect(screen.getByTestId('button-container-outer-layer')).toHaveStyle({ - transform: [{ scale: 1.5 }], +}); + +it('drops the shadow when an elevated button is disabled', async () => { + await render( + + ); + + expect(screen.getByTestId('elevated-container-outer-layer')).toHaveStyle({ + shadowOpacity: 0, }); }); +it('forwards `style` to the shadow host', async () => { + await render( + + ); + + expect(screen.getByTestId('button-container-outer-layer')).toHaveStyle( + styles.scaled + ); +}); + describe('shape morph animation', () => { - const lastSpringToValue = (spy: jest.Spied) => - spy.mock.calls.map((call) => call[1]?.toValue); + const springTargets = (spy: jest.Spied) => + spy.mock.calls.map((call) => call[0]); it('springs the corner radius to corner.small on press in', async () => { - const spy = jest.spyOn(Animated, 'spring'); + const spy = jest.spyOn(Reanimated, 'withSpring'); await render( ); // selected flips square -> round; large round resting radius = 96 / 2 = 48 - expect(lastSpringToValue(spy)).toContain(48); + expect(springTargets(spy)).toContain(48); spy.mockRestore(); }); it('morphs a default button, with no size or shape passed', async () => { - const spy = jest.spyOn(Animated, 'spring'); + const spy = jest.spyOn(Reanimated, 'withSpring'); await render( ); + spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); - await act(() => { - jest.advanceTimersByTime(1000); - }); - expect(screen.getByTestId('button-container')).toHaveStyle({ - borderRadius: getTheme().shapes.corner.small, - }); + // `pressedContainerShape` is `small` at every size. + expect(springTargets(spy)).toContain(getTheme().shapes.corner.small); + spy.mockRestore(); }); }); diff --git a/src/components/__tests__/Card/Card.test.tsx b/src/components/__tests__/Card/Card.test.tsx index 8d6b8c3b75..b0c59f5270 100644 --- a/src/components/__tests__/Card/Card.test.tsx +++ b/src/components/__tests__/Card/Card.test.tsx @@ -156,7 +156,10 @@ describe('CardActions', () => { ); - expect(screen.getByTestId('card-actions-button')).toHaveStyle({ + // `style` lands on the shadow host, which is also what carries the radius. + expect( + screen.getByTestId('card-actions-button-container-outer-layer') + ).toHaveStyle({ borderRadius: 32, }); }); diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index 2ecdd41c1f..f1abcb8e35 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -118,34 +118,12 @@ exports[`render visible banner, with custom theme 1`] = ` } > - + @@ -199,9 +203,7 @@ exports[`render visible banner, with custom theme 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -556,34 +558,12 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` } > - + @@ -637,9 +643,7 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -837,34 +841,12 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` } > - + @@ -918,9 +926,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -994,34 +1000,12 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` - + @@ -1075,9 +1085,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index 7930431cda..340e36e4a9 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -2,33 +2,12 @@ exports[`renders button with an accessibility hint 1`] = ` - + @@ -83,9 +86,7 @@ exports[`renders button with an accessibility hint 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -162,34 +163,13 @@ exports[`renders button with an accessibility hint 1`] = ` exports[`renders button with an accessibility label 1`] = ` - + @@ -243,9 +247,7 @@ exports[`renders button with an accessibility label 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -322,33 +324,12 @@ exports[`renders button with an accessibility label 1`] = ` exports[`renders button with button color 1`] = ` - + @@ -402,9 +407,7 @@ exports[`renders button with button color 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -481,33 +484,12 @@ exports[`renders button with button color 1`] = ` exports[`renders button with color 1`] = ` - + @@ -561,9 +567,7 @@ exports[`renders button with color 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -640,33 +644,12 @@ exports[`renders button with color 1`] = ` exports[`renders button with custom testID 1`] = ` - + @@ -720,9 +727,7 @@ exports[`renders button with custom testID 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="custom:testID" @@ -799,33 +804,12 @@ exports[`renders button with custom testID 1`] = ` exports[`renders button with icon 1`] = ` - + @@ -879,9 +887,7 @@ exports[`renders button with icon 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -991,33 +997,12 @@ exports[`renders button with icon 1`] = ` exports[`renders button with icon in reverse order 1`] = ` - + @@ -1071,9 +1080,7 @@ exports[`renders button with icon in reverse order 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -1185,33 +1192,12 @@ exports[`renders button with icon in reverse order 1`] = ` exports[`renders disabled button 1`] = ` - + @@ -1236,7 +1246,6 @@ exports[`renders disabled button 1`] = ` }, { "backgroundColor": "rgba(29, 27, 32, 1)", - "borderRadius": 20, "opacity": 0.1, }, ] @@ -1284,9 +1293,7 @@ exports[`renders disabled button 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -1363,33 +1370,12 @@ exports[`renders disabled button 1`] = ` exports[`renders filled button by default 1`] = ` - + @@ -1443,9 +1453,7 @@ exports[`renders filled button by default 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -1522,33 +1530,12 @@ exports[`renders filled button by default 1`] = ` exports[`renders filled button with mode 1`] = ` - + @@ -1602,9 +1613,7 @@ exports[`renders filled button with mode 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -1681,33 +1690,12 @@ exports[`renders filled button with mode 1`] = ` exports[`renders loading button 1`] = ` - + @@ -1761,9 +1773,7 @@ exports[`renders loading button 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -2031,33 +2041,12 @@ exports[`renders loading button 1`] = ` exports[`renders outlined button with mode 1`] = ` - + @@ -2111,9 +2124,7 @@ exports[`renders outlined button with mode 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 19, - }, + undefined, ] } testID="button" @@ -2190,33 +2201,12 @@ exports[`renders outlined button with mode 1`] = ` exports[`renders text button with mode 1`] = ` - + @@ -2270,9 +2284,7 @@ exports[`renders text button with mode 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" @@ -2349,33 +2361,12 @@ exports[`renders text button with mode 1`] = ` exports[`size prop renders a button with per-size metrics 1`] = ` - + @@ -2423,9 +2438,7 @@ exports[`size prop renders a button with per-size metrics 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 28, - }, + undefined, ] } testID="button" diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index bbaf4b972f..27d55e542c 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1682,34 +1682,12 @@ exports[`DataTable.Pagination renders data table pagination with options select collapsable={false} > - + @@ -1764,9 +1768,7 @@ exports[`DataTable.Pagination renders data table pagination with options select { "overflow": "hidden", }, - { - "borderRadius": 19, - }, + undefined, ] } testID="button" diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index 467692f7bd..05d7e4c2ea 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -15,33 +15,12 @@ exports[`renders menu with content styles 1`] = ` collapsable={false} > - + @@ -95,9 +98,7 @@ exports[`renders menu with content styles 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 19, - }, + undefined, ] } testID="button" @@ -584,33 +585,12 @@ exports[`renders not visible menu 1`] = ` collapsable={false} > - + @@ -664,9 +668,7 @@ exports[`renders not visible menu 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 19, - }, + undefined, ] } testID="button" @@ -758,33 +760,12 @@ exports[`renders visible menu 1`] = ` collapsable={false} > - + @@ -838,9 +843,7 @@ exports[`renders visible menu 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 19, - }, + undefined, ] } testID="button" diff --git a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap index f7bb00a0f0..148bbcb056 100644 --- a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap @@ -319,35 +319,12 @@ exports[`renders snackbar with action button 1`] = ` } > - + @@ -401,9 +408,7 @@ exports[`renders snackbar with action button 1`] = ` { "overflow": "hidden", }, - { - "borderRadius": 20, - }, + undefined, ] } testID="button" From 5c0a7488df9ba59d42e65cc3a5ecaa2930796a46 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Tue, 1 Sep 2026 12:37:33 +0200 Subject: [PATCH 29/66] feat: allow opting out of the shape morph --- src/components/Button/Button.tsx | 27 ++++++-- src/components/__tests__/Button.test.tsx | 84 ++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 5 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index bede7c7811..46a1db8b95 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -76,6 +76,18 @@ export type Props = $Omit & { * in `style`. */ shape?: ButtonShape; + /** + * Whether the container animates its corner radius. Defaults to `true`. + * + * When `true` the corner springs to the pressed shape while the button is + * pressed, and animates between the two shapes as `selected` changes. When + * `false` the shape still changes, it just snaps instead of animating, and + * there is no press morph at all. + * + * Forced off when `style` pins a `borderRadius`, and the press morph is + * skipped when the OS reduce-motion setting is on. + */ + animateShape?: boolean; /** * Turns the button into a Material Design 3 expressive toggle and sets its * state. Leave it **undefined** for a plain button — a toggle that is merely @@ -225,6 +237,7 @@ const Button = ({ mode = 'filled', size = 'small', shape = 'round', + animateShape: animateShapeProp = true, selected, dark, loading, @@ -311,7 +324,10 @@ const Button = ({ // Shape morph: animate the corner on press (→ the pressed shape token) and on // the `selected`/shape toggle. Skipped when the user pins a radius via `style`. const hasPinnedRadius = Object.keys(borderRadiusStyles).length > 0; - const animateShape = !hasPinnedRadius; + const animateShape = animateShapeProp && !hasPinnedRadius; + // A press morph under reduce-motion would pop instantly rather than animate, + // so skip it; a `selected` change still snaps to the new shape. + const morphOnPress = animateShape && !reduceMotion; // `round` resolves to the `cornerFull` sentinel, so use the real pill radius // (half the container height) instead: it keeps the spring bounded and lets // the ripple clip match the container exactly. @@ -344,7 +360,7 @@ const Button = ({ const handlePressIn = React.useCallback( (e: GestureResponderEvent) => { onPressIn?.(e); - if (animateShape) { + if (morphOnPress) { springRadiusTo(pressedRadius); } if (isElevationEntitled) { @@ -353,7 +369,7 @@ const Button = ({ }, [ onPressIn, - animateShape, + morphOnPress, springRadiusTo, pressedRadius, isElevationEntitled, @@ -365,7 +381,7 @@ const Button = ({ const handlePressOut = React.useCallback( (e: GestureResponderEvent) => { onPressOut?.(e); - if (animateShape) { + if (morphOnPress) { springRadiusTo(restingRadiusRef.current); } if (isElevationEntitled) { @@ -374,7 +390,7 @@ const Button = ({ }, [ onPressOut, - animateShape, + morphOnPress, springRadiusTo, isElevationEntitled, pressProgress, @@ -438,6 +454,7 @@ const Button = ({ const containerColor = backgroundOpacity < 1 ? 'transparent' : backgroundColor; + // Clamp so a spring overshoot can never render a negative radius. const outerStyle = useAnimatedStyle( () => ({ borderRadius: pinnedRadius ?? Math.max(0, animatedRadius.value), diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index da1ad8f5e8..c7c3d5da95 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -8,9 +8,11 @@ import * as Reanimated from 'react-native-reanimated'; import { LocaleProvider } from '../../core/locale'; import { getTheme } from '../../core/theming'; import { render, screen } from '../../test-utils'; +import { ReduceMotionContext } from '../../theme/accessibility/ReduceMotionContext'; import { pink500, white } from '../../theme/colors'; import { tokens } from '../../theme/tokens'; import { shadowLayers } from '../../theme/tokens/sys/elevation'; +import { toRawSpring } from '../../theme/tokens/sys/motion'; import Button from '../Button/Button'; import { getButtonColors, @@ -1240,4 +1242,86 @@ describe('shape morph animation', () => { expect(springTargets(spy)).toContain(getTheme().shapes.corner.small); spy.mockRestore(); }); + + it('springs with the same spatial config as the rest of the library', async () => { + const spy = jest.spyOn(Reanimated, 'withSpring'); + await render( + + ); + spy.mockClear(); + await fireEvent(screen.getByTestId('button'), 'onPressIn'); + + // Same spring FAB and Switch use, so the overshoot matches them. + const { damping, stiffness } = toRawSpring( + getTheme().motion.spring.fast.spatial + ); + expect(spy).toHaveBeenCalledWith( + expect.any(Number), + expect.objectContaining({ damping, stiffness }) + ); + spy.mockRestore(); + }); + + it('skips the press morph under reduce motion', async () => { + const spy = jest.spyOn(Reanimated, 'withSpring'); + await render( + + + + ); + spy.mockClear(); + await fireEvent(screen.getByTestId('button'), 'onPressIn'); + expect(spy).not.toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('does not morph when animateShape is false', async () => { + const spy = jest.spyOn(Reanimated, 'withSpring'); + await render( + + ); + spy.mockClear(); + await fireEvent(screen.getByTestId('button'), 'onPressIn'); + expect(spy).not.toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('snaps instead of springing when animateShape is false and selected flips', async () => { + const spy = jest.spyOn(Reanimated, 'withSpring'); + await render( + + ); + await screen.rerender( + + ); + + // The shape still changes to the flipped radius, it just doesn't animate. + expect(spy).not.toHaveBeenCalled(); + expect(screen.getByTestId('button-container')).toHaveStyle({ + borderRadius: 48, + }); + spy.mockRestore(); + }); }); From f11cb50a93f4ebc7c6a2960baf7f2a8a073eb782 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Tue, 1 Sep 2026 15:20:16 +0200 Subject: [PATCH 30/66] fix: decouple button static styles from the animation --- src/components/Button/Button.tsx | 39 +++++--- src/components/__tests__/Button.test.tsx | 55 ++++++++++++ .../__snapshots__/Banner.test.tsx.snap | 24 ++++- .../__snapshots__/Button.test.tsx.snap | 90 +++++++++++++++---- .../__snapshots__/DataTable.test.tsx.snap | 12 ++- .../__snapshots__/Menu.test.tsx.snap | 36 +++++--- .../__snapshots__/Snackbar.test.tsx.snap | 6 +- 7 files changed, 210 insertions(+), 52 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 46a1db8b95..91683012aa 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -458,9 +458,8 @@ const Button = ({ const outerStyle = useAnimatedStyle( () => ({ borderRadius: pinnedRadius ?? Math.max(0, animatedRadius.value), - backgroundColor: containerColor, }), - [animatedRadius, pinnedRadius, containerColor] + [animatedRadius, pinnedRadius] ); // The clip carries the same animated radius, so the ripple and the disabled @@ -468,11 +467,18 @@ const Button = ({ const clipStyle = useAnimatedStyle( () => ({ borderRadius: pinnedRadius ?? Math.max(0, animatedRadius.value), - backgroundColor: containerColor, - borderColor, - borderWidth, }), - [animatedRadius, pinnedRadius, containerColor, borderColor, borderWidth] + [animatedRadius, pinnedRadius] + ); + + const containerStyle = React.useMemo( + () => ({ backgroundColor: containerColor }), + [containerColor] + ); + + const outlineStyle = React.useMemo( + () => ({ backgroundColor: containerColor, borderColor, borderWidth }), + [containerColor, borderColor, borderWidth] ); // TODO: move this back to `Surface` once #5078 lands. @@ -563,6 +569,16 @@ const Button = ({ }; }, [hitSlop, sizeStyle]); + const contentBoxStyle = React.useMemo( + () => ({ + minHeight: sizeStyle.minHeight - borderWidth * 2, + paddingStart: sizeStyle.paddingStart - borderWidth, + paddingEnd: sizeStyle.paddingEnd - borderWidth, + gap: sizeStyle.iconGap, + }), + [sizeStyle, borderWidth] + ); + const labelTypeStyle = React.useMemo( () => ({ color: labelColor, @@ -576,11 +592,11 @@ const Button = ({ {...rest} ref={ref} testID={`${testID}-container-outer-layer`} - style={[styles.button, shadowStyle, outerStyle, style]} + style={[styles.button, shadowStyle, containerStyle, outerStyle, style]} > {backgroundOpacity < 1 && ( { ); }); +describe('container height', () => { + const MODES = ['filled', 'tonal', 'elevated', 'outlined', 'text'] as const; + const { containerHeight, leadingSpace } = Tokens.sizes.small; + + // The rendered container is the content box plus the outline Yoga draws + // around it, so those two have to add up to the token height in every mode. + const renderedBox = (testID: string) => { + const content = StyleSheet.flatten( + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + screen.getByTestId(`${testID}-content`).props.style + ); + const clip = StyleSheet.flatten( + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + screen.getByTestId(`${testID}-container`).props.style + ); + const outline = clip.borderWidth ?? 0; + return { + height: content.minHeight + outline * 2, + leading: content.paddingStart + outline, + }; + }; + + it.each(MODES)('renders %s at the token height', async (mode) => { + await render( + + ); + + expect(renderedBox('button')).toEqual({ + height: containerHeight, + leading: leadingSpace, + }); + }); + + it('keeps an outlined toggle the same size in both states', async () => { + await render( + + ); + const unselected = renderedBox('button'); + + await render( + + ); + + // Selecting drops the outline; without the inset this shrank by 2dp. + expect(renderedBox('button')).toEqual(unselected); + }); +}); + describe('shape morph animation', () => { const springTargets = (spy: jest.Spied) => spy.mock.calls.map((call) => call[0]); diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index f1abcb8e35..a47893f0bd 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -134,6 +134,8 @@ exports[`render visible banner, with custom theme 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, { @@ -153,9 +155,11 @@ exports[`render visible banner, with custom theme 1`] = ` { "backgroundColor": "transparent", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -574,6 +578,8 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, { @@ -593,9 +599,11 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` { "backgroundColor": "transparent", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -857,6 +865,8 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, { @@ -876,9 +886,11 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "backgroundColor": "transparent", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -1016,6 +1028,8 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, { @@ -1035,9 +1049,11 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "backgroundColor": "transparent", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index 340e36e4a9..e07a8b19dd 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -18,6 +18,8 @@ exports[`renders button with an accessibility hint 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -35,9 +37,11 @@ exports[`renders button with an accessibility hint 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -180,6 +184,8 @@ exports[`renders button with an accessibility label 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -197,9 +203,11 @@ exports[`renders button with an accessibility label 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -340,6 +348,8 @@ exports[`renders button with button color 1`] = ` }, { "backgroundColor": "#e91e63", + }, + { "borderRadius": 20, }, undefined, @@ -357,9 +367,11 @@ exports[`renders button with button color 1`] = ` { "backgroundColor": "#e91e63", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -500,6 +512,8 @@ exports[`renders button with color 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -517,9 +531,11 @@ exports[`renders button with color 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -660,6 +676,8 @@ exports[`renders button with custom testID 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -677,9 +695,11 @@ exports[`renders button with custom testID 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -820,6 +840,8 @@ exports[`renders button with icon 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -837,9 +859,11 @@ exports[`renders button with icon 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -1013,6 +1037,8 @@ exports[`renders button with icon in reverse order 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -1030,9 +1056,11 @@ exports[`renders button with icon in reverse order 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -1208,6 +1236,8 @@ exports[`renders disabled button 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, undefined, @@ -1225,9 +1255,11 @@ exports[`renders disabled button 1`] = ` { "backgroundColor": "transparent", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -1386,6 +1418,8 @@ exports[`renders filled button by default 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -1403,9 +1437,11 @@ exports[`renders filled button by default 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -1546,6 +1582,8 @@ exports[`renders filled button with mode 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -1563,9 +1601,11 @@ exports[`renders filled button with mode 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -1706,6 +1746,8 @@ exports[`renders loading button 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 20, }, undefined, @@ -1723,9 +1765,11 @@ exports[`renders loading button 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -2057,6 +2101,8 @@ exports[`renders outlined button with mode 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, undefined, @@ -2074,9 +2120,11 @@ exports[`renders outlined button with mode 1`] = ` { "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, "borderWidth": 1, }, + { + "borderRadius": 20, + }, {}, ] } @@ -2140,9 +2188,9 @@ exports[`renders outlined button with mode 1`] = ` false, { "gap": 8, - "minHeight": 40, - "paddingEnd": 16, - "paddingStart": 16, + "minHeight": 38, + "paddingEnd": 15, + "paddingStart": 15, }, { "opacity": 1, @@ -2217,6 +2265,8 @@ exports[`renders text button with mode 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, undefined, @@ -2234,9 +2284,11 @@ exports[`renders text button with mode 1`] = ` { "backgroundColor": "transparent", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } @@ -2377,6 +2429,8 @@ exports[`size prop renders a button with per-size metrics 1`] = ` }, { "backgroundColor": "rgba(103, 80, 164, 1)", + }, + { "borderRadius": 28, }, undefined, @@ -2394,9 +2448,11 @@ exports[`size prop renders a button with per-size metrics 1`] = ` { "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", - "borderRadius": 28, "borderWidth": 0, }, + { + "borderRadius": 28, + }, {}, ] } diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 27d55e542c..72a0846d4f 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1698,6 +1698,8 @@ exports[`DataTable.Pagination renders data table pagination with options select }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, { @@ -1718,9 +1720,11 @@ exports[`DataTable.Pagination renders data table pagination with options select { "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, "borderWidth": 1, }, + { + "borderRadius": 20, + }, {}, ] } @@ -1786,9 +1790,9 @@ exports[`DataTable.Pagination renders data table pagination with options select }, { "gap": 8, - "minHeight": 40, - "paddingEnd": 16, - "paddingStart": 16, + "minHeight": 38, + "paddingEnd": 15, + "paddingStart": 15, }, { "opacity": 1, diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index 05d7e4c2ea..8873765bbc 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -31,6 +31,8 @@ exports[`renders menu with content styles 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, undefined, @@ -48,9 +50,11 @@ exports[`renders menu with content styles 1`] = ` { "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, "borderWidth": 1, }, + { + "borderRadius": 20, + }, {}, ] } @@ -114,9 +118,9 @@ exports[`renders menu with content styles 1`] = ` false, { "gap": 8, - "minHeight": 40, - "paddingEnd": 16, - "paddingStart": 16, + "minHeight": 38, + "paddingEnd": 15, + "paddingStart": 15, }, { "opacity": 1, @@ -601,6 +605,8 @@ exports[`renders not visible menu 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, undefined, @@ -618,9 +624,11 @@ exports[`renders not visible menu 1`] = ` { "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, "borderWidth": 1, }, + { + "borderRadius": 20, + }, {}, ] } @@ -684,9 +692,9 @@ exports[`renders not visible menu 1`] = ` false, { "gap": 8, - "minHeight": 40, - "paddingEnd": 16, - "paddingStart": 16, + "minHeight": 38, + "paddingEnd": 15, + "paddingStart": 15, }, { "opacity": 1, @@ -776,6 +784,8 @@ exports[`renders visible menu 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, undefined, @@ -793,9 +803,11 @@ exports[`renders visible menu 1`] = ` { "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, "borderWidth": 1, }, + { + "borderRadius": 20, + }, {}, ] } @@ -859,9 +871,9 @@ exports[`renders visible menu 1`] = ` false, { "gap": 8, - "minHeight": 40, - "paddingEnd": 16, - "paddingStart": 16, + "minHeight": 38, + "paddingEnd": 15, + "paddingStart": 15, }, { "opacity": 1, diff --git a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap index 148bbcb056..85ac3c079d 100644 --- a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap @@ -335,6 +335,8 @@ exports[`renders snackbar with action button 1`] = ` }, { "backgroundColor": "transparent", + }, + { "borderRadius": 20, }, [ @@ -358,9 +360,11 @@ exports[`renders snackbar with action button 1`] = ` { "backgroundColor": "transparent", "borderColor": "transparent", - "borderRadius": 20, "borderWidth": 0, }, + { + "borderRadius": 20, + }, {}, ] } From d9e7b999cc1fa833dcd97b3af35a71698e1a6684 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Wed, 2 Sep 2026 11:06:07 +0200 Subject: [PATCH 31/66] chore: revamp the Button playground controls --- example/src/Examples/ButtonExample.tsx | 49 ++++++++++++++++++++++---- src/components/Button/Button.tsx | 9 +++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index e1c1cb5f01..5e973c891a 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -9,6 +9,12 @@ type Mode = 'text' | 'outlined' | 'elevated' | 'filled' | 'tonal'; type Size = 'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'; type Shape = 'round' | 'square'; type IconPosition = 'leading' | 'trailing'; +/** + * `selected` is nullable: leaving it undefined is a plain button, which MD3 + * colours differently from a toggle that happens to be unselected. A boolean + * switch can't express all three, so this is a three-way. + */ +type ToggleState = 'off' | 'unselected' | 'selected'; const MODES: Mode[] = ['filled', 'tonal', 'elevated', 'outlined', 'text']; const SIZES: Size[] = [ @@ -20,6 +26,10 @@ const SIZES: Size[] = [ ]; const SHAPES: Shape[] = ['round', 'square']; const ICON_POSITIONS: IconPosition[] = ['leading', 'trailing']; +const TOGGLE_STATES: ToggleState[] = ['off', 'unselected', 'selected']; + +const selectedFor = (state: ToggleState) => + state === 'off' ? undefined : state === 'selected'; function OptionRow({ label, @@ -84,7 +94,8 @@ const ButtonExample = () => { const [showIcon, setShowIcon] = React.useState(false); const [disabled, setDisabled] = React.useState(false); const [loading, setLoading] = React.useState(false); - const [selected, setSelected] = React.useState(false); + const [toggleState, setToggleState] = React.useState('off'); + const [animateShape, setAnimateShape] = React.useState(true); // Selected state for the static toggle showcase below. const [selectedToggles, setSelectedToggles] = React.useState< @@ -105,7 +116,8 @@ const ButtonExample = () => { icon={showIcon ? 'camera' : undefined} disabled={disabled} loading={loading} - selected={selected} + selected={selectedFor(toggleState)} + animateShape={animateShape} onPress={() => {}} > Play me @@ -150,10 +162,16 @@ const ButtonExample = () => { onValueChange={setDisabled} /> + @@ -209,9 +227,9 @@ const ButtonExample = () => { - {(['round', 'square'] as const).map((shapeVariant) => ( + {SHAPES.map((shapeVariant) => ( - {(['extra-small', 'small', 'medium', 'large'] as const).map((s) => ( + {SIZES.map((s) => ( + + -); - -export default MyComponent; -``` ## Props @@ -54,11 +40,35 @@ export default MyComponent;
-### compact +### size + +
+ + + +
+ +### shape + +
+ + + +
+ +### animateShape + +
+ + + +
+ +### selected
- +
@@ -94,6 +104,14 @@ export default MyComponent;
+### iconPosition + +
+ + + +
+ ### disabled
@@ -110,19 +128,19 @@ export default MyComponent;
-### uppercase +### background
- +
-### background +### rippleColor
- +
@@ -267,7 +285,7 @@ export default MyComponent; ## Theme colors - + diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 1fcd32bd25..3e7e056511 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -126,3 +126,100 @@ const theme = { style={{ fontSize: 16, color: '#1C1B1F' }} /> ``` + +### Button + +`Button` is modernized to Material Design 3 Expressive. The button modes were +renamed, the default mode changed, and a size and shape scale were added. + +#### Mode + +- **`mode="contained"`** → **`mode="filled"`** +- **`mode="contained-tonal"`** → **`mode="tonal"`** + +```tsx +// Before (v5) + + + +// After (v6) + + +``` + +#### Default mode + +The default is now **`filled`**, not `text`, matching the MD3 emphasis +hierarchy. Pass `mode="text"` to keep the old look. + +```tsx +// Before (v5) - rendered as a text button + + +// After (v6) - same appearance + +``` + +#### Size and shape + +New `size` and `shape` props. `size` defaults to `small`, which matches the +only size 5.x had, so existing buttons keep their metrics. + +```tsx + + +``` + +The container now also morphs its corner while pressed. Pass +`animateShape={false}` to opt out. + +#### Toggle + +`selected` turns a button into a toggle. Leave it **undefined** to make it +a plain button + +```tsx + +``` + +#### Removed props + +- **`uppercase`** → `labelStyle={{ textTransform: 'uppercase' }}`. +- **`compact`** → `size="extra-small"`. +- **`contentStyle={{ flexDirection: 'row-reverse' }}`** → `iconPosition="trailing"`. + +```tsx +// Before (v5) + + +// After (v6) + +``` + +#### Appearance changes + +These need no code change, but the rendering differs: + +- The `outlined` label and icon use **`onSurfaceVariant`** instead of + `primary`. +- The default icon size is **20dp**, up from **18dp**. +- `labelStyle={{ fontSize }}` no longer changes the icon size. The icon follows + `size`; set both if you need them to match. +- The resting corner is the full pill radius instead of a fixed **20dp**. + +#### Card.Actions + +`Card.Actions` defaults its buttons to `outlined` for the first child and +**`filled`** for the rest, where it used to default to `contained`. It also no +longer forwards `compact` or `uppercase` to them. diff --git a/docs/src/data/componentDocs6x.json b/docs/src/data/componentDocs6x.json index 300c315b99..77a1f9469a 100644 --- a/docs/src/data/componentDocs6x.json +++ b/docs/src/data/componentDocs6x.json @@ -2962,10 +2962,10 @@ "Button/Button": { "filepath": "Button/Button.tsx", "title": "Button", - "description": "A button is component that the user can press to trigger an action.\n\n## Usage\n```js\nimport * as React from 'react';\nimport { Button } from 'react-native-paper';\n\nconst MyComponent = () => (\n \n);\n\nexport default MyComponent;\n```", + "description": "", "link": "button", "data": { - "description": "A button is component that the user can press to trigger an action.\n\n## Usage\n```js\nimport * as React from 'react';\nimport { Button } from 'react-native-paper';\n\nconst MyComponent = () => (\n \n);\n\nexport default MyComponent;\n```", + "description": "", "displayName": "Button", "methods": [], "statics": [], @@ -2974,7 +2974,7 @@ "required": false, "tsType": { "name": "union", - "raw": "'text' | 'outlined' | 'contained' | 'elevated' | 'contained-tonal'", + "raw": "'text' | 'outlined' | 'filled' | 'elevated' | 'tonal'", "elements": [ { "name": "literal", @@ -2986,7 +2986,7 @@ }, { "name": "literal", - "value": "'contained'" + "value": "'filled'" }, { "name": "literal", @@ -2994,13 +2994,13 @@ }, { "name": "literal", - "value": "'contained-tonal'" + "value": "'tonal'" } ] }, - "description": "Mode of the button. You can change the mode to adjust the styling to give it desired emphasis.\n- `text` - flat button without background or outline, used for the lowest priority actions, especially when presenting multiple options.\n- `outlined` - button with an outline without background, typically used for important, but not primary action – represents medium emphasis.\n- `contained` - button with a background color, used for important action, have the most visual impact and high emphasis.\n- `elevated` - button with a background color and elevation, used when absolutely necessary e.g. button requires visual separation from a patterned background. @supported Available in v5.x with theme version 3\n- `contained-tonal` - button with a secondary background color, an alternative middle ground between contained and outlined buttons. @supported Available in v5.x with theme version 3", + "description": "Mode of the button. You can change the mode to adjust the styling to give it desired emphasis. Defaults to `filled`.\n- `filled` - button with a background color, used for the most important action, has the most visual impact and high emphasis. (default)\n- `tonal` - button with a secondary background color, an alternative middle ground between filled and outlined buttons.\n- `elevated` - button with a background color and elevation, used when absolutely necessary e.g. button requires visual separation from a patterned background.\n- `outlined` - button with an outline without background, typically used for important, but not primary action – represents medium emphasis.\n- `text` - flat button without background or outline, used for the lowest priority actions, especially when presenting multiple options.", "defaultValue": { - "value": "'text'", + "value": "'filled'", "computed": false } }, @@ -3009,14 +3009,47 @@ "tsType": { "name": "boolean" }, - "description": "Whether the color is a dark color. A dark button will render light text and vice-versa. Only applicable for:\n * `contained` mode for theme version 2\n * `contained`, `contained-tonal` and `elevated` modes for theme version 3." + "description": "Whether the color is a dark color. A dark button will render light text and vice-versa. Only applicable for the `filled`, `tonal` and `elevated` modes." }, - "compact": { + "size": { + "required": false, + "tsType": { + "name": "ButtonSize" + }, + "description": "Size of the button (Material Design 3 expressive). Defaults to `small`.\n\nThe size controls the minimum height, horizontal padding, icon size, the\ngap between icon and label, and the label typescale.", + "defaultValue": { + "value": "'small'", + "computed": false + } + }, + "shape": { + "required": false, + "tsType": { + "name": "ButtonShape" + }, + "description": "Shape variant of the button (Material Design 3 expressive). Defaults to\n`round`. `'round'` uses the full-pill corner radius; `'square'` uses a\nsmaller per-size corner radius. Overridden by an explicit `borderRadius`\nin `style`.", + "defaultValue": { + "value": "'round'", + "computed": false + } + }, + "animateShape": { "required": false, "tsType": { "name": "boolean" }, - "description": "Use a compact look, useful for `text` buttons in a row." + "description": "Whether the container animates its corner radius. Defaults to `true`.\n\nWhen `true` the corner springs to the pressed shape while the button is\npressed, and animates between the two shapes as `selected` changes. When\n`false` the shape still changes, it just snaps instead of animating, and\nthere is no press morph at all.\n\nForced off when `style` pins a `borderRadius`, and the press morph is\nskipped when the OS reduce-motion setting is on.", + "defaultValue": { + "value": "true", + "computed": false + } + }, + "selected": { + "required": false, + "tsType": { + "name": "boolean" + }, + "description": "Turns the button into a Material Design 3 expressive toggle and sets its\nstate. Leave it **undefined** for a plain button — a toggle that is merely\nunselected is a different state, and MD3 gives the two different colours.\n\nWhen defined:\n\n- The button takes its container and label colours from the toggle set for\n its `mode`, which differ between `false` and `true`. `text` is the one\n exception: MD3 defines no text toggle, so it keeps its plain colours.\n- When `true` the `shape` is flipped: `'round'` renders square and vice\n versa, and an `outlined` button drops its outline.\n- `aria-selected` is set so screen readers announce the toggle state." }, "buttonColor": { "required": false, @@ -3046,6 +3079,24 @@ }, "description": "Icon to display for the `Button`." }, + "iconPosition": { + "required": false, + "tsType": { + "name": "union", + "raw": "'leading' | 'trailing'", + "elements": [ + { + "name": "literal", + "value": "'leading'" + }, + { + "name": "literal", + "value": "'trailing'" + } + ] + }, + "description": "Position of the `icon` relative to the label. Defaults to `'leading'`." + }, "disabled": { "required": false, "tsType": { @@ -3061,19 +3112,19 @@ }, "description": "Label text of the button." }, - "uppercase": { + "background": { "required": false, "tsType": { - "name": "boolean" + "name": "PressableAndroidRippleConfig" }, - "description": "Make the label text uppercased. Note that this won't work if you pass React elements as children." + "description": "Type of background drawabale to display the feedback (Android).\nhttps://reactnative.dev/docs/pressable#rippleconfig" }, - "background": { + "rippleColor": { "required": false, "tsType": { - "name": "PressableAndroidRippleConfig" + "name": "ColorValue" }, - "description": "Type of background drawabale to display the feedback (Android).\nhttps://reactnative.dev/docs/pressable#rippleconfig" + "description": "Color of the ripple effect / state layer. Defaults to the label color at\nthe pressed-state opacity." }, "aria-label": { "required": false, @@ -3206,7 +3257,7 @@ ], "raw": "StyleProp" }, - "description": "Style of button's inner content.\nUse this prop to apply custom height and width, to set a custom padding or to set the icon on the right with `flexDirection: 'row-reverse'`." + "description": "Style of button's inner content.\nUse this prop to apply custom height and width or to set a custom padding." }, "maxFontSizeMultiplier": { "required": false, @@ -3226,19 +3277,13 @@ "style": { "required": false, "tsType": { - "name": "Animated.WithAnimatedValue", + "name": "StyleProp", "elements": [ { - "name": "StyleProp", - "elements": [ - { - "name": "ViewStyle" - } - ], - "raw": "StyleProp" + "name": "ViewStyle" } ], - "raw": "Animated.WithAnimatedValue>" + "raw": "StyleProp" }, "description": "" }, From c2009355e11e24b9b821e8b56672af025be0d9e6 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Wed, 2 Sep 2026 13:16:11 +0200 Subject: [PATCH 33/66] fix: tighten the pressed corner per size --- src/components/Button/tokens.ts | 11 +++++++---- src/components/__tests__/Button.test.tsx | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/components/Button/tokens.ts b/src/components/Button/tokens.ts index 81cee33e6b..2dbe07f54f 100644 --- a/src/components/Button/tokens.ts +++ b/src/components/Button/tokens.ts @@ -18,7 +18,10 @@ type ButtonSizeTokens = { labelVariant: ButtonLabelVariant; containerShapeRound: ShapeToken; containerShapeSquare: ShapeToken; - /** Corner the container morphs to while pressed (MD3: always `small`). */ + /** + * Corner the container morphs to while pressed. Tightens with the size: + * 8dp for XS/S, 12dp for M, 16dp for L/XL. + */ pressedContainerShape: ShapeToken; selectedContainerShapeRound: ShapeToken; selectedContainerShapeSquare: ShapeToken; @@ -63,7 +66,7 @@ const sizes = { labelVariant: 'titleMedium', containerShapeRound: 'full', containerShapeSquare: 'large', - pressedContainerShape: 'small', + pressedContainerShape: 'medium', selectedContainerShapeRound: 'full', selectedContainerShapeSquare: 'large', }, @@ -77,7 +80,7 @@ const sizes = { labelVariant: 'headlineSmall', containerShapeRound: 'full', containerShapeSquare: 'extraLarge', - pressedContainerShape: 'small', + pressedContainerShape: 'large', selectedContainerShapeRound: 'full', selectedContainerShapeSquare: 'extraLarge', }, @@ -91,7 +94,7 @@ const sizes = { labelVariant: 'headlineLarge', containerShapeRound: 'full', containerShapeSquare: 'extraLarge', - pressedContainerShape: 'small', + pressedContainerShape: 'large', selectedContainerShapeRound: 'full', selectedContainerShapeSquare: 'extraLarge', }, diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 1661a33438..d5ebe2f1c1 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -17,6 +17,7 @@ import Button from '../Button/Button'; import { Tokens } from '../Button/tokens'; import { getButtonColors, + getButtonPressedRadius, getButtonRippleColor, getButtonShapeRadius, getButtonSizeStyle, @@ -842,6 +843,24 @@ describe('getButtonShapeRadius', () => { ); }); +// The pressed corner tightens with the size, per the MD3 corner table. +const pressedRadii: [size: ButtonSize, pressed: number][] = [ + ['extra-small', 8], + ['small', 8], + ['medium', 12], + ['large', 16], + ['extra-large', 16], +]; + +describe('getButtonPressedRadius', () => { + it.each(pressedRadii)( + 'returns the pressed radius for size=%s', + (size, pressed) => { + expect(getButtonPressedRadius({ size, theme: getTheme() })).toBe(pressed); + } + ); +}); + describe('shape prop', () => { it('applies the round (full-pill) radius', async () => { await render( @@ -1215,8 +1234,8 @@ describe('shape morph animation', () => { ); spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); - // `pressedContainerShape` is `small` at every size. - expect(springTargets(spy)).toContain(getTheme().shapes.corner.small); + // A large button presses to `large` (16dp), not the small sizes' 8dp. + expect(springTargets(spy)).toContain(getTheme().shapes.corner.large); spy.mockClear(); }); From 5e8c183201d90e4bc862ab203b2bc02c1122de6a Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Wed, 2 Sep 2026 13:20:12 +0200 Subject: [PATCH 34/66] docs: attach the docblock and regenerate the page --- docs/6.x/docs/components/Button/Button.mdx | 14 ++++ docs/src/data/componentDocs6x.json | 85 +++++++++++++++++----- src/components/Button/Button.tsx | 17 ++--- 3 files changed, 90 insertions(+), 26 deletions(-) diff --git a/docs/6.x/docs/components/Button/Button.mdx b/docs/6.x/docs/components/Button/Button.mdx index dbd81347ba..c49d70012e 100644 --- a/docs/6.x/docs/components/Button/Button.mdx +++ b/docs/6.x/docs/components/Button/Button.mdx @@ -8,13 +8,27 @@ import ThemeColorsTable from '@docs/components/ThemeColorsTable.tsx'; import ScreenshotTabs from '@docs/components/ScreenshotTabs.tsx'; import ExtendedExample from '@docs/components/ExtendedExample.tsx'; +A button is component that the user can press to trigger an action. + +## Usage +```js +import * as React from 'react'; +import { Button } from 'react-native-paper'; + +const MyComponent = () => ( + +); +export default MyComponent; +``` ## Props diff --git a/docs/src/data/componentDocs6x.json b/docs/src/data/componentDocs6x.json index b223c9c0a8..65b767b6e0 100644 --- a/docs/src/data/componentDocs6x.json +++ b/docs/src/data/componentDocs6x.json @@ -2990,10 +2990,10 @@ "Button/Button": { "filepath": "Button/Button.tsx", "title": "Button", - "description": "A button is component that the user can press to trigger an action.\n\n## Usage\n```js\nimport * as React from 'react';\nimport { Button } from 'react-native-paper';\n\nconst MyComponent = () => (\n \n);\n\nexport default MyComponent;\n```", + "description": "A button is component that the user can press to trigger an action.\n\n## Usage\n```js\nimport * as React from 'react';\nimport { Button } from 'react-native-paper';\n\nconst MyComponent = () => (\n \n);\n\nexport default MyComponent;\n```", "link": "button", "data": { - "description": "A button is component that the user can press to trigger an action.\n\n## Usage\n```js\nimport * as React from 'react';\nimport { Button } from 'react-native-paper';\n\nconst MyComponent = () => (\n \n);\n\nexport default MyComponent;\n```", + "description": "A button is component that the user can press to trigger an action.\n\n## Usage\n```js\nimport * as React from 'react';\nimport { Button } from 'react-native-paper';\n\nconst MyComponent = () => (\n \n);\n\nexport default MyComponent;\n```", "displayName": "Button", "methods": [], "statics": [], @@ -3002,7 +3002,7 @@ "required": false, "tsType": { "name": "union", - "raw": "'text' | 'outlined' | 'contained' | 'elevated' | 'contained-tonal'", + "raw": "'text' | 'outlined' | 'filled' | 'elevated' | 'tonal'", "elements": [ { "name": "literal", @@ -3014,7 +3014,7 @@ }, { "name": "literal", - "value": "'contained'" + "value": "'filled'" }, { "name": "literal", @@ -3022,13 +3022,13 @@ }, { "name": "literal", - "value": "'contained-tonal'" + "value": "'tonal'" } ] }, - "description": "Mode of the button. You can change the mode to adjust the styling to give it desired emphasis.\n- `text` - flat button without background or outline, used for the lowest priority actions, especially when presenting multiple options.\n- `outlined` - button with an outline without background, typically used for important, but not primary action – represents medium emphasis.\n- `contained` - button with a background color, used for important action, have the most visual impact and high emphasis.\n- `elevated` - button with a background color and elevation, used when absolutely necessary e.g. button requires visual separation from a patterned background. @supported Available in v5.x with theme version 3\n- `contained-tonal` - button with a secondary background color, an alternative middle ground between contained and outlined buttons. @supported Available in v5.x with theme version 3", + "description": "Mode of the button. You can change the mode to adjust the styling to give it desired emphasis. Defaults to `filled`.\n- `filled` - button with a background color, used for the most important action, has the most visual impact and high emphasis. (default)\n- `tonal` - button with a secondary background color, an alternative middle ground between filled and outlined buttons.\n- `elevated` - button with a background color and elevation, used when absolutely necessary e.g. button requires visual separation from a patterned background.\n- `outlined` - button with an outline without background, typically used for important, but not primary action – represents medium emphasis.\n- `text` - flat button without background or outline, used for the lowest priority actions, especially when presenting multiple options.", "defaultValue": { - "value": "'text'", + "value": "'filled'", "computed": false } }, @@ -3037,14 +3037,47 @@ "tsType": { "name": "boolean" }, - "description": "Whether the color is a dark color. A dark button will render light text and vice-versa. Only applicable for:\n * `contained` mode for theme version 2\n * `contained`, `contained-tonal` and `elevated` modes for theme version 3." + "description": "Whether the color is a dark color. A dark button will render light text and vice-versa. Only applicable for the `filled`, `tonal` and `elevated` modes." }, - "compact": { + "size": { + "required": false, + "tsType": { + "name": "ButtonSize" + }, + "description": "Size of the button (Material Design 3 expressive). Defaults to `small`.\n\nThe size controls the minimum height, horizontal padding, icon size, the\ngap between icon and label, and the label typescale.", + "defaultValue": { + "value": "'small'", + "computed": false + } + }, + "shape": { + "required": false, + "tsType": { + "name": "ButtonShape" + }, + "description": "Shape variant of the button (Material Design 3 expressive). Defaults to\n`round`. `'round'` uses the full-pill corner radius; `'square'` uses a\nsmaller per-size corner radius.", + "defaultValue": { + "value": "'round'", + "computed": false + } + }, + "animateShape": { "required": false, "tsType": { "name": "boolean" }, - "description": "Use a compact look, useful for `text` buttons in a row." + "description": "Whether the container animates its corner radius. Defaults to `true`.\n\nWhen `true` the corner springs to the pressed shape while the button is\npressed, and animates between the two shapes as `selected` changes. When\n`false` the shape still changes, it just snaps instead of animating, and\nthere is no press morph at all.\n\nThe press morph is skipped when the OS reduce-motion setting is on.", + "defaultValue": { + "value": "true", + "computed": false + } + }, + "selected": { + "required": false, + "tsType": { + "name": "boolean" + }, + "description": "Turns the button into a Material Design 3 expressive toggle and sets its\nstate. Leave it **undefined** for a plain button — a toggle that is merely\nunselected is a different state, and MD3 gives the two different colours.\n\nWhen defined:\n\n- The button takes its container and label colours from the toggle set for\n its `mode`, which differ between `false` and `true`. `text` is the one\n exception: MD3 defines no text toggle, so it keeps its plain colours.\n- When `true` the `shape` is flipped: `'round'` renders square and vice\n versa, and an `outlined` button drops its outline.\n- `aria-selected` is set so screen readers announce the toggle state." }, "buttonColor": { "required": false, @@ -3074,6 +3107,24 @@ }, "description": "Icon to display for the `Button`." }, + "iconPosition": { + "required": false, + "tsType": { + "name": "union", + "raw": "'leading' | 'trailing'", + "elements": [ + { + "name": "literal", + "value": "'leading'" + }, + { + "name": "literal", + "value": "'trailing'" + } + ] + }, + "description": "Position of the `icon` relative to the label. Defaults to `'leading'`." + }, "disabled": { "required": false, "tsType": { @@ -3089,19 +3140,19 @@ }, "description": "Label text of the button." }, - "uppercase": { + "background": { "required": false, "tsType": { - "name": "boolean" + "name": "PressableAndroidRippleConfig" }, - "description": "Make the label text uppercased. Note that this won't work if you pass React elements as children." + "description": "Type of background drawabale to display the feedback (Android).\nhttps://reactnative.dev/docs/pressable#rippleconfig" }, - "background": { + "rippleColor": { "required": false, "tsType": { - "name": "PressableAndroidRippleConfig" + "name": "ColorValue" }, - "description": "Type of background drawabale to display the feedback (Android).\nhttps://reactnative.dev/docs/pressable#rippleconfig" + "description": "Color of the ripple effect / state layer. Defaults to the label color at\nthe pressed-state opacity." }, "aria-label": { "required": false, @@ -3234,7 +3285,7 @@ ], "raw": "StyleProp" }, - "description": "Style of button's inner content.\nUse this prop to apply custom height and width, to set a custom padding or to set the icon on the right with `flexDirection: 'row-reverse'`." + "description": "Style of button's inner content.\nUse this prop to apply custom height and width or to set a custom padding." }, "maxFontSizeMultiplier": { "required": false, diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 6bc4501489..1ea54cee99 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -42,6 +42,14 @@ import TouchableRipple from '../TouchableRipple/TouchableRipple'; import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; import Text from '../Typography/Text'; +// Elevation levels (MD3) used by the `elevated` mode: level 1 at rest, +// level 2 while pressed. +const initialElevation = 1; +const activeElevation = 2; +// Minimum accessible touch target (dp). Extra-small/small buttons are shorter +// than this and get expanded via hitSlop. +const MIN_TOUCH_TARGET = 48; + export type Props = Omit & { /** * Mode of the button. You can change the mode to adjust the styling to give it desired emphasis. Defaults to `filled`. @@ -215,15 +223,6 @@ export type Props = Omit & { * export default MyComponent; * ``` */ - -// Elevation levels (MD3) used by the `elevated` mode: level 1 at rest, -// level 2 while pressed. -const initialElevation = 1; -const activeElevation = 2; -// Minimum accessible touch target (dp). Extra-small/small buttons are shorter -// than this and get expanded via hitSlop. -const MIN_TOUCH_TARGET = 48; - const Button = ({ disabled, mode = 'filled', From 07a32a98db87801746451ff72fef58b481ef18f5 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Wed, 2 Sep 2026 13:35:07 +0200 Subject: [PATCH 35/66] docs: document Button's style restrictions in the migration guide --- docs/6.x/docs/guides/migration.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index c29b07a3cb..6343846c3e 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -343,6 +343,7 @@ a plain button - **`uppercase`** → `labelStyle={{ textTransform: 'uppercase' }}`. - **`compact`** → `size="extra-small"`. - **`contentStyle={{ flexDirection: 'row-reverse' }}`** → `iconPosition="trailing"`. +- **`elevation`** → elevation follows `mode="elevated"`. ```tsx // Before (v5) @@ -361,6 +362,34 @@ a plain button ``` +#### Style + +`Button` renders on `Surface`, so its `style` follows the same rules: + +- **Border radius** is no longer set through `style`. Use `shape`, or override + the corner token that the size maps to. `round` is the full pill; `square` + reads `corner.medium` at `extra-small` and `small`; `corner.large` at + `medium`; and `corner.extraLarge` at `large` and `extra-large`. +- **Background color** is no longer set through `style`. Use `buttonColor`. +- **Animated styles** must come from Reanimated. A React Native + `Animated.Value` in `style` is not applied. + +```tsx +// Before (v5) + + +// After (v6) + +``` + #### Appearance changes These need no code change, but the rendering differs: From fc5c897fee062327c78310d967ad209b35be2e9e Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Wed, 2 Sep 2026 15:26:53 +0200 Subject: [PATCH 36/66] docs: correct the theme colors table --- docs/6.x/docs/components/Button/Button.mdx | 2 +- docs/src/data/themeColors.ts | 60 +++++++++++++++++----- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/docs/6.x/docs/components/Button/Button.mdx b/docs/6.x/docs/components/Button/Button.mdx index c49d70012e..5fbb154ecc 100644 --- a/docs/6.x/docs/components/Button/Button.mdx +++ b/docs/6.x/docs/components/Button/Button.mdx @@ -299,7 +299,7 @@ export default MyComponent; ## Theme colors - + diff --git a/docs/src/data/themeColors.ts b/docs/src/data/themeColors.ts index bbf2121c66..d05d6b58c8 100644 --- a/docs/src/data/themeColors.ts +++ b/docs/src/data/themeColors.ts @@ -46,7 +46,7 @@ export const themeColors = { }, }, Button: { - active: { + default: { filled: { backgroundColor: 'theme.colors.primary', textColor: 'theme.colors.onPrimary', @@ -56,36 +56,72 @@ export const themeColors = { textColor: 'theme.colors.onSecondaryContainer', }, elevated: { - backgroundColor: 'theme.colors.elevation.level1', + backgroundColor: 'theme.colors.surfaceContainerLow', textColor: 'theme.colors.primary', }, outlined: { textColor: 'theme.colors.onSurfaceVariant', - borderColor: 'theme.colors.outline', + borderColor: 'theme.colors.outlineVariant', }, text: { textColor: 'theme.colors.primary', }, }, + selected: { + filled: { + backgroundColor: 'theme.colors.primary', + textColor: 'theme.colors.onPrimary', + }, + tonal: { + backgroundColor: 'theme.colors.secondary', + textColor: 'theme.colors.onSecondary', + }, + elevated: { + backgroundColor: 'theme.colors.primary', + textColor: 'theme.colors.onPrimary', + }, + outlined: { + backgroundColor: 'theme.colors.inverseSurface', + textColor: 'theme.colors.inverseOnSurface', + }, + }, + unselected: { + filled: { + backgroundColor: 'theme.colors.surfaceContainer', + textColor: 'theme.colors.onSurfaceVariant', + }, + tonal: { + backgroundColor: 'theme.colors.secondaryContainer', + textColor: 'theme.colors.onSecondaryContainer', + }, + elevated: { + backgroundColor: 'theme.colors.surfaceContainerLow', + textColor: 'theme.colors.primary', + }, + outlined: { + textColor: 'theme.colors.onSurfaceVariant', + borderColor: 'theme.colors.outlineVariant', + }, + }, disabled: { filled: { - backgroundColor: 'theme.colors.surfaceDisabled', - textColor: 'theme.colors.onSurfaceDisabled', + backgroundColor: 'theme.colors.onSurface', + textColor: 'theme.colors.onSurface', }, tonal: { - backgroundColor: 'theme.colors.surfaceDisabled', - textColor: 'theme.colors.onSurfaceDisabled', + backgroundColor: 'theme.colors.onSurface', + textColor: 'theme.colors.onSurface', }, elevated: { - backgroundColor: 'theme.colors.surfaceDisabled', - textColor: 'theme.colors.onSurfaceDisabled', + backgroundColor: 'theme.colors.onSurface', + textColor: 'theme.colors.onSurface', }, outlined: { - textColor: 'theme.colors.onSurfaceDisabled', - borderColor: 'theme.colors.surfaceDisabled', + textColor: 'theme.colors.onSurface', + borderColor: 'theme.colors.outlineVariant', }, text: { - textColor: 'theme.colors.onSurfaceDisabled', + textColor: 'theme.colors.onSurface', }, }, }, From 8fbbf546ba2410f218568961922ad79ecb6f863b Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Thu, 3 Sep 2026 10:59:45 +0200 Subject: [PATCH 37/66] fix: update example custom ripple color for button --- example/src/Examples/ButtonExample.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index 22102d015a..21cc82be37 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -289,7 +289,7 @@ const ButtonExample = () => { ); + // No shape flip: a round text button stays the pill, 40 / 2 = 20. expect(screen.getByTestId('button-container')).toHaveStyle({ - borderRadius: 12, + borderRadius: 20, }); - expect(screen.getByTestId('button')).toBeSelected(); + // And no toggle state is announced. + expect(screen.getByTestId('button')).not.toBeSelected(); }); it('leaves a plain button untouched when `selected` is omitted', () => { From 160e81773f08a9305b858262b3b9efdc4b78260f Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 4 Sep 2026 12:09:03 +0200 Subject: [PATCH 40/66] fix: drop the inert 48dp hitSlop expansion --- src/components/Button/Button.tsx | 30 +------ src/components/__tests__/Button.test.tsx | 53 ------------- .../__snapshots__/Banner.test.tsx.snap | 24 ------ .../__snapshots__/Button.test.tsx.snap | 78 ------------------- .../__snapshots__/DataTable.test.tsx.snap | 6 -- .../__snapshots__/Menu.test.tsx.snap | 12 --- .../__snapshots__/Snackbar.test.tsx.snap | 6 -- 7 files changed, 1 insertion(+), 208 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 6010e695f3..5fa6afad58 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -47,9 +47,6 @@ import Text from '../Typography/Text'; // level 2 while pressed. const initialElevation = 1; const activeElevation = 2; -// Minimum accessible touch target (dp). Extra-small/small buttons are shorter -// than this and get expanded via hitSlop. -const MIN_TOUCH_TARGET = 48; export type Props = Omit & { /** @@ -464,31 +461,6 @@ const Button = ({ [labelStyle] ); - // Extra-small/small buttons are shorter than the 48dp minimum accessible - // touch target, so expand the press area with hitSlop without changing the - // visual size. A user-supplied `hitSlop` wins on the axes it sets. - const hitSlopWithMinTarget = React.useMemo(() => { - const verticalSlop = Math.max( - 0, - (MIN_TOUCH_TARGET - sizeStyle.minHeight) / 2 - ); - if (verticalSlop === 0) { - return hitSlop; - } - if (hitSlop == null) { - return { top: verticalSlop, bottom: verticalSlop }; - } - // A numeric hitSlop is an explicit uniform override — respect it as-is. - if (typeof hitSlop === 'number') { - return hitSlop; - } - return { - ...hitSlop, - top: hitSlop.top ?? verticalSlop, - bottom: hitSlop.bottom ?? verticalSlop, - }; - }, [hitSlop, sizeStyle]); - const contentBoxStyle = React.useMemo( () => ({ minHeight: sizeStyle.minHeight - borderWidth * 2, @@ -546,7 +518,7 @@ const Button = ({ aria-disabled={disabled} aria-selected={toggleSelected} accessible={accessible} - hitSlop={hitSlopWithMinTarget} + hitSlop={hitSlop} disabled={disabled} testID={testID} theme={theme} diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 446d22e307..8718f56db2 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -770,59 +770,6 @@ describe('size prop', () => { ); }); -describe('accessible touch target', () => { - it('expands extra-small buttons to the 48dp minimum target', async () => { - await render( - - ); - // (48 - 32) / 2 = 8 - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - expect(screen.getByTestId('button').props.hitSlop).toMatchObject({ - top: 8, - bottom: 8, - }); - }); - - it('expands small buttons to the 48dp minimum target', async () => { - await render( - - ); - // (48 - 40) / 2 = 4 - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - expect(screen.getByTestId('button').props.hitSlop).toMatchObject({ - top: 4, - bottom: 4, - }); - }); - - it('does not add hitSlop for buttons already at least 48dp tall', async () => { - await render( - - ); - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - expect(screen.getByTestId('button').props.hitSlop).toBeUndefined(); - }); - - it('keeps a user-supplied hitSlop axis while filling the rest', async () => { - await render( - - ); - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - expect(screen.getByTestId('button').props.hitSlop).toMatchObject({ - top: 20, - bottom: 8, - }); - }); -}); - const shapeRadii: [size: ButtonSize, round: number, square: number][] = [ ['extra-small', 9999, 12], ['small', 9999, 12], diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index eb8d4dfa04..62cfa8fa4e 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -310,12 +310,6 @@ exports[`render visible banner, with custom theme 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -938,12 +932,6 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1348,12 +1336,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1574,12 +1556,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index a2003868c9..ade5af6e0a 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -132,12 +132,6 @@ exports[`renders button with an accessibility hint 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -360,12 +354,6 @@ exports[`renders button with an accessibility label 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -587,12 +575,6 @@ exports[`renders button with button color 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -814,12 +796,6 @@ exports[`renders button with color 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1041,12 +1017,6 @@ exports[`renders button with custom testID 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1268,12 +1238,6 @@ exports[`renders button with icon 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1528,12 +1492,6 @@ exports[`renders button with icon in reverse order 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1808,12 +1766,6 @@ exports[`renders disabled button 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -2035,12 +1987,6 @@ exports[`renders filled button by default 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -2262,12 +2208,6 @@ exports[`renders filled button with mode 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -2489,12 +2429,6 @@ exports[`renders loading button 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -2907,12 +2841,6 @@ exports[`renders outlined button with mode 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -3134,12 +3062,6 @@ exports[`renders text button with mode 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index a39909198f..7ac3b07f7f 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1654,12 +1654,6 @@ exports[`DataTable.Pagination renders data table pagination with options select accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index d3b7da8309..5cbc506d71 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -143,12 +143,6 @@ exports[`renders not visible menu 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -385,12 +379,6 @@ exports[`renders visible menu 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} diff --git a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap index 2ce38971b0..4cd4c319f2 100644 --- a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap @@ -612,12 +612,6 @@ exports[`renders snackbar with action button 1`] = ` accessible={true} collapsable={false} focusable={true} - hitSlop={ - { - "bottom": 4, - "top": 4, - } - } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} From 8450323bfa9477e0a895c150ff321a11f0522462 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Mon, 7 Sep 2026 10:25:50 +0200 Subject: [PATCH 41/66] fix: thicken the outlined stroke on l and xl buttons --- src/components/Button/tokens.ts | 4 ++-- src/components/__tests__/Button.test.tsx | 29 ++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/components/Button/tokens.ts b/src/components/Button/tokens.ts index 9f1ef057bf..6c4e9666ae 100644 --- a/src/components/Button/tokens.ts +++ b/src/components/Button/tokens.ts @@ -76,7 +76,7 @@ const sizes = { iconLabelSpace: 12, leadingSpace: 48, trailingSpace: 48, - outlinedOutlineWidth: 1, + outlinedOutlineWidth: 2, labelVariant: 'headlineSmall', containerShapeRound: 'full', containerShapeSquare: 'extraLarge', @@ -90,7 +90,7 @@ const sizes = { iconLabelSpace: 16, leadingSpace: 64, trailingSpace: 64, - outlinedOutlineWidth: 1, + outlinedOutlineWidth: 3, labelVariant: 'headlineLarge', containerShapeRound: 'full', containerShapeSquare: 'extraLarge', diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 8718f56db2..4b911ba256 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -705,8 +705,8 @@ const sizeMetrics: [ ['extra-small', 32, 12, 12, 20, 4, 1, 'labelLarge'], ['small', 40, 16, 16, 20, 8, 1, 'labelLarge'], ['medium', 56, 24, 24, 24, 8, 1, 'titleMedium'], - ['large', 96, 48, 48, 32, 12, 1, 'headlineSmall'], - ['extra-large', 136, 64, 64, 40, 16, 1, 'headlineLarge'], + ['large', 96, 48, 48, 32, 12, 2, 'headlineSmall'], + ['extra-large', 136, 64, 64, 40, 16, 3, 'headlineLarge'], ]; describe('getButtonSizeStyle', () => { @@ -1102,6 +1102,15 @@ it('forwards `style` to the shadow host', async () => { describe('container height', () => { const MODES = ['filled', 'tonal', 'elevated', 'outlined', 'text'] as const; + // The outlined stroke thickens with the size (1dp up to M, 2dp L, 3dp XL), + // so every size has to be checked against its own token metrics. + const SIZES = [ + 'extra-small', + 'small', + 'medium', + 'large', + 'extra-large', + ] as const; const { containerHeight, leadingSpace } = Tokens.sizes.small; // The rendered container is the content box plus the outline Yoga draws @@ -1135,6 +1144,22 @@ describe('container height', () => { }); }); + it.each(SIZES)( + 'insets the %s outline so the box stays on the token metrics', + async (size) => { + await render( + + ); + + expect(renderedBox('button')).toEqual({ + height: Tokens.sizes[size].containerHeight, + leading: Tokens.sizes[size].leadingSpace, + }); + } + ); + it('keeps an outlined toggle the same size in both states', async () => { await render( + ); + + // The slop expands past the container, so an ancestor sized to the + // container must not clip. This is what made the previous `hitSlop` + // inert, and a passed-through prop alone would not catch it. + expect(styleOf('button-container-outer-layer').overflow).not.toBe('hidden'); + expect(styleOf('button-container').overflow).not.toBe('hidden'); + }); + + it('still clips the ripple to the container radius', async () => { + await render( + + ); + + // Clipping moved onto the touchable, so the ripple keeps the pill shape + // now that the clip view above it no longer hides the overflow. + const style = styleOf('button'); + expect(style.overflow).toBe('hidden'); + expect(style.borderRadius).toBe(Tokens.sizes.small.containerHeight / 2); + }); + + // An outlined button's children sit inside the outline, so they round a + // border width tighter than the container. Given the outer radius instead, + // their corners overshoot the outline's inner edge and the background shows + // through the gap — worst on XL, which has the thickest stroke. + it.each(sizeMetrics)( + 'insets the %s ripple radius by the outline width', + async (size) => { + await render( + + ); + + const outer = StyleSheet.flatten( + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + screen.getByTestId('button-container').props.style + ); + + expect(styleOf('button').borderRadius).toBe( + outer.borderRadius - Tokens.sizes[size].outlinedOutlineWidth + ); + } + ); + + it('leaves the ripple radius alone when there is no outline', async () => { + await render( + + ); + + const outer = StyleSheet.flatten( + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + screen.getByTestId('button-container').props.style + ); + + expect(styleOf('button').borderRadius).toBe(outer.borderRadius); + }); + + it.each(sizeMetrics)( + 'expands the %s target to the outer bound', + async (size) => { + await render( + + ); + + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + const { hitSlop } = screen.getByTestId('button').props; + const { containerHeight, outerBoundHeight } = Tokens.sizes[size]; + + expect(containerHeight + hitSlop.top + hitSlop.bottom).toBe( + outerBoundHeight + ); + } + ); + + it('lets a caller hitSlop win', async () => { + await render( + + ); + + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + expect(screen.getByTestId('button').props.hitSlop).toBe(12); + }); + + it('takes `null` as an opt out', async () => { + await render( + + ); + + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + const { hitSlop } = screen.getByTestId('button').props; + + expect(hitSlop).toBeNull(); + }); +}); + describe('shape morph animation', () => { beforeEach(() => { jest.mocked(Reanimated.withSpring).mockClear(); diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index 62cfa8fa4e..c1ac80cc5c 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -275,7 +275,6 @@ exports[`render visible banner, with custom theme 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "transparent", @@ -310,6 +309,14 @@ exports[`render visible banner, with custom theme 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -325,7 +332,11 @@ exports[`render visible banner, with custom theme 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -897,7 +908,6 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "transparent", @@ -932,6 +942,14 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -947,7 +965,11 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -1301,7 +1323,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "transparent", @@ -1336,6 +1357,14 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1351,7 +1380,11 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -1521,7 +1554,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "transparent", @@ -1556,6 +1588,14 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1571,7 +1611,11 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index ade5af6e0a..a7dec0db4c 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -96,7 +96,6 @@ exports[`renders button with an accessibility hint 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "rgba(103, 80, 164, 1)", @@ -132,6 +131,14 @@ exports[`renders button with an accessibility hint 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -147,7 +154,11 @@ exports[`renders button with an accessibility hint 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -319,7 +330,6 @@ exports[`renders button with an accessibility label 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "rgba(103, 80, 164, 1)", @@ -354,6 +364,14 @@ exports[`renders button with an accessibility label 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -369,7 +387,11 @@ exports[`renders button with an accessibility label 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -540,7 +562,6 @@ exports[`renders button with button color 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "#e91e63", @@ -575,6 +596,14 @@ exports[`renders button with button color 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -590,7 +619,11 @@ exports[`renders button with button color 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -761,7 +794,6 @@ exports[`renders button with color 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "rgba(103, 80, 164, 1)", @@ -796,6 +828,14 @@ exports[`renders button with color 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -811,7 +851,11 @@ exports[`renders button with color 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -982,7 +1026,6 @@ exports[`renders button with custom testID 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "rgba(103, 80, 164, 1)", @@ -1017,6 +1060,14 @@ exports[`renders button with custom testID 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1032,7 +1083,11 @@ exports[`renders button with custom testID 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="custom:testID" @@ -1203,7 +1258,6 @@ exports[`renders button with icon 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "rgba(103, 80, 164, 1)", @@ -1238,6 +1292,14 @@ exports[`renders button with icon 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1253,7 +1315,11 @@ exports[`renders button with icon 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -1457,7 +1523,6 @@ exports[`renders button with icon in reverse order 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "rgba(103, 80, 164, 1)", @@ -1492,6 +1557,14 @@ exports[`renders button with icon in reverse order 1`] = ` accessible={true} collapsable={false} focusable={true} + hitSlop={ + { + "bottom": 4, + "left": 0, + "right": 0, + "top": 4, + } + } onBlur={[Function]} onClick={[Function]} onFocus={[Function]} @@ -1507,7 +1580,11 @@ exports[`renders button with icon in reverse order 1`] = ` { "overflow": "hidden", }, - undefined, + [ + { + "borderRadius": 20, + }, + ], ] } testID="button" @@ -1713,7 +1790,6 @@ exports[`renders disabled button 1`] = ` [ { "borderStyle": "solid", - "overflow": "hidden", }, { "backgroundColor": "transparent", @@ -1728,6 +1804,7 @@ exports[`renders disabled button 1`] = ` testID="button-container" > Date: Fri, 23 Oct 2026 10:15:36 +0200 Subject: [PATCH 43/66] fix: apply review suggestions from @satya164 --- example/src/Examples/CardExample.tsx | 6 ++++-- src/components/Button/utils.tsx | 28 ++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/example/src/Examples/CardExample.tsx b/example/src/Examples/CardExample.tsx index 921d7399d2..09b732d9d0 100644 --- a/example/src/Examples/CardExample.tsx +++ b/example/src/Examples/CardExample.tsx @@ -109,14 +109,16 @@ const CardExample = () => { + diff --git a/example/src/Examples/Dialogs/DialogWithCustomColors.tsx b/example/src/Examples/Dialogs/DialogWithCustomColors.tsx index df24a5f3f6..e688913d11 100644 --- a/example/src/Examples/Dialogs/DialogWithCustomColors.tsx +++ b/example/src/Examples/Dialogs/DialogWithCustomColors.tsx @@ -26,7 +26,7 @@ const DialogWithCustomColors = ({ - diff --git a/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx b/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx index d7a37e5569..dd52bd5d92 100644 --- a/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx +++ b/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx @@ -23,10 +23,12 @@ const DialogWithDismissableBackButton = ({ - - + ); diff --git a/example/src/Examples/Dialogs/DialogWithIcon.tsx b/example/src/Examples/Dialogs/DialogWithIcon.tsx index 0d5f595f9c..5a577343cd 100644 --- a/example/src/Examples/Dialogs/DialogWithIcon.tsx +++ b/example/src/Examples/Dialogs/DialogWithIcon.tsx @@ -22,10 +22,12 @@ const DialogWithIcon = ({ - - + ); diff --git a/example/src/Examples/Dialogs/DialogWithLongText.tsx b/example/src/Examples/Dialogs/DialogWithLongText.tsx index eed30ec358..bd0dd349e4 100644 --- a/example/src/Examples/Dialogs/DialogWithLongText.tsx +++ b/example/src/Examples/Dialogs/DialogWithLongText.tsx @@ -61,7 +61,9 @@ const DialogWithLongText = ({ - + ); diff --git a/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx b/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx index 5a729d6299..73b61b14eb 100644 --- a/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx +++ b/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx @@ -82,8 +82,12 @@ const DialogWithRadioBtns = ({ visible, close }: Props) => { - - + + ); diff --git a/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx b/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx index d775ba011c..deae141b13 100644 --- a/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx +++ b/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx @@ -18,10 +18,12 @@ const DialogWithUndismissableBackButton = ({ - - + ); diff --git a/example/src/Examples/Dialogs/UndismissableDialog.tsx b/example/src/Examples/Dialogs/UndismissableDialog.tsx index 79dc777484..fa10e5c6a4 100644 --- a/example/src/Examples/Dialogs/UndismissableDialog.tsx +++ b/example/src/Examples/Dialogs/UndismissableDialog.tsx @@ -15,10 +15,12 @@ const UndismissableDialog = ({ This is an undismissable dialog!! - - + ); diff --git a/src/components/Dialog/Dialog.tsx b/src/components/Dialog/Dialog.tsx index 218353d5e8..b15803ea0c 100644 --- a/src/components/Dialog/Dialog.tsx +++ b/src/components/Dialog/Dialog.tsx @@ -87,7 +87,7 @@ const DIALOG_ELEVATION: Elevation = 3; * This is simple dialog * * - * + * * * * diff --git a/src/components/Dialog/DialogActions.tsx b/src/components/Dialog/DialogActions.tsx index fb8e0c56e4..29ab90006f 100644 --- a/src/components/Dialog/DialogActions.tsx +++ b/src/components/Dialog/DialogActions.tsx @@ -33,8 +33,8 @@ export type Props = ViewProps & { * return ( * * - * - * + * + * * * * ); From 8ef85382baab5914942f1f80143e305f02a224f9 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 23 Oct 2026 10:43:22 +0200 Subject: [PATCH 45/66] docs: correct the size and padding notes for Button --- docs/6.x/docs/guides/migration.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 9d6c7f4b78..fdf2732844 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -463,8 +463,9 @@ hierarchy. Pass `mode="text"` to keep the old look. #### Size and shape -New `size` and `shape` props. `size` defaults to `small`, which matches the -only size 5.x had, so existing buttons keep their metrics. +New `size` and `shape` props. `size` defaults to `small`, which keeps the +**40dp** height of the only size 5.x had. Its horizontal padding is different, +see the appearance changes below. ```tsx @@ -547,6 +548,10 @@ These need no code change, but the rendering differs: - `labelStyle={{ fontSize }}` no longer changes the icon size. The icon follows `size`; set both if you need them to match. - The resting corner is the full pill radius instead of a fixed **20dp**. +- At the default `small` size, the horizontal padding is **16dp** for every + mode. In 5.x, the label margins were **24dp** for `filled`, `tonal`, + `elevated` and `outlined`, and **12dp** for `text` (**16dp** with an icon). + Existing buttons change width. ### Card From 57d5c9c81492cccc3fecb23e4f29db15d548435b Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 23 Oct 2026 10:43:31 +0200 Subject: [PATCH 46/66] docs: correct the compact and elevation notes for Button --- docs/6.x/docs/guides/migration.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index fdf2732844..82d4394a1c 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -253,7 +253,7 @@ When the dialog is `dismissable`, screen reader users can dismiss it with a visu #### `Dialog.Actions` -`Dialog.Actions` no longer injects `compact` on its buttons. To keep the previous behavior, you need to add `size="extra-small"` (which replaces `compact`) and `mode="text"` (since the default [`Button`](#button) mode is now `filled`) to each button: +`Dialog.Actions` no longer injects `compact` on its buttons. To get close to the previous look, add `size="extra-small"` (which replaces `compact`) and `mode="text"` (since the default [`Button`](#button) mode is now `filled`) to each button. The buttons get shorter: `extra-small` is **32dp** tall, while `compact` buttons kept the **40dp** height: ```tsx // Before (v5) @@ -489,9 +489,10 @@ a plain button. It is ignored on `mode="text"`, which MD3 gives no toggle. #### Removed props - **`uppercase`** → `labelStyle={{ textTransform: 'uppercase' }}`. -- **`compact`** → `size="extra-small"`. +- **`compact`** → `size="extra-small"`. This also changes the height: + `extra-small` is **32dp** tall, while `compact` only reduced the horizontal + margins and the min width and kept the **40dp** height. - **`contentStyle={{ flexDirection: 'row-reverse' }}`** → `iconPosition="trailing"`. -- **`elevation`** → elevation follows `mode="elevated"`. ```tsx // Before (v5) From 3c5c83f27b2647f178cee6f92d2f5b656af32aa7 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Mon, 26 Oct 2026 17:11:02 +0100 Subject: [PATCH 47/66] fix: stop reading a ref during render in Button --- src/components/Button/Button.tsx | 10 +++++----- src/components/Button/utils.tsx | 8 +------- src/components/__tests__/Button.test.tsx | 2 -- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index fbf8821dba..3c24e5c937 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -428,18 +428,18 @@ const Button = ({ const containerColor = backgroundOpacity < 1 ? 'transparent' : backgroundColor; - // Snap rather than cross-fade when a transparent container is involved — see - // `getButtonTransitionDuration`. - const previousContainerColorRef = React.useRef(containerColor); + // Snap rather than animate when a transparent container is involved. + const [committedContainerColor, setCommittedContainerColor] = + React.useState(containerColor); React.useEffect(() => { - previousContainerColorRef.current = containerColor; + setCommittedContainerColor(containerColor); }, [containerColor]); const surfaceTransitionDuration = getButtonTransitionDuration({ theme, pressed, containerColor, - previousContainerColor: previousContainerColorRef.current, + previousContainerColor: committedContainerColor, }); // The clip carries the same animated radius as the `Surface`, so the ripple diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 58939dc0a7..20a4c4030c 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -122,13 +122,7 @@ export const getButtonShapeRadius = ({ /** * Duration of `Surface`'s background and elevation transition. * - * `Surface` cross-fades its background, and Reanimated interpolates colors in - * RGB, so a fade to or from `transparent` passes through gray. `outlined` and - * `text` are the modes that show it: their container is transparent, so the - * clip inside doesn't paint over the fade. `mode` is a static prop rather than - * a state, so there is nothing worth cross-fading — snap instead. A toggle's - * color change still animates, both of its containers being opaque, and so does - * the elevation of an `elevated` button, whose container is never transparent. + * Snaps when the container is or was transparent. */ export const getButtonTransitionDuration = ({ theme, diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index b9df09afea..81496bb153 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -885,8 +885,6 @@ describe('getButtonTransitionDuration', () => { ).toBe(scaled('short4')); }); - // Fading to or from `transparent` interpolates through gray, which is what - // made a mode switch leave `outlined` looking like a filled gray button. it('snaps when the container becomes transparent', () => { expect( getButtonTransitionDuration({ From f4e502a45d1d9d8c05d9e3bf04443f3dcf0ce6ed Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Tue, 27 Oct 2026 11:05:35 +0100 Subject: [PATCH 48/66] refactor: animate the Button shape with a spring-shaped CSS transition --- src/components/Button/Button.tsx | 125 ++++++---------- src/components/Surface.tsx | 90 ++++++++--- src/components/__tests__/Button.test.tsx | 109 +++----------- .../__snapshots__/Banner.test.tsx.snap | 40 ++++- .../__snapshots__/Button.test.tsx.snap | 140 ++++++++++++++---- .../__snapshots__/DataTable.test.tsx.snap | 10 +- .../__snapshots__/Menu.test.tsx.snap | 40 ++++- .../__snapshots__/Snackbar.test.tsx.snap | 10 +- src/theme/__tests__/motion.test.ts | 48 ++++++ src/theme/tokens/sys/motion.ts | 51 +++++++ 10 files changed, 423 insertions(+), 240 deletions(-) create mode 100644 src/theme/__tests__/motion.test.ts diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 3c24e5c937..1a8a9f9e9b 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -11,12 +11,7 @@ import type { ViewStyle, } from 'react-native'; -import Reanimated, { - ReduceMotion, - useAnimatedStyle, - useSharedValue, - withSpring, -} from 'react-native-reanimated'; +import Reanimated, { linear } from 'react-native-reanimated'; import { getButtonColors, @@ -32,7 +27,7 @@ import type { ButtonMode, ButtonShape, ButtonSize } from './utils'; import { getDefaultDirection, useLocale } from '../../core/locale'; import { useInternalTheme } from '../../core/theming'; import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; -import { toRawSpring } from '../../theme/tokens/sys/motion'; +import { toSpringCurve } from '../../theme/tokens/sys/motion'; import type { ThemeProp } from '../../theme/types'; import hasTouchHandler from '../../utils/hasTouchHandler'; import ActivityIndicator from '../ActivityIndicator'; @@ -82,7 +77,7 @@ export type Props = Omit & { /** * Whether the container animates its corner radius. Defaults to `true`. * - * When `true` the corner springs to the pressed shape while the button is + * When `true` the corner animates to the pressed shape while the button is * pressed, and animates between the two shapes as `selected` changes. When * `false` the shape still changes, it just snaps instead of animating, and * there is no press morph at all. @@ -280,9 +275,6 @@ const Button = ({ }); const reduceMotion = useReduceMotion(); - const reanimatedReduceMotion = reduceMotion - ? ReduceMotion.Always - : ReduceMotion.Never; const isElevationEntitled = !disabled && isMode('elevated'); @@ -308,87 +300,51 @@ const Button = ({ const effectiveHitSlop = hitSlop === undefined ? getButtonHitSlop(size) : hitSlop; - // Shape morph: animate the corner on press (→ the pressed shape token) and on - // the `selected`/shape toggle. - const animateShape = animateShapeProp; - // A press morph under reduce-motion would pop instantly rather than animate, - // so skip it; a `selected` change still snaps to the new shape. - const morphOnPress = animateShape && !reduceMotion; + // A CSS transition shaped like the MD3 fast spatial spring animates the + // corner. Under reduce motion, or with `animateShape` off, it snaps. + const animatesShape = animateShapeProp && !reduceMotion; // `round` resolves to the `cornerFull` sentinel, so use the real pill radius - // (half the container height) instead: it keeps the spring bounded and lets - // the ripple clip match the container exactly. + // (half the container height) instead: it keeps the transition bounded and + // lets the ripple clip match the container exactly. const restingRadius = effectiveShape === 'round' ? sizeStyle.minHeight / 2 : getButtonShapeRadius({ size, shape, theme, selected: toggleSelected }); - const pressedRadius = getButtonPressedRadius({ size, theme }); - const animatedRadius = useSharedValue(restingRadius); - const restingRadiusRef = React.useRef(restingRadius); - const isRadiusMountedRef = React.useRef(false); - - // The morph stays imperative: a press can interrupt it mid-flight, which a - // duration-based transition can't express. - const radiusSpringConfig = React.useMemo( - () => ({ - ...toRawSpring(theme.motion.spring.fast.spatial), - reduceMotion: reanimatedReduceMotion, - }), - [theme.motion.spring.fast.spatial, reanimatedReduceMotion] - ); + const radius = + animatesShape && pressed + ? getButtonPressedRadius({ size, theme }) + : restingRadius; + const shapeSpring = React.useMemo(() => { + const { points, duration } = toSpringCurve( + theme.motion.spring.fast.spatial + ); + return { timingFunction: linear(...points), duration }; + }, [theme.motion.spring.fast.spatial]); + const shapeTransitionDuration = animatesShape + ? shapeSpring.duration * theme.animation.scale + : 0; - const springRadiusTo = React.useCallback( - (toValue: number) => { - animatedRadius.value = withSpring(toValue, radiusSpringConfig); - }, - [animatedRadius, radiusSpringConfig] - ); + const tracksPress = animatesShape || isElevationEntitled; const handlePressIn = React.useCallback( (e: GestureResponderEvent) => { onPressIn?.(e); - if (morphOnPress) { - springRadiusTo(pressedRadius); - } - if (isElevationEntitled) { + if (tracksPress) { setPressed(true); } }, - [ - onPressIn, - morphOnPress, - springRadiusTo, - pressedRadius, - isElevationEntitled, - ] + [onPressIn, tracksPress] ); const handlePressOut = React.useCallback( (e: GestureResponderEvent) => { onPressOut?.(e); - if (morphOnPress) { - springRadiusTo(restingRadiusRef.current); - } - if (isElevationEntitled) { - setPressed(false); - } + // Unconditional, so a stale `pressed` can't stick. + setPressed(false); }, - [onPressOut, morphOnPress, springRadiusTo, isElevationEntitled] + [onPressOut] ); - // Snap on mount; animate when a toggle/shape change moves the resting radius. - React.useEffect(() => { - restingRadiusRef.current = restingRadius; - if (!isRadiusMountedRef.current) { - isRadiusMountedRef.current = true; - return; - } - if (animateShape) { - springRadiusTo(restingRadius); - } else { - animatedRadius.value = restingRadius; - } - }, [restingRadius, animateShape, animatedRadius, springRadiusTo]); - const { backgroundColor, borderColor, @@ -442,8 +398,8 @@ const Button = ({ previousContainerColor: committedContainerColor, }); - // The clip carries the same animated radius as the `Surface`, so the ripple - // and the disabled overlay follow the morph. + // The clip carries the same radius transition as the `Surface`, so the + // ripple and the disabled overlay follow the morph. // // TODO: revisit the focus ring's placement once #5084 lands. // https://github.com/callstack/react-native-paper/pull/5084 adds MD3 keyboard @@ -453,15 +409,18 @@ const Button = ({ // `focusRing="outward"` and warns an outward ring is trimmed by "any clipping // ancestor sized to its content", which is exactly this view. Button will // likely need `focusRing="inward"`, or the ring on the outer view. - const clipStyle = useAnimatedStyle( - () => ({ borderRadius: animatedRadius.value }), - [animatedRadius] - ); + const shapeTransitionStyle = { + transitionProperty: 'borderRadius', + transitionDuration: shapeTransitionDuration, + transitionTimingFunction: shapeSpring.timingFunction, + } as const; - const innerClipStyle = useAnimatedStyle( - () => ({ borderRadius: Math.max(0, animatedRadius.value - borderWidth) }), - [animatedRadius, borderWidth] - ); + const clipStyle = { ...shapeTransitionStyle, borderRadius: radius }; + + const innerClipStyle = { + ...shapeTransitionStyle, + borderRadius: Math.max(0, radius - borderWidth), + }; const outlineStyle = React.useMemo( () => ({ backgroundColor: containerColor, borderColor, borderWidth }), @@ -496,9 +455,11 @@ const Button = ({ {...rest} ref={ref} backgroundColor={containerColor} - borderRadius={animatedRadius} + borderRadius={radius} elevation={elevation} transitionDuration={surfaceTransitionDuration} + shapeTransitionDuration={shapeTransitionDuration} + shapeTransitionTimingFunction={shapeSpring.timingFunction} style={[styles.button, style]} > diff --git a/src/components/Surface.tsx b/src/components/Surface.tsx index a91fd938d5..10da26b27b 100644 --- a/src/components/Surface.tsx +++ b/src/components/Surface.tsx @@ -6,6 +6,7 @@ import Animated, { cubicBezier, isSharedValue, type AnimatedStyle, + type CSSTransitionTimingFunction, useAnimatedStyle, } from 'react-native-reanimated'; @@ -95,6 +96,15 @@ export type Props = Omit & * milliseconds. */ transitionDuration?: number; + /** + * Duration of the `borderRadius` transition in milliseconds. Undefined + * means no transition. + */ + shapeTransitionDuration?: number; + /** + * Timing function of the `borderRadius` transition. + */ + shapeTransitionTimingFunction?: CSSTransitionTimingFunction; /** * Style of the Surface. * @@ -191,6 +201,8 @@ const Surface = ({ testID, mode = 'elevated', transitionDuration: customTransitionDuration, + shapeTransitionDuration, + shapeTransitionTimingFunction, ref, ...rest }: Props) => { @@ -203,8 +215,13 @@ const Surface = ({ const backgroundStyle = { backgroundColor }; + // A CSS transition can't animate a value set by the animated style. + const transitionsRadius = + shapeTransitionDuration !== undefined && typeof borderRadius === 'number'; + const radiusStyles = transitionsRadius ? [{ borderRadius }] : []; + const shapeProps = { - borderRadius, + ...(transitionsRadius ? null : { borderRadius }), borderBottomEndRadius, borderBottomLeftRadius, borderBottomRightRadius, @@ -230,21 +247,51 @@ const Surface = ({ const transitionDuration = customTransitionDuration ?? theme.motion.duration.short3 * theme.animation.scale; - const transitionDurationStyle: AnimatedStyle = { - transitionDuration, - }; const transitionTimingFunction = cubicBezier(...theme.motion.easing.standard); - const transitionProperty = + const colorTransitionProperty = // FIXME: Reanimated can't animate PlatformColor and DynamicColorIOS typeof backgroundColor === 'string' ? ['backgroundColor' as const] : []; + const getTransitionStyles = ( + shadowProperties: ( + | 'boxShadow' + | 'elevation' + | 'shadowOpacity' + | 'shadowOffset' + | 'shadowRadius' + )[] + ) => { + const properties = [...colorTransitionProperty, ...shadowProperties]; + const transitionStyle: AnimatedStyle = { + transitionTimingFunction: + transitionsRadius && shapeTransitionTimingFunction !== undefined + ? [ + ...properties.map(() => transitionTimingFunction), + shapeTransitionTimingFunction, + ] + : transitionTimingFunction, + transitionProperty: transitionsRadius + ? [...properties, 'borderRadius'] + : properties, + }; + const transitionDurationStyle: AnimatedStyle = { + transitionDuration: + transitionsRadius && shapeTransitionDuration !== undefined + ? [ + ...properties.map(() => transitionDuration), + shapeTransitionDuration, + ] + : transitionDuration, + }; + return { transitionStyle, transitionDurationStyle }; + }; + if (Platform.OS === 'web') { const [elevationShadow] = shadow(elevation, theme.colors.shadow); - const transitionStyle: AnimatedStyle = { - transitionTimingFunction, - transitionProperty: [...transitionProperty, 'boxShadow'], - }; + const { transitionStyle, transitionDurationStyle } = getTransitionStyles([ + 'boxShadow', + ]); return ( @@ -269,10 +317,9 @@ const Surface = ({ if (Platform.OS === 'android') { const elevationAndroid = androidElevationLevels[elevation]; - const transitionStyle: AnimatedStyle = { - transitionTimingFunction, - transitionProperty: [...transitionProperty, 'elevation'], - }; + const { transitionStyle, transitionDurationStyle } = getTransitionStyles([ + 'elevation', + ]); return ( @@ -295,15 +343,11 @@ const Surface = ({ const [spotShadow, ambientShadow] = shadow(elevation, theme.colors.shadow); - const transitionStyle: AnimatedStyle = { - transitionTimingFunction, - transitionProperty: [ - ...transitionProperty, - 'shadowOpacity', - 'shadowOffset', - 'shadowRadius', - ], - }; + const { transitionStyle, transitionDurationStyle } = getTransitionStyles([ + 'shadowOpacity', + 'shadowOffset', + 'shadowRadius', + ]); return ( diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 81496bb153..45329860b2 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -1,9 +1,8 @@ import { PlatformColor, StyleSheet } from 'react-native'; -import { beforeEach, describe, expect, it, jest } from '@jest/globals'; +import { describe, expect, it, jest } from '@jest/globals'; import { fireEvent } from '@testing-library/react-native'; import color from 'color'; -import * as Reanimated from 'react-native-reanimated'; import { LocaleProvider } from '../../core/locale'; import { render, screen } from '../../test-utils'; @@ -12,7 +11,6 @@ import { pink500, white } from '../../theme/colors'; import { DarkTheme, LightTheme } from '../../theme/schemes'; import { tokens } from '../../theme/tokens'; import { shadow } from '../../theme/tokens/sys/elevation'; -import { toRawSpring } from '../../theme/tokens/sys/motion'; import Button from '../Button/Button'; import { Tokens } from '../Button/tokens'; import { @@ -26,20 +24,6 @@ import { } from '../Button/utils'; import type { ButtonLabelVariant, ButtonSize } from '../Button/utils'; -jest.mock('react-native-reanimated', () => { - const ReanimatedModule = jest.requireActual< - typeof import('react-native-reanimated') - >('react-native-reanimated'); - - return { - __esModule: true, - ...ReanimatedModule, - default: ReanimatedModule.default, - // Wrapped so the shape-morph tests can observe the spring targets. - withSpring: jest.fn(ReanimatedModule.withSpring), - }; -}); - const stateOpacity = tokens.md.sys.state.opacity; const styles = StyleSheet.create({ @@ -1348,50 +1332,37 @@ describe('touch target', () => { }); describe('shape morph animation', () => { - beforeEach(() => { - jest.mocked(Reanimated.withSpring).mockClear(); - }); - - const springTargets = ( - spy: jest.MockedFunction - ) => spy.mock.calls.map((call) => call[0]); - - it('springs the corner radius to corner.small on press in', async () => { - const spy = jest.mocked(Reanimated.withSpring); + // `filled` has no outline, so the touchable's radius is the container's. + it('moves the corner to corner.small while pressed', async () => { await render( ); - spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); - expect(springTargets(spy)).toContain(LightTheme.shapes.corner.small); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ + borderRadius: LightTheme.shapes.corner.small, + }); }); - it('springs the corner radius back to the resting pill radius on press out', async () => { - const spy = jest.mocked(Reanimated.withSpring); + it('returns to the resting pill radius on press out', async () => { await render( ); - spy.mockClear(); + await fireEvent(screen.getByTestId('button'), 'onPressIn'); await fireEvent(screen.getByTestId('button'), 'onPressOut'); // small round resting radius = minHeight (40) / 2 = 20 - expect(springTargets(spy)).toContain(20); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20 }); }); - it('animates between round and square radii when toggled (no spring on mount)', async () => { - const spy = jest.mocked(Reanimated.withSpring); + it('switches between round and square radii when selected flips', async () => { await render( ); - // Mount snaps to the resting radius — no spring. - expect(spy).not.toHaveBeenCalled(); await screen.rerender( ); // selected flips square -> round; large round resting radius = 96 / 2 = 48 - expect(springTargets(spy)).toContain(48); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 48 }); }); it('morphs a default button, with no size or shape passed', async () => { - const spy = jest.mocked(Reanimated.withSpring); await render( ); - spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); - expect(springTargets(spy)).toContain(LightTheme.shapes.corner.small); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ + borderRadius: LightTheme.shapes.corner.small, + }); }); it('reads the pressed corner from the size tokens', async () => { - const spy = jest.mocked(Reanimated.withSpring); await render( ); - spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); // A large button presses to `large` (16dp), not the small sizes' 8dp. - expect(springTargets(spy)).toContain(LightTheme.shapes.corner.large); - spy.mockClear(); - }); - - it('springs with the same spatial config as the rest of the library', async () => { - const spy = jest.mocked(Reanimated.withSpring); - await render( - - ); - spy.mockClear(); - await fireEvent(screen.getByTestId('button'), 'onPressIn'); - - // Same spring FAB and Switch use, so the overshoot matches them. - const { damping, stiffness } = toRawSpring( - LightTheme.motion.spring.fast.spatial - ); - expect(spy).toHaveBeenCalledWith( - expect.any(Number), - expect.objectContaining({ damping, stiffness }) - ); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ + borderRadius: LightTheme.shapes.corner.large, + }); }); it('skips the press morph under reduce motion', async () => { - const spy = jest.mocked(Reanimated.withSpring); await render( ); - spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); - expect(spy).not.toHaveBeenCalled(); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20 }); }); it('does not morph when animateShape is false', async () => { - const spy = jest.mocked(Reanimated.withSpring); await render( ); - spy.mockClear(); await fireEvent(screen.getByTestId('button'), 'onPressIn'); - expect(spy).not.toHaveBeenCalled(); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20 }); }); - it('snaps instead of springing when animateShape is false and selected flips', async () => { - const spy = jest.mocked(Reanimated.withSpring); + it('still changes the shape when animateShape is false and selected flips', async () => { await render( ); - // The shape still changes to the flipped radius, it just doesn't animate. - expect(spy).not.toHaveBeenCalled(); - // A direct shared-value write reaches the style on the next frame. - await jest.runAllTimersAsync(); - expect(Reanimated.getAnimatedStyle(containerOf('button'))).toMatchObject({ - borderRadius: 48, - }); - spy.mockClear(); + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 48 }); }); }); diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index 354bd2ee86..43ddd8c54a 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -201,7 +201,7 @@ exports[`render visible banner, with custom theme 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -209,6 +209,9 @@ exports[`render visible banner, with custom theme 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -248,7 +251,7 @@ exports[`render visible banner, with custom theme 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -256,6 +259,9 @@ exports[`render visible banner, with custom theme 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -829,7 +835,7 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -837,6 +843,9 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -876,7 +885,7 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -884,6 +893,9 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1239,7 +1251,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1247,6 +1259,9 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1286,7 +1301,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1294,6 +1309,9 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1465,7 +1483,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1473,6 +1491,9 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1512,7 +1533,7 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1520,6 +1541,9 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index 9988752965..81003d4b3b 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -22,7 +22,7 @@ exports[`renders button with an accessibility hint 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -30,6 +30,9 @@ exports[`renders button with an accessibility hint 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -69,7 +72,7 @@ exports[`renders button with an accessibility hint 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -77,6 +80,9 @@ exports[`renders button with an accessibility hint 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -251,7 +257,7 @@ exports[`renders button with an accessibility label 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -259,6 +265,9 @@ exports[`renders button with an accessibility label 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -298,7 +307,7 @@ exports[`renders button with an accessibility label 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -306,6 +315,9 @@ exports[`renders button with an accessibility label 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -478,7 +490,7 @@ exports[`renders button with button color 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -486,6 +498,9 @@ exports[`renders button with button color 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -525,7 +540,7 @@ exports[`renders button with button color 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -533,6 +548,9 @@ exports[`renders button with button color 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -705,7 +723,7 @@ exports[`renders button with color 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -713,6 +731,9 @@ exports[`renders button with color 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -752,7 +773,7 @@ exports[`renders button with color 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -760,6 +781,9 @@ exports[`renders button with color 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -932,7 +956,7 @@ exports[`renders button with custom testID 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -940,6 +964,9 @@ exports[`renders button with custom testID 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -979,7 +1006,7 @@ exports[`renders button with custom testID 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -987,6 +1014,9 @@ exports[`renders button with custom testID 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1160,7 +1190,7 @@ exports[`renders button with icon 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1168,6 +1198,9 @@ exports[`renders button with icon 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1207,7 +1240,7 @@ exports[`renders button with icon 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1215,6 +1248,9 @@ exports[`renders button with icon 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1418,7 +1454,7 @@ exports[`renders button with icon in reverse order 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1426,6 +1462,9 @@ exports[`renders button with icon in reverse order 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1465,7 +1504,7 @@ exports[`renders button with icon in reverse order 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1473,6 +1512,9 @@ exports[`renders button with icon in reverse order 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1678,7 +1720,7 @@ exports[`renders disabled button 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1686,6 +1728,9 @@ exports[`renders disabled button 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1725,7 +1770,7 @@ exports[`renders disabled button 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1733,6 +1778,9 @@ exports[`renders disabled button 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1927,7 +1975,7 @@ exports[`renders filled button by default 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1935,6 +1983,9 @@ exports[`renders filled button by default 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1974,7 +2025,7 @@ exports[`renders filled button by default 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1982,6 +2033,9 @@ exports[`renders filled button by default 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -2154,7 +2208,7 @@ exports[`renders filled button with mode 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -2162,6 +2216,9 @@ exports[`renders filled button with mode 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -2201,7 +2258,7 @@ exports[`renders filled button with mode 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -2209,6 +2266,9 @@ exports[`renders filled button with mode 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -2381,7 +2441,7 @@ exports[`renders loading button 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -2389,6 +2449,9 @@ exports[`renders loading button 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -2428,7 +2491,7 @@ exports[`renders loading button 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -2436,6 +2499,9 @@ exports[`renders loading button 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -2799,7 +2865,7 @@ exports[`renders outlined button with mode 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -2807,6 +2873,9 @@ exports[`renders outlined button with mode 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -2846,7 +2915,7 @@ exports[`renders outlined button with mode 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -2854,6 +2923,9 @@ exports[`renders outlined button with mode 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -3026,7 +3098,7 @@ exports[`renders text button with mode 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -3034,6 +3106,9 @@ exports[`renders text button with mode 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -3073,7 +3148,7 @@ exports[`renders text button with mode 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -3081,6 +3156,9 @@ exports[`renders text button with mode 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -3253,7 +3331,7 @@ exports[`size prop renders a button with per-size metrics 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 28, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -3261,6 +3339,9 @@ exports[`size prop renders a button with per-size metrics 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 28, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -3300,7 +3381,7 @@ exports[`size prop renders a button with per-size metrics 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 28, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -3308,6 +3389,9 @@ exports[`size prop renders a button with per-size metrics 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 28, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 06afe617dd..1fb036169a 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1829,7 +1829,7 @@ exports[`DataTable.Pagination renders data table pagination with options select "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1837,6 +1837,9 @@ exports[`DataTable.Pagination renders data table pagination with options select "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1876,7 +1879,7 @@ exports[`DataTable.Pagination renders data table pagination with options select "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1884,6 +1887,9 @@ exports[`DataTable.Pagination renders data table pagination with options select "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index 845e6ace46..b6ac626ac9 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -34,7 +34,7 @@ exports[`renders not visible menu 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -42,6 +42,9 @@ exports[`renders not visible menu 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -81,7 +84,7 @@ exports[`renders not visible menu 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -89,6 +92,9 @@ exports[`renders not visible menu 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -276,7 +282,7 @@ exports[`renders visible menu 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -284,6 +290,9 @@ exports[`renders visible menu 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -323,7 +332,7 @@ exports[`renders visible menu 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -331,6 +340,9 @@ exports[`renders visible menu 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -975,7 +987,7 @@ exports[`respects anchorPosition bottom 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -983,6 +995,9 @@ exports[`respects anchorPosition bottom 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1022,7 +1037,7 @@ exports[`respects anchorPosition bottom 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1030,6 +1045,9 @@ exports[`respects anchorPosition bottom 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1676,7 +1694,7 @@ exports[`uses the default anchorPosition of top 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1684,6 +1702,9 @@ exports[`uses the default anchorPosition of top 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -1723,7 +1744,7 @@ exports[`uses the default anchorPosition of top 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -1731,6 +1752,9 @@ exports[`uses the default anchorPosition of top 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { diff --git a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap index d1a93afef8..af749470ff 100644 --- a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap @@ -503,7 +503,7 @@ exports[`renders snackbar with action button 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -511,6 +511,9 @@ exports[`renders snackbar with action button 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { @@ -550,7 +553,7 @@ exports[`renders snackbar with action button 1`] = ` "borderCurve": "continuous", "borderEndEndRadius": undefined, "borderEndStartRadius": undefined, - "borderRadius": 20, + "borderRadius": undefined, "borderStartEndRadius": undefined, "borderStartStartRadius": undefined, "borderTopEndRadius": undefined, @@ -558,6 +561,9 @@ exports[`renders snackbar with action button 1`] = ` "borderTopRightRadius": undefined, "borderTopStartRadius": undefined, }, + { + "borderRadius": 20, + }, { "shadowColor": "rgba(0, 0, 0, 1)", "shadowOffset": { diff --git a/src/theme/__tests__/motion.test.ts b/src/theme/__tests__/motion.test.ts new file mode 100644 index 0000000000..44c45f0c0c --- /dev/null +++ b/src/theme/__tests__/motion.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from '@jest/globals'; + +import { + expressiveMotion, + standardMotion, + toSpringCurve, +} from '../tokens/sys/motion'; + +describe('toSpringCurve', () => { + const { spring } = expressiveMotion; + + it('starts at 0 and ends at 1', () => { + const { points } = toSpringCurve(spring.fast.spatial); + + expect(points[0]).toBe(0); + expect(points[points.length - 1]).toBe(1); + }); + + // A damping ratio of 0.6 overshoots by e^(-π · 0.6 / √(1 - 0.6²)) ≈ 9.5%. + it('overshoots an underdamped spring by the expected amount', () => { + const { points } = toSpringCurve(spring.fast.spatial); + + expect(Math.max(...points)).toBeCloseTo(1.095, 1); + }); + + it('never overshoots a critically damped spring', () => { + const { points } = toSpringCurve(spring.fast.effects); + + expect(Math.max(...points)).toBeLessThanOrEqual(1); + }); + + it('settles a stiffer spring sooner', () => { + const fast = toSpringCurve(spring.fast.spatial).duration; + const slow = toSpringCurve(spring.slow.spatial).duration; + + expect(fast).toBeLessThan(slow); + }); + + it('settles the expressive fast spatial spring in about 400 ms', () => { + expect(toSpringCurve(spring.fast.spatial).duration).toBeCloseTo(407, -1); + }); + + it('settles the standard fast spatial spring sooner', () => { + expect( + toSpringCurve(standardMotion.spring.fast.spatial).duration + ).toBeLessThan(toSpringCurve(spring.fast.spatial).duration); + }); +}); diff --git a/src/theme/tokens/sys/motion.ts b/src/theme/tokens/sys/motion.ts index 878d63e0e9..f9e6dee79c 100644 --- a/src/theme/tokens/sys/motion.ts +++ b/src/theme/tokens/sys/motion.ts @@ -111,3 +111,54 @@ export function toRawSpring({ mass: 1, // as per MD specs }; } + +const SPRING_SETTLE_THRESHOLD = 0.001; +const SPRING_SAMPLE_COUNT = 30; + +/** + * Samples a `SpringConfig` into evenly spaced progress points from 0 to 1, + * with the time the spring takes to settle in milliseconds. + * + * @example + * const { points, duration } = toSpringCurve(theme.motion.spring.fast.spatial); + * + * const style = { + * transitionProperty: 'borderRadius', + * transitionDuration: duration * theme.animation.scale, + * transitionTimingFunction: linear(...points), + * }; + */ +export function toSpringCurve(spring: SpringConfig): { + points: number[]; + duration: number; +} { + const { stiffness, damping, mass } = toRawSpring(spring); + const frequency = Math.sqrt(stiffness / mass); + const ratio = damping / (2 * Math.sqrt(stiffness * mass)); + const settleTime = + Math.log(1 / SPRING_SETTLE_THRESHOLD) / (ratio * frequency); + + const getPosition = (time: number) => { + if (ratio >= 1) { + return 1 - Math.exp(-frequency * time) * (1 + frequency * time); + } + + const decay = ratio * frequency; + const dampedFrequency = frequency * Math.sqrt(1 - ratio * ratio); + + return ( + 1 - + Math.exp(-decay * time) * + (Math.cos(dampedFrequency * time) + + (decay / dampedFrequency) * Math.sin(dampedFrequency * time)) + ); + }; + + const points = Array.from({ length: SPRING_SAMPLE_COUNT + 1 }, (_, index) => + index === SPRING_SAMPLE_COUNT + ? 1 + : getPosition((settleTime * index) / SPRING_SAMPLE_COUNT) + ); + + return { points, duration: settleTime * 1000 }; +} From b274f6260ef8d959473142524e79718c648c04c4 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Tue, 27 Oct 2026 11:22:55 +0100 Subject: [PATCH 49/66] test: assert on public Button output and drop redundant tests --- src/components/Button/Button.tsx | 8 +- src/components/Button/utils.tsx | 11 + src/components/__tests__/Button.test.tsx | 500 +---- .../__snapshots__/Button.test.tsx.snap | 1951 +---------------- 4 files changed, 110 insertions(+), 2360 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 1a8a9f9e9b..18d795322b 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -15,6 +15,7 @@ import Reanimated, { linear } from 'react-native-reanimated'; import { getButtonColors, + getButtonContentBoxStyle, getButtonHitSlop, getButtonPressedRadius, getButtonRippleColor, @@ -433,12 +434,7 @@ const Button = ({ ); const contentBoxStyle = React.useMemo( - () => ({ - minHeight: sizeStyle.minHeight - borderWidth * 2, - paddingStart: sizeStyle.paddingStart - borderWidth, - paddingEnd: sizeStyle.paddingEnd - borderWidth, - gap: sizeStyle.iconGap, - }), + () => getButtonContentBoxStyle(sizeStyle, borderWidth), [sizeStyle, borderWidth] ); diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 20a4c4030c..c82c6a041e 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -57,6 +57,17 @@ export const getButtonSizeStyle = (size: ButtonSize): ButtonSizeStyle => { }; }; +/** The content box sits inside the outline, so the outline can't grow it. */ +export const getButtonContentBoxStyle = ( + sizeStyle: ButtonSizeStyle, + borderWidth: number +) => ({ + minHeight: sizeStyle.minHeight - borderWidth * 2, + paddingStart: sizeStyle.paddingStart - borderWidth, + paddingEnd: sizeStyle.paddingEnd - borderWidth, + gap: sizeStyle.iconGap, +}); + /** * Slop that brings the touch target up to the size's `outerBoundHeight`, * expanding outside the container. diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 45329860b2..d0b441ade9 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -8,13 +8,14 @@ import { LocaleProvider } from '../../core/locale'; import { render, screen } from '../../test-utils'; import { ReduceMotionContext } from '../../theme/accessibility/ReduceMotionContext'; import { pink500, white } from '../../theme/colors'; -import { DarkTheme, LightTheme } from '../../theme/schemes'; +import { LightTheme } from '../../theme/schemes'; import { tokens } from '../../theme/tokens'; import { shadow } from '../../theme/tokens/sys/elevation'; import Button from '../Button/Button'; import { Tokens } from '../Button/tokens'; import { getButtonColors, + getButtonContentBoxStyle, getButtonHitSlop, getButtonPressedRadius, getButtonRippleColor, @@ -35,8 +36,7 @@ const styles = StyleSheet.create({ }, }); -// Button's internal layers intentionally carry no test IDs, so they are reached -// by walking up from the elements a user can actually query. +// The content row has no test ID, so it is reached from its label. const parentOf = ( element: ReturnType, describeChild: string @@ -55,10 +55,6 @@ const parentOf = ( const labelOf = (label: string) => screen.getByText(label); const contentOf = (label: string) => parentOf(labelOf(label), `the "${label}" label`); -const containerOf = (testID: string) => - parentOf(screen.getByTestId(testID), `"${testID}"`); -const surfaceOf = (testID: string) => - parentOf(containerOf(testID), `"${testID}"'s container`); it('renders filled button by default', async () => { const tree = (await render()).toJSON(); @@ -82,14 +78,6 @@ it('renders outlined button with mode', async () => { expect(tree).toMatchSnapshot(); }); -it('renders filled button with mode', async () => { - const tree = ( - await render() - ).toJSON(); - - expect(tree).toMatchSnapshot(); -}); - it('renders button with icon', async () => { const tree = ( await render() @@ -98,18 +86,6 @@ it('renders button with icon', async () => { expect(tree).toMatchSnapshot(); }); -it('renders button with icon in reverse order', async () => { - const tree = ( - await render( - - ) - ).toJSON(); - - expect(tree).toMatchSnapshot(); -}); - it('swaps the icon to the trailing edge under RTL', async () => { await render( ) - ).toJSON(); - - expect(tree).toMatchSnapshot(); -}); - -it('renders button with button color', async () => { - const tree = ( - await render() - ).toJSON(); +it('applies a custom label color', async () => { + await render(); - expect(tree).toMatchSnapshot(); + expect(screen.getByText('Custom Button')).toHaveStyle({ color: pink500 }); }); -it('renders button with custom testID', async () => { - const tree = ( - await render( - - ) - ).toJSON(); +it('applies a custom container color', async () => { + await render(); - expect(tree).toMatchSnapshot(); + expect(screen.root).toHaveStyle({ backgroundColor: pink500 }); }); -it('renders button with an accessibility label', async () => { - const tree = ( - await render( - - ) - ).toJSON(); +it('exposes the accessibility label', async () => { + await render(); - expect(tree).toMatchSnapshot(); + expect(screen.getByRole('button', { name: 'label' })).toBeOnTheScreen(); }); -it('renders button with an accessibility hint', async () => { - const tree = ( - await render( - - ) - ).toJSON(); +it('exposes the accessibility hint', async () => { + await render(); - expect(tree).toMatchSnapshot(); + expect(screen.getByRole('button')).toHaveProp('accessibilityHint', 'hint'); }); it('should execute onPressIn', async () => { @@ -295,19 +247,6 @@ describe('getButtonColors - background color', () => { }) ); - (['outlined', 'text'] as const).forEach((mode) => - it(`should return correct disabled color, for theme version 3, dark theme, ${mode} mode`, () => { - expect( - getButtonColors({ - customButtonColor, - theme: LightTheme, - mode, - disabled: true, - }) - ).toMatchObject({ backgroundColor: 'transparent' }); - }) - ); - (['filled', 'tonal', 'elevated'] as const).forEach((mode) => it(`should return correct disabled color, for theme version 3, ${mode} mode`, () => { return expect( @@ -324,22 +263,6 @@ describe('getButtonColors - background color', () => { }) ); - (['filled', 'tonal', 'elevated'] as const).forEach((mode) => - it(`should return correct disabled color, for theme version 3, dark theme, ${mode} mode`, () => { - return expect( - getButtonColors({ - customButtonColor, - theme: DarkTheme, - mode, - disabled: true, - }) - ).toMatchObject({ - backgroundColor: DarkTheme.colors.onSurface, - backgroundOpacity: stateOpacity.pressed, - }); - }) - ); - it('should return correct theme color, for theme version 3, elevated mode', () => { expect( getButtonColors({ @@ -351,17 +274,6 @@ describe('getButtonColors - background color', () => { }); }); - it('should return correct theme color, for theme version 3, dark theme, elevated mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode: 'elevated', - }) - ).toMatchObject({ - backgroundColor: DarkTheme.colors.surfaceContainerLow, - }); - }); - it('should return correct theme color, for theme version 3, filled mode', () => { expect( getButtonColors({ @@ -373,17 +285,6 @@ describe('getButtonColors - background color', () => { }); }); - it('should return correct theme color, for theme version 3, dark theme, filled mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode: 'filled', - }) - ).toMatchObject({ - backgroundColor: DarkTheme.colors.primary, - }); - }); - it('should return correct theme color, for theme version 3, tonal mode', () => { expect( getButtonColors({ @@ -395,17 +296,6 @@ describe('getButtonColors - background color', () => { }); }); - it('should return correct theme color, for theme version 3, dark theme, tonal mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode: 'tonal', - }) - ).toMatchObject({ - backgroundColor: DarkTheme.colors.secondaryContainer, - }); - }); - (['text', 'outlined'] as const).forEach((mode) => it(`should return transparent color, for theme version 3, ${mode} mode`, () => { return expect( @@ -418,19 +308,6 @@ describe('getButtonColors - background color', () => { }); }) ); - - (['text', 'outlined'] as const).forEach((mode) => - it(`should return transparent color, for theme version 3, dark theme, ${mode} mode`, () => { - return expect( - getButtonColors({ - theme: DarkTheme, - mode, - }) - ).toMatchObject({ - backgroundColor: 'transparent', - }); - }) - ); }); describe('getButtonColors - text color', () => { @@ -461,20 +338,6 @@ describe('getButtonColors - text color', () => { }); }); - it('should return correct disabled text color, for theme version 3, dark theme, no matter what the mode is', () => { - expect( - getButtonColors({ - customLabelColor, - theme: DarkTheme, - disabled: true, - mode: 'text', - }) - ).toMatchObject({ - labelColor: DarkTheme.colors.onSurface, - labelOpacity: stateOpacity.disabled, - }); - }); - (['filled', 'tonal', 'elevated'] as const).forEach((mode) => it(`should return correct text color for dark prop, for theme version 3, ${mode} mode`, () => { expect( @@ -502,19 +365,6 @@ describe('getButtonColors - text color', () => { }) ); - (['text', 'elevated'] as const).forEach((mode) => - it(`should return correct theme text color, for theme version 3, dark theme, ${mode} mode`, () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode, - }) - ).toMatchObject({ - labelColor: DarkTheme.colors.primary, - }); - }) - ); - it('should return onSurfaceVariant label color, for theme version 3, outlined mode', () => { expect( getButtonColors({ @@ -526,17 +376,6 @@ describe('getButtonColors - text color', () => { }); }); - it('should return onSurfaceVariant label color, for theme version 3, dark theme, outlined mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode: 'outlined', - }) - ).toMatchObject({ - labelColor: DarkTheme.colors.onSurfaceVariant, - }); - }); - it('should return correct theme text color, for theme version 3, filled mode', () => { expect( getButtonColors({ @@ -548,17 +387,6 @@ describe('getButtonColors - text color', () => { }); }); - it('should return correct theme text color, for theme version 3, dark theme, filled mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode: 'filled', - }) - ).toMatchObject({ - labelColor: DarkTheme.colors.onPrimary, - }); - }); - it('should return correct theme text color, for theme version 3, tonal mode', () => { expect( getButtonColors({ @@ -569,17 +397,6 @@ describe('getButtonColors - text color', () => { labelColor: LightTheme.colors.onSecondaryContainer, }); }); - - it('should return correct theme text color, for theme version 3, dark theme tonal mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode: 'tonal', - }) - ).toMatchObject({ - labelColor: DarkTheme.colors.onSecondaryContainer, - }); - }); }); describe('getButtonColors - border color', () => { @@ -595,18 +412,6 @@ describe('getButtonColors - border color', () => { }); }); - it('should return correct border color, for theme version 3, when disabled, dark theme, outlined mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - disabled: true, - mode: 'outlined', - }) - ).toMatchObject({ - borderColor: DarkTheme.colors.outlineVariant, - }); - }); - it('should return correct border color, for theme version 3, outlined mode', () => { expect( getButtonColors({ @@ -618,17 +423,6 @@ describe('getButtonColors - border color', () => { }); }); - it('should return correct border color, for theme version 3, dark theme, outlined mode', () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode: 'outlined', - }) - ).toMatchObject({ - borderColor: DarkTheme.colors.outlineVariant, - }); - }); - (['text', 'filled', 'tonal', 'elevated'] as const).forEach((mode) => it(`should return transparent border, for theme version 3, ${mode} mode`, () => { expect( @@ -641,19 +435,6 @@ describe('getButtonColors - border color', () => { }); }) ); - - (['text', 'filled', 'tonal', 'elevated'] as const).forEach((mode) => - it(`should return transparent border, for theme version 3, dark theme, ${mode} mode`, () => { - expect( - getButtonColors({ - theme: DarkTheme, - mode, - }) - ).toMatchObject({ - borderColor: 'transparent', - }); - }) - ); }); describe('getButtonColors - border width', () => { @@ -768,18 +549,6 @@ describe('getButtonHitSlop', () => { }); describe('size prop', () => { - it('renders a button with per-size metrics', async () => { - const tree = ( - await render( - - ) - ).toJSON(); - - expect(tree).toMatchSnapshot(); - }); - ( [ ['extra-small', 14], @@ -900,7 +669,7 @@ describe('shape prop', () => { ); // Half the small container height (40dp) is the real pill radius. - expect(containerOf('button')).toHaveStyle({ + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20, }); }); @@ -911,21 +680,10 @@ describe('shape prop', () => { X ); - expect(containerOf('button')).toHaveStyle({ + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 12, }); }); - - it('uses the per-size square radius when both size and shape are set', async () => { - await render( - - ); - expect(containerOf('button')).toHaveStyle({ - borderRadius: 28, - }); - }); }); describe('selected prop', () => { @@ -946,7 +704,7 @@ describe('selected prop', () => { ); - expect(containerOf('button')).toHaveStyle({ + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 28, }); }); @@ -958,7 +716,7 @@ describe('selected prop', () => { ); - expect(containerOf('button')).toHaveStyle({ + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20, }); }); @@ -993,7 +751,7 @@ describe('selected prop', () => { ); // No shape flip: a round text button stays the pill, 40 / 2 = 20. - expect(containerOf('button')).toHaveStyle({ + expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20, }); // And no toggle state is announced. @@ -1092,14 +850,14 @@ it('gives an elevated button a resting shadow, and other modes none', async () = Elevated ); - expect(surfaceOf('elevated')).toHaveStyle(spotShadow); + expect(screen.root).toHaveStyle(spotShadow); await render( ); - expect(surfaceOf('filled')).toHaveStyle({ + expect(screen.root).toHaveStyle({ shadowOpacity: 0, }); }); @@ -1111,7 +869,7 @@ it('drops the shadow when an elevated button is disabled', async () => { ); - expect(surfaceOf('elevated')).toHaveStyle({ + expect(screen.root).toHaveStyle({ shadowOpacity: 0, }); }); @@ -1123,107 +881,73 @@ it('forwards `style` to the shadow host', async () => { ); - expect(surfaceOf('button')).toHaveStyle(styles.scaled); + expect(screen.root).toHaveStyle(styles.scaled); }); describe('container height', () => { const MODES = ['filled', 'tonal', 'elevated', 'outlined', 'text'] as const; - // The outlined stroke thickens with the size (1dp up to M, 2dp L, 3dp XL), - // so every size has to be checked against its own token metrics. - const SIZES = [ - 'extra-small', - 'small', - 'medium', - 'large', - 'extra-large', - ] as const; - const { containerHeight, leadingSpace } = Tokens.sizes.small; - - // The rendered container is the content box plus the outline Yoga draws - // around it, so those two have to add up to the token height in every mode. - const renderedBox = (testID: string) => { - const content = StyleSheet.flatten( - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - contentOf('X').props.style - ); - const clip = StyleSheet.flatten( - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - containerOf(testID).props.style + + // The content box plus the outline drawn around it must add up to the token + // metrics, or the outline grows the container. + const boxOf = (size: ButtonSize, borderWidth: number) => { + const content = getButtonContentBoxStyle( + getButtonSizeStyle(size), + borderWidth ); - const outline = clip.borderWidth ?? 0; return { - height: content.minHeight + outline * 2, - leading: content.paddingStart + outline, + height: content.minHeight + borderWidth * 2, + leading: content.paddingStart + borderWidth, }; }; - it.each(MODES)('renders %s at the token height', async (mode) => { - await render( - - ); + it.each(MODES)('keeps %s on the token height', (mode) => { + const { borderWidth } = getButtonColors({ theme: LightTheme, mode }); - expect(renderedBox('button')).toEqual({ - height: containerHeight, - leading: leadingSpace, + expect(boxOf('small', borderWidth)).toEqual({ + height: Tokens.sizes.small.containerHeight, + leading: Tokens.sizes.small.leadingSpace, }); }); - it.each(SIZES)( + it.each(sizeMetrics)( 'insets the %s outline so the box stays on the token metrics', - async (size) => { - await render( - - ); + (size) => { + const { borderWidth } = getButtonColors({ + theme: LightTheme, + mode: 'outlined', + size, + }); - expect(renderedBox('button')).toEqual({ + expect(boxOf(size, borderWidth)).toEqual({ height: Tokens.sizes[size].containerHeight, leading: Tokens.sizes[size].leadingSpace, }); } ); - it('keeps an outlined toggle the same size in both states', async () => { - await render( - - ); - const unselected = renderedBox('button'); - - await render( - - ); + it('keeps an outlined toggle the same size in both states', () => { + const widthWhen = (selected: boolean) => + getButtonColors({ theme: LightTheme, mode: 'outlined', selected }) + .borderWidth; // Selecting drops the outline; without the inset this shrank by 2dp. - expect(renderedBox('button')).toEqual(unselected); + expect(boxOf('small', widthWhen(true))).toEqual( + boxOf('small', widthWhen(false)) + ); }); }); describe('touch target', () => { - const styleOf = (element: ReturnType) => - StyleSheet.flatten( - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - element.props.style - ); - - it('leaves no clipping ancestor to swallow the expanded target', async () => { + it('leaves the outer view unclipped for the expanded target', async () => { await render( ); - // The slop expands past the container, so an ancestor sized to the - // container must not clip. This is what made the previous `hitSlop` - // inert, and a passed-through prop alone would not catch it. - expect(styleOf(surfaceOf('button')).overflow).not.toBe('hidden'); - expect(styleOf(containerOf('button')).overflow).not.toBe('hidden'); + // The slop expands past the container, so a clipping ancestor would make + // it inert. + expect(screen.root).not.toHaveStyle({ overflow: 'hidden' }); }); it('still clips the ripple to the container radius', async () => { @@ -1233,17 +957,14 @@ describe('touch target', () => { ); - // Clipping moved onto the touchable, so the ripple keeps the pill shape - // now that the clip view above it no longer hides the overflow. - const style = styleOf(screen.getByTestId('button')); - expect(style.overflow).toBe('hidden'); - expect(style.borderRadius).toBe(Tokens.sizes.small.containerHeight / 2); + expect(screen.getByTestId('button')).toHaveStyle({ + overflow: 'hidden', + borderRadius: Tokens.sizes.small.containerHeight / 2, + }); }); - // An outlined button's children sit inside the outline, so they round a - // border width tighter than the container. Given the outer radius instead, - // their corners overshoot the outline's inner edge and the background shows - // through the gap — worst on XL, which has the thickest stroke. + // Inside the outline, the ripple has to round a border width tighter, or + // the background shows through at the corners. it.each(sizeMetrics)( 'insets the %s ripple radius by the outline width', async (size) => { @@ -1259,14 +980,11 @@ describe('touch target', () => { ); - const outer = StyleSheet.flatten( - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - containerOf('button').props.style - ); - - expect(styleOf(screen.getByTestId('button')).borderRadius).toBe( - outer.borderRadius - Tokens.sizes[size].outlinedOutlineWidth - ); + expect(screen.getByTestId('button')).toHaveStyle({ + borderRadius: + getButtonShapeRadius({ size, shape: 'square', theme: LightTheme }) - + Tokens.sizes[size].outlinedOutlineWidth, + }); } ); @@ -1277,35 +995,28 @@ describe('touch target', () => { ); - const outer = StyleSheet.flatten( - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - containerOf('button').props.style + expect(screen.getByTestId('button')).toHaveStyle({ + borderRadius: getButtonShapeRadius({ + size: 'small', + shape: 'square', + theme: LightTheme, + }), + }); + }); + + it('expands the target to the outer bound', async () => { + await render( + ); - expect(styleOf(screen.getByTestId('button')).borderRadius).toBe( - outer.borderRadius + expect(screen.getByTestId('button')).toHaveProp( + 'hitSlop', + getButtonHitSlop('medium') ); }); - it.each(sizeMetrics)( - 'expands the %s target to the outer bound', - async (size) => { - await render( - - ); - - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - const { hitSlop } = screen.getByTestId('button').props; - const { containerHeight, outerBoundHeight } = Tokens.sizes[size]; - - expect(containerHeight + hitSlop.top + hitSlop.bottom).toBe( - outerBoundHeight - ); - } - ); - it('lets a caller hitSlop win', async () => { await render( ); - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - expect(screen.getByTestId('button').props.hitSlop).toBe(12); + expect(screen.getByTestId('button')).toHaveProp('hitSlop', 12); }); it('takes `null` as an opt out', async () => { @@ -1324,10 +1034,7 @@ describe('touch target', () => { ); - // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - const { hitSlop } = screen.getByTestId('button').props; - - expect(hitSlop).toBeNull(); + expect(screen.getByTestId('button')).toHaveProp('hitSlop', null); }); }); @@ -1357,39 +1064,6 @@ describe('shape morph animation', () => { expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 20 }); }); - it('switches between round and square radii when selected flips', async () => { - await render( - - ); - await screen.rerender( - - ); - // selected flips square -> round; large round resting radius = 96 / 2 = 48 - expect(screen.getByTestId('button')).toHaveStyle({ borderRadius: 48 }); - }); - - it('morphs a default button, with no size or shape passed', async () => { - await render( - - ); - await fireEvent(screen.getByTestId('button'), 'onPressIn'); - expect(screen.getByTestId('button')).toHaveStyle({ - borderRadius: LightTheme.shapes.corner.small, - }); - }); - it('reads the pressed corner from the size tokens', async () => { await render( + ); + + expect(screen.getByTestId('button')).toHaveProp('aria-pressed', true); + expect(screen.getByTestId('button')).not.toHaveProp('aria-selected'); + }); + it('flips a round button into the square radius when selected', async () => { await render( + Press me + ``` ## Disable ripple effect in all components diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index 66424d1c57..3bf6da025e 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -289,14 +289,6 @@ const ButtonExample = () => { > Custom label color - + + + ); + + expect(contentOf('Leading')).toHaveStyle({ flexDirection: 'row' }); + expect(contentOf('Trailing')).toHaveStyle({ flexDirection: 'row-reverse' }); +}); + it('renders loading button', async () => { const tree = (await render()).toJSON(); From 7db7f051b994aa3a89501a12405f02aed278f9a6 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 09:55:25 +0100 Subject: [PATCH 54/66] fix: paint the Button container once and snap PlatformColor changes --- src/components/Button/Button.tsx | 4 +-- src/components/Button/utils.tsx | 4 ++- src/components/__tests__/Button.test.tsx | 32 +++++++++++++++++++ .../__snapshots__/Banner.test.tsx.snap | 4 --- .../__snapshots__/Button.test.tsx.snap | 6 ---- .../__snapshots__/DataTable.test.tsx.snap | 1 - .../__snapshots__/Menu.test.tsx.snap | 4 --- .../__snapshots__/Snackbar.test.tsx.snap | 1 - 8 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 84d867d18a..9ec42658d7 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -427,8 +427,8 @@ const Button = ({ }; const outlineStyle = React.useMemo( - () => ({ backgroundColor: containerColor, borderColor, borderWidth }), - [containerColor, borderColor, borderWidth] + () => ({ borderColor, borderWidth }), + [borderColor, borderWidth] ); const { color: labelStyleColor } = React.useMemo( diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 809f150bf4..caae91c6ba 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -124,7 +124,7 @@ export const getButtonShapeRadius = ({ /** * Duration of `Surface`'s background and elevation transition. * - * Snaps when the container is or was transparent. + * Snaps when either color is transparent or not a plain string. */ export const getButtonTransitionDuration = ({ theme, @@ -138,6 +138,8 @@ export const getButtonTransitionDuration = ({ previousContainerColor: ColorValue; }): number => { if ( + typeof containerColor !== 'string' || + typeof previousContainerColor !== 'string' || containerColor === 'transparent' || previousContainerColor === 'transparent' ) { diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 01ae6f6841..36e0eb9d1d 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -173,6 +173,21 @@ it('applies a custom container color', async () => { expect(screen.root).toHaveStyle({ backgroundColor: pink500 }); }); +it('paints the container color only on the container', async () => { + const translucent = 'rgba(0, 0, 0, 0.5)'; + + await render( + + ); + + expect(screen.root).toHaveStyle({ backgroundColor: translucent }); + expect(parentOf(screen.getByTestId('button'), 'the touchable')).toHaveStyle({ + backgroundColor: undefined, + }); +}); + it('exposes the accessibility label', async () => { await render(); @@ -675,6 +690,23 @@ describe('getButtonTransitionDuration', () => { }) ).toBe(0); }); + + it.each([ + ['to', PlatformColor('?attr/colorPrimary'), opaque], + ['from', opaque, PlatformColor('?attr/colorPrimary')], + ])( + 'snaps when the container changes %s a PlatformColor', + (_, containerColor, previousContainerColor) => { + expect( + getButtonTransitionDuration({ + theme, + pressed: false, + containerColor, + previousContainerColor, + }) + ).toBe(0); + } + ); }); describe('shape prop', () => { diff --git a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap index 43ddd8c54a..0503283dfd 100644 --- a/src/components/__tests__/__snapshots__/Banner.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Banner.test.tsx.snap @@ -282,7 +282,6 @@ exports[`render visible banner, with custom theme 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "transparent", "borderWidth": 0, }, @@ -916,7 +915,6 @@ exports[`renders visible banner, with action buttons and with image 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "transparent", "borderWidth": 0, }, @@ -1332,7 +1330,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "transparent", "borderWidth": 0, }, @@ -1564,7 +1561,6 @@ exports[`renders visible banner, with action buttons and without image 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "transparent", "borderWidth": 0, }, diff --git a/src/components/__tests__/__snapshots__/Button.test.tsx.snap b/src/components/__tests__/__snapshots__/Button.test.tsx.snap index 8f860d6168..8c46d9fd1e 100644 --- a/src/components/__tests__/__snapshots__/Button.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Button.test.tsx.snap @@ -103,7 +103,6 @@ exports[`renders button with icon 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", "borderWidth": 0, }, @@ -367,7 +366,6 @@ exports[`renders disabled button 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "transparent", "borderWidth": 0, }, @@ -622,7 +620,6 @@ exports[`renders filled button by default 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", "borderWidth": 0, }, @@ -855,7 +852,6 @@ exports[`renders loading button 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderColor": "transparent", "borderWidth": 0, }, @@ -1279,7 +1275,6 @@ exports[`renders outlined button with mode 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", "borderWidth": 1, }, @@ -1512,7 +1507,6 @@ exports[`renders text button with mode 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "transparent", "borderWidth": 0, }, diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 1fb036169a..819b512f9d 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -1910,7 +1910,6 @@ exports[`DataTable.Pagination renders data table pagination with options select "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", "borderWidth": 1, }, diff --git a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap index b6ac626ac9..ec9310f66f 100644 --- a/src/components/__tests__/__snapshots__/Menu.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Menu.test.tsx.snap @@ -115,7 +115,6 @@ exports[`renders not visible menu 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", "borderWidth": 1, }, @@ -363,7 +362,6 @@ exports[`renders visible menu 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", "borderWidth": 1, }, @@ -1068,7 +1066,6 @@ exports[`respects anchorPosition bottom 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", "borderWidth": 1, }, @@ -1775,7 +1772,6 @@ exports[`uses the default anchorPosition of top 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "rgba(202, 196, 208, 1)", "borderWidth": 1, }, diff --git a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap index af749470ff..d6294ba589 100644 --- a/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap @@ -584,7 +584,6 @@ exports[`renders snackbar with action button 1`] = ` "borderStyle": "solid", }, { - "backgroundColor": "transparent", "borderColor": "transparent", "borderWidth": 0, }, From 045e1698ffc141e2709172c24899caca043635ae Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 09:56:45 +0100 Subject: [PATCH 55/66] fix: handle custom Button colors in the ripple and the toggle label --- src/components/Button/Button.tsx | 1 + src/components/Button/utils.tsx | 26 ++++++++++++------ src/components/__tests__/Button.test.tsx | 34 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 9ec42658d7..c626b15bce 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -60,6 +60,7 @@ export type Props = Omit & { mode?: 'text' | 'outlined' | 'filled' | 'elevated' | 'tonal'; /** * Whether the color is a dark color. A dark button will render light text and vice-versa. Only applicable for the `filled`, `tonal` and `elevated` modes. + * On a toggle (`selected` defined), it applies only with a custom `buttonColor`. */ dark?: boolean; /** diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index caae91c6ba..84af338922 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -257,11 +257,13 @@ const getButtonLabelColor = ({ theme, disabled, customLabelColor, + customButtonColor, backgroundColor, dark, toggleColors, }: Omit & { customLabelColor?: ColorValue; + customButtonColor?: ColorValue; backgroundColor: ColorValue; dark?: boolean; toggleColors?: ToggleColors; @@ -275,14 +277,16 @@ const getButtonLabelColor = ({ return theme.colors.onSurface; } - if (toggleColors) { + const appliesDark = + typeof dark === 'boolean' && + (isMode('filled') || isMode('tonal') || isMode('elevated')); + + if (toggleColors && !(customButtonColor && appliesDark)) { return colors[toggleColors.label]; } - if (typeof dark === 'boolean') { - if (isMode('filled') || isMode('tonal') || isMode('elevated')) { - return isDark({ dark, backgroundColor }) ? white : black; - } + if (appliesDark) { + return isDark({ dark, backgroundColor }) ? white : black; } // Outlined uses the neutral on-surface-variant label per MD3 spec; text and @@ -380,6 +384,7 @@ export const getButtonColors = ({ theme, disabled, customLabelColor, + customButtonColor, backgroundColor, dark, toggleColors, @@ -421,8 +426,9 @@ export const getButtonColors = ({ * 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`), `undefined` is returned so `TouchableRipple` falls back to - * its own default state-layer color. + * `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, @@ -433,5 +439,9 @@ export const getButtonRippleColor = ({ return undefined; } - return color(labelColor).alpha(stateOpacity.pressed).rgb().string(); + try { + return color(labelColor).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 36e0eb9d1d..a273137294 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -514,6 +514,18 @@ describe('getButtonRippleColor', () => { }); }); +it('renders with a label color the ripple cannot parse', async () => { + await render( + + ); + + expect(screen.getByText('Custom property')).toHaveStyle({ + color: 'var(--label)', + }); +}); + const sizeMetrics: [ size: ButtonSize, minHeight: number, @@ -890,6 +902,28 @@ describe('toggle colors', () => { expect(unselected.labelColor).not.toBe(plain.labelColor); }); + it('lets `dark` pick the label on a custom container color', async () => { + await render( + + ); + + expect(screen.getByText('Custom')).toHaveStyle({ color: white }); + }); + + it('keeps the toggle label on the toggle container color', async () => { + await render( + + ); + + expect(screen.getByText('Theme')).toHaveStyle({ + color: LightTheme.colors.onSurfaceVariant, + }); + }); + it('ignores the toggle table when disabled', () => { const theme = LightTheme; From 29ba5071c560afef7764282a5da2a61e32b1cc78 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 09:59:02 +0100 Subject: [PATCH 56/66] fix: announce both Button toggle states and fill disabled outlined ones --- src/components/Button/Button.tsx | 7 ++-- src/components/Button/utils.tsx | 23 ++++++------- src/components/__tests__/Button.test.tsx | 44 +++++++++++++++++++----- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index c626b15bce..725326fc0e 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -99,7 +99,7 @@ export type Props = Omit & { * - When `true` the `shape` is flipped: `'round'` renders square and vice * versa, and an `outlined` button drops its outline. * - Screen readers announce the toggle state: `aria-pressed` on web, - * `aria-selected` on native. + * `aria-checked` on native. * * Ignored on `mode="text"`, which MD3 gives no toggle. */ @@ -290,12 +290,11 @@ const Button = ({ // toggle colors, no shape flip, and no toggle state. Every other use of // the toggle state goes through this value. const toggleSelected = isMode('text') ? undefined : selected; - // ARIA has no `aria-selected` on the button role, and React Native's types - // have no `aria-pressed`. + // React Native's types have no `aria-pressed`. const toggleStateProps = Platform.OS === 'web' ? { 'aria-pressed': toggleSelected } - : { 'aria-selected': toggleSelected }; + : { 'aria-checked': toggleSelected }; // When the button is `selected`, flip the requested shape so the // unselected/selected pair contrasts visually (round ↔ square). diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 84af338922..0729454ea4 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -214,9 +214,10 @@ const getButtonBackgroundColor = ({ isMode, theme, disabled, + selected, customButtonColor, toggleColors, -}: Omit & { +}: BaseProps & { customButtonColor?: ColorValue; toggleColors?: ToggleColors; }) => { @@ -226,7 +227,7 @@ const getButtonBackgroundColor = ({ } if (disabled) { - if (isMode('outlined') || isMode('text')) { + if (isMode('text') || (isMode('outlined') && !selected)) { return 'transparent'; } return colors.onSurface; @@ -313,12 +314,10 @@ const getButtonLabelColor = ({ const getButtonBorderColor = ({ isMode, theme, - disabled, selected, -}: BaseProps) => { - // A selected outlined toggle drops its outline (the filled inverse-surface - // background takes over as the visual affordance). - if (selected && !disabled && isMode('outlined')) { +}: Omit) => { + // A selected outlined toggle drops its outline, disabled or not. + if (selected && isMode('outlined')) { return 'transparent'; } if (isMode('outlined')) { @@ -330,11 +329,10 @@ const getButtonBorderColor = ({ const getButtonBorderWidth = ({ isMode, - disabled, selected, size, -}: Omit & { size: ButtonSize }) => { - if (selected && !disabled && isMode('outlined')) { +}: Omit & { size: ButtonSize }) => { + if (selected && isMode('outlined')) { return 0; } if (isMode('outlined')) { @@ -375,6 +373,7 @@ export const getButtonColors = ({ isMode, theme, disabled, + selected, customButtonColor, toggleColors, }); @@ -393,13 +392,11 @@ export const getButtonColors = ({ const borderColor = getButtonBorderColor({ isMode, theme, - disabled, selected, }); const borderWidth = getButtonBorderWidth({ isMode, - disabled, selected, size, }); @@ -407,7 +404,7 @@ export const getButtonColors = ({ const labelOpacity = disabled ? stateOpacity.disabled : stateOpacity.enabled; const backgroundOpacity = - disabled && !isMode('outlined') && !isMode('text') + disabled && backgroundColor !== 'transparent' ? stateOpacity.pressed : stateOpacity.enabled; diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index a273137294..2e4ce470c1 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -747,15 +747,22 @@ describe('shape prop', () => { }); describe('selected prop', () => { - it('marks the button as selected for screen readers', async () => { - await render( - - ); + it.each([true, false])( + 'announces the toggle state %s with aria-checked on native', + async (selected) => { + await render( + + ); - expect(screen.getByTestId('button')).toBeSelected(); - }); + expect(screen.getByRole('button')).toHaveProp( + 'accessibilityState', + expect.objectContaining({ checked: selected }) + ); + expect(screen.getByRole('button')).not.toBeSelected(); + } + ); it('announces the toggle state with aria-pressed on web', async () => { jest.replaceProperty(Platform, 'OS', 'web'); @@ -807,6 +814,22 @@ describe('selected prop', () => { }); }); + it('fills a disabled selected outlined toggle instead of the outline', () => { + expect( + getButtonColors({ + theme: LightTheme, + mode: 'outlined', + selected: true, + disabled: true, + }) + ).toMatchObject({ + backgroundColor: LightTheme.colors.onSurface, + backgroundOpacity: stateOpacity.pressed, + borderColor: 'transparent', + borderWidth: 0, + }); + }); + // MD3 gives the text style no toggle, so `selected` is inert there. it('ignores `selected` entirely on a text button', async () => { const plain = getButtonColors({ theme: LightTheme, mode: 'text' }); @@ -828,7 +851,10 @@ describe('selected prop', () => { borderRadius: 20, }); // And no toggle state is announced. - expect(screen.getByTestId('button')).not.toBeSelected(); + expect(screen.getByTestId('button')).not.toHaveProp( + 'accessibilityState', + expect.objectContaining({ checked: expect.any(Boolean) }) + ); }); it('leaves a plain button untouched when `selected` is omitted', () => { From de816d84c0e13458f421e340b7f8eb88fdd1a519 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 10:00:39 +0100 Subject: [PATCH 57/66] fix: cap the Button touch target slop at 48dp --- src/components/Button/tokens.ts | 10 ++-- src/components/Button/utils.tsx | 8 ++- src/components/__tests__/Button.test.tsx | 69 ++++++++++++++---------- 3 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/components/Button/tokens.ts b/src/components/Button/tokens.ts index 9fc5c23778..b104eef681 100644 --- a/src/components/Button/tokens.ts +++ b/src/components/Button/tokens.ts @@ -15,7 +15,6 @@ type ButtonSizeTokens = { leadingSpace: number; trailingSpace: number; outlinedOutlineWidth: number; - outerBoundHeight: number; labelVariant: ButtonLabelVariant; containerShapeRound: ShapeToken; containerShapeSquare: ShapeToken; @@ -34,7 +33,6 @@ const sizes = { leadingSpace: 12, trailingSpace: 12, outlinedOutlineWidth: 1, - outerBoundHeight: 48, labelVariant: 'labelLarge', containerShapeRound: 'full', containerShapeSquare: 'medium', @@ -47,7 +45,6 @@ const sizes = { leadingSpace: 16, trailingSpace: 16, outlinedOutlineWidth: 1, - outerBoundHeight: 48, labelVariant: 'labelLarge', containerShapeRound: 'full', containerShapeSquare: 'medium', @@ -60,7 +57,6 @@ const sizes = { leadingSpace: 24, trailingSpace: 24, outlinedOutlineWidth: 1, - outerBoundHeight: 64, labelVariant: 'titleMedium', containerShapeRound: 'full', containerShapeSquare: 'large', @@ -73,7 +69,6 @@ const sizes = { leadingSpace: 48, trailingSpace: 48, outlinedOutlineWidth: 2, - outerBoundHeight: 104, labelVariant: 'headlineSmall', containerShapeRound: 'full', containerShapeSquare: 'extraLarge', @@ -86,7 +81,6 @@ const sizes = { leadingSpace: 64, trailingSpace: 64, outlinedOutlineWidth: 3, - outerBoundHeight: 144, labelVariant: 'headlineLarge', containerShapeRound: 'full', containerShapeSquare: 'extraLarge', @@ -138,4 +132,6 @@ const toggle = { { unselected: ToggleColorSpec; selected: ToggleColorSpec } >; -export const Tokens = { sizes, toggle }; +const minTouchTargetHeight = 48; + +export const Tokens = { sizes, toggle, minTouchTargetHeight }; diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 0729454ea4..c430668a24 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -36,7 +36,6 @@ export type ButtonSizeStyle = { iconGap: number; outlineWidth: number; labelVariant: ButtonLabelVariant; - outerBoundHeight: number; }; /** @@ -53,7 +52,6 @@ export const getButtonSizeStyle = (size: ButtonSize): ButtonSizeStyle => { iconGap: t.iconLabelSpace, outlineWidth: t.outlinedOutlineWidth, labelVariant: t.labelVariant, - outerBoundHeight: t.outerBoundHeight, }; }; @@ -69,14 +67,14 @@ export const getButtonContentBoxStyle = ( }); /** - * Slop that brings the touch target up to the size's `outerBoundHeight`, + * Slop that brings the touch target up to `max(48, containerHeight)`, * expanding outside the container. * * Returns `undefined` when the container is already tall enough. */ export const getButtonHitSlop = (size: ButtonSize): Insets | undefined => { - const { containerHeight, outerBoundHeight } = Tokens.sizes[size]; - const vertical = (outerBoundHeight - containerHeight) / 2; + const { containerHeight } = Tokens.sizes[size]; + const vertical = (Tokens.minTouchTargetHeight - containerHeight) / 2; if (vertical <= 0) { return undefined; diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 2e4ce470c1..0cb21638d1 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -535,13 +535,12 @@ const sizeMetrics: [ iconGap: number, outlineWidth: number, labelVariant: ButtonLabelVariant, - outerBoundHeight: number, ][] = [ - ['extra-small', 32, 12, 12, 20, 4, 1, 'labelLarge', 48], - ['small', 40, 16, 16, 20, 8, 1, 'labelLarge', 48], - ['medium', 56, 24, 24, 24, 8, 1, 'titleMedium', 64], - ['large', 96, 48, 48, 32, 12, 2, 'headlineSmall', 104], - ['extra-large', 136, 64, 64, 40, 16, 3, 'headlineLarge', 144], + ['extra-small', 32, 12, 12, 20, 4, 1, 'labelLarge'], + ['small', 40, 16, 16, 20, 8, 1, 'labelLarge'], + ['medium', 56, 24, 24, 24, 8, 1, 'titleMedium'], + ['large', 96, 48, 48, 32, 12, 2, 'headlineSmall'], + ['extra-large', 136, 64, 64, 40, 16, 3, 'headlineLarge'], ]; describe('getButtonSizeStyle', () => { @@ -555,8 +554,7 @@ describe('getButtonSizeStyle', () => { iconSize, iconGap, outlineWidth, - labelVariant, - outerBoundHeight + labelVariant ) => { expect(getButtonSizeStyle(size)).toEqual({ minHeight, @@ -566,29 +564,30 @@ describe('getButtonSizeStyle', () => { iconGap, outlineWidth, labelVariant, - outerBoundHeight, }); } ); }); describe('getButtonHitSlop', () => { - // The slop closes the gap between the drawn container and the outer bound, - // halved because it applies to both edges. - it.each(sizeMetrics)('reaches the %s outer bound', (size) => { - const { containerHeight, outerBoundHeight } = Tokens.sizes[size]; - const slop = getButtonHitSlop(size); - - expect(containerHeight + (slop?.top ?? 0) + (slop?.bottom ?? 0)).toBe( - outerBoundHeight - ); - }); + it.each(sizeMetrics)( + 'brings the %s touch target to at least 48dp tall', + (size, minHeight) => { + const slop = getButtonHitSlop(size); - // The outer bound is a height; width comes off the 64dp minimum, which - // already clears the target. - it.each(sizeMetrics)('leaves the %s width alone', (size) => { - expect(getButtonHitSlop(size)).toMatchObject({ left: 0, right: 0 }); - }); + expect(minHeight + (slop?.top ?? 0) + (slop?.bottom ?? 0)).toBe( + Math.max(48, minHeight) + ); + } + ); + + // Width comes off the 64dp minimum, which already clears the target. + it.each(['extra-small', 'small'] as const)( + 'leaves the %s width alone', + (size) => { + expect(getButtonHitSlop(size)).toMatchObject({ left: 0, right: 0 }); + } + ); }); describe('size prop', () => { @@ -1125,17 +1124,29 @@ describe('touch target', () => { }); }); - it('expands the target to the outer bound', async () => { + it('expands a 32dp target to 48dp', async () => { await render( - ); - expect(screen.getByTestId('button')).toHaveProp( - 'hitSlop', - getButtonHitSlop('medium') + expect(screen.getByTestId('button')).toHaveProp('hitSlop', { + top: 8, + bottom: 8, + left: 0, + right: 0, + }); + }); + + it('adds no slop to a target taller than 48dp', async () => { + await render( + ); + + expect(screen.getByTestId('button')).not.toHaveProp('hitSlop'); }); it('lets a caller hitSlop win', async () => { From 18b49c25edc2c49195cad7f658bb34ef75afe8b3 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 10:02:01 +0100 Subject: [PATCH 58/66] fix: keep the Button corner at 0 or above during the shape spring --- src/components/Button/Button.tsx | 38 ++++++++++++++++-------- src/components/Button/utils.tsx | 22 ++++++++++++++ src/components/__tests__/Button.test.tsx | 29 ++++++++++++++++++ 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 725326fc0e..c2bd11eb50 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -19,6 +19,7 @@ import { getButtonHitSlop, getButtonPressedRadius, getButtonRippleColor, + getButtonShapeCurvePoints, getButtonShapeRadius, getButtonSizeStyle, getButtonTransitionDuration, @@ -318,12 +319,10 @@ const Button = ({ animatesShape && pressed ? getButtonPressedRadius({ size, theme }) : restingRadius; - const shapeSpring = React.useMemo(() => { - const { points, duration } = toSpringCurve( - theme.motion.spring.fast.spatial - ); - return { timingFunction: linear(...points), duration }; - }, [theme.motion.spring.fast.spatial]); + const shapeSpring = React.useMemo( + () => toSpringCurve(theme.motion.spring.fast.spatial), + [theme.motion.spring.fast.spatial] + ); const shapeTransitionDuration = animatesShape ? shapeSpring.duration * theme.animation.scale : 0; @@ -402,6 +401,24 @@ 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] + ); + // The clip carries the same radius transition as the `Surface`, so the // ripple and the disabled overlay follow the morph. // @@ -416,15 +433,12 @@ const Button = ({ const shapeTransitionStyle = { transitionProperty: 'borderRadius', transitionDuration: shapeTransitionDuration, - transitionTimingFunction: shapeSpring.timingFunction, + transitionTimingFunction: shapeTimingFunction, } as const; const clipStyle = { ...shapeTransitionStyle, borderRadius: radius }; - const innerClipStyle = { - ...shapeTransitionStyle, - borderRadius: Math.max(0, radius - borderWidth), - }; + const innerClipStyle = { ...shapeTransitionStyle, borderRadius: innerRadius }; const outlineStyle = React.useMemo( () => ({ borderColor, borderWidth }), @@ -458,7 +472,7 @@ const Button = ({ elevation={elevation} transitionDuration={surfaceTransitionDuration} shapeTransitionDuration={shapeTransitionDuration} - shapeTransitionTimingFunction={shapeSpring.timingFunction} + shapeTransitionTimingFunction={shapeTimingFunction} style={[styles.button, style]} > diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index c430668a24..200d007d4f 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -149,6 +149,28 @@ export const getButtonTransitionDuration = ({ ); }; +/** + * 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, diff --git a/src/components/__tests__/Button.test.tsx b/src/components/__tests__/Button.test.tsx index 0cb21638d1..5851772bca 100644 --- a/src/components/__tests__/Button.test.tsx +++ b/src/components/__tests__/Button.test.tsx @@ -11,6 +11,7 @@ import { pink500, white } from '../../theme/colors'; import { LightTheme } from '../../theme/schemes'; import { tokens } from '../../theme/tokens'; import { shadow } from '../../theme/tokens/sys/elevation'; +import { toSpringCurve } from '../../theme/tokens/sys/motion'; import Button from '../Button/Button'; import { Tokens } from '../Button/tokens'; import { @@ -19,6 +20,7 @@ import { getButtonHitSlop, getButtonPressedRadius, getButtonRippleColor, + getButtonShapeCurvePoints, getButtonShapeRadius, getButtonSizeStyle, getButtonTransitionDuration, @@ -652,6 +654,33 @@ describe('getButtonPressedRadius', () => { ); }); +describe('getButtonShapeCurvePoints', () => { + const { points } = toSpringCurve(LightTheme.motion.spring.fast.spatial); + const cornersOnTheWay = (curve: number[], from: number, to: number) => + curve.map((progress) => from + (to - from) * progress); + + it('keeps the corner at 0 or above on its way to a 0 corner', () => { + const curve = getButtonShapeCurvePoints({ + points, + radius: 0, + largestRadius: 20, + }); + + expect(Math.min(...cornersOnTheWay(curve, 20, 0))).toBe(0); + }); + + it('keeps the overshoot when the corner has room for it', () => { + const curve = getButtonShapeCurvePoints({ + points, + radius: 8, + largestRadius: 20, + }); + + expect(curve).toEqual(points); + expect(Math.min(...cornersOnTheWay(curve, 20, 8))).toBeLessThan(8); + }); +}); + describe('getButtonTransitionDuration', () => { const theme = LightTheme; const opaque = theme.colors.primary; From f6a22de81877773a4ca221dd84b809afcceb56d9 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 10:03:02 +0100 Subject: [PATCH 59/66] docs: hide the Surface shape props and inline the Button unions --- src/components/Button/Button.tsx | 6 +++--- src/components/Surface.tsx | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index c2bd11eb50..9d807c1908 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -25,7 +25,7 @@ import { getButtonTransitionDuration, getEffectiveButtonShape, } from './utils'; -import type { ButtonMode, ButtonShape, ButtonSize } from './utils'; +import type { ButtonMode } from './utils'; import { getDefaultDirection, useLocale } from '../../core/locale'; import { useInternalTheme } from '../../core/theming'; import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; @@ -70,13 +70,13 @@ export type Props = Omit & { * The size controls the minimum height, horizontal padding, icon size, the * gap between icon and label, and the label typescale. */ - size?: ButtonSize; + size?: 'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'; /** * Shape variant of the button (Material Design 3 expressive). Defaults to * `round`. `'round'` uses the full-pill corner radius; `'square'` uses a * smaller per-size corner radius. */ - shape?: ButtonShape; + shape?: 'round' | 'square'; /** * Whether the container animates its corner radius. Defaults to `true`. * diff --git a/src/components/Surface.tsx b/src/components/Surface.tsx index 10da26b27b..c05857ed34 100644 --- a/src/components/Surface.tsx +++ b/src/components/Surface.tsx @@ -97,11 +97,13 @@ export type Props = Omit & */ transitionDuration?: number; /** + * @internal * Duration of the `borderRadius` transition in milliseconds. Undefined * means no transition. */ shapeTransitionDuration?: number; /** + * @internal * Timing function of the `borderRadius` transition. */ shapeTransitionTimingFunction?: CSSTransitionTimingFunction; From b88fc68638c23a653ccdc407803522781bc5d409 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 10:04:05 +0100 Subject: [PATCH 60/66] docs: note the Button style and contentStyle limits --- docs/6.x/docs/guides/migration.md | 21 +++++++++++++++++++++ src/components/Button/Button.tsx | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 1ea26f8c05..0363571776 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -522,6 +522,10 @@ a plain button. It is ignored on `mode="text"`, which MD3 gives no toggle. reads `corner.medium` at `extra-small` and `small`; `corner.large` at `medium`; and `corner.extraLarge` at `large` and `extra-large`. - **Background color** is no longer set through `style`. Use `buttonColor`. +- **Border color and width** are no longer set through `style`. An inner view + draws the outline, so `borderColor` in `style` does not recolor it, and + `borderWidth` adds a second border around it. To recolor the outline, + override `theme.colors.outlineVariant`. - **Animated styles** must come from Reanimated. A React Native `Animated.Value` in `style` is not applied. @@ -541,6 +545,23 @@ a plain button. It is ignored on `mode="text"`, which MD3 gives no toggle. ``` +#### Content style + +The content takes `minHeight`, `paddingStart` and `paddingEnd` from `size`. +To change them in `contentStyle`, set these same properties. On Android and +iOS, `padding` and `paddingHorizontal` do not override them, and a `height` +below `minHeight` has no effect. + +```tsx +// Before (v5) + + +// After (v6) + +``` + #### Appearance changes These need no code change, but the rendering differs: diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 9d807c1908..98d648e53f 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -173,6 +173,11 @@ export type Props = Omit & { /** * Style of button's inner content. * Use this prop to apply custom height and width or to set a custom padding. + * + * The content takes `minHeight`, `paddingStart` and `paddingEnd` from `size`. + * To change them, set these same properties. On Android and iOS, `padding` + * and `paddingHorizontal` do not override them, and a `height` below + * `minHeight` has no effect. */ contentStyle?: StyleProp; /** From 64f33934daadea4768e7c7523d33ee8c832cc567 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 10:04:30 +0100 Subject: [PATCH 61/66] docs: recommend text buttons for Dialog.Actions in the migration guide --- docs/6.x/docs/guides/migration.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 0363571776..f19df777e7 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -253,7 +253,7 @@ When the dialog is `dismissable`, screen reader users can dismiss it with a visu #### `Dialog.Actions` -`Dialog.Actions` no longer injects `compact` on its buttons. To get close to the previous look, add `size="extra-small"` (which replaces `compact`) and `mode="text"` (since the default [`Button`](#button) mode is now `filled`) to each button. The buttons get shorter: `extra-small` is **32dp** tall, while `compact` buttons kept the **40dp** height: +`Dialog.Actions` no longer injects `compact` on its buttons, and the default [`Button`](#button) mode is now `filled`. Add `mode="text"` to each button. At the default `small` size, the buttons are **40dp** tall, the Material Design 3 size for dialog actions. The `compact` text buttons in 5.x were **38dp** tall. For a more compact look, also add `size="extra-small"`, which is **32dp** tall: ```tsx // Before (v5) @@ -263,7 +263,7 @@ When the dialog is `dismissable`, screen reader users can dismiss it with a visu // After (v6) - @@ -491,7 +491,8 @@ a plain button. It is ignored on `mode="text"`, which MD3 gives no toggle. - **`uppercase`** → `labelStyle={{ textTransform: 'uppercase' }}`. - **`compact`** → `size="extra-small"`. This also changes the height: `extra-small` is **32dp** tall, while `compact` only reduced the horizontal - margins and the min width and kept the **40dp** height. + margins and the min width, and kept the height: **40dp**, or **38dp** for + `text` buttons. - **`contentStyle={{ flexDirection: 'row-reverse' }}`** → `iconPosition="trailing"`. - **`rippleColor`** is removed, with no replacement. The ripple uses the label color at the pressed-state opacity. From d8c08a3af0de8726d502a6361076c5d460778a5c Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 15:19:04 +0100 Subject: [PATCH 62/66] docs(button): document custom colors on toggle states --- example/src/Examples/ButtonExample.tsx | 19 +++++++++++++++++++ src/components/Button/Button.tsx | 11 ++++++++++- src/components/__tests__/Button.test.tsx | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index 3bf6da025e..dc8da7cfaf 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -105,6 +105,7 @@ const ButtonExample = () => { >({}); const toggle = (key: string) => setSelectedToggles((prev) => ({ ...prev, [key]: !prev[key] })); + const customSelected = !!selectedToggles.custom; return ( @@ -289,6 +290,24 @@ const ButtonExample = () => { > Custom label color + + ); + + expect(screen.root).toHaveStyle({ backgroundColor: pink500 }); + expect(screen.getByText('Custom toggle')).toHaveStyle({ color: white }); + } +); + it('paints the container color only on the container', async () => { const translucent = 'rgba(0, 0, 0, 0.5)'; From c7568784a0156152f1eb3afe873f023947cef674 Mon Sep 17 00:00:00 2001 From: Oleksandr Zavarzin Date: Fri, 6 Nov 2026 15:24:30 +0100 Subject: [PATCH 63/66] refactor(button): share one icon color with the loading indicator --- src/components/Button/Button.tsx | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index 810705bc86..ed59845dbe 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -468,6 +468,8 @@ const Button = ({ () => StyleSheet.flatten(labelStyle) || {}, [labelStyle] ); + const iconColor = + typeof labelStyleColor === 'string' ? labelStyleColor : labelColor; const contentBoxStyle = React.useMemo( () => getButtonContentBoxStyle(sizeStyle, borderWidth), @@ -542,23 +544,12 @@ const Button = ({ ) : null} {loading ? ( - + ) : null} Date: Fri, 6 Nov 2026 15:25:29 +0100 Subject: [PATCH 64/66] refactor(button): share the isMode helper between Button and utils --- src/components/Button/Button.tsx | 4 ++-- src/components/Button/utils.tsx | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/Button/Button.tsx b/src/components/Button/Button.tsx index ed59845dbe..d2d195c55a 100644 --- a/src/components/Button/Button.tsx +++ b/src/components/Button/Button.tsx @@ -14,6 +14,7 @@ import type { import Reanimated, { linear } from 'react-native-reanimated'; import { + createIsMode, getButtonColors, getButtonContentBoxStyle, getButtonHitSlop, @@ -25,7 +26,6 @@ import { getButtonTransitionDuration, getEffectiveButtonShape, } from './utils'; -import type { ButtonMode } from './utils'; import { getDefaultDirection, useLocale } from '../../core/locale'; import { useInternalTheme } from '../../core/theming'; import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; @@ -276,7 +276,7 @@ const Button = ({ }: Props) => { const theme = useInternalTheme(themeOverrides); const { direction } = useLocale(); - const isMode = (modeToCompare: ButtonMode) => mode === modeToCompare; + const isMode = createIsMode(mode); const requestedTrailingIcon = iconPosition === 'trailing'; // On web, the browser already mirrors the row under `dir="rtl"`. diff --git a/src/components/Button/utils.tsx b/src/components/Button/utils.tsx index 841ff39849..5765714b6b 100644 --- a/src/components/Button/utils.tsx +++ b/src/components/Button/utils.tsx @@ -181,6 +181,11 @@ export const getButtonPressedRadius = ({ }): number => resolveCornerRadius(theme, Tokens.sizes[size].pressedContainerShape); +export const createIsMode = + (mode: ButtonMode) => + (modeToCompare: ButtonMode): boolean => + mode === modeToCompare; + type BaseProps = { isMode: (mode: ButtonMode) => boolean; theme: InternalTheme; @@ -388,9 +393,7 @@ export const getButtonColors = ({ borderWidth: number; backgroundOpacity: number; } => { - const isMode = (modeToCompare: ButtonMode) => { - return mode === modeToCompare; - }; + const isMode = createIsMode(mode); const toggleColors = disabled ? undefined From d06db9dafe71d335ab64360e7e58ebab5c10da12 Mon Sep 17 00:00:00 2001 From: Aziz Becha Date: Sat, 7 Nov 2026 18:02:27 +0100 Subject: [PATCH 65/66] docs(migration): point Button roundness at the square shape --- docs/6.x/docs/guides/migration.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index d379c4f1ec..e08481ac6a 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -203,16 +203,20 @@ In 5.x, each of these components multiplied `roundness` by a fixed number, and t | `extraSmall` | 4 | 1 | `Menu`, `Snackbar`, `Tooltip` | | `small` | 8 | 2 | `Chip` | | `medium` | 12 | 3 | `Card` | -| `largeIncreased` | 20 | 5 | `Button`, `SegmentedButtons` | +| `largeIncreased` | 20 | 5 | `SegmentedButtons` | | `extraLarge` | 28 | 7 | `Dialog`, `Drawer.Item`, `Searchbar` (`mode="bar"`) | +`Button` (5.x multiplier 5) is a full pill by default and doesn't read a corner role. With `shape="square"` it reads the corner of its `size`, as listed in the Button section's [Style](#style) notes: `medium` at the default `small` size. + `FAB` and `FAB.Extended` use the role of their `size`: `large` (16) by default, `largeIncreased` for `size="medium"` and `extraLarge` for `size="large"`. `TextInput` no longer follows the theme: its corners are always 4. A component's `theme` prop is merged deeply, so you can set only the role you need: ```diff - -+ ++ ``` `PaperProvider` doesn't merge `shapes` deeply: a `shapes` key in its `theme` replaces the default one. So spread the base `shapes` and `corner`, or the roles you leave out are `undefined` and those components lose their rounded corners: From d3f23b1f7af15b24c3a626dac8fcbdfc8916ad94 Mon Sep 17 00:00:00 2001 From: Aziz Becha Date: Sat, 7 Nov 2026 18:09:11 +0100 Subject: [PATCH 66/66] docs(theming): point Button corner examples at the square shape --- docs/6.x/docs/guides/theming.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index 7acca1d202..c54c1d1294 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -67,7 +67,7 @@ You can change the theme prop dynamically and all the components will automatica A theme usually contains the following properties: - `dark` (`boolean`): whether this is a dark theme or light theme. -- `shapes` (`object`): corner radii of components, under `corner`, from `extraSmall` (4) to `extraExtraLarge` (48). Each component uses one of them, e.g. `Button` uses `largeIncreased`. +- `shapes` (`object`): corner radii of components, under `corner`, from `extraSmall` (4) to `extraExtraLarge` (48). Each component uses one of them, e.g. `Card` uses `medium`. `Button` is a full pill by default; with `shape="square"` it uses the role of its `size`, `medium` at the default `small` size. - `colors` (`object`): various colors used throughout different elements. > The primary key color is used to derive roles for key components across the UI, such as the FAB, prominent buttons, active states, as well as the tint of elevated surfaces. @@ -473,8 +473,8 @@ import { Button } from 'react-native-paper'; export default function ButtonExample() { return (