Skip to content
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

fix: Fix Talkback focus cursor movement when inside Dialogs #7478

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions packages/@react-aria/focus/src/FocusScope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import {FocusableElement, RefObject} from '@react-types/shared';
import {focusSafely} from './focusSafely';
import {getOwnerDocument, useLayoutEffect} from '@react-aria/utils';
import {getInteractionModality} from '@react-aria/interactions';
import {getOwnerDocument, isAndroid, isChrome, useLayoutEffect} from '@react-aria/utils';
import {isElementVisible} from './isElementVisible';
import React, {ReactNode, useContext, useEffect, useMemo, useRef} from 'react';

Expand Down Expand Up @@ -381,8 +382,14 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
cancelAnimationFrame(raf.current);
}
raf.current = requestAnimationFrame(() => {
// Patches infinite focus coersion loop for Android Talkback where the user isn't able to move the virtual cursor
// if within a containing focus scope. Bug filed against Chrome: https://issuetracker.google.com/issues/384844019.
// Note that this means focus can leave focus containing modals due to this, but it is isolated to Chrome Talkback.
let modality = getInteractionModality();
let shouldSkipFocusRestore = (modality === 'virtual' || modality === null) && isAndroid() && isChrome();

// Use document.activeElement instead of e.relatedTarget so we can tell if user clicked into iframe
if (ownerDocument.activeElement && shouldContainFocus(scopeRef) && !isElementInChildScope(ownerDocument.activeElement, scopeRef)) {
if (!shouldSkipFocusRestore && ownerDocument.activeElement && shouldContainFocus(scopeRef) && !isElementInChildScope(ownerDocument.activeElement, scopeRef)) {
Comment on lines +385 to +392
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated here, this is a temporary patch. Note that this will break use cases where focus is truly lost to the body such as this story on main where due to the dialog's content changing, the previously focused button disappears. We would usually recoerce focus back into the dialog but at the moment we cannot differentiate between that case and if the user is simply navigating between elements in the FocusScope in Chrome Talkback

Copy link
Member Author

@LFDanLu LFDanLu Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that this is indeed a Chrome specific bug, Firefox retains DOM focus on whatever Talkback was previously virtually focusing until you trigger a click via double tapping on a different element, can be tested via: https://64qz97.csb.app/ . This is most likely what Chrome's behavior used to be which is why this code used to work

activeScope = scopeRef;
if (ownerDocument.body.contains(e.target)) {
focusedNode.current = e.target;
Expand Down
1 change: 0 additions & 1 deletion packages/@react-spectrum/menu/test/MenuTrigger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1123,4 +1123,3 @@ AriaMenuTests({
)
}
});

Loading