Skip to content
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

feat: Segment accessibility #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 35 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React, { useEffect } from 'react';
import {
AccessibilityRole,
AccessibilityState,
AccessibilityValue,
Pressable,
StyleSheet,
Text,
Expand All @@ -14,11 +17,20 @@ import Animated, {
} from 'react-native-reanimated';
import { widthPercentageToDP } from 'react-native-responsive-screen';

interface SegmentedControlProps {
export interface Segment {
label: string;
accessibilityLabel?: string;
accessibilityHint?: string;
accessibilityRole?: AccessibilityRole;
accessibilityState?: AccessibilityState;
accessibilityValue?: AccessibilityValue;
}

export interface SegmentedControlProps {
/**
* The Segments Text Array
* An array of Segments. Can be a mix of strings for the Segment labels, or an object with a `label` and accessibility props.
*/
segments: Array<string>;
segments: Array<string | Segment>;
/**
* The Current Active Segment Index
*/
Expand Down Expand Up @@ -115,6 +127,14 @@ const SegmentedControl: React.FC<SegmentedControlProps> = ({
const translateValue = width / segments.length;
const tabTranslateValue = useSharedValue(0);

// Transform and memoize all segments into a `Segment` array.
// This allows for the segments to be transformed only when they change, and to be treated as `Segment` on render.
const memoizedSegments = React.useMemo<Segment[]>(() => {
return segments.map((segment) =>
typeof segment === 'string' ? { label: segment } : segment
);
}, [segments]);

// useCallBack with an empty array as input, which will call inner lambda only once and memoize the reference for future calls
const memoizedTabPressCallback = React.useCallback(
(index) => {
Expand Down Expand Up @@ -193,12 +213,22 @@ const SegmentedControl: React.FC<SegmentedControlProps> = ({
tabTranslateAnimatedStyles,
]}
/>
{segments.map((segment, index) => {
{memoizedSegments.map((segment, index) => {
const isSelected = currentIndex === index;
const { label, ...accessibilityProps } = segment;

return (
<Pressable
onPress={() => memoizedTabPressCallback(index)}
key={index}
style={[styles.touchableContainer, pressableWrapper]}
accessibilityState={{ selected: isSelected }}
accessibilityHint={!isSelected ? `Selects ${label} option` : ''}
accessibilityLabel={`${label}, option, ${index + 1} of ${
segments.length
}`}
accessibilityRole="button"
{...accessibilityProps}
>
<View style={styles.textWrapper}>
<Text
Expand All @@ -208,7 +238,7 @@ const SegmentedControl: React.FC<SegmentedControlProps> = ({
: finalisedInActiveTextStyle,
]}
>
{segment}
{label}
</Text>
{badgeValues[index] && (
<View
Expand Down