Skip to content

TabController - Screen reader page auto focus #3743

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/components/tabController/TabPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {PropsWithChildren, useCallback, useContext, useState, useMemo} from 'react';
import {type StyleProp, StyleSheet, type ViewStyle} from 'react-native';
import React, {PropsWithChildren, useCallback, useContext, useState, useMemo, useRef, useEffect} from 'react';
import {type StyleProp, StyleSheet, type ViewStyle, findNodeHandle, AccessibilityInfo} from 'react-native';
import Reanimated, {useAnimatedStyle, useAnimatedReaction, runOnJS} from 'react-native-reanimated';
// import {Freeze} from 'react-freeze';
import TabBarContext from './TabBarContext';
Expand Down Expand Up @@ -31,6 +31,8 @@ export interface TabControllerPageProps {
style?: StyleProp<ViewStyle>;
}

const READER_FOCUS_DELAY = 300;

/**
* @description: TabController's TabPage
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/TabControllerScreen/index.tsx
Expand All @@ -46,6 +48,8 @@ export default function TabPage({
}: PropsWithChildren<TabControllerPageProps>) {
const {currentPage, asCarousel, nestedInScrollView, containerWidth} = useContext(TabBarContext);
const [shouldLoad, setLoaded] = useState(!lazy);
const [isActive, setIsActive] = useState(false);
const pageRef = useRef(null);
// const [focused, setFocused] = useState(false);

const lazyLoad = useCallback(() => {
Expand All @@ -60,13 +64,13 @@ export default function TabPage({
return currentPage.value;
},
(currentPage /* , previousPage */) => {
const isActive = currentPage === index;
// const wasActive = previousPage === index;
// const nearActive = asCarousel && (currentPage - 1 === index || currentPage + 1 === index);
// const wasNearActive =
// asCarousel && previousPage !== null && (previousPage - 1 === index || previousPage + 1 === index);
const newIsActive = currentPage === index;

if (newIsActive !== isActive) {
runOnJS(setIsActive)(newIsActive);
}

if (isActive) {
if (newIsActive) {
runOnJS(lazyLoad)();
}

Expand All @@ -76,7 +80,25 @@ export default function TabPage({
// runOnJS(setFocused)(false);
// }
},
[currentPage, lazyLoad]);
[currentPage, lazyLoad, isActive]);

useEffect(() => {
let timeoutId: NodeJS.Timeout;
if (isActive && pageRef.current && shouldLoad) {
timeoutId = setTimeout(() => {
const node = findNodeHandle(pageRef.current);
if (node) {
AccessibilityInfo.setAccessibilityFocus(node);
}
}, READER_FOCUS_DELAY);
}

return () => {
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, [isActive, shouldLoad]);

const animatedPageStyle = useAnimatedStyle(() => {
const isActive = Math.round(currentPage.value) === index;
Expand All @@ -99,7 +121,7 @@ export default function TabPage({
}, [asCarousel, animatedPageStyle, containerWidth, style]);

return (
<Reanimated.View style={_style} testID={testID}>
<Reanimated.View ref={pageRef} style={_style} testID={testID} accessible={false}>
{!shouldLoad && renderLoading?.()}
{shouldLoad && props.children}
{/* <Freeze freeze={!shouldLoad || !focused}>{props.children}</Freeze> */}
Expand Down