Skip to content

Commit 7521519

Browse files
committed
seperate hook into its own file
1 parent 11cefaf commit 7521519

4 files changed

Lines changed: 35 additions & 34 deletions

File tree

packages/react-native-gesture-handler/src/components/Pressable/Pressable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ import {
2828
isTouchWithinInset,
2929
} from './utils';
3030
import { PressabilityDebugView } from '../../handlers/PressabilityDebugView';
31-
import { INT32_MAX, isTestEnv, useIsScreenReaderEnabled } from '../../utils';
31+
import { INT32_MAX, isTestEnv } from '../../utils';
3232
import {
3333
applyRelationProp,
3434
RelationPropName,
3535
RelationPropType,
3636
} from '../utils';
3737
import { getStatesConfig, StateMachineEvent } from './stateDefinitions';
3838
import { PressableStateMachine } from './StateMachine';
39+
import { useIsScreenReaderEnabled } from '../../useIsScreenReaderEnabled';
3940

4041
const DEFAULT_LONG_PRESS_DURATION = 500;
4142
const IS_TEST_ENV = isTestEnv();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useEffect, useState } from 'react';
2+
import { AccessibilityInfo } from 'react-native';
3+
4+
export function useIsScreenReaderEnabled() {
5+
const [isEnabled, setIsEnabled] = useState(false);
6+
7+
useEffect(() => {
8+
const checkStatus = async () => {
9+
try {
10+
const res = await AccessibilityInfo.isScreenReaderEnabled();
11+
setIsEnabled(res);
12+
} catch (error) {
13+
console.warn('Could not read accessibility info: defaulting to false');
14+
}
15+
};
16+
17+
checkStatus();
18+
19+
const listener = AccessibilityInfo.addEventListener(
20+
'screenReaderChanged',
21+
(enabled) => {
22+
setIsEnabled(enabled);
23+
}
24+
);
25+
26+
return () => {
27+
listener.remove();
28+
};
29+
}, []);
30+
return isEnabled;
31+
}

packages/react-native-gesture-handler/src/utils.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { AccessibilityInfo } from 'react-native';
2-
import { useEffect, useState } from 'react';
3-
41
export function toArray<T>(object: T | T[]): T[] {
52
if (!Array.isArray(object)) {
63
return [object];
@@ -96,32 +93,3 @@ export function deepEqual(obj1: any, obj2: any) {
9693
}
9794

9895
export const INT32_MAX = 2 ** 31 - 1;
99-
100-
export function useIsScreenReaderEnabled() {
101-
const [isEnabled, setIsEnabled] = useState(false);
102-
103-
useEffect(() => {
104-
const checkStatus = async () => {
105-
try {
106-
const res = await AccessibilityInfo.isScreenReaderEnabled();
107-
setIsEnabled(res);
108-
} catch (error) {
109-
console.warn('Could not read accessibility info: defaulting to false');
110-
}
111-
};
112-
113-
checkStatus();
114-
115-
const listener = AccessibilityInfo.addEventListener(
116-
'screenReaderChanged',
117-
(enabled) => {
118-
setIsEnabled(enabled);
119-
}
120-
);
121-
122-
return () => {
123-
listener.remove();
124-
};
125-
}, []);
126-
return isEnabled;
127-
}

packages/react-native-gesture-handler/src/v3/components/Pressable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import { GestureDetector } from '../detectors';
3939
import { PureNativeButton } from './GestureButtons';
4040

4141
import { PressabilityDebugView } from '../../handlers/PressabilityDebugView';
42-
import { INT32_MAX, isTestEnv, useIsScreenReaderEnabled } from '../../utils';
42+
import { INT32_MAX, isTestEnv } from '../../utils';
43+
import { useIsScreenReaderEnabled } from '../../useIsScreenReaderEnabled';
4344

4445
const DEFAULT_LONG_PRESS_DURATION = 500;
4546
const IS_TEST_ENV = isTestEnv();

0 commit comments

Comments
 (0)