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
14 changes: 13 additions & 1 deletion src/components/NavigationBar/NavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
NativeSyntheticEvent,
StyleProp,
TextStyle,
ViewProps,
ViewStyle,
} from 'react-native';

Expand Down Expand Up @@ -55,6 +56,17 @@ import TouchableRipple from '../TouchableRipple/TouchableRipple';
import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';

// React Native gives iOS no trait for `role="tab"` or `role="tablist"`, so the
// tabs stay buttons there. VoiceOver reads the buttons in a container with the
// tab bar trait as tabs, with their position ("Tab, 1 of 4"), as it reads a
// native tab bar. Only `accessibilityRole="tabbar"` sets that trait, and `role`
// would win over it. A View with only a role is flattened away, so
// `collapsable={false}` keeps the container.
const getTablistProps = (): ViewProps =>
Platform.OS === 'ios'
? { accessibilityRole: 'tabbar', collapsable: false }
: { role: 'tablist' };

export type BaseRoute = {
key: string;
title?: string;
Expand Down Expand Up @@ -818,7 +830,7 @@ const NavigationBar = <Route extends BaseRoute>({
maxWidth: maxTabBarWidth,
},
]}
role={'tablist'}
{...getTablistProps()}
>
{routes.map((route, index) => {
const focused = navigationState.index === index;
Expand Down
49 changes: 47 additions & 2 deletions src/components/__tests__/NavigationBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,60 @@ it('exposes the tabs pattern through accessibility roles', async () => {
);

// The `tablist` container is a group, not an accessibility element (making it
// one would swallow the tabs on iOS), so it is not queryable. Its role is
// covered by the structural snapshots.
// one would swallow the tabs on iOS), so it is not queryable. The
// `tablist container` tests cover its role.
expect(screen.getAllByRole(tabRole)).toHaveLength(2);
expect(
screen.getByRole(tabRole, { name: 'Alpha destination' })
).toBeSelected();
expect(screen.getByRole(tabRole, { name: 'Beta' })).not.toBeSelected();
});

describe('tablist container', () => {
const renderTabs = () =>
render(
<NavigationBar
navigationState={{
index: 0,
routes: [
{ key: 'a', title: 'Alpha', focusedIcon: 'magnify', testID: 'tab' },
{ key: 'b', title: 'Beta', focusedIcon: 'camera' },
],
}}
onTabPress={jest.fn()}
/>
);

afterEach(() => {
jest.restoreAllMocks();
});

// VoiceOver reads the buttons in a tab bar as tabs, with their position.
it('groups the tabs in a tab bar on iOS', async () => {
jest.replaceProperty(Platform, 'OS', 'ios');
await renderTabs();

const container = screen.getByTestId('tab').parent;

expect(container).toHaveProp('accessibilityRole', 'tabbar');
expect(container).toHaveProp('collapsable', false);
expect(container).not.toHaveProp('role');
});

it.each(['android', 'web'] as const)(
'groups the tabs in a tablist on %s',
async (os) => {
jest.replaceProperty(Platform, 'OS', os);
await renderTabs();

const container = screen.getByTestId('tab').parent;

expect(container).toHaveProp('role', 'tablist');
expect(container).not.toHaveProp('accessibilityRole');
}
);
});

it('names icon-only tabs with the label text', async () => {
await render(
<NavigationBar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,8 @@ exports[`renders the horizontal (flexible) variant 1`] = `
}
>
<View
role="tablist"
accessibilityRole="tabbar"
collapsable={false}
style={
[
{
Expand Down
Loading