diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 1904a3aa1f..6e2733ad28 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -10,6 +10,7 @@ React Native Paper 6 uses [Reanimated](https://docs.swmansion.com/react-native-r The following props now accept animated styles returned from `useAnimatedStyle`. They no longer accept `Animated.Value` or `Animated.AnimatedInterpolation` where these were previously supported: +- `Appbar` and `Appbar.Header`: `style` - `Appbar.Action` and `Appbar.BackAction`: `style` - `Badge`: `style` - `Banner`: `style` @@ -57,6 +58,8 @@ You can use an elevation level from `0` to `5` instead. Changes to the elevation The following component style props no longer support overriding their background color or border radius: +- `Appbar` and `Appbar.Header` +- `Avatar.Icon`, `Avatar.Image` and `Avatar.Text`: background color - `Banner` - `Button` - `Card` @@ -66,7 +69,17 @@ The following component style props no longer support overriding their backgroun - `Searchbar` - `Snackbar` -You can use the component's color prop where available, or override the corresponding theme colors. +You can use the component's color prop where available, or override the corresponding theme colors. See the component sections below for details. + +Components no longer read values from the `style` prop to derive other styles, since this is not possible with animated styles. The affected props have been replaced with dedicated props: + +- `Appbar` and `Appbar.Header`: `backgroundColor` and the border radius props (`borderRadius`, `borderTopLeftRadius` etc.) replace `style.backgroundColor` and the border radius in `style` +- `Avatar.Icon`, `Avatar.Image` and `Avatar.Text`: `backgroundColor` replaces `style.backgroundColor` +- `Button`: `iconSize` replaces `labelStyle.fontSize` for the size of the icon and the loading indicator +- `Button`: `textColor` replaces `labelStyle.color` for the color of the icon and the loading indicator +- `Card`: `borderColor` replaces `style.borderColor` for the outline color + +`Tooltip` no longer reads positioning styles from the wrapped element. Apply them to a parent `View` wrapping the `Tooltip`, see [Tooltip](#tooltip). ### Ripple color @@ -285,9 +298,66 @@ Libraries built on Paper may not support 6.x yet. For example, `react-native-pap + ``` -The `style` props for `Appbar` and `Appbar.Header` no longer accept `Animated.Value` or `Animated.AnimatedInterpolation`. They only accept static styles. +- The `style` props for `Appbar` and `Appbar.Header` no longer configure the background color or border radius. Use the `backgroundColor` prop and the border radius props (`borderRadius`, `borderTopLeftRadius`, `borderCurve` etc.) instead. +- The `style.elevation` property is no longer supported. Use the `elevated` prop to control Appbar elevation. +- The default height of `Appbar` now includes the `safeAreaInsets`, so the content area keeps the height of the selected `mode`. +- The `height` specified in `style` for `Appbar.Header` now includes the status bar height. Previously, the status bar height was added to it automatically. + +e.g.: + +```diff + + + +``` + +To keep the height of the content row when setting `height` in `style`, add the top inset from `useSafeAreaInsets()` of `react-native-safe-area-context`: -The `style.elevation` property is no longer supported. Use the `elevated` prop to control Appbar elevation. +```diff ++const { top } = useSafeAreaInsets(); ++ + + + +``` + +### Avatar + +The `style` prop for `Avatar.Icon`, `Avatar.Image` and `Avatar.Text` no longer configures the background color. Use the `backgroundColor` prop instead. The text or icon color is still derived from the background color unless `color` is specified. + +e.g.: + +```diff + +``` + +### Tooltip + +`Tooltip` no longer reads positioning styles such as `position: 'absolute'` from the `style` of the wrapped element. Apply them to a parent `View` wrapping the `Tooltip` instead. + +e.g.: + +```diff +- +- +- ++ ++ ++ ++ ++ +``` ### Surface @@ -738,6 +808,14 @@ below `minHeight` has no effect. ``` +#### Label style + +- `labelStyle={{ fontSize }}` no longer changes the icon size. The icon follows + `size`; use the `iconSize` prop to change the size of the icon and the + loading indicator. +- `labelStyle={{ color }}` no longer changes the color of the icon and the + loading indicator. Use the `textColor` prop instead. + #### Appearance changes These need no code change, but the rendering differs: @@ -752,8 +830,6 @@ These need no code change, but the rendering differs: wider: **16dp** of horizontal padding at the default `small` size instead of **8dp**, and a **64dp** minimum width. - 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**. - At the default `small` size, the horizontal padding is **16dp** for every mode. In 5.x, the label margins were **24dp** for `filled`, `tonal`, @@ -836,6 +912,20 @@ We have also added a `direction` prop to the `Card` component, which lets you ar ``` +#### Outline color + +The `borderColor` in `style` no longer changes the outline color in `outlined` mode. Use the `borderColor` prop instead: + +```diff + + ... + +``` + #### `Card.Cover` The `style` prop of `Card.Cover` is now typed as a `View` style. Image style properties such as `resizeMode` or `tintColor` were never applied, so pass them as `Image` props instead: @@ -845,6 +935,8 @@ The `style` prop of `Card.Cover` is now typed as a `View` style. Image style pro + ``` +`Card.Cover` no longer applies the border radius from `style` to the image directly. The image is clipped by the container, so any border radius passed in `style` still applies. + #### `Card.Actions` `Card.Actions` no longer injects `mode` on the buttons. To keep the previous behavior, you need to set `mode="outlined"` on the first button and `mode="filled"` (formerly `contained`) on the rest: diff --git a/eslint.config.mjs b/eslint.config.mjs index ac6e65951f..d415994fde 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -119,6 +119,16 @@ export default defineConfig( }, ], + 'no-restricted-properties': [ + 'error', + { + object: 'StyleSheet', + property: 'flatten', + message: + 'Do not flatten styles. Animated styles from `useAnimatedStyle` cannot be flattened. Accept the values you need as props instead.', + }, + ], + '@react-native/platform-colors': 'error', 'promise/no-callback-in-promise': 'error', @@ -174,6 +184,8 @@ export default defineConfig( rules: { ...testingLibraryReact.rules, + 'no-restricted-properties': 'off', + 'no-restricted-syntax': [ 'error', { diff --git a/example/src/Examples/AppbarExample.tsx b/example/src/Examples/AppbarExample.tsx index 2d4eb0bbc4..08bbbcbc1b 100644 --- a/example/src/Examples/AppbarExample.tsx +++ b/example/src/Examples/AppbarExample.tsx @@ -45,7 +45,7 @@ const AppbarExample = () => { navigation.setOptions({ header: () => ( @@ -169,10 +169,8 @@ const AppbarExample = () => { { height: height + bottom, }, - { - backgroundColor: theme.colors.surfaceContainerHigh, - }, ]} + backgroundColor={theme.colors.surfaceContainerHigh} safeAreaInsets={{ bottom, left, right }} > {}} /> @@ -217,7 +215,4 @@ const styles = StyleSheet.create({ position: 'absolute', right: 16, }, - customColor: { - backgroundColor: Palette.secondary80, - }, }); diff --git a/example/src/Examples/AvatarExample.tsx b/example/src/Examples/AvatarExample.tsx index 6d6349cbba..03a7576337 100644 --- a/example/src/Examples/AvatarExample.tsx +++ b/example/src/Examples/AvatarExample.tsx @@ -10,12 +10,8 @@ const AvatarExample = () => { @@ -26,12 +22,8 @@ const AvatarExample = () => { diff --git a/example/src/Examples/ButtonExample.tsx b/example/src/Examples/ButtonExample.tsx index dc8da7cfaf..554f80cda1 100644 --- a/example/src/Examples/ButtonExample.tsx +++ b/example/src/Examples/ButtonExample.tsx @@ -338,6 +338,7 @@ const ButtonExample = () => { + ); + + expect(renderIcon).toHaveBeenCalledWith( + expect.objectContaining({ size: 24 }) + ); +}); + +it('renders the loading indicator at the size set by iconSize', async () => { + const tree = ( + await render( + + ) + ).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + it('renders loading button', async () => { const tree = (await render()).toJSON(); diff --git a/src/components/__tests__/Card/Card.test.tsx b/src/components/__tests__/Card/Card.test.tsx index 2bfb8584a2..6bea220fad 100644 --- a/src/components/__tests__/Card/Card.test.tsx +++ b/src/components/__tests__/Card/Card.test.tsx @@ -5,10 +5,9 @@ import { afterEach, describe, expect, it, jest } from '@jest/globals'; import { render, screen } from '../../../test-utils'; import { LightTheme } from '../../../theme/schemes'; -import { Palette } from '../../../theme/tokens'; import Button from '../../Button/Button'; import Card from '../../Card/Card'; -import { getCardColors, getCardCoverStyle } from '../../Card/utils'; +import { getCardColors } from '../../Card/utils'; const styles = StyleSheet.create({ customCoverRadius: { @@ -47,13 +46,9 @@ describe('Card', () => { expect(toJSON()).toMatchSnapshot(); }); - it('renders an outlined card with custom border color', async () => { + it('renders the outline with the color passed to borderColor prop', async () => { const { toJSON } = await render( - + {null} ); @@ -119,6 +114,21 @@ describe('Card', () => { }); describe('CardCover', () => { + it('renders with the theme border radius by default', async () => { + await render( + + + + ); + + expect(screen.getByTestId('card-cover').parent).toHaveStyle({ + borderRadius: LightTheme.shapes.corner.medium, + }); + }); + it('renders with custom border radius', async () => { await render( @@ -130,7 +140,8 @@ describe('CardCover', () => { ); - expect(screen.getByTestId('card-cover')).toHaveStyle( + // The border radius is applied to the container which clips the image + expect(screen.getByTestId('card-cover').parent).toHaveStyle( styles.customCoverRadius ); }); @@ -245,26 +256,6 @@ describe('getCardColors - border color', () => { }); }); -describe('getCardCoverStyle - border radius', () => { - it('should return custom border radius', () => { - expect( - getCardCoverStyle({ - theme: LightTheme, - borderRadiusStyles: styles.customCoverRadius, - }) - ).toMatchObject(styles.customCoverRadius); - }); - - it('should return correct border radius based on roundness, for theme version 3', () => { - expect( - getCardCoverStyle({ - theme: LightTheme, - borderRadiusStyles: {}, - }) - ).toMatchObject({ borderRadius: LightTheme.shapes.corner.medium }); - }); -}); - describe('CardContent', () => { it('uses the Card padding when it follows a cover and a title', async () => { await render( diff --git a/src/components/__tests__/Card/__snapshots__/Card.test.tsx.snap b/src/components/__tests__/Card/__snapshots__/Card.test.tsx.snap index 2f7cee90ba..c746b6a75b 100644 --- a/src/components/__tests__/Card/__snapshots__/Card.test.tsx.snap +++ b/src/components/__tests__/Card/__snapshots__/Card.test.tsx.snap @@ -6,9 +6,6 @@ exports[`Card renders an outlined card 1`] = ` style={ [ {}, - { - "borderColor": "rgba(121, 116, 126, 1)", - }, undefined, {}, { @@ -140,9 +137,6 @@ exports[`Card renders an outlined card with a custom outline color 1`] = ` style={ [ {}, - { - "borderColor": "purple", - }, undefined, {}, { @@ -267,19 +261,13 @@ exports[`Card renders an outlined card with a custom outline color 1`] = ` `; -exports[`Card renders an outlined card with custom border color 1`] = ` +exports[`Card renders the outline with the color passed to borderColor prop 1`] = ` { top: 250, // pageY (200) + height (50) }); }); + + it('anchors the tooltip to the wrapper when the child is absolutely positioned', async () => { + const { + wrapper: { getByText, findByText }, + } = await setup({ + children: ( + + ), + }); + + await userEvent.longPress(getTrigger(getByText)); + + const tooltip = await findByText('some tooltip text'); + await fireEvent(tooltip, 'layout', { + nativeEvent: { + layout: { width: TOOLTIP_WIDTH, height: TOOLTIP_HEIGHT }, + }, + }); + + expect(tooltip.parent).toHaveStyle({ + left: 210, + top: 250, + }); + }); }); describe('When it overflows to left', () => { diff --git a/src/components/__tests__/__snapshots__/Avatar.test.tsx.snap b/src/components/__tests__/__snapshots__/Avatar.test.tsx.snap index 9e55666cef..31731acd4c 100644 --- a/src/components/__tests__/__snapshots__/Avatar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Avatar.test.tsx.snap @@ -5,7 +5,6 @@ exports[`renders avatar with icon 1`] = ` style={ [ { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderRadius": 32, "height": 64, "width": 64, @@ -14,7 +13,10 @@ exports[`renders avatar with icon 1`] = ` "alignItems": "center", "justifyContent": "center", }, - {}, + undefined, + { + "backgroundColor": "rgba(103, 80, 164, 1)", + }, ] } > @@ -55,7 +57,6 @@ exports[`renders avatar with icon and custom background color 1`] = ` style={ [ { - "backgroundColor": "#f44336", "borderRadius": 32, "height": 64, "width": 64, @@ -64,7 +65,10 @@ exports[`renders avatar with icon and custom background color 1`] = ` "alignItems": "center", "justifyContent": "center", }, - {}, + undefined, + { + "backgroundColor": "#f44336", + }, ] } > @@ -105,12 +109,14 @@ exports[`renders avatar with image 1`] = ` style={ [ { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderRadius": 32, "height": 64, "width": 64, }, undefined, + { + "backgroundColor": "rgba(103, 80, 164, 1)", + }, ] } > @@ -137,7 +143,6 @@ exports[`renders avatar with text 1`] = ` style={ [ { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderRadius": 32, "height": 64, "width": 64, @@ -146,7 +151,10 @@ exports[`renders avatar with text 1`] = ` "alignItems": "center", "justifyContent": "center", }, - {}, + undefined, + { + "backgroundColor": "rgba(103, 80, 164, 1)", + }, ] } > @@ -191,7 +199,6 @@ exports[`renders avatar with text and custom background color 1`] = ` style={ [ { - "backgroundColor": "#f44336", "borderRadius": 32, "height": 64, "width": 64, @@ -200,7 +207,10 @@ exports[`renders avatar with text and custom background color 1`] = ` "alignItems": "center", "justifyContent": "center", }, - {}, + undefined, + { + "backgroundColor": "#f44336", + }, ] } > @@ -245,7 +255,6 @@ exports[`renders avatar with text and custom colors 1`] = ` style={ [ { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderRadius": 32, "height": 64, "width": 64, @@ -254,7 +263,10 @@ exports[`renders avatar with text and custom colors 1`] = ` "alignItems": "center", "justifyContent": "center", }, - {}, + undefined, + { + "backgroundColor": "rgba(103, 80, 164, 1)", + }, ] } > @@ -299,7 +311,6 @@ exports[`renders avatar with text and custom size 1`] = ` style={ [ { - "backgroundColor": "rgba(103, 80, 164, 1)", "borderRadius": 48, "height": 96, "width": 96, @@ -308,7 +319,10 @@ exports[`renders avatar with text and custom size 1`] = ` "alignItems": "center", "justifyContent": "center", }, - {}, + undefined, + { + "backgroundColor": "rgba(103, 80, 164, 1)", + }, ] } > diff --git a/src/components/__tests__/__snapshots__/BottomNavigation.test.tsx.snap b/src/components/__tests__/__snapshots__/BottomNavigation.test.tsx.snap index 22b83abaaa..625e3e15ee 100644 --- a/src/components/__tests__/__snapshots__/BottomNavigation.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/BottomNavigation.test.tsx.snap @@ -76,6 +76,7 @@ exports[`allows customizing Route's type via generics 1`] = ` onLayout={[Function]} style={ { + "backgroundColor": "rgba(243, 237, 247, 1)", "bottom": 0, "left": 0, "pointerEvents": "none", @@ -84,11 +85,9 @@ exports[`allows customizing Route's type via generics 1`] = ` } > `; + +exports[`renders the loading indicator at the size set by iconSize 1`] = ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Loading Button + + + + + +`; diff --git a/src/utils/__tests__/splitStyles.test.ts b/src/utils/__tests__/splitStyles.test.ts deleted file mode 100644 index c27a53d940..0000000000 --- a/src/utils/__tests__/splitStyles.test.ts +++ /dev/null @@ -1,86 +0,0 @@ -import type { ViewStyle } from 'react-native'; - -import { describe, expect, it } from '@jest/globals'; - -import { splitStyles } from '../splitStyles'; - -describe('splitStyles', () => { - const styles: Readonly = Object.freeze({ - backgroundColor: 'red', - marginTop: 1, - marginBottom: 2, - marginLeft: 3, - padding: 4, - borderTopLeftRadius: 5, - borderTopRightRadius: 6, - right: 4, - }); - - it('splits margins, paddings, and border radiuses correctly', () => { - const marginPredicate = (style: string) => style.startsWith('margin'); - const paddingPredicate = (style: string) => style.startsWith('padding'); - const borderRadiusPredicate = (style: string) => - style.startsWith('border') && style.endsWith('Radius'); - const [filteredStyles, marginStyles, paddingStyles, borderRadiusStyles] = - splitStyles( - styles, - marginPredicate, - paddingPredicate, - borderRadiusPredicate - ); - - expect(keysLength(filteredStyles)).toBeGreaterThan(0); - expect(keysLength(filteredStyles)).toBeLessThan(keysLength(styles)); - for (const style in filteredStyles) { - expect(marginPredicate(style)).toBeFalsy(); - expect(paddingPredicate(style)).toBeFalsy(); - expect(borderRadiusPredicate(style)).toBeFalsy(); - } - - expect(keysLength(marginStyles)).toBeGreaterThan(0); - for (const style in marginStyles) { - expect(marginPredicate(style)).toBeTruthy(); - } - - expect(keysLength(paddingStyles)).toBeGreaterThan(0); - for (const style in paddingStyles) { - expect(paddingPredicate(style)).toBeTruthy(); - } - - expect(keysLength(borderRadiusStyles)).toBeGreaterThan(0); - for (const style in borderRadiusStyles) { - expect(borderRadiusPredicate(style)).toBeTruthy(); - } - }); - - it('filtered styles is an empty object if all styles matched some predicate', () => { - const styles = { - margin: 5, - padding: 6, - }; - const [filteredStyles] = splitStyles( - styles, - (style) => style.startsWith('margin'), - (style) => style.startsWith('padding') - ); - - expect(keysLength(filteredStyles)).toBe(0); - }); - - it('processes predicates in order', () => { - const [, marginStyles, marginStyles2, marginStyles3] = splitStyles( - styles, - (style) => style.startsWith('margin'), - (style) => style.startsWith('margin'), - (style) => style.startsWith('margin') - ); - - expect(keysLength(marginStyles)).toBeGreaterThan(0); - expect(keysLength(marginStyles2)).toBe(0); - expect(keysLength(marginStyles3)).toBe(0); - }); -}); - -function keysLength(object: object): number { - return Object.keys(object).length; -} diff --git a/src/utils/splitStyles.ts b/src/utils/splitStyles.ts deleted file mode 100644 index 910c62eb45..0000000000 --- a/src/utils/splitStyles.ts +++ /dev/null @@ -1,62 +0,0 @@ -import type { ViewStyle } from 'react-native'; - -type FiltersArray = readonly ((style: keyof ViewStyle) => boolean)[]; - -type MappedTuple = { - [Index in keyof Tuple]: ViewStyle; -} & { length: Tuple['length'] }; - -type Style = ViewStyle[keyof ViewStyle]; -type Entry = [keyof ViewStyle, Style]; - -/** - * Utility function to extract styles in separate objects - * - * @param styles The style object you want to filter - * @param filters The filters by which you want to split the styles - * @returns An array of filtered style objects: - * - The first style object contains the properties that didn't match any filter - * - After that there will be a style object for each filter you passed in the same order as the matching filters - * - A style property will exist in a single style object, the first filter it matched - */ -export function splitStyles( - styles: ViewStyle, - ...filters: Tuple -) { - if (process.env.NODE_ENV !== 'production' && filters.length === 0) { - console.error('No filters were passed when calling splitStyles'); - } - - // `Object.entries` will be used to iterate over the styles and `Object.fromEntries` will be called before returning - // Entries which match the given filters will be temporarily stored in `newStyles` - const newStyles: Entry[][] = filters.map(() => []); - - // Entries which match no filter - const rest: Entry[] = []; - - // Iterate every style property - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - outer: for (const item of Object.entries(styles) as Entry[]) { - // Check each filter - for (let i = 0; i < filters.length; i++) { - // Check if filter matches - if (filters[i](item[0])) { - newStyles[i].push(item); // Push to temporary filtered entries array - continue outer; // Skip to checking next style property - } - } - - // Adds to rest styles if not filtered - rest.push(item); - } - - // Put unmatched styles in the beginning - newStyles.unshift(rest); - - // Convert arrays of entries into objects - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - return newStyles.map((styles) => Object.fromEntries(styles)) as unknown as [ - ViewStyle, - ...MappedTuple, - ]; -}