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/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ As with `roundness`, Paper ignores these keys, and TypeScript only reports them

Components now use React Native's `aria-*` props and `role` instead of the `accessibility*` props. `accessibilityLabel` is now `aria-label`, the `accessibilityRole` of `Button` is now `role`, and the `accessibilityState` of `FAB` and `Menu.Item` is now `aria-checked` and `aria-selected`.

`Checkbox.Item`, `RadioButton.Item`, `FAB`, `Menu.Item`, `List.Accordion` and `Switch` no longer accept `accessibilityLabel`, so TypeScript reports an error. Components whose props extend React Native's view props, such as `Button`, `Chip` and `DataTable.Pagination`, still compile with the old props, but pass them to their outer container instead of the element they used to label.
`Checkbox.Item`, `RadioButton.Item`, `FAB`, `Menu.Item`, `List.Accordion` and `Switch` no longer accept `accessibilityLabel`, so TypeScript reports an error. Components whose props extend React Native's view props, such as `Chip` and `DataTable.Pagination`, still compile with the old props, but pass them to their outer container instead of the element they used to label.

Props that label a part of a component keep their names, e.g. `closeIconAccessibilityLabel` on `Chip` and `dismissAccessibilityLabel` on `Modal`.

Expand Down
69 changes: 61 additions & 8 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,14 @@ const Button = ({
children,
'aria-label': ariaLabel,
'aria-expanded': ariaExpanded,
'aria-busy': ariaBusy,
'aria-checked': ariaChecked,
'aria-selected': ariaSelected,
accessibilityLabel,
accessibilityState,
accessibilityRole,
accessibilityHint,
role = 'button',
role,
hitSlop,
onPress,
onPressIn,
Expand Down Expand Up @@ -316,11 +322,43 @@ const Button = ({
// toggle colors, no shape flip, and no toggle state. Every other use of
// the toggle state goes through this value.
const toggleSelected = isMode('text') ? undefined : selected;
// React Native's types have no `aria-pressed`.

// As on React Native itself, an `aria-*` prop wins over the same
// `accessibilityState` field. Both are passed on as `aria-*` props, since
// react-native-web does not support `accessibilityState`.
const busy = loading || (ariaBusy ?? accessibilityState?.busy);
const checked = ariaChecked ?? accessibilityState?.checked;

// Android only: React Native puts a busy view's state into its content
// description ("Save draft, busy"), and TalkBack reads that instead of the
// text inside the button. Without a label, the description would be just
// "busy", so the button's text becomes its label. When the content is not
// only text (strings and numbers, e.g. `Uploading {count} files`), it
// cannot be read here: the busy state is left out instead, so TalkBack
// still reads the content.
const needsAndroidBusyLabel =
Platform.OS === 'android' &&
busy === true &&
ariaLabel === undefined &&
accessibilityLabel === undefined;
const childrenParts = React.Children.toArray(children);
const childrenText =
childrenParts.length > 0 &&
childrenParts.every(
(part) => typeof part === 'string' || typeof part === 'number'
)
? childrenParts.join('')
: undefined;
const pressableLabel = needsAndroidBusyLabel ? childrenText : ariaLabel;
const pressableBusy =
needsAndroidBusyLabel && childrenText === undefined ? undefined : busy;

// React Native's types have no `aria-pressed`. A given `checked` state
// still applies where the toggle state is not exposed as checked.
const toggleStateProps =
Platform.OS === 'web'
? { 'aria-pressed': toggleSelected }
: { 'aria-checked': toggleSelected };
? { 'aria-pressed': toggleSelected, 'aria-checked': checked }
: { 'aria-checked': toggleSelected ?? checked };

// When the button is `selected`, flip the requested shape so the
// unselected/selected pair contrasts visually (round ↔ square).
Expand Down Expand Up @@ -528,10 +566,19 @@ const Button = ({
onPressIn={hasPassedTouchHandler ? handlePressIn : undefined}
onPressOut={hasPassedTouchHandler ? handlePressOut : undefined}
delayLongPress={delayLongPress}
aria-label={ariaLabel}
aria-expanded={ariaExpanded}
accessibilityLabel={accessibilityLabel}
accessibilityRole={accessibilityRole}
aria-label={pressableLabel}
aria-expanded={ariaExpanded ?? accessibilityState?.expanded}
aria-busy={pressableBusy}
aria-selected={ariaSelected ?? accessibilityState?.selected}
accessibilityHint={accessibilityHint}
role={role}
// `role` wins over `accessibilityRole`, so the default role steps
// aside for a given `accessibilityRole`. The two use different
// names (`header` / `heading`), so one is never passed as the other.
role={
role ?? (accessibilityRole === undefined ? 'button' : undefined)
}
aria-disabled={disabled}
{...toggleStateProps}
accessible={accessible}
Expand All @@ -556,7 +603,13 @@ const Button = ({
</View>
) : null}
{loading ? (
<ActivityIndicator size={iconSize} color={labelColor} />
// The button itself is busy (`aria-busy`), so the indicator is
// not a separate progress element for assistive technology.
<ActivityIndicator
size={iconSize}
color={labelColor}
aria-hidden
/>
) : null}
<Text
variant={sizeStyle.labelVariant}
Expand Down
102 changes: 102 additions & 0 deletions src/components/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,108 @@ it('exposes the accessibility hint', async () => {
expect(screen.getByRole('button')).toHaveProp('accessibilityHint', 'hint');
});

describe('accessibility props', () => {
it('exposes accessibilityLabel, accessibilityState and loading on the button only', async () => {
await render(
<Button
accessibilityLabel="Save draft"
accessibilityState={{ expanded: false }}
loading
onPress={() => {}}
>
Save
</Button>
);

expect(
screen.getByRole('button', {
name: 'Save draft',
expanded: false,
busy: true,
})
).toBeOnTheScreen();
expect(screen.root).not.toHaveProp('accessibilityLabel');
expect(screen.root).not.toHaveProp('accessibilityState');
// The button itself is busy, so the indicator is hidden.
expect(screen.queryByRole('progressbar')).not.toBeOnTheScreen();
expect(
screen.getByRole('progressbar', { includeHiddenElements: true })
).toBeOnTheScreen();
});

it('exposes aria-* states on the button, ahead of accessibilityState', async () => {
await render(
<Button
aria-expanded
aria-busy={false}
aria-selected
aria-checked="mixed"
accessibilityState={{ expanded: false, busy: true }}
onPress={() => {}}
>
Save
</Button>
);

const button = screen.getByRole('button', {
name: 'Save',
expanded: true,
busy: false,
selected: true,
});
// `getByRole` filters `checked` for checkable roles only.
expect(button).toHaveProp(
'accessibilityState',
expect.objectContaining({ checked: 'mixed' })
);
expect(screen.root).not.toHaveProp('accessibilityState');
});

// On Android, React Native puts "busy" into the content description, which
// TalkBack reads instead of the button's text, so a loading button without
// a label is labelled by its text there.
it('labels a loading button by its text on Android', async () => {
jest.replaceProperty(Platform, 'OS', 'android');

await render(
<Button loading onPress={() => {}}>
Uploading {3} files
</Button>
);

expect(screen.getByRole('button', { busy: true })).toHaveProp(
'accessibilityLabel',
'Uploading 3 files'
);
});

it('leaves the name of a loading button to content that is not only text on Android', async () => {
jest.replaceProperty(Platform, 'OS', 'android');

await render(
<Button loading onPress={() => {}}>
<>Uploading</>
</Button>
);

// The busy state is left out, so TalkBack reads the content.
expect(screen.getByRole('button', { busy: false })).not.toHaveProp(
'accessibilityLabel'
);
});

it('uses a given accessibilityRole instead of the default button role', async () => {
await render(
<Button accessibilityRole="link" onPress={() => {}}>
Docs
</Button>
);

expect(screen.getByRole('link', { name: 'Docs' })).toBeOnTheScreen();
expect(screen.queryByRole('button')).not.toBeOnTheScreen();
});
});

it('should execute onPressIn', async () => {
const onPressInMock = jest.fn();
const onPress = jest.fn();
Expand Down
4 changes: 4 additions & 0 deletions src/components/__tests__/__snapshots__/Button.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ exports[`renders loading button 1`] = `
>
<View
accessible={true}
aria-busy={true}
collapsable={false}
role="none"
style={
Expand Down Expand Up @@ -825,6 +826,7 @@ exports[`renders loading button 1`] = `
<View
accessible={true}
aria-busy={true}
aria-hidden={true}
role="progressbar"
style={
[
Expand Down Expand Up @@ -1567,6 +1569,7 @@ exports[`renders the loading indicator at the size set by iconSize 1`] = `
>
<View
accessible={true}
aria-busy={true}
collapsable={false}
role="none"
style={
Expand Down Expand Up @@ -1607,6 +1610,7 @@ exports[`renders the loading indicator at the size set by iconSize 1`] = `
<View
accessible={true}
aria-busy={true}
aria-hidden={true}
role="progressbar"
style={
[
Expand Down
Loading