diff --git a/docs/6.x/docs/guides/getting-started.mdx b/docs/6.x/docs/guides/getting-started.mdx index 317298838d..8777daff41 100644 --- a/docs/6.x/docs/guides/getting-started.mdx +++ b/docs/6.x/docs/guides/getting-started.mdx @@ -11,7 +11,7 @@ import Tabs from '@docs/components/Tabs'; React Native Paper 6.x requires React 19 and React Native's New Architecture. -Open a Terminal in your project's folder and install the library: +Open a Terminal in your project's folder and install the library. In a Community CLI app, install Reanimated and Worklets first, as the Community CLI tab below explains: ```bash npm2yarn npm install react-native-paper@alpha @@ -35,6 +35,8 @@ Minimum requirements: - `react-native-worklets` **≥ 0.8.1** - `react-native-safe-area-context`: a version compatible with your React Native toolchain. +`npx expo install` installs the versions that your Expo SDK includes. SDK 56 is the first whose Reanimated and Worklets meet these minimums (SDK 55 has Reanimated 4.2.1 and Worklets 0.7.4), so on an older SDK, upgrade Expo first. + You also need to install [@react-native-vector-icons/material-design-icons](https://github.com/oblador/react-native-vector-icons/tree/master/packages/material-design-icons), because some components use these icons internally: ```bash @@ -47,18 +49,29 @@ Follow the [React Native Reanimated installation guide](https://docs.swmansion.c If you have a `babel.config.js` or `.babelrc` file, make sure that it includes `babel-preset-expo`. +The app runs in Expo Go, with no development build: Expo Go includes Reanimated, Worklets and the safe area context, and loads the icon font when the app starts. + +Install the Reanimated and Worklets pair for your React Native version from [Reanimated and Worklets versions](#reanimated-and-worklets-versions), pinned with `~`, before `react-native-paper`. For example, on React Native 0.81: + +```bash npm2yarn +npm install react-native-reanimated@~4.3.0 react-native-worklets@~0.8.1 +``` + +Without `~`, or when you install Paper first, npm picks the newest Reanimated, which may not support your React Native version, and the install fails with `ERESOLVE`. + +Then install the safe area context: + ```bash npm2yarn -npm install react-native-safe-area-context react-native-reanimated react-native-worklets +npm install react-native-safe-area-context ``` Minimum requirements: - `react-native` **≥ 0.81** -- `react-native-reanimated` **≥ 4.3.0** -- `react-native-worklets` **≥ 0.8.1** +- `react-native-reanimated` **≥ 4.3.0** and `react-native-worklets` **≥ 0.8.1**, in the pair that supports your React Native version - `react-native-safe-area-context`: a version compatible with your React Native toolchain. Follow the [React Native Reanimated installation guide](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/getting-started/). @@ -88,6 +101,19 @@ If you don't want to install vector icons, leave the package out. Components the +### Reanimated and Worklets versions + +Each Reanimated release supports a few React Native versions and needs a matching Worklets version. The newest pair for each React Native version: + +| React Native | `react-native-reanimated` | `react-native-worklets` | +| ------------ | ------------------------- | ----------------------- | +| 0.81, 0.82 | 4.3.x | 0.8.x (0.8.1 or later) | +| 0.83 to 0.85 | 4.6.x | 0.12.x | +| 0.86 | 4.7.x | 0.13.x | +| 0.87 to 0.89 | 4.8.x | 0.14.x | + +For other pairs, see Reanimated's [compatibility table](https://docs.swmansion.com/react-native-reanimated/docs/guides/compatibility/). + ### Bundle size optimization To get smaller bundle size by excluding modules you don't use, you can use our optional babel plugin. The plugin automatically rewrites the import statements so that only the modules you use are imported instead of the whole library. Add `react-native-paper/babel` to the `plugins` section in your `babel.config.js` for production environment. It should look like this: diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 89a4c875f0..83cead4660 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -6,7 +6,7 @@ title: Migration from Paper 5.x to 6.x ### Animations -React Native Paper 6 uses [Reanimated](https://docs.swmansion.com/react-native-reanimated/) for most animations instead of the built-in React Native `Animated` API. So make sure to install `react-native-reanimated` 4.3.0 or later and `react-native-worklets` 0.8.1 or later, then complete the Reanimated setup. See the [getting started guide](./getting-started) for Expo and Community CLI instructions. +React Native Paper 6 uses [Reanimated](https://docs.swmansion.com/react-native-reanimated/) for most animations instead of the built-in React Native `Animated` API. So make sure to install the `react-native-reanimated` and `react-native-worklets` pair that matches your React Native version, as listed in [Reanimated and Worklets versions](./getting-started#reanimated-and-worklets-versions), then complete the Reanimated setup. See the [getting started guide](./getting-started) for Expo and Community CLI instructions. The following props now accept animated styles returned from `useAnimatedStyle`. They no longer accept `Animated.Value` or `Animated.AnimatedInterpolation` where these were previously supported: @@ -42,6 +42,25 @@ const MyComponent = () => { }; ``` +### `PaperProvider` is required + +In 5.x, most components rendered without a `PaperProvider` and used the default theme. In 6.x, most of them, such as `Text`, `Button`, `IconButton`, `TextInput`, `Icon` and `ProgressBar`, throw `useLocale must be used within a LocaleProvider` outside it. Wrap your app in `PaperProvider`, as [Usage](./getting-started#usage) in the getting started guide shows. + +`PaperProvider` also provides the safe area context, so outside it, `Appbar.Header` throws `No safe area value available` first, unless your app renders its own `SafeAreaProvider`. + +Tests that render a Paper component on its own fail the same way. Render it inside `PaperProvider`, for example with the `wrapper` option of React Native Testing Library: + +```tsx +import { render, screen } from '@testing-library/react-native'; +import { PaperProvider } from 'react-native-paper'; + +test('shows the greeting', async () => { + await render(, { wrapper: PaperProvider }); + + expect(screen.getByText('Hello')).toBeOnTheScreen(); +}); +``` + ### Reduce motion The `animation` theme key and its `scale` were removed. The new `reduceMotion` prop of `PaperProvider` turns Paper's animations on or off instead: @@ -139,6 +158,7 @@ Components built on `TouchableRipple` that extend its props still take `rippleCo Hardcoded default test IDs have been removed for the components listed below: +- `AnimatedFAB`: `animated-fab` - `Appbar.Content`: `appbar-content` - `Appbar.Header`: `appbar-header` - `BottomNavigation`: `bottom-navigation` @@ -147,9 +167,7 @@ Hardcoded default test IDs have been removed for the components listed below: - `Card`: `card` - `Chip`: `chip` - `Drawer.CollapsedItem`: `drawer-collapsed-item` -- `FAB`: `floating-action-button` -- `FAB.Extended`: `extended-floating-action-button` -- `FAB.Menu`: `floating-action-button-menu` +- `FAB`: `fab`, also on the buttons of `FAB.Group` - `IconButton`: `icon-button` - `Menu`: `menu` - `Menu.Item`: `menu-item` @@ -157,8 +175,9 @@ Hardcoded default test IDs have been removed for the components listed below: - `ProgressBar`: `progress-bar` - `Searchbar`: `search-bar` - `Surface`: `surface` +- `TextInput`: `text-input-flat` or `text-input-outlined`, by `mode` -You can specify a `testID` explicitly to restore each component's own test ID. +You can specify a `testID` explicitly to restore each component's own test ID. On `TextInput`, the `testID` goes to the input. You can also find a field by its label, for example with `getByLabelText`. These components used to also derive test IDs for internal, implementation-only elements by appending a suffix to the `testID` prop (e.g. `${testID}-container`, `${testID}-icon`, `${testID}-outline`). They have been removed entirely. @@ -217,6 +236,8 @@ In JavaScript, the old names import as `undefined` without an error: spreading ` Watch for `DarkTheme` in particular. 5.x had no `DarkTheme` export, so in a JavaScript app an import of it was `undefined` and Paper used its default theme instead. In 6.x the same import is the Material Design 3 dark theme, so the app can look different after the upgrade. React Navigation and `adaptNavigationTheme` also use the names `LightTheme`, `DarkTheme` and `DefaultTheme`, so when a file uses both, import Paper's themes under other names, e.g. `LightTheme as PaperLightTheme`. +If your app builds its Android dynamic colors from another library's scheme, such as `@pchmn/expo-material3-theme` (`{ ...LightTheme, colors: theme.light }`), it still type checks, but the scheme has none of the `*Fixed` roles that 6.x adds (see [Color roles](#color-roles)), so those roles keep Paper's default colors instead of the system's. Paper 6 has its own Android dynamic themes, `DynamicLightTheme` and `DynamicDarkTheme`, which map those roles to the system colors too. Use them instead, and `isDynamicColorSupported` to pick your own theme on devices without dynamic colors. See [Sync dynamic colors with system colors](./theming#sync-dynamic-colors-with-system-colors). + The theme types were renamed too: - **`MD3Theme`** → **`Theme`** @@ -253,6 +274,36 @@ If you import it from Paper's type declarations instead, the path changed: +const color = Palette.tertiary70; ``` +#### Color roles + +The 6.x color scheme has new roles, and three 5.x roles were removed: + +| Roles | In 6.x | +| ------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | +| `surfaceContainerLowest`, `surfaceContainerLow`, `surfaceContainer`, `surfaceContainerHigh`, `surfaceContainerHighest` | Added | +| `surfaceDim`, `surfaceBright` | Added | +| `primaryFixed`, `primaryFixedDim`, `onPrimaryFixed`, `onPrimaryFixedVariant`, and the same roles for `secondary` and `tertiary` | Added | +| `stateLayerPressed` | Added: the default ripple color | +| `surfaceDisabled`, `onSurfaceDisabled` | Removed: disabled states use `onSurface` at a lower opacity | +| `backdrop` | Removed: the `Modal` overlay uses `scrim` at 32% opacity | + +Components read the new roles: for example, a filled `TextInput` and the `Switch` track use `surfaceContainerHighest`, `Dialog` and `Searchbar` use `surfaceContainerHigh`, and the `BottomNavigation` bar uses `surfaceContainer`. `PaperProvider` fills a role your theme doesn't set from `LightTheme` or `DarkTheme`, without a warning, so in an app with its own 5.x color scheme these components take Paper's default colors. Regenerate your scheme with every role, for example with the [Material Theme Builder](https://material-foundation.github.io/material-theme-builder/), or add the new roles to it. + +#### Color types + +Theme colors are typed `ColorValue` instead of `string`, because the colors of `DynamicLightTheme` and `DynamicDarkTheme` are `PlatformColor` values. So passing one to a prop or a variable typed `string` fails with `TS2322`. Type your own props and variables as `ColorValue`: + +```diff ++import type { ColorValue } from 'react-native'; ++ + type StatusDotProps = { +- color: string; ++ color: ColorValue; + }; +``` + +If a library prop only takes a `string`, check which theme you pass. The colors of `LightTheme`, `DarkTheme` and themes built from them are strings at runtime, so you can cast them, e.g. `theme.colors.error as string`. The colors of `DynamicLightTheme` and `DynamicDarkTheme` are objects on Android 12 and later, so pass such a prop a color of your own instead. + #### `roundness` The `roundness` theme property was removed. Corner radii are now set in `theme.shapes.corner`, a scale of roles from `extraSmall` (4) to `extraExtraLarge` (48), and each component uses one fixed role. Paper ignores a `roundness` left in your theme. TypeScript reports it in a theme object written inline in the `theme` prop of `PaperProvider`, `ThemeProvider` or a component, or typed as `Theme`, but not in an untyped variable that also holds other theme keys. @@ -264,6 +315,7 @@ 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` | +| `large` | 16 | 4 | `FAB` and `FAB.Extended` at the default `size` | | `largeIncreased` | 20 | 5 | `SegmentedButtons` | | `extraLarge` | 28 | 7 | `Dialog`, `Drawer.Item`, `Searchbar` (`mode="bar"`) | @@ -546,6 +598,15 @@ e.g.: `Avatar.Text` uses the `titleMedium` type style: a font weight of 500 and a letter spacing of 0.15. Its font size and line height still scale with `size`. +### Badge + +`Badge` no longer takes `size`. Without children it is a 6dp dot, and with children a 16dp pill. In 5.x both were 20dp by default. TypeScript reports `size`, and in JavaScript it's ignored. There is no replacement for another size, so remove the prop: + +```diff +-3 ++3 +``` + ### Banner Showing and hiding use the `standardDecelerate` and `standardAccelerate` easing curves instead of ease-in-out. @@ -618,6 +679,8 @@ e.g.: ``` +TypeScript reports `backgroundColor` and the border radius keys only in a style object written inline in `style`. A style kept in a variable, such as one typed `ViewStyle` or a `StyleSheet.create` entry that also sets other keys, type checks, and `Surface` ignores its `backgroundColor` without a warning, and on Android and iOS its border radius too. The web still applies that border radius. Since TypeScript won't find these, search your app's `Surface` styles for them and move them to the props. + ### Modal `Modal` now uses a `Portal` internally and doesn't require an explicit `Portal` wrapper. So you need to remove any existing `Portal` wrappers around `Modal`: @@ -1212,6 +1275,20 @@ const theme = { ; ``` +#### Layout styles + +In 5.x, `TextInput` applied `height` and the text styles to the input, and the rest of `style`, such as `width`, `margin*`, `alignSelf` and `flex`, to the field around it. In 6.x the whole `style` goes to the input inside the field, and there is no prop for the field's style. So `width` sizes the input instead of the field, and `marginBottom` adds space inside the field instead of below it. `height` sets the height of the input: the field grows to fit a taller input, but it is never shorter than 56dp. Move the layout styles to a `View` around the `TextInput`: + +```tsx +// Before (v5) + + +// After (v6) + + + +``` + ### Button `Button` is modernized to Material Design 3 Expressive. The button modes were @@ -1259,6 +1336,19 @@ see the appearance changes below. The container now also morphs its corner while pressed. Pass `animateShape={false}` to opt out. +`style` goes to the container, so a `height` there doesn't resize the button. +With `style={{ height: 56 }}`, the container is 56dp tall, but the label, the +ripple and the pressable area keep the 40dp of the `small` size at its top, the +bottom of the container doesn't respond to presses, and the corner stays 20dp +instead of a full pill. Pick a `size` instead: `extra-small` is **32dp** tall, +`small` **40dp**, `medium` **56dp**, `large` **96dp** and `extra-large` +**136dp**. A larger size also has more padding and a larger label and icon. + +```diff +- ++ +``` + #### Toggle `selected` turns a button into a toggle. Leave it **undefined** to make it @@ -1310,7 +1400,9 @@ a plain button. It is ignored on `mode="text"`, which MD3 gives no toggle. - **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`. + override `theme.colors.outlineVariant`. Its width follows `size`: **1dp** + up to `medium`, **2dp** at `large` and **3dp** at `extra-large`. No prop or + theme value changes it, so drop `borderWidth`. - **Animated styles** must come from Reanimated. A React Native `Animated.Value` in `style` is not applied. @@ -1334,7 +1426,8 @@ a plain button. It is ignored on `mode="text"`, which MD3 gives no toggle. 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` +iOS, `padding`, `paddingHorizontal`, `paddingLeft` and `paddingRight` do not +override them, and on the web `paddingHorizontal` doesn't either. A `height` below `minHeight` has no effect. ```tsx diff --git a/docs/6.x/docs/guides/theming-with-react-navigation.md b/docs/6.x/docs/guides/theming-with-react-navigation.md index d16b4f8a62..fc8dcd730f 100644 --- a/docs/6.x/docs/guides/theming-with-react-navigation.md +++ b/docs/6.x/docs/guides/theming-with-react-navigation.md @@ -19,15 +19,16 @@ However, to simplify adapting React Navigation theme colors, to use the ones fro ```ts import { - DarkTheme as NavigationDarkTheme, - DefaultTheme as NavigationDefaultTheme, + DarkTheme as ReactNavigationDarkTheme, + DefaultTheme as ReactNavigationDefaultTheme, } from '@react-navigation/native'; import { adaptNavigationTheme } from 'react-native-paper'; -const { LightTheme, DarkTheme } = adaptNavigationTheme({ - reactNavigationLight: NavigationDefaultTheme, - reactNavigationDark: NavigationDarkTheme, -}); +const { LightTheme: NavigationLightTheme, DarkTheme: NavigationDarkTheme } = + adaptNavigationTheme({ + reactNavigationLight: ReactNavigationDefaultTheme, + reactNavigationDark: ReactNavigationDarkTheme, + }); ``` Library exports also Material Design 3 themes in both modes: @@ -104,8 +105,8 @@ We can merge them with object spread. React Navigation 7 themes also have `fonts ```js import { NavigationContainer, - DarkTheme as NavigationDarkTheme, - DefaultTheme as NavigationDefaultTheme, + DarkTheme as ReactNavigationDarkTheme, + DefaultTheme as ReactNavigationDefaultTheme, } from '@react-navigation/native'; import { DarkTheme as PaperDarkTheme, @@ -113,33 +114,34 @@ import { adaptNavigationTheme, } from 'react-native-paper'; -const { LightTheme, DarkTheme } = adaptNavigationTheme({ - reactNavigationLight: NavigationDefaultTheme, - reactNavigationDark: NavigationDarkTheme, -}); +const { LightTheme: NavigationLightTheme, DarkTheme: NavigationDarkTheme } = + adaptNavigationTheme({ + reactNavigationLight: ReactNavigationDefaultTheme, + reactNavigationDark: ReactNavigationDarkTheme, + }); const CombinedDefaultTheme = { ...PaperLightTheme, - ...LightTheme, + ...NavigationLightTheme, colors: { ...PaperLightTheme.colors, - ...LightTheme.colors, + ...NavigationLightTheme.colors, }, fonts: { ...PaperLightTheme.fonts, - ...LightTheme.fonts, + ...NavigationLightTheme.fonts, }, }; const CombinedDarkTheme = { ...PaperDarkTheme, - ...DarkTheme, + ...NavigationDarkTheme, colors: { ...PaperDarkTheme.colors, - ...DarkTheme.colors, + ...NavigationDarkTheme.colors, }, fonts: { ...PaperDarkTheme.fonts, - ...DarkTheme.fonts, + ...NavigationDarkTheme.fonts, }, }; ``` diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index 8f5c6b5f38..f12ef73f2b 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -345,14 +345,14 @@ import { const Stack = createNativeStackNavigator(); -const { LightTheme } = adaptNavigationTheme({ +const { LightTheme: NavigationLightTheme } = adaptNavigationTheme({ reactNavigationLight: DefaultTheme, }); export default function App() { return ( - + @@ -597,7 +597,25 @@ The dark theme adds a white overlay with opacity depending on elevation of surfa If you don't use a custom theme, Paper will automatically change between the default theme and the default dark theme, depending on device settings. -Otherwise, your custom theme will need to handle it manually, using React Native's [Appearance API](https://reactnative.dev/docs/appearance). +`PaperProvider` follows the device setting only when it gets no `theme`. With a `theme`, it uses `DarkTheme` only when that theme sets `dark: true`, so a theme that only changes colors, such as `theme={{ colors: { primary: '#8E24AA' } }}`, stays light in dark mode. A nested `PaperProvider` with such a theme stays light inside a dark app too, because it doesn't read the theme above it. To follow the device setting with a custom theme, pick `LightTheme` or `DarkTheme` with React Native's [`useColorScheme()`](https://reactnative.dev/docs/usecolorscheme) and spread your colors over it: + +```tsx +import * as React from 'react'; +import { useColorScheme } from 'react-native'; +import { DarkTheme, LightTheme, PaperProvider } from 'react-native-paper'; +import App from './src/App'; + +export default function Main() { + const base = useColorScheme() === 'dark' ? DarkTheme : LightTheme; + const theme = { ...base, colors: { ...base.colors, primary: '#8E24AA' } }; + + return ( + + + + ); +} +``` ## Gotchas diff --git a/example/src/Examples/ActivityIndicatorExample.tsx b/example/src/Examples/ActivityIndicatorExample.tsx index e5fc90f032..e8e5b3917e 100644 --- a/example/src/Examples/ActivityIndicatorExample.tsx +++ b/example/src/Examples/ActivityIndicatorExample.tsx @@ -18,7 +18,11 @@ const ActivityIndicatorExample = () => { - + @@ -26,6 +30,7 @@ const ActivityIndicatorExample = () => { animating={animating} size="large" hidesWhenStopped={false} + aria-label="Loading" /> @@ -34,6 +39,7 @@ const ActivityIndicatorExample = () => { animating={animating} size={100} hidesWhenStopped={false} + aria-label="Loading" /> @@ -42,6 +48,7 @@ const ActivityIndicatorExample = () => { animating={animating} color={Palette.error20} hidesWhenStopped={false} + aria-label="Loading" /> diff --git a/example/src/Examples/ProgressBarExample.tsx b/example/src/Examples/ProgressBarExample.tsx index e6ef656cb7..9c70477cbc 100644 --- a/example/src/Examples/ProgressBarExample.tsx +++ b/example/src/Examples/ProgressBarExample.tsx @@ -50,12 +50,16 @@ const ProgressBarExample = () => { Default ProgressBar - + Indeterminate ProgressBar - + @@ -64,6 +68,7 @@ const ProgressBarExample = () => { progress={progress} visible={visible} color={Palette.error50} + aria-label="Progress" /> @@ -78,6 +83,7 @@ const ProgressBarExample = () => { style={{ backgroundColor: Palette.secondary50, }} + aria-label="Progress" /> @@ -87,6 +93,7 @@ const ProgressBarExample = () => { progress={progress} visible={visible} style={styles.customHeight} + aria-label="Progress" /> @@ -97,6 +104,7 @@ const ProgressBarExample = () => { style={styles.progressBar} animatedValue={progressBarValue} theme={theme} + aria-label="Progress" /> @@ -108,6 +116,7 @@ const ProgressBarExample = () => { style={styles.customPercentageHeight} indeterminate visible={visible} + aria-label="Loading" /> diff --git a/src/components/ActivityIndicator.tsx b/src/components/ActivityIndicator.tsx index dd794ff2e2..d75f01adda 100644 --- a/src/components/ActivityIndicator.tsx +++ b/src/components/ActivityIndicator.tsx @@ -37,6 +37,8 @@ const DURATION = 2400; * Activity indicator is used to present progress of some activity in the app. * It can be used as a drop-in replacement for the ActivityIndicator shipped with React Native. * + * Pass `aria-label` to name the indicator for assistive technology, e.g. `aria-label="Loading"`. + * * ## Usage * ```js * import * as React from 'react'; diff --git a/src/components/ProgressBar.tsx b/src/components/ProgressBar.tsx index b34d3144f2..29dbf859cd 100644 --- a/src/components/ProgressBar.tsx +++ b/src/components/ProgressBar.tsx @@ -57,6 +57,8 @@ const INDETERMINATE_MAX_WIDTH = 0.6; /** * Progress bar is an indicator used to present progress of some activity in the app. * + * Pass `aria-label` to name the indicator for assistive technology, e.g. `aria-label="Upload progress"`. + * * ## Usage * ```js * import * as React from 'react';