UNPKG

1.8 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, 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 if (getInteractionModality() === 'virtual') {
28 let lastFocusedElement = document.activeElement;
29 runAfterTransition(() => {
30 // If focus did not move and the element is still in the document, focus it.
31 if (document.activeElement === lastFocusedElement && document.contains(element)) {
32 focusWithoutScrolling(element);
33 }
34 });
35 } else {
36 focusWithoutScrolling(element);
37 }
38}