@react-aria/interactions 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/PressResponder.main.js +62 -0
- package/dist/PressResponder.main.js.map +1 -0
- package/dist/PressResponder.mjs +52 -0
- package/dist/PressResponder.module.js +52 -0
- package/dist/PressResponder.module.js.map +1 -0
- package/dist/Pressable.main.js +43 -0
- package/dist/Pressable.main.js.map +1 -0
- package/dist/Pressable.mjs +34 -0
- package/dist/Pressable.module.js +34 -0
- package/dist/Pressable.module.js.map +1 -0
- package/dist/context.main.js +30 -0
- package/dist/context.main.js.map +1 -0
- package/dist/context.mjs +21 -0
- package/dist/context.module.js +21 -0
- package/dist/context.module.js.map +1 -0
- package/dist/createEventHandler.main.js +42 -0
- package/dist/createEventHandler.main.js.map +1 -0
- package/dist/createEventHandler.mjs +37 -0
- package/dist/createEventHandler.module.js +37 -0
- package/dist/createEventHandler.module.js.map +1 -0
- package/dist/import.mjs +39 -0
- package/dist/main.js +62 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +39 -0
- package/dist/module.js.map +1 -0
- package/dist/textSelection.main.js +77 -0
- package/dist/textSelection.main.js.map +1 -0
- package/dist/textSelection.mjs +71 -0
- package/dist/textSelection.module.js +71 -0
- package/dist/textSelection.module.js.map +1 -0
- package/dist/types.d.ts +218 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/useFocus.main.js +64 -0
- package/dist/useFocus.main.js.map +1 -0
- package/dist/useFocus.mjs +59 -0
- package/dist/useFocus.module.js +59 -0
- package/dist/useFocus.module.js.map +1 -0
- package/dist/useFocusVisible.main.js +248 -0
- package/dist/useFocusVisible.main.js.map +1 -0
- package/dist/useFocusVisible.mjs +237 -0
- package/dist/useFocusVisible.module.js +237 -0
- package/dist/useFocusVisible.module.js.map +1 -0
- package/dist/useFocusWithin.main.js +76 -0
- package/dist/useFocusWithin.main.js.map +1 -0
- package/dist/useFocusWithin.mjs +71 -0
- package/dist/useFocusWithin.module.js +71 -0
- package/dist/useFocusWithin.module.js.map +1 -0
- package/dist/useHover.main.js +142 -0
- package/dist/useHover.main.js.map +1 -0
- package/dist/useHover.mjs +137 -0
- package/dist/useHover.module.js +137 -0
- package/dist/useHover.module.js.map +1 -0
- package/dist/useInteractOutside.main.js +101 -0
- package/dist/useInteractOutside.main.js.map +1 -0
- package/dist/useInteractOutside.mjs +96 -0
- package/dist/useInteractOutside.module.js +96 -0
- package/dist/useInteractOutside.module.js.map +1 -0
- package/dist/useKeyboard.main.js +30 -0
- package/dist/useKeyboard.main.js.map +1 -0
- package/dist/useKeyboard.mjs +25 -0
- package/dist/useKeyboard.module.js +25 -0
- package/dist/useKeyboard.module.js.map +1 -0
- package/dist/useLongPress.main.js +84 -0
- package/dist/useLongPress.main.js.map +1 -0
- package/dist/useLongPress.mjs +79 -0
- package/dist/useLongPress.module.js +79 -0
- package/dist/useLongPress.module.js.map +1 -0
- package/dist/useMove.main.js +236 -0
- package/dist/useMove.main.js.map +1 -0
- package/dist/useMove.mjs +231 -0
- package/dist/useMove.module.js +231 -0
- package/dist/useMove.module.js.map +1 -0
- package/dist/usePress.main.js +612 -0
- package/dist/usePress.main.js.map +1 -0
- package/dist/usePress.mjs +607 -0
- package/dist/usePress.module.js +607 -0
- package/dist/usePress.module.js.map +1 -0
- package/dist/useScrollWheel.main.js +41 -0
- package/dist/useScrollWheel.main.js.map +1 -0
- package/dist/useScrollWheel.mjs +36 -0
- package/dist/useScrollWheel.module.js +36 -0
- package/dist/useScrollWheel.module.js.map +1 -0
- package/dist/utils.main.js +120 -0
- package/dist/utils.main.js.map +1 -0
- package/dist/utils.mjs +115 -0
- package/dist/utils.module.js +115 -0
- package/dist/utils.module.js.map +1 -0
- package/package.json +37 -0
- package/src/DOMPropsContext.ts +39 -0
- package/src/DOMPropsResponder.tsx +47 -0
- package/src/PressResponder.tsx +64 -0
- package/src/Pressable.tsx +31 -0
- package/src/context.ts +23 -0
- package/src/createEventHandler.ts +48 -0
- package/src/index.ts +44 -0
- package/src/textSelection.ts +99 -0
- package/src/useDOMPropsResponder.ts +27 -0
- package/src/useFocus.ts +87 -0
- package/src/useFocusVisible.ts +331 -0
- package/src/useFocusWithin.ts +103 -0
- package/src/useHover.ts +206 -0
- package/src/useInteractOutside.ts +134 -0
- package/src/useKeyboard.ts +36 -0
- package/src/useLongPress.ts +129 -0
- package/src/useMove.ts +231 -0
- package/src/usePress.ts +955 -0
- package/src/useScrollWheel.ts +41 -0
- package/src/utils.ts +130 -0
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-aria/interactions",
|
|
3
|
+
"version": "3.0.0-nightly-641446f65-240905",
|
|
4
|
+
"description": "Spectrum UI components in React",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"main": "dist/main.js",
|
|
7
|
+
"module": "dist/module.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"types": "./dist/types.d.ts",
|
|
10
|
+
"import": "./dist/import.mjs",
|
|
11
|
+
"require": "./dist/main.js"
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/types.d.ts",
|
|
14
|
+
"source": "src/index.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/adobe/react-spectrum"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@react-aria/ssr": "^3.0.0-nightly-641446f65-240905",
|
|
26
|
+
"@react-aria/utils": "^3.0.0-nightly-641446f65-240905",
|
|
27
|
+
"@react-types/shared": "^3.0.0-nightly-641446f65-240905",
|
|
28
|
+
"@swc/helpers": "^0.5.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"stableVersion": "3.22.2"
|
|
37
|
+
}
|
|
@@ -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 {DOMAttributes, RefObject} from '@react-types/shared';
|
|
14
|
+
import {mergeProps, useSyncRef} from '@react-aria/utils';
|
|
15
|
+
import React, {MutableRefObject, useContext} from 'react';
|
|
16
|
+
|
|
17
|
+
interface DOMPropsResponderProps extends DOMAttributes {
|
|
18
|
+
ref?: RefObject<Element | null>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface IDOMPropsResponderContext extends DOMAttributes {
|
|
22
|
+
register(): void,
|
|
23
|
+
ref?: MutableRefObject<Element | null>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const DOMPropsResponderContext = React.createContext<IDOMPropsResponderContext | null>(null);
|
|
27
|
+
|
|
28
|
+
export function useDOMPropsResponderContext(props: DOMPropsResponderProps): DOMPropsResponderProps {
|
|
29
|
+
// Consume context from <DOMPropsResponder> and merge with props.
|
|
30
|
+
let context = useContext(DOMPropsResponderContext);
|
|
31
|
+
if (context) {
|
|
32
|
+
let {register, ...contextProps} = context;
|
|
33
|
+
props = mergeProps(contextProps, props);
|
|
34
|
+
register();
|
|
35
|
+
}
|
|
36
|
+
useSyncRef(context, props.ref);
|
|
37
|
+
|
|
38
|
+
return props;
|
|
39
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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 {DOMPropsResponderContext} from './DOMPropsContext';
|
|
14
|
+
import {HoverProps} from './useHover';
|
|
15
|
+
import React, {ForwardedRef, ReactNode, useEffect, useRef} from 'react';
|
|
16
|
+
import {useObjectRef} from '@react-aria/utils';
|
|
17
|
+
|
|
18
|
+
interface DOMPropsResponderProps extends HoverProps {
|
|
19
|
+
children: ReactNode
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const DOMPropsResponder = React.forwardRef(({children, ...props}: DOMPropsResponderProps, ref: ForwardedRef<Element>) => {
|
|
23
|
+
let isRegistered = useRef(false);
|
|
24
|
+
let objRef = useObjectRef(ref);
|
|
25
|
+
let context = {
|
|
26
|
+
...props,
|
|
27
|
+
ref: objRef,
|
|
28
|
+
register() {
|
|
29
|
+
isRegistered.current = true;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// TODO: Think of a more generic message when this replaces the PressResponder as well
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (!isRegistered.current) {
|
|
36
|
+
console.warn(
|
|
37
|
+
'A DOMPropsResponder was ultilized without a hoverable DOM node.'
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}, []);
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<DOMPropsResponderContext.Provider value={context}>
|
|
44
|
+
{children}
|
|
45
|
+
</DOMPropsResponderContext.Provider>
|
|
46
|
+
);
|
|
47
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
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 {mergeProps, useObjectRef, useSyncRef} from '@react-aria/utils';
|
|
15
|
+
import {PressProps} from './usePress';
|
|
16
|
+
import {PressResponderContext} from './context';
|
|
17
|
+
import React, {ForwardedRef, ReactNode, useContext, useEffect, useMemo, useRef} from 'react';
|
|
18
|
+
|
|
19
|
+
interface PressResponderProps extends PressProps {
|
|
20
|
+
children: ReactNode
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const PressResponder = React.forwardRef(({children, ...props}: PressResponderProps, ref: ForwardedRef<FocusableElement>) => {
|
|
24
|
+
let isRegistered = useRef(false);
|
|
25
|
+
let prevContext = useContext(PressResponderContext);
|
|
26
|
+
ref = useObjectRef(ref || prevContext?.ref);
|
|
27
|
+
let context = mergeProps(prevContext || {}, {
|
|
28
|
+
...props,
|
|
29
|
+
ref,
|
|
30
|
+
register() {
|
|
31
|
+
isRegistered.current = true;
|
|
32
|
+
if (prevContext) {
|
|
33
|
+
prevContext.register();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
useSyncRef(prevContext, ref);
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (!isRegistered.current) {
|
|
42
|
+
console.warn(
|
|
43
|
+
'A PressResponder was rendered without a pressable child. ' +
|
|
44
|
+
'Either call the usePress hook, or wrap your DOM node with <Pressable> component.'
|
|
45
|
+
);
|
|
46
|
+
isRegistered.current = true; // only warn once in strict mode.
|
|
47
|
+
}
|
|
48
|
+
}, []);
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<PressResponderContext.Provider value={context}>
|
|
52
|
+
{children}
|
|
53
|
+
</PressResponderContext.Provider>
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export function ClearPressResponder({children}: {children: ReactNode}) {
|
|
58
|
+
let context = useMemo(() => ({register: () => {}}), []);
|
|
59
|
+
return (
|
|
60
|
+
<PressResponderContext.Provider value={context}>
|
|
61
|
+
{children}
|
|
62
|
+
</PressResponderContext.Provider>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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} from '@react-types/shared';
|
|
14
|
+
import {mergeProps, useObjectRef} from '@react-aria/utils';
|
|
15
|
+
import {PressProps, usePress} from './usePress';
|
|
16
|
+
import React, {ForwardedRef, ReactElement} from 'react';
|
|
17
|
+
|
|
18
|
+
interface PressableProps extends PressProps {
|
|
19
|
+
children: ReactElement<DOMAttributes, string>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const Pressable = React.forwardRef(({children, ...props}: PressableProps, ref: ForwardedRef<HTMLElement>) => {
|
|
23
|
+
ref = useObjectRef(ref);
|
|
24
|
+
let {pressProps} = usePress({...props, ref});
|
|
25
|
+
let child = React.Children.only(children);
|
|
26
|
+
return React.cloneElement(
|
|
27
|
+
child,
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
{ref, ...mergeProps(child.props, pressProps)}
|
|
30
|
+
);
|
|
31
|
+
});
|
package/src/context.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
|
+
import {FocusableElement} from '@react-types/shared';
|
|
14
|
+
import {PressProps} from './usePress';
|
|
15
|
+
import React, {MutableRefObject} from 'react';
|
|
16
|
+
|
|
17
|
+
interface IPressResponderContext extends PressProps {
|
|
18
|
+
register(): void,
|
|
19
|
+
ref?: MutableRefObject<FocusableElement>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const PressResponderContext = React.createContext<IPressResponderContext>({register: () => {}});
|
|
23
|
+
PressResponderContext.displayName = 'PressResponderContext';
|
|
@@ -0,0 +1,48 @@
|
|
|
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 {BaseEvent} from '@react-types/shared';
|
|
14
|
+
import {SyntheticEvent} from 'react';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This function wraps a React event handler to make stopPropagation the default, and support continuePropagation instead.
|
|
18
|
+
*/
|
|
19
|
+
export function createEventHandler<T extends SyntheticEvent>(handler?: (e: BaseEvent<T>) => void): ((e: T) => void) | undefined {
|
|
20
|
+
if (!handler) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let shouldStopPropagation = true;
|
|
25
|
+
return (e: T) => {
|
|
26
|
+
let event: BaseEvent<T> = {
|
|
27
|
+
...e,
|
|
28
|
+
preventDefault() {
|
|
29
|
+
e.preventDefault();
|
|
30
|
+
},
|
|
31
|
+
isDefaultPrevented() {
|
|
32
|
+
return e.isDefaultPrevented();
|
|
33
|
+
},
|
|
34
|
+
stopPropagation() {
|
|
35
|
+
console.error('stopPropagation is now the default behavior for events in React Spectrum. You can use continuePropagation() to revert this behavior.');
|
|
36
|
+
},
|
|
37
|
+
continuePropagation() {
|
|
38
|
+
shouldStopPropagation = false;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
handler(event);
|
|
43
|
+
|
|
44
|
+
if (shouldStopPropagation) {
|
|
45
|
+
e.stopPropagation();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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 {Pressable} from './Pressable';
|
|
14
|
+
export {PressResponder, ClearPressResponder} from './PressResponder';
|
|
15
|
+
export {useFocus} from './useFocus';
|
|
16
|
+
export {
|
|
17
|
+
isFocusVisible,
|
|
18
|
+
getInteractionModality,
|
|
19
|
+
setInteractionModality,
|
|
20
|
+
addWindowFocusTracking,
|
|
21
|
+
useInteractionModality,
|
|
22
|
+
useFocusVisible,
|
|
23
|
+
useFocusVisibleListener
|
|
24
|
+
} from './useFocusVisible';
|
|
25
|
+
export {useFocusWithin} from './useFocusWithin';
|
|
26
|
+
export {useHover} from './useHover';
|
|
27
|
+
export {useInteractOutside} from './useInteractOutside';
|
|
28
|
+
export {useKeyboard} from './useKeyboard';
|
|
29
|
+
export {useMove} from './useMove';
|
|
30
|
+
export {usePress} from './usePress';
|
|
31
|
+
export {useScrollWheel} from './useScrollWheel';
|
|
32
|
+
export {useLongPress} from './useLongPress';
|
|
33
|
+
|
|
34
|
+
export type {FocusProps, FocusResult} from './useFocus';
|
|
35
|
+
export type {FocusVisibleHandler, FocusVisibleProps, FocusVisibleResult, Modality} from './useFocusVisible';
|
|
36
|
+
export type {FocusWithinProps, FocusWithinResult} from './useFocusWithin';
|
|
37
|
+
export type {HoverProps, HoverResult} from './useHover';
|
|
38
|
+
export type {InteractOutsideProps} from './useInteractOutside';
|
|
39
|
+
export type {KeyboardProps, KeyboardResult} from './useKeyboard';
|
|
40
|
+
export type {PressProps, PressHookProps, PressResult} from './usePress';
|
|
41
|
+
export type {PressEvent, PressEvents, MoveStartEvent, MoveMoveEvent, MoveEndEvent, MoveEvents, HoverEvent, HoverEvents, FocusEvents, KeyboardEvents} from '@react-types/shared';
|
|
42
|
+
export type {MoveResult} from './useMove';
|
|
43
|
+
export type {LongPressProps, LongPressResult} from './useLongPress';
|
|
44
|
+
export type {ScrollWheelProps} from './useScrollWheel';
|
|
@@ -0,0 +1,99 @@
|
|
|
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 {getOwnerDocument, isIOS, runAfterTransition} from '@react-aria/utils';
|
|
14
|
+
|
|
15
|
+
// Safari on iOS starts selecting text on long press. The only way to avoid this, it seems,
|
|
16
|
+
// is to add user-select: none to the entire page. Adding it to the pressable element prevents
|
|
17
|
+
// that element from being selected, but nearby elements may still receive selection. We add
|
|
18
|
+
// user-select: none on touch start, and remove it again on touch end to prevent this.
|
|
19
|
+
// This must be implemented using global state to avoid race conditions between multiple elements.
|
|
20
|
+
|
|
21
|
+
// There are three possible states due to the delay before removing user-select: none after
|
|
22
|
+
// pointer up. The 'default' state always transitions to the 'disabled' state, which transitions
|
|
23
|
+
// to 'restoring'. The 'restoring' state can either transition back to 'disabled' or 'default'.
|
|
24
|
+
|
|
25
|
+
// For non-iOS devices, we apply user-select: none to the pressed element instead to avoid possible
|
|
26
|
+
// performance issues that arise from applying and removing user-select: none to the entire page
|
|
27
|
+
// (see https://github.com/adobe/react-spectrum/issues/1609).
|
|
28
|
+
type State = 'default' | 'disabled' | 'restoring';
|
|
29
|
+
|
|
30
|
+
// Note that state only matters here for iOS. Non-iOS gets user-select: none applied to the target element
|
|
31
|
+
// rather than at the document level so we just need to apply/remove user-select: none for each pressed element individually
|
|
32
|
+
let state: State = 'default';
|
|
33
|
+
let savedUserSelect = '';
|
|
34
|
+
let modifiedElementMap = new WeakMap<Element, string>();
|
|
35
|
+
|
|
36
|
+
export function disableTextSelection(target?: Element) {
|
|
37
|
+
if (isIOS()) {
|
|
38
|
+
if (state === 'default') {
|
|
39
|
+
// eslint-disable-next-line no-restricted-globals
|
|
40
|
+
const documentObject = getOwnerDocument(target);
|
|
41
|
+
savedUserSelect = documentObject.documentElement.style.webkitUserSelect;
|
|
42
|
+
documentObject.documentElement.style.webkitUserSelect = 'none';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
state = 'disabled';
|
|
46
|
+
} else if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
47
|
+
// If not iOS, store the target's original user-select and change to user-select: none
|
|
48
|
+
// Ignore state since it doesn't apply for non iOS
|
|
49
|
+
modifiedElementMap.set(target, target.style.userSelect);
|
|
50
|
+
target.style.userSelect = 'none';
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function restoreTextSelection(target?: Element) {
|
|
55
|
+
if (isIOS()) {
|
|
56
|
+
// If the state is already default, there's nothing to do.
|
|
57
|
+
// If it is restoring, then there's no need to queue a second restore.
|
|
58
|
+
if (state !== 'disabled') {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
state = 'restoring';
|
|
63
|
+
|
|
64
|
+
// There appears to be a delay on iOS where selection still might occur
|
|
65
|
+
// after pointer up, so wait a bit before removing user-select.
|
|
66
|
+
setTimeout(() => {
|
|
67
|
+
// Wait for any CSS transitions to complete so we don't recompute style
|
|
68
|
+
// for the whole page in the middle of the animation and cause jank.
|
|
69
|
+
runAfterTransition(() => {
|
|
70
|
+
// Avoid race conditions
|
|
71
|
+
if (state === 'restoring') {
|
|
72
|
+
// eslint-disable-next-line no-restricted-globals
|
|
73
|
+
const documentObject = getOwnerDocument(target);
|
|
74
|
+
if (documentObject.documentElement.style.webkitUserSelect === 'none') {
|
|
75
|
+
documentObject.documentElement.style.webkitUserSelect = savedUserSelect || '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
savedUserSelect = '';
|
|
79
|
+
state = 'default';
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}, 300);
|
|
83
|
+
} else if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
84
|
+
// If not iOS, restore the target's original user-select if any
|
|
85
|
+
// Ignore state since it doesn't apply for non iOS
|
|
86
|
+
if (target && modifiedElementMap.has(target)) {
|
|
87
|
+
let targetOldUserSelect = modifiedElementMap.get(target) as string;
|
|
88
|
+
|
|
89
|
+
if (target.style.userSelect === 'none') {
|
|
90
|
+
target.style.userSelect = targetOldUserSelect;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (target.getAttribute('style') === '') {
|
|
94
|
+
target.removeAttribute('style');
|
|
95
|
+
}
|
|
96
|
+
modifiedElementMap.delete(target);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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 {RefObject} from '@react-types/shared';
|
|
14
|
+
import {useDOMPropsResponderContext} from './DOMPropsContext';
|
|
15
|
+
|
|
16
|
+
export function useDOMPropsResponder(domRef: RefObject<Element | null>) {
|
|
17
|
+
|
|
18
|
+
let domProps = useDOMPropsResponderContext({ref: domRef}) || {};
|
|
19
|
+
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
22
|
+
const {register, isDisabled, onPress, ...partialDomProps} = domProps;
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
contextProps: partialDomProps
|
|
26
|
+
};
|
|
27
|
+
}
|
package/src/useFocus.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
// Portions of the code in this file are based on code from react.
|
|
14
|
+
// Original licensing for the following can be found in the
|
|
15
|
+
// NOTICE file in the root directory of this source tree.
|
|
16
|
+
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
|
|
17
|
+
|
|
18
|
+
import {DOMAttributes, FocusableElement, FocusEvents} from '@react-types/shared';
|
|
19
|
+
import {FocusEvent, useCallback} from 'react';
|
|
20
|
+
import {getOwnerDocument} from '@react-aria/utils';
|
|
21
|
+
import {useSyntheticBlurEvent} from './utils';
|
|
22
|
+
|
|
23
|
+
export interface FocusProps<Target = FocusableElement> extends FocusEvents<Target> {
|
|
24
|
+
/** Whether the focus events should be disabled. */
|
|
25
|
+
isDisabled?: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FocusResult<Target = FocusableElement> {
|
|
29
|
+
/** Props to spread onto the target element. */
|
|
30
|
+
focusProps: DOMAttributes<Target>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Handles focus events for the immediate target.
|
|
35
|
+
* Focus events on child elements will be ignored.
|
|
36
|
+
*/
|
|
37
|
+
export function useFocus<Target extends FocusableElement = FocusableElement>(props: FocusProps<Target>): FocusResult<Target> {
|
|
38
|
+
let {
|
|
39
|
+
isDisabled,
|
|
40
|
+
onFocus: onFocusProp,
|
|
41
|
+
onBlur: onBlurProp,
|
|
42
|
+
onFocusChange
|
|
43
|
+
} = props;
|
|
44
|
+
|
|
45
|
+
const onBlur: FocusProps<Target>['onBlur'] = useCallback((e: FocusEvent<Target>) => {
|
|
46
|
+
if (e.target === e.currentTarget) {
|
|
47
|
+
if (onBlurProp) {
|
|
48
|
+
onBlurProp(e);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (onFocusChange) {
|
|
52
|
+
onFocusChange(false);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}, [onBlurProp, onFocusChange]);
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
const onSyntheticFocus = useSyntheticBlurEvent<Target>(onBlur);
|
|
61
|
+
|
|
62
|
+
const onFocus: FocusProps<Target>['onFocus'] = useCallback((e: FocusEvent<Target>) => {
|
|
63
|
+
// Double check that document.activeElement actually matches e.target in case a previously chained
|
|
64
|
+
// focus handler already moved focus somewhere else.
|
|
65
|
+
|
|
66
|
+
const ownerDocument = getOwnerDocument(e.target);
|
|
67
|
+
|
|
68
|
+
if (e.target === e.currentTarget && ownerDocument.activeElement === e.target) {
|
|
69
|
+
if (onFocusProp) {
|
|
70
|
+
onFocusProp(e);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (onFocusChange) {
|
|
74
|
+
onFocusChange(true);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
onSyntheticFocus(e);
|
|
78
|
+
}
|
|
79
|
+
}, [onFocusChange, onFocusProp, onSyntheticFocus]);
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
focusProps: {
|
|
83
|
+
onFocus: (!isDisabled && (onFocusProp || onFocusChange || onBlurProp)) ? onFocus : undefined,
|
|
84
|
+
onBlur: (!isDisabled && (onBlurProp || onFocusChange)) ? onBlur : undefined
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|