Skip to content

Commit

Permalink
feat: add accessibility support (#117)
Browse files Browse the repository at this point in the history
* chore: added accessibility base support

* chore: updated backdrop to avoid covering whole screen when its invisible

* chore: remove unused comments
  • Loading branch information
gorhom authored Dec 16, 2020
1 parent d78f220 commit bfb7a8e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
31 changes: 29 additions & 2 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
useLayoutEffect,
useEffect,
} from 'react';
import { ViewStyle } from 'react-native';
import { ViewStyle, AccessibilityInfo } from 'react-native';
import isEqual from 'lodash.isequal';
import invariant from 'invariant';
import Animated, {
Expand Down Expand Up @@ -486,6 +486,28 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
) {
return;
}

/**
* Here we announce the bottom sheet position
* for accessibility service.
*/
AccessibilityInfo.isScreenReaderEnabled().then(isEnabled => {
if (!isEnabled) {
return;
}
const positionInScreen = Math.max(
Math.floor(
((WINDOW_HEIGHT - snapPoints[currentPositionIndex] || 1) /
WINDOW_HEIGHT) *
100
),
0
).toFixed(0);
AccessibilityInfo.announceForAccessibility(
`Bottom sheet snapped to ${positionInScreen}% of the screen`
);
});

currentIndexRef.current = currentPositionIndex;
refreshUIElements();
handleOnChange(currentPositionIndex);
Expand Down Expand Up @@ -536,7 +558,12 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
ref={containerTapGestureRef}
{...containerTapGestureHandler}
>
<Animated.View style={containerStyle}>
<Animated.View
accessible={true}
accessibilityRole="adjustable"
accessibilityLabel="Bottom Sheet"
style={containerStyle}
>
<BottomSheetInternalProvider value={internalContextVariables}>
<BottomSheetBackgroundContainer
key="BottomSheetBackgroundContainer"
Expand Down
53 changes: 36 additions & 17 deletions src/components/bottomSheetBackdrop/BottomSheetBackdrop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
DEFAULT_ENABLE_TOUCH_THROUGH,
DEFAULT_CLOSE_ON_PRESS,
} from './constants';
import { WINDOW_HEIGHT } from '../../constants';
import type { BottomSheetDefaultBackdropProps } from './types';
import { styles } from './styles';

Expand All @@ -29,6 +30,10 @@ const {
} = require('react-native-reanimated');
const interpolate = interpolateV2 || interpolateV1;

const AnimatedTouchableWithoutFeedback = Animated.createAnimatedComponent(
TouchableWithoutFeedback
);

const BottomSheetBackdropComponent = ({
animatedIndex,
animatedPosition,
Expand Down Expand Up @@ -70,6 +75,15 @@ const BottomSheetBackdropComponent = ({
//#endregion

//#region styles
const buttonStyle = useMemo(
() => [
style,
{
top: cond(eq(animatedIndex, disappearsOnIndex), WINDOW_HEIGHT, 0),
},
],
[disappearsOnIndex, style, animatedIndex]
);
const containerStyle = useMemo(
() => [
style,
Expand All @@ -81,20 +95,22 @@ const BottomSheetBackdropComponent = ({
[style, animatedOpacity]
);
//#endregion

//#region effects

//#endregion

return closeOnPress ? (
<>
<TouchableWithoutFeedback onPress={handleOnPress} style={style}>
<AnimatedTouchableWithoutFeedback
accessible={true}
accessibilityRole="button"
accessibilityLabel="Bottom Sheet backdrop"
accessibilityHint="Tap to close the Bottom Sheet"
onPress={handleOnPress}
style={buttonStyle}
>
<Animated.View ref={containerRef} style={containerStyle} />
</TouchableWithoutFeedback>
</AnimatedTouchableWithoutFeedback>
<Animated.Code>
{() =>
cond(
and(eq(animatedPosition, 0), isTouchable),
and(eq(animatedPosition, disappearsOnIndex), isTouchable),
[
set(isTouchable, 0),
call([], () => {
Expand All @@ -104,15 +120,18 @@ const BottomSheetBackdropComponent = ({
});
}),
],
cond(and(neq(animatedPosition, 0), not(isTouchable)), [
set(isTouchable, 1),
call([], () => {
// @ts-ignore
containerRef.current.setNativeProps({
pointerEvents: 'auto',
});
}),
])
cond(
and(neq(animatedPosition, disappearsOnIndex), not(isTouchable)),
[
set(isTouchable, 1),
call([], () => {
// @ts-ignore
containerRef.current.setNativeProps({
pointerEvents: 'auto',
});
}),
]
)
)
}
</Animated.Code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const BottomSheetHandleContainerComponent = ({
onHandlerStateChange={onHandlerStateChange}
>
<Animated.View
accessible={true}
accessibilityRole="adjustable"
accessibilityLabel="Bottom Sheet handle"
accessibilityHint="Drag up or down to extend or minimize the Bottom Sheet"
onLayout={shouldMeasureHeight ? handleOnLayout : undefined}
>
{renderHandle()}
Expand Down

0 comments on commit bfb7a8e

Please sign in to comment.