UNPKG

1.87 kBPlain TextView Raw
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the 'License');
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13import {FocusableElement} from '@react-types/shared';
14import {focusWithoutScrolling, getOwnerDocument, runAfterTransition} from '@react-aria/utils';
15import {getInteractionModality} from '@react-aria/interactions';
16
17/**
18 * A utility function that focuses an element while avoiding undesired side effects such
19 * as page scrolling and screen reader issues with CSS transitions.
20 */
21export function focusSafely(element: FocusableElement) {
22 // If the user is interacting with a virtual cursor, e.g. screen reader, then
23 // wait until after any animated transitions that are currently occurring on
24 // the page before shifting focus. This avoids issues with VoiceOver on iOS
25 // causing the page to scroll when moving focus if the element is transitioning
26 // from off the screen.
27 const ownerDocument = getOwnerDocument(element);
28 if (getInteractionModality() === 'virtual') {
29 let lastFocusedElement = ownerDocument.activeElement;
30 runAfterTransition(() => {
31 // If focus did not move and the element is still in the document, focus it.
32 if (ownerDocument.activeElement === lastFocusedElement && element.isConnected) {
33 focusWithoutScrolling(element);
34 }
35 });
36 } else {
37 focusWithoutScrolling(element);
38 }
39}