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: add accessibility support #117

Merged
merged 3 commits into from
Dec 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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