Skip to content

TabBar - Better accessibility roles and instructions #3501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 24, 2025
Merged
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 src/components/tabController/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const TabBar = (props: Props) => {
onContentSizeChange={onContentSizeChange}
onLayout={onLayout}
>
<View style={tabBarContainerStyle}>{tabBarItems}</View>
<View style={tabBarContainerStyle} accessibilityRole={Constants.isIOS ? 'tabbar' : 'tablist'} >{tabBarItems}</View>
{itemsCount > 1 && (
<Reanimated.View style={[styles.selectedIndicator, indicatorStyle, _indicatorTransitionStyle]}/>
)}
Expand Down
16 changes: 14 additions & 2 deletions src/components/tabController/TabBarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// TODO: support commented props
import React, {useCallback, useContext, useEffect, useRef, useMemo, ReactElement} from 'react';
import React, {useCallback, useContext, useEffect, useRef, useMemo, ReactElement, useState} from 'react';
import {StyleSheet, TextStyle, LayoutChangeEvent, StyleProp, ViewStyle, TextProps} from 'react-native';
import _ from 'lodash';
import Reanimated, {runOnJS, useAnimatedStyle, useSharedValue} from 'react-native-reanimated';
import Reanimated, {runOnJS, useAnimatedReaction, useAnimatedStyle, useSharedValue} from 'react-native-reanimated';
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
import {Colors, Typography, Spacings} from '../../style';
import Badge, {BadgeProps} from '../badge';
Expand Down Expand Up @@ -144,6 +144,7 @@ export default function TabBarItem({
// JSON.parse(JSON.stringify is due to an issue with reanimated
const sharedLabelStyle = useSharedValue(JSON.parse(JSON.stringify(StyleSheet.flatten(labelStyle))));
const sharedSelectedLabelStyle = useSharedValue(JSON.parse(JSON.stringify(StyleSheet.flatten(selectedLabelStyle))));
const [isSelected, setIsSelected] = useState(currentPage.value === index);

// NOTE: We clone these color values in refs because they might contain a PlatformColor value
// which throws an error (see https://github.com/software-mansion/react-native-reanimated/issues/3164)
Expand All @@ -157,6 +158,12 @@ export default function TabBarItem({
}
}, []);

useAnimatedReaction(() => currentPage.value === index, (isSelected, prevIsSelected) => {
if (isSelected !== prevIsSelected) {
runOnJS(setIsSelected)(isSelected);
}
});

const onLayout = useCallback((event: LayoutChangeEvent) => {
const {width} = event.nativeEvent.layout;

Expand Down Expand Up @@ -201,6 +208,8 @@ export default function TabBarItem({
return [styles.tabItem, {flex}, style, constantWidthStyle, pressStyle];
}, [style, spreadItems]);

const accessibilityState = useMemo(() => ({selected: isSelected}), [isSelected]);

const gesture = Gesture.Tap()
.maxDuration(60000)
.onEnd(() => {
Expand All @@ -226,6 +235,9 @@ export default function TabBarItem({
style={_style}
onLayout={onLayout}
testID={testID}
accessible
accessibilityRole="tab"
accessibilityState={accessibilityState}
>
{leadingAccessory}
{icon && (
Expand Down