Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/6.x/docs/guides/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/6.x/docs/guides/icons.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ A `theme` set on a part is merged over the `theme` of the `Appbar`, so it takes
</Appbar.Header>
```

#### 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.
Expand Down
19 changes: 15 additions & 4 deletions src/components/Appbar/AppbarBackIcon.tsx
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand All @@ -12,10 +16,18 @@
color: ColorValue;
}) => {
const { direction } = useLocale();
const { icon } = React.useContext<Settings>(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;

Check warning on line 28 in src/components/Appbar/AppbarBackIcon.tsx

View check run for this annotation

apex-sentinel-staging / Apex Sentinel

minor: iOS back icon renders blank when settings.icon is explicitly undefined

`settings={{icon: undefined}}` leaves `Appbar.BackAction` blank on iOS. The new reference check fails for `undefined`, so iOS takes the `Icon` branch whose string path calls the renderer optionally and yields nothing, where the pre-change code drew the bundled chevron. Keep the chevron when `icon` is falsy as well as when it is the default.

return shouldUseIOSAsset ? (
Comment thread
alpharius-ck marked this conversation as resolved.
Resolved
<View
style={[
styles.wrapper,
Comment thread
alpharius-ck marked this conversation as resolved.
Resolved
Expand All @@ -34,11 +46,10 @@
/>
</View>
) : (
<MaterialCommunityIcon
name="arrow-left"
<Icon
source={{ source: 'arrow-left', direction }}
color={color}
size={size}
direction={direction}
/>
);
};
Expand Down
32 changes: 31 additions & 1 deletion src/components/__tests__/Appbar/Appbar.test.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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';
Expand Down Expand Up @@ -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<NonNullable<Settings['icon']>>(() => null);
await render(
<PaperProvider direction={direction} settings={{ icon }}>
<Appbar.BackAction onPress={() => {}} />
</PaperProvider>
);

expect(icon).toHaveBeenCalledWith(
expect.objectContaining({ name: 'arrow-left', direction })
);
}
);
});
});
Loading