@react-aria/focus 3.21.4 → 3.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/import.mjs +16 -8
  2. package/dist/main.js +32 -24
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +16 -8
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +16 -0
  7. package/package.json +14 -14
  8. package/src/index.ts +16 -13
  9. package/dist/FocusRing.main.js +0 -44
  10. package/dist/FocusRing.main.js.map +0 -1
  11. package/dist/FocusRing.mjs +0 -35
  12. package/dist/FocusRing.module.js +0 -35
  13. package/dist/FocusRing.module.js.map +0 -1
  14. package/dist/FocusScope.main.js +0 -759
  15. package/dist/FocusScope.main.js.map +0 -1
  16. package/dist/FocusScope.mjs +0 -746
  17. package/dist/FocusScope.module.js +0 -746
  18. package/dist/FocusScope.module.js.map +0 -1
  19. package/dist/types.d.ts +0 -133
  20. package/dist/types.d.ts.map +0 -1
  21. package/dist/useFocusRing.main.js +0 -55
  22. package/dist/useFocusRing.main.js.map +0 -1
  23. package/dist/useFocusRing.mjs +0 -50
  24. package/dist/useFocusRing.module.js +0 -50
  25. package/dist/useFocusRing.module.js.map +0 -1
  26. package/dist/useHasTabbableChild.main.js +0 -62
  27. package/dist/useHasTabbableChild.main.js.map +0 -1
  28. package/dist/useHasTabbableChild.mjs +0 -57
  29. package/dist/useHasTabbableChild.module.js +0 -57
  30. package/dist/useHasTabbableChild.module.js.map +0 -1
  31. package/dist/virtualFocus.main.js +0 -46
  32. package/dist/virtualFocus.main.js.map +0 -1
  33. package/dist/virtualFocus.mjs +0 -38
  34. package/dist/virtualFocus.module.js +0 -38
  35. package/dist/virtualFocus.module.js.map +0 -1
  36. package/src/FocusRing.tsx +0 -55
  37. package/src/FocusScope.tsx +0 -1024
  38. package/src/useFocusRing.ts +0 -79
  39. package/src/useHasTabbableChild.ts +0 -66
  40. package/src/virtualFocus.ts +0 -33
@@ -1,79 +0,0 @@
1
- import {DOMAttributes} from '@react-types/shared';
2
- import {isFocusVisible, useFocus, useFocusVisibleListener, useFocusWithin} from '@react-aria/interactions';
3
- import {useCallback, useRef, useState} from 'react';
4
-
5
- export interface AriaFocusRingProps {
6
- /**
7
- * Whether to show the focus ring when something
8
- * inside the container element has focus (true), or
9
- * only if the container itself has focus (false).
10
- * @default 'false'
11
- */
12
- within?: boolean,
13
-
14
- /** Whether the element is a text input. */
15
- isTextInput?: boolean,
16
-
17
- /** Whether the element will be auto focused. */
18
- autoFocus?: boolean
19
- }
20
-
21
- export interface FocusRingAria {
22
- /** Whether the element is currently focused. */
23
- isFocused: boolean,
24
-
25
- /** Whether keyboard focus should be visible. */
26
- isFocusVisible: boolean,
27
-
28
- /** Props to apply to the container element with the focus ring. */
29
- focusProps: DOMAttributes
30
- }
31
-
32
- /**
33
- * Determines whether a focus ring should be shown to indicate keyboard focus.
34
- * Focus rings are visible only when the user is interacting with a keyboard,
35
- * not with a mouse, touch, or other input methods.
36
- */
37
- export function useFocusRing(props: AriaFocusRingProps = {}): FocusRingAria {
38
- let {
39
- autoFocus = false,
40
- isTextInput,
41
- within
42
- } = props;
43
- let state = useRef({
44
- isFocused: false,
45
- isFocusVisible: autoFocus || isFocusVisible()
46
- });
47
- let [isFocused, setFocused] = useState(false);
48
- let [isFocusVisibleState, setFocusVisible] = useState(() => state.current.isFocused && state.current.isFocusVisible);
49
-
50
- let updateState = useCallback(() => setFocusVisible(state.current.isFocused && state.current.isFocusVisible), []);
51
-
52
- let onFocusChange = useCallback(isFocused => {
53
- state.current.isFocused = isFocused;
54
- state.current.isFocusVisible = isFocusVisible();
55
- setFocused(isFocused);
56
- updateState();
57
- }, [updateState]);
58
-
59
- useFocusVisibleListener((isFocusVisible) => {
60
- state.current.isFocusVisible = isFocusVisible;
61
- updateState();
62
- }, [isTextInput, isFocused], {enabled: isFocused, isTextInput});
63
-
64
- let {focusProps} = useFocus({
65
- isDisabled: within,
66
- onFocusChange
67
- });
68
-
69
- let {focusWithinProps} = useFocusWithin({
70
- isDisabled: !within,
71
- onFocusWithinChange: onFocusChange
72
- });
73
-
74
- return {
75
- isFocused,
76
- isFocusVisible: isFocusVisibleState,
77
- focusProps: within ? focusWithinProps : focusProps
78
- };
79
- }
@@ -1,66 +0,0 @@
1
- /*
2
- * Copyright 2022 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
-
13
- import {getFocusableTreeWalker} from './FocusScope';
14
- import {RefObject} from '@react-types/shared';
15
- import {useLayoutEffect} from '@react-aria/utils';
16
- import {useState} from 'react';
17
-
18
- interface AriaHasTabbableChildOptions {
19
- isDisabled?: boolean
20
- }
21
-
22
- // This was created for a special empty case of a component that can have child or
23
- // be empty, like Collection/Virtualizer/Table/ListView/etc. When these components
24
- // are empty they can have a message with a tabbable element, which is like them
25
- // being not empty, when it comes to focus and tab order.
26
-
27
- /**
28
- * Returns whether an element has a tabbable child, and updates as children change.
29
- * @private
30
- */
31
- export function useHasTabbableChild(ref: RefObject<Element | null>, options?: AriaHasTabbableChildOptions): boolean {
32
- let isDisabled = options?.isDisabled;
33
- let [hasTabbableChild, setHasTabbableChild] = useState(false);
34
-
35
- useLayoutEffect(() => {
36
- if (ref?.current && !isDisabled) {
37
- let update = () => {
38
- if (ref.current) {
39
- let walker = getFocusableTreeWalker(ref.current, {tabbable: true});
40
- setHasTabbableChild(!!walker.nextNode());
41
- }
42
- };
43
-
44
- update();
45
-
46
- // Update when new elements are inserted, or the tabIndex/disabled attribute updates.
47
- let observer = new MutationObserver(update);
48
- observer.observe(ref.current, {
49
- subtree: true,
50
- childList: true,
51
- attributes: true,
52
- attributeFilter: ['tabIndex', 'disabled']
53
- });
54
-
55
- return () => {
56
- // Disconnect mutation observer when a React update occurs on the top-level component
57
- // so we update synchronously after re-rendering. Otherwise React will emit act warnings
58
- // in tests since mutation observers fire asynchronously. The mutation observer is necessary
59
- // so we also update if a child component re-renders and adds/removes something tabbable.
60
- observer.disconnect();
61
- };
62
- }
63
- });
64
-
65
- return isDisabled ? false : hasTabbableChild;
66
- }
@@ -1,33 +0,0 @@
1
- import {getActiveElement, getOwnerDocument} from '@react-aria/utils';
2
-
3
- export function moveVirtualFocus(to: Element | null): void {
4
- let from = getVirtuallyFocusedElement(getOwnerDocument(to));
5
- if (from !== to) {
6
- if (from) {
7
- dispatchVirtualBlur(from, to);
8
- }
9
- if (to) {
10
- dispatchVirtualFocus(to, from);
11
- }
12
- }
13
- }
14
-
15
- export function dispatchVirtualBlur(from: Element, to: Element | null): void {
16
- from.dispatchEvent(new FocusEvent('blur', {relatedTarget: to}));
17
- from.dispatchEvent(new FocusEvent('focusout', {bubbles: true, relatedTarget: to}));
18
- }
19
-
20
- export function dispatchVirtualFocus(to: Element, from: Element | null): void {
21
- to.dispatchEvent(new FocusEvent('focus', {relatedTarget: from}));
22
- to.dispatchEvent(new FocusEvent('focusin', {bubbles: true, relatedTarget: from}));
23
- }
24
-
25
- export function getVirtuallyFocusedElement(document: Document): Element | null {
26
- let activeElement = getActiveElement(document);
27
- let activeDescendant = activeElement?.getAttribute('aria-activedescendant');
28
- if (activeDescendant) {
29
- return document.getElementById(activeDescendant) || activeElement;
30
- }
31
-
32
- return activeElement;
33
- }