Skip to content

Commit

Permalink
Merge pull request #27847 from kubabutkiewicz/ts-migration/Accessibil…
Browse files Browse the repository at this point in the history
…ity-lib

[No QA] [TS migration] Migrate 'Accessibility' lib to TypeScript
  • Loading branch information
Joel Bettner authored Sep 27, 2023
2 parents 5a3cce2 + 8544d6f commit d61f55a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import {useEffect, useState, useCallback} from 'react';
import {AccessibilityInfo} from 'react-native';
import _ from 'underscore';
import {AccessibilityInfo, LayoutChangeEvent} from 'react-native';
import moveAccessibilityFocus from './moveAccessibilityFocus';

const useScreenReaderStatus = () => {
type HitSlop = {x: number; y: number};

const useScreenReaderStatus = (): boolean => {
const [isScreenReaderEnabled, setIsScreenReaderEnabled] = useState(false);
useEffect(() => {
const subscription = AccessibilityInfo.addEventListener('screenReaderChanged', setIsScreenReaderEnabled);

return subscription && subscription.remove;
return () => {
subscription?.remove();
};
}, []);

return isScreenReaderEnabled;
};

const getHitSlopForSize = ({x, y}) => {
const getHitSlopForSize = ({x, y}: HitSlop) => {
/* according to https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/
the minimum tappable area is 44x44 points */
const minimumSize = 44;
const hitSlopVertical = _.max([minimumSize - x, 0]) / 2;
const hitSlopHorizontal = _.max([minimumSize - y, 0]) / 2;
const hitSlopVertical = Math.max(minimumSize - x, 0) / 2;
const hitSlopHorizontal = Math.max(minimumSize - y, 0) / 2;
return {
top: hitSlopVertical,
bottom: hitSlopVertical,
Expand All @@ -31,7 +34,7 @@ const getHitSlopForSize = ({x, y}) => {
const useAutoHitSlop = () => {
const [frameSize, setFrameSize] = useState({x: 0, y: 0});
const onLayout = useCallback(
(event) => {
(event: LayoutChangeEvent) => {
const {layout} = event.nativeEvent;
if (layout.width !== frameSize.x && layout.height !== frameSize.y) {
setFrameSize({x: layout.width, y: layout.height});
Expand Down
8 changes: 0 additions & 8 deletions src/libs/Accessibility/moveAccessibilityFocus/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {AccessibilityInfo} from 'react-native';
import MoveAccessibilityFocus from './types';

const moveAccessibilityFocus = (ref) => {
const moveAccessibilityFocus: MoveAccessibilityFocus = (ref) => {
if (!ref) {
return;
}

AccessibilityInfo.sendAccessibilityEvent(ref, 'focus');
};

Expand Down
10 changes: 10 additions & 0 deletions src/libs/Accessibility/moveAccessibilityFocus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import MoveAccessibilityFocus from './types';

const moveAccessibilityFocus: MoveAccessibilityFocus = (ref) => {
if (!ref?.current) {
return;
}
ref.current.focus();
};

export default moveAccessibilityFocus;
6 changes: 6 additions & 0 deletions src/libs/Accessibility/moveAccessibilityFocus/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {ElementRef, RefObject} from 'react';
import {HostComponent} from 'react-native';

type MoveAccessibilityFocus = (ref?: ElementRef<HostComponent<unknown>> & RefObject<HTMLOrSVGElement>) => void;

export default MoveAccessibilityFocus;

0 comments on commit d61f55a

Please sign in to comment.