diff --git a/docs/6.x/docs/guides/getting-started.mdx b/docs/6.x/docs/guides/getting-started.mdx index 8777daff41..304093d3ad 100644 --- a/docs/6.x/docs/guides/getting-started.mdx +++ b/docs/6.x/docs/guides/getting-started.mdx @@ -78,7 +78,7 @@ Follow the [React Native Reanimated installation guide](https://docs.swmansion.c If you're on a vanilla React Native project, 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). -Specifically, the `MaterialDesignIcons` icon pack needs to be included in the project because some components use those icons internally, such as `AppBar.BackAction` on Android. +Specifically, the `MaterialDesignIcons` icon pack needs to be included in the project when you use the default icon renderer, because some components use those icons internally, such as `Appbar.BackAction`. ```bash npm2yarn npm install @react-native-vector-icons/material-design-icons diff --git a/docs/6.x/docs/guides/icons.mdx b/docs/6.x/docs/guides/icons.mdx index e871db1407..fae73a371a 100644 --- a/docs/6.x/docs/guides/icons.mdx +++ b/docs/6.x/docs/guides/icons.mdx @@ -97,6 +97,8 @@ Example: If you want to use icons other than `MaterialDesignIcons` you need to import the icons and pass it to the `settings` prop within `PaperProvider`. +Paper's own components, such as `Appbar.BackAction`, pass Material Design icon names like `arrow-left` to this renderer, so use an icon set that has them or map those names yourself. + Example: ```js diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 155a18f11d..40171b7fba 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -575,6 +575,10 @@ A `theme` set on a part is merged over the `theme` of the `Appbar`, so it takes ``` +#### Back icon + +`Appbar.BackAction` now renders its icon through the `icon` in `PaperProvider`'s `settings`. On Android and web it always does, and on iOS only when you set a custom `icon`, otherwise the bundled chevron is kept. A custom renderer now receives `name: 'arrow-left'` for it, so make sure your icon set handles that name. + #### Bottom bar Using `Appbar` as a bottom bar is deprecated. Material Design 3 moves the bottom bar to toolbars. It continues to work in 6.x, and will be replaced by the `Toolbar` component once it is available. diff --git a/src/components/Appbar/AppbarBackIcon.tsx b/src/components/Appbar/AppbarBackIcon.tsx index ff827b3f5f..0df967b567 100644 --- a/src/components/Appbar/AppbarBackIcon.tsx +++ b/src/components/Appbar/AppbarBackIcon.tsx @@ -1,7 +1,11 @@ +import * as React from 'react'; import { Image, Platform, StyleSheet, View } from 'react-native'; import type { ColorValue } from 'react-native'; import { useLocale } from '../../core/locale'; +import { SettingsContext } from '../../core/settings'; +import type { Settings } from '../../core/settings'; +import Icon from '../Icon'; import MaterialCommunityIcon from '../MaterialCommunityIcon'; const AppbarBackIcon = ({ @@ -12,10 +16,18 @@ const AppbarBackIcon = ({ color: ColorValue; }) => { const { direction } = useLocale(); + const { icon } = React.useContext(SettingsContext); const isRTL = direction === 'rtl'; const iosIconSize = size - 3; - return Platform.OS === 'ios' ? ( + // The bundled chevron is only kept while the default icon renderer is in + // place, so a renderer configured through `PaperProvider` wins on iOS too. + // This is a reference check: any wrapper, even around the default renderer + // (e.g. `React.memo(MaterialCommunityIcon)`), counts as a custom renderer. + const shouldUseIOSAsset = + Platform.OS === 'ios' && icon === MaterialCommunityIcon; + + return shouldUseIOSAsset ? ( ) : ( - ); }; diff --git a/src/components/__tests__/Appbar/Appbar.test.tsx b/src/components/__tests__/Appbar/Appbar.test.tsx index 28c927d95a..a128fa6d71 100644 --- a/src/components/__tests__/Appbar/Appbar.test.tsx +++ b/src/components/__tests__/Appbar/Appbar.test.tsx @@ -1,4 +1,4 @@ -import { I18nManager, Text as NativeText, View } from 'react-native'; +import { I18nManager, Platform, Text as NativeText, View } from 'react-native'; import type { ColorValue } from 'react-native'; import { @@ -16,6 +16,8 @@ import { } from 'react-native-reanimated'; import { SafeAreaProvider } from 'react-native-safe-area-context'; +import PaperProvider from '../../../core/PaperProvider'; +import type { Settings } from '../../../core/settings'; import { fireEvent, render, screen, userEvent } from '../../../test-utils'; import { DarkTheme, LightTheme } from '../../../theme/schemes'; import { tokens } from '../../../theme/tokens'; @@ -1055,3 +1057,31 @@ describe('getAppbarColors', () => { ); }); }); + +describe('Appbar.BackAction icon', () => { + describe.each(['ios', 'android', 'web'] as const)('on %s', (os) => { + beforeEach(() => { + jest.replaceProperty(Platform, 'OS', os); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it.each(['ltr', 'rtl'] as const)( + 'draws the icon from the settings in a %s layout', + async (direction) => { + const icon = jest.fn>(() => null); + await render( + + {}} /> + + ); + + expect(icon).toHaveBeenCalledWith( + expect.objectContaining({ name: 'arrow-left', direction }) + ); + } + ); + }); +});