Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #827 from cds-snc/issue/799/memo-fix
Browse files Browse the repository at this point in the history
a11y auto focus
  • Loading branch information
timarney authored Jul 21, 2020
2 parents 2335efb + 6806040 commit a3cfb6b
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/shared/useAccessibilityAutoFocus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useLayoutEffect, useState} from 'react';
import {useLayoutEffect, useMemo, useState} from 'react';
import {AccessibilityInfo, findNodeHandle} from 'react-native';

import {createCancellableCallbackPromise} from './cancellablePromise';
Expand All @@ -7,7 +7,7 @@ import {createCancellableCallbackPromise} from './cancellablePromise';
* Look like there is timing issue with AccessibilityInfo.setAccessibilityFocus
* Ref https://github.com/react-native-community/discussions-and-proposals/issues/118
*/
const AUTO_FOCUS_DELAY = 200;
const AUTO_FOCUS_DELAY = 500;

export const focusOnElement = (elementRef: any) => {
const node = findNodeHandle(elementRef);
Expand All @@ -23,15 +23,18 @@ export const focusOnElement = (elementRef: any) => {
*/
export const useAccessibilityAutoFocus = (isActive = true) => {
const [autoFocusRef, setAutoFocusRef] = useState<any>();
const {callable, cancelable} = createCancellableCallbackPromise(
() => AccessibilityInfo.isScreenReaderEnabled().then(delay(AUTO_FOCUS_DELAY)),
isScreenReaderEnabled => {
if (!isScreenReaderEnabled) {
return;
}
focusOnElement(autoFocusRef);
},
);

const {callable, cancelable} = useMemo(() => {
return createCancellableCallbackPromise(
() => AccessibilityInfo.isScreenReaderEnabled().then(delay(AUTO_FOCUS_DELAY)),
isScreenReaderEnabled => {
if (!isScreenReaderEnabled) {
return;
}
focusOnElement(autoFocusRef);
},
);
}, [autoFocusRef]);

useLayoutEffect(() => {
if (!autoFocusRef || !isActive) {
Expand Down

0 comments on commit a3cfb6b

Please sign in to comment.