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.
Comment thread
Doberjohn marked this conversation as resolved.
Resolved

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

Expand Down
63 changes: 57 additions & 6 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ const Button = ({
children,
'aria-label': ariaLabel,
'aria-expanded': ariaExpanded,
'aria-busy': ariaBusy,
'aria-checked': ariaChecked,
'aria-selected': ariaSelected,
accessibilityLabel,
accessibilityState,
accessibilityHint,
role = 'button',
hitSlop,
Expand Down Expand Up @@ -316,11 +321,48 @@ 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: TalkBack reads the content description React Native builds
// from the label and the busy state ("Save draft, busy"), instead of the
// text inside the button. React Native rewrites that description only when
// it has text to write, so without a label a busy button would read "busy"
// and keep reading it once it is no longer busy. The button's own text is
// therefore always its label there, and `busy` always a boolean. When the
// content is not only text (strings and numbers, e.g.
// `Uploading {count} files`), it cannot be read here: the button is not
// marked busy, so TalkBack still reads the content.
const needsAndroidBusyLabel =
Platform.OS === 'android' &&
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 =
Platform.OS === 'android'
? needsAndroidBusyLabel && childrenText === undefined
? false
: busy === true
: 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,8 +570,11 @@ const Button = ({
onPressIn={hasPassedTouchHandler ? handlePressIn : undefined}
onPressOut={hasPassedTouchHandler ? handlePressOut : undefined}
delayLongPress={delayLongPress}
aria-label={ariaLabel}
aria-expanded={ariaExpanded}
accessibilityLabel={accessibilityLabel}
aria-label={pressableLabel}
aria-expanded={ariaExpanded ?? accessibilityState?.expanded}
aria-busy={pressableBusy}
aria-selected={ariaSelected ?? accessibilityState?.selected}
accessibilityHint={accessibilityHint}
role={role}
aria-disabled={disabled}
Expand All @@ -556,7 +601,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
121 changes: 121 additions & 0 deletions src/components/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,127 @@ 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('stops marking a button busy on Android once it stops loading', async () => {
jest.replaceProperty(Platform, 'OS', 'android');

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

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

await rerender(
<Button loading={false} onPress={() => {}}>
Upload
</Button>
);

// `false`, not unset: React Native rewrites the content description
// TalkBack reads only for a value, so "Uploading, busy" would stay.
const button = screen.getByRole('button');
expect(button).toHaveProp('accessibilityLabel', 'Upload');
expect(button).toHaveProp(
'accessibilityState',
expect.objectContaining({ busy: false })
);
});
});

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