From bca1e6861ae2f98ae67c8779a8a7a402e30a7773 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 24 Jan 2025 13:38:23 +0800 Subject: [PATCH 1/3] prevent scroll when syncing focus --- src/hooks/useSyncFocus/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/hooks/useSyncFocus/index.ts b/src/hooks/useSyncFocus/index.ts index 6211b9988a82..f5b538e4a268 100644 --- a/src/hooks/useSyncFocus/index.ts +++ b/src/hooks/useSyncFocus/index.ts @@ -1,6 +1,5 @@ import {useContext, useLayoutEffect} from 'react'; import type {RefObject} from 'react'; -import type {View} from 'react-native'; import {ScreenWrapperStatusContext} from '@components/ScreenWrapper'; /** @@ -8,7 +7,7 @@ import {ScreenWrapperStatusContext} from '@components/ScreenWrapper'; * When the user navigates through the app using the arrows and then the tab button, the focus on the element and the native focus of the browser differs. * To maintain consistency when an element is focused in the app, the focus() method is additionally called on the focused element to eliminate the difference between native browser focus and application focus. */ -const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus = true) => { +const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus = true) => { // this hook can be used outside ScreenWrapperStatusContext (eg. in Popovers). So we to check if the context is present. // If we are outside context we don't have to look at transition status const contextValue = useContext(ScreenWrapperStatusContext); @@ -20,7 +19,7 @@ const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus return; } - ref.current?.focus(); + ref.current?.focus({preventScroll: true}); // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps }, [didScreenTransitionEnd, isFocused, ref]); }; From d9024e1c19f82b56263dc307587eb291f7fb9cd2 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Sat, 25 Jan 2025 01:43:04 +0800 Subject: [PATCH 2/3] fix type --- src/components/FocusableMenuItem.tsx | 2 +- src/hooks/useSyncFocus/index.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/components/FocusableMenuItem.tsx b/src/components/FocusableMenuItem.tsx index fc873196231d..799a4e7bd336 100644 --- a/src/components/FocusableMenuItem.tsx +++ b/src/components/FocusableMenuItem.tsx @@ -1,5 +1,5 @@ import React, {useRef} from 'react'; -import type {View} from 'react-native'; +import {View} from 'react-native'; import useSyncFocus from '@hooks/useSyncFocus'; import type {MenuItemProps} from './MenuItem'; import MenuItem from './MenuItem'; diff --git a/src/hooks/useSyncFocus/index.ts b/src/hooks/useSyncFocus/index.ts index f5b538e4a268..b5e960144882 100644 --- a/src/hooks/useSyncFocus/index.ts +++ b/src/hooks/useSyncFocus/index.ts @@ -1,5 +1,6 @@ import {useContext, useLayoutEffect} from 'react'; import type {RefObject} from 'react'; +import {View} from 'react-native'; import {ScreenWrapperStatusContext} from '@components/ScreenWrapper'; /** @@ -7,7 +8,7 @@ import {ScreenWrapperStatusContext} from '@components/ScreenWrapper'; * When the user navigates through the app using the arrows and then the tab button, the focus on the element and the native focus of the browser differs. * To maintain consistency when an element is focused in the app, the focus() method is additionally called on the focused element to eliminate the difference between native browser focus and application focus. */ -const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus = true) => { +const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus = true) => { // this hook can be used outside ScreenWrapperStatusContext (eg. in Popovers). So we to check if the context is present. // If we are outside context we don't have to look at transition status const contextValue = useContext(ScreenWrapperStatusContext); @@ -15,11 +16,11 @@ const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyn const didScreenTransitionEnd = contextValue ? contextValue.didScreenTransitionEnd : true; useLayoutEffect(() => { - if (!isFocused || !shouldSyncFocus || !didScreenTransitionEnd) { + if (!isFocused || !shouldSyncFocus || !didScreenTransitionEnd || !ref.current) { return; } - ref.current?.focus({preventScroll: true}); + (ref.current as unknown as HTMLElement).focus({preventScroll: true}); // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps }, [didScreenTransitionEnd, isFocused, ref]); }; From 13dad8b6d6e1dfcdd7d815479d72dce13b481dec Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Sat, 25 Jan 2025 01:50:33 +0800 Subject: [PATCH 3/3] lint --- src/components/FocusableMenuItem.tsx | 2 +- src/hooks/useSyncFocus/index.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/components/FocusableMenuItem.tsx b/src/components/FocusableMenuItem.tsx index 799a4e7bd336..fc873196231d 100644 --- a/src/components/FocusableMenuItem.tsx +++ b/src/components/FocusableMenuItem.tsx @@ -1,5 +1,5 @@ import React, {useRef} from 'react'; -import {View} from 'react-native'; +import type {View} from 'react-native'; import useSyncFocus from '@hooks/useSyncFocus'; import type {MenuItemProps} from './MenuItem'; import MenuItem from './MenuItem'; diff --git a/src/hooks/useSyncFocus/index.ts b/src/hooks/useSyncFocus/index.ts index b5e960144882..11c636cd5843 100644 --- a/src/hooks/useSyncFocus/index.ts +++ b/src/hooks/useSyncFocus/index.ts @@ -1,6 +1,6 @@ import {useContext, useLayoutEffect} from 'react'; import type {RefObject} from 'react'; -import {View} from 'react-native'; +import type {View} from 'react-native'; import {ScreenWrapperStatusContext} from '@components/ScreenWrapper'; /** @@ -8,7 +8,7 @@ import {ScreenWrapperStatusContext} from '@components/ScreenWrapper'; * When the user navigates through the app using the arrows and then the tab button, the focus on the element and the native focus of the browser differs. * To maintain consistency when an element is focused in the app, the focus() method is additionally called on the focused element to eliminate the difference between native browser focus and application focus. */ -const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus = true) => { +const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus = true) => { // this hook can be used outside ScreenWrapperStatusContext (eg. in Popovers). So we to check if the context is present. // If we are outside context we don't have to look at transition status const contextValue = useContext(ScreenWrapperStatusContext); @@ -16,11 +16,11 @@ const useSyncFocus = (ref: RefObject, isFocused: boolean, shouldSyncFocus const didScreenTransitionEnd = contextValue ? contextValue.didScreenTransitionEnd : true; useLayoutEffect(() => { - if (!isFocused || !shouldSyncFocus || !didScreenTransitionEnd || !ref.current) { + if (!isFocused || !shouldSyncFocus || !didScreenTransitionEnd) { return; } - (ref.current as unknown as HTMLElement).focus({preventScroll: true}); + ref.current?.focus({preventScroll: true}); // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps }, [didScreenTransitionEnd, isFocused, ref]); };