-
Notifications
You must be signed in to change notification settings - Fork 4
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
spike/5010-roettger-ScreenReaderResponsiveTransitionHeader #5047
Changes from 4 commits
55ea06f
6f1b7f7
4da6a8c
e85fda0
fd5163a
e9ef172
8bbb460
126eec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import HooksInfo from '../../../../src/components/HooksInfo' | ||
|
||
export const exampleString = `const screenReaderEnabled = useIsScreenReaderEnabled()\n | ||
const setFocus = useCallback(() => { | ||
if (ref.current && screenReaderEnabled) {} | ||
}` | ||
|
||
<HooksInfo componentName="useIsScreenReaderEnabled" example={exampleString}/> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
import { AccessibilityInfo, ActionSheetIOS, Alert, AlertButton, AppState, Dimensions, Linking, PixelRatio, ScrollView, UIManager, View, findNodeHandle } from 'react-native' | ||
import { | ||
AccessibilityInfo, | ||
ActionSheetIOS, | ||
Alert, | ||
AlertButton, | ||
AppState, | ||
Dimensions, | ||
EmitterSubscription, | ||
Linking, | ||
PixelRatio, | ||
ScrollView, | ||
UIManager, | ||
View, | ||
findNodeHandle, | ||
} from 'react-native' | ||
import { EventArg, useNavigation } from '@react-navigation/native' | ||
import { ImagePickerResponse } from 'react-native-image-picker' | ||
import { MutableRefObject, ReactNode, useCallback, useContext, useEffect, useRef, useState } from 'react' | ||
|
@@ -147,10 +161,10 @@ export function useAccessibilityFocus<T>(): [MutableRefObject<T>, () => void] { | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const ref: MutableRefObject<any> = useRef<T>(null) | ||
const dispatch = useAppDispatch() | ||
const screanReaderEnabled = useIsScreanReaderEnabled() | ||
const screenReaderEnabled = useIsScreenReaderEnabled() | ||
|
||
const setFocus = useCallback(() => { | ||
if (ref.current && screanReaderEnabled) { | ||
if (ref.current && screenReaderEnabled) { | ||
/** | ||
* There is a race condition during transition that causes the accessibility focus | ||
* to intermittently fail to be set https://github.com/facebook/react-native/issues/30097 | ||
|
@@ -181,32 +195,46 @@ export function useAccessibilityFocus<T>(): [MutableRefObject<T>, () => void] { | |
|
||
return () => clearTimeout(timeOutPageFocus) | ||
} | ||
}, [ref, dispatch, screanReaderEnabled]) | ||
}, [ref, dispatch, screenReaderEnabled]) | ||
|
||
return [ref, setFocus] | ||
} | ||
|
||
/** | ||
* Hook to check if the screan reader is enabled | ||
* Hook to check if the screen reader is enabled | ||
* | ||
* withListener - True to add a listener to live update screen reader status, default false | ||
* @returns boolean if the screen reader is on | ||
*/ | ||
export function useIsScreanReaderEnabled(): boolean { | ||
const [screanReaderEnabled, setScreanReaderEnabled] = useState(false) | ||
export function useIsScreenReaderEnabled(withListener = false): boolean { | ||
const [screenReaderEnabled, setScreenReaderEnabled] = useState(false) | ||
|
||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple notes:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with the details, but is it worth adding a todo for future cleanup or investigation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have created an upkeep ticket #5057 to track cleaning it up at a later date. |
||
let isMounted = true | ||
let screenReaderChangedSubscription: EmitterSubscription | ||
|
||
if (withListener) { | ||
screenReaderChangedSubscription = AccessibilityInfo.addEventListener('screenReaderChanged', (isScreenReaderEnabled) => { | ||
if (isMounted) { | ||
setScreenReaderEnabled(isScreenReaderEnabled) | ||
} | ||
}) | ||
} | ||
AccessibilityInfo.isScreenReaderEnabled().then((isScreenReaderEnabled) => { | ||
if (isMounted) { | ||
setScreanReaderEnabled(isScreenReaderEnabled) | ||
setScreenReaderEnabled(isScreenReaderEnabled) | ||
} | ||
}) | ||
|
||
return () => { | ||
isMounted = false | ||
if (withListener) { | ||
screenReaderChangedSubscription.remove() | ||
} | ||
} | ||
}, [screanReaderEnabled]) | ||
}, [screenReaderEnabled, withListener]) | ||
|
||
return screanReaderEnabled | ||
return screenReaderEnabled | ||
} | ||
|
||
/** | ||
|
@@ -301,7 +329,7 @@ export function useAutoScrollToElement(): [MutableRefObject<ScrollView>, Mutable | |
const scrollRef = useRef() as MutableRefObject<ScrollView> | ||
const [viewRef, setFocus] = useAccessibilityFocus<View>() | ||
const [shouldFocus, setShouldFocus] = useState(true) | ||
const screenReaderEnabled = useIsScreanReaderEnabled() | ||
const screenReaderEnabled = useIsScreenReaderEnabled() | ||
|
||
const scrollToElement = useCallback( | ||
(offset?: number) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only changes were fixing the spelling typo.