Skip to content

Commit 73e9b4c

Browse files
fawcilizesatya164
authored andcommitted
feat: add accessibility role and state to bottom bar (facebook#90)
### Motivation Exposing `accessibilityRole` and `accessibilityStates` through props in the same way as `accessibilityLabel`. This allows screen readers to announce things like: `Selected. Banana. Button.` Reasonable defaults were provided, however, consumers can provide custom overrides in the cases where the role might be `imagebutton` or the state might be `disabled`. ### Test plan Select a button with VoiceOver (iOS) or Talkback (Android) enabled. The screen reader should read something like `Selected. <AccessibilityLabel>. Button.` or something slightly different depending on which platform you are on. Buttons that are not focused should not have `Selected` announced.
1 parent a9c97c8 commit 73e9b4c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/bottom-tabs/src/navigators/createBottomTabNavigator.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import BottomTabBar, { type TabBarOptions } from '../views/BottomTabBar';
1313
import ResourceSavingScene from '../views/ResourceSavingScene';
1414

1515
type Props = InjectedProps & {
16+
getAccessibilityRole: (props: { route: any }) => string,
17+
getAccessibilityStates: (props: { route: any }) => Array<string>,
1618
lazy?: boolean,
1719
tabBarComponent?: React.ComponentType<*>,
1820
tabBarOptions?: TabBarOptions,
@@ -62,6 +64,8 @@ class TabNavigationView extends React.PureComponent<Props, State> {
6264
screenProps,
6365
getLabelText,
6466
getAccessibilityLabel,
67+
getAccessibilityRole,
68+
getAccessibilityStates,
6569
getTestID,
6670
renderIcon,
6771
onTabPress,
@@ -89,6 +93,8 @@ class TabNavigationView extends React.PureComponent<Props, State> {
8993
getLabelText={getLabelText}
9094
getButtonComponent={this._getButtonComponent}
9195
getAccessibilityLabel={getAccessibilityLabel}
96+
getAccessibilityRole={getAccessibilityRole}
97+
getAccessibilityStates={getAccessibilityStates}
9298
getTestID={getTestID}
9399
renderIcon={renderIcon}
94100
/>

packages/bottom-tabs/src/views/BottomTabBar.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Props = TabBarOptions & {
3434
onTabPress: any,
3535
onTabLongPress: any,
3636
getAccessibilityLabel: (props: { route: any }) => string,
37+
getAccessibilityRole: (props: { route: any }) => string,
38+
getAccessibilityStates: (props: { route: any }) => Array<string>,
3739
getButtonComponent: ({ route: any }) => any,
3840
getLabelText: ({ route: any }) => any,
3941
getTestID: (props: { route: any }) => string,
@@ -56,6 +58,8 @@ class TouchableWithoutFeedbackWrapper extends React.Component<*> {
5658
onLongPress,
5759
testID,
5860
accessibilityLabel,
61+
accessibilityRole,
62+
accessibilityStates,
5963
...props
6064
} = this.props;
6165

@@ -66,6 +70,8 @@ class TouchableWithoutFeedbackWrapper extends React.Component<*> {
6670
testID={testID}
6771
hitSlop={{ left: 15, right: 15, top: 0, bottom: 5 }}
6872
accessibilityLabel={accessibilityLabel}
73+
accessibilityRole={accessibilityRole}
74+
accessibilityStates={accessibilityStates}
6975
>
7076
<View {...props} />
7177
</TouchableWithoutFeedback>
@@ -220,9 +226,24 @@ class TabBarBottom extends React.Component<Props> {
220226
{routes.map((route, index) => {
221227
const focused = index === navigation.state.index;
222228
const scene = { route, focused };
229+
223230
const accessibilityLabel = this.props.getAccessibilityLabel({
224231
route,
225232
});
233+
234+
const accessibilityRole =
235+
this.props.getAccessibilityRole({
236+
route,
237+
}) || 'button';
238+
239+
let accessibilityStates = this.props.getAccessibilityStates({
240+
route,
241+
});
242+
243+
if (!accessibilityStates) {
244+
accessibilityStates = focused ? ['selected'] : [];
245+
}
246+
226247
const testID = this.props.getTestID({ route });
227248

228249
const backgroundColor = focused
@@ -240,6 +261,8 @@ class TabBarBottom extends React.Component<Props> {
240261
onLongPress={() => onTabLongPress({ route })}
241262
testID={testID}
242263
accessibilityLabel={accessibilityLabel}
264+
accessibilityRole={accessibilityRole}
265+
accessibilityStates={accessibilityStates}
243266
style={[
244267
styles.tab,
245268
{ backgroundColor },

0 commit comments

Comments
 (0)