@react-aria/focus 3.0.0-nightly-641446f65-240905
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.
- package/README.md +3 -0
- package/dist/FocusRing.main.js +44 -0
- package/dist/FocusRing.main.js.map +1 -0
- package/dist/FocusRing.mjs +35 -0
- package/dist/FocusRing.module.js +35 -0
- package/dist/FocusRing.module.js.map +1 -0
- package/dist/FocusScope.main.js +748 -0
- package/dist/FocusScope.main.js.map +1 -0
- package/dist/FocusScope.mjs +734 -0
- package/dist/FocusScope.module.js +734 -0
- package/dist/FocusScope.module.js.map +1 -0
- package/dist/focusSafely.main.js +39 -0
- package/dist/focusSafely.main.js.map +1 -0
- package/dist/focusSafely.mjs +34 -0
- package/dist/focusSafely.module.js +34 -0
- package/dist/focusSafely.module.js.map +1 -0
- package/dist/import.mjs +27 -0
- package/dist/isElementVisible.main.js +41 -0
- package/dist/isElementVisible.main.js.map +1 -0
- package/dist/isElementVisible.mjs +36 -0
- package/dist/isElementVisible.module.js +36 -0
- package/dist/isElementVisible.module.js.map +1 -0
- package/dist/main.js +43 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +27 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +148 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/useFocusRing.main.js +50 -0
- package/dist/useFocusRing.main.js.map +1 -0
- package/dist/useFocusRing.mjs +45 -0
- package/dist/useFocusRing.module.js +45 -0
- package/dist/useFocusRing.module.js.map +1 -0
- package/dist/useFocusable.main.js +75 -0
- package/dist/useFocusable.main.js.map +1 -0
- package/dist/useFocusable.mjs +65 -0
- package/dist/useFocusable.module.js +65 -0
- package/dist/useFocusable.module.js.map +1 -0
- package/dist/useHasTabbableChild.main.js +62 -0
- package/dist/useHasTabbableChild.main.js.map +1 -0
- package/dist/useHasTabbableChild.mjs +57 -0
- package/dist/useHasTabbableChild.module.js +57 -0
- package/dist/useHasTabbableChild.module.js.map +1 -0
- package/package.json +38 -0
- package/src/FocusRing.tsx +55 -0
- package/src/FocusScope.tsx +988 -0
- package/src/focusSafely.ts +39 -0
- package/src/index.ts +23 -0
- package/src/isElementVisible.ts +69 -0
- package/src/useFocusRing.ts +78 -0
- package/src/useFocusable.tsx +97 -0
- package/src/useHasTabbableChild.ts +66 -0
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
|
|
13
|
+
import {FocusableElement} from '@react-types/shared';
|
|
14
|
+
import {focusWithoutScrolling, getOwnerDocument, runAfterTransition} from '@react-aria/utils';
|
|
15
|
+
import {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
|
+
*/
|
|
21
|
+
export 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
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
13
|
+
export {FocusScope, useFocusManager, getFocusableTreeWalker, createFocusManager, isElementInChildOfActiveScope, isFocusable} from './FocusScope';
|
|
14
|
+
export {FocusRing} from './FocusRing';
|
|
15
|
+
export {FocusableProvider, useFocusable} from './useFocusable';
|
|
16
|
+
export {useFocusRing} from './useFocusRing';
|
|
17
|
+
export {focusSafely} from './focusSafely';
|
|
18
|
+
export {useHasTabbableChild} from './useHasTabbableChild';
|
|
19
|
+
|
|
20
|
+
export type {FocusScopeProps, FocusManager, FocusManagerOptions} from './FocusScope';
|
|
21
|
+
export type {FocusRingProps} from './FocusRing';
|
|
22
|
+
export type {FocusableAria, FocusableOptions, FocusableProviderProps} from './useFocusable';
|
|
23
|
+
export type {AriaFocusRingProps, FocusRingAria} from './useFocusRing';
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 {getOwnerWindow} from '@react-aria/utils';
|
|
14
|
+
|
|
15
|
+
function isStyleVisible(element: Element) {
|
|
16
|
+
const windowObject = getOwnerWindow(element);
|
|
17
|
+
if (!(element instanceof windowObject.HTMLElement) && !(element instanceof windowObject.SVGElement)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let {display, visibility} = element.style;
|
|
22
|
+
|
|
23
|
+
let isVisible = (
|
|
24
|
+
display !== 'none' &&
|
|
25
|
+
visibility !== 'hidden' &&
|
|
26
|
+
visibility !== 'collapse'
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
if (isVisible) {
|
|
30
|
+
const {getComputedStyle} = element.ownerDocument.defaultView as unknown as Window;
|
|
31
|
+
let {display: computedDisplay, visibility: computedVisibility} = getComputedStyle(element);
|
|
32
|
+
|
|
33
|
+
isVisible = (
|
|
34
|
+
computedDisplay !== 'none' &&
|
|
35
|
+
computedVisibility !== 'hidden' &&
|
|
36
|
+
computedVisibility !== 'collapse'
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return isVisible;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isAttributeVisible(element: Element, childElement?: Element) {
|
|
44
|
+
return (
|
|
45
|
+
!element.hasAttribute('hidden') &&
|
|
46
|
+
// Ignore HiddenSelect when tree walking.
|
|
47
|
+
!element.hasAttribute('data-react-aria-prevent-focus') &&
|
|
48
|
+
(element.nodeName === 'DETAILS' &&
|
|
49
|
+
childElement &&
|
|
50
|
+
childElement.nodeName !== 'SUMMARY'
|
|
51
|
+
? element.hasAttribute('open')
|
|
52
|
+
: true)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Adapted from https://github.com/testing-library/jest-dom and
|
|
58
|
+
* https://github.com/vuejs/vue-test-utils-next/.
|
|
59
|
+
* Licensed under the MIT License.
|
|
60
|
+
* @param element - Element to evaluate for display or visibility.
|
|
61
|
+
*/
|
|
62
|
+
export function isElementVisible(element: Element, childElement?: Element) {
|
|
63
|
+
return (
|
|
64
|
+
element.nodeName !== '#comment' &&
|
|
65
|
+
isStyleVisible(element) &&
|
|
66
|
+
isAttributeVisible(element, childElement) &&
|
|
67
|
+
(!element.parentElement || isElementVisible(element.parentElement, element))
|
|
68
|
+
);
|
|
69
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
setFocused(isFocused);
|
|
55
|
+
updateState();
|
|
56
|
+
}, [updateState]);
|
|
57
|
+
|
|
58
|
+
useFocusVisibleListener((isFocusVisible) => {
|
|
59
|
+
state.current.isFocusVisible = isFocusVisible;
|
|
60
|
+
updateState();
|
|
61
|
+
}, [], {isTextInput});
|
|
62
|
+
|
|
63
|
+
let {focusProps} = useFocus({
|
|
64
|
+
isDisabled: within,
|
|
65
|
+
onFocusChange
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
let {focusWithinProps} = useFocusWithin({
|
|
69
|
+
isDisabled: !within,
|
|
70
|
+
onFocusWithinChange: onFocusChange
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
isFocused,
|
|
75
|
+
isFocusVisible: isFocusVisibleState,
|
|
76
|
+
focusProps: within ? focusWithinProps : focusProps
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
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
|
+
|
|
13
|
+
import {DOMAttributes, FocusableDOMProps, FocusableElement, FocusableProps, RefObject} from '@react-types/shared';
|
|
14
|
+
import {focusSafely} from './';
|
|
15
|
+
import {mergeProps, useObjectRef, useSyncRef} from '@react-aria/utils';
|
|
16
|
+
import React, {ForwardedRef, MutableRefObject, ReactNode, useContext, useEffect, useRef} from 'react';
|
|
17
|
+
import {useFocus, useKeyboard} from '@react-aria/interactions';
|
|
18
|
+
|
|
19
|
+
export interface FocusableOptions extends FocusableProps, FocusableDOMProps {
|
|
20
|
+
/** Whether focus should be disabled. */
|
|
21
|
+
isDisabled?: boolean
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FocusableProviderProps extends DOMAttributes {
|
|
25
|
+
/** The child element to provide DOM props to. */
|
|
26
|
+
children?: ReactNode
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface FocusableContextValue extends FocusableProviderProps {
|
|
30
|
+
ref?: MutableRefObject<FocusableElement | null>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let FocusableContext = React.createContext<FocusableContextValue | null>(null);
|
|
34
|
+
|
|
35
|
+
function useFocusableContext(ref: RefObject<FocusableElement | null>): FocusableContextValue {
|
|
36
|
+
let context = useContext(FocusableContext) || {};
|
|
37
|
+
useSyncRef(context, ref);
|
|
38
|
+
|
|
39
|
+
// eslint-disable-next-line
|
|
40
|
+
let {ref: _, ...otherProps} = context;
|
|
41
|
+
return otherProps;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Provides DOM props to the nearest focusable child.
|
|
46
|
+
*/
|
|
47
|
+
function FocusableProvider(props: FocusableProviderProps, ref: ForwardedRef<FocusableElement>) {
|
|
48
|
+
let {children, ...otherProps} = props;
|
|
49
|
+
let objRef = useObjectRef(ref);
|
|
50
|
+
let context = {
|
|
51
|
+
...otherProps,
|
|
52
|
+
ref: objRef
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<FocusableContext.Provider value={context}>
|
|
57
|
+
{children}
|
|
58
|
+
</FocusableContext.Provider>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let _FocusableProvider = React.forwardRef(FocusableProvider);
|
|
63
|
+
export {_FocusableProvider as FocusableProvider};
|
|
64
|
+
|
|
65
|
+
export interface FocusableAria {
|
|
66
|
+
/** Props for the focusable element. */
|
|
67
|
+
focusableProps: DOMAttributes
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Used to make an element focusable and capable of auto focus.
|
|
72
|
+
*/
|
|
73
|
+
export function useFocusable(props: FocusableOptions, domRef: RefObject<FocusableElement | null>): FocusableAria {
|
|
74
|
+
let {focusProps} = useFocus(props);
|
|
75
|
+
let {keyboardProps} = useKeyboard(props);
|
|
76
|
+
let interactions = mergeProps(focusProps, keyboardProps);
|
|
77
|
+
let domProps = useFocusableContext(domRef);
|
|
78
|
+
let interactionProps = props.isDisabled ? {} : domProps;
|
|
79
|
+
let autoFocusRef = useRef(props.autoFocus);
|
|
80
|
+
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (autoFocusRef.current && domRef.current) {
|
|
83
|
+
focusSafely(domRef.current);
|
|
84
|
+
}
|
|
85
|
+
autoFocusRef.current = false;
|
|
86
|
+
}, [domRef]);
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
focusableProps: mergeProps(
|
|
90
|
+
{
|
|
91
|
+
...interactions,
|
|
92
|
+
tabIndex: props.excludeFromTabOrder && !props.isDisabled ? -1 : undefined
|
|
93
|
+
},
|
|
94
|
+
interactionProps
|
|
95
|
+
)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
}
|