@react-aria/utils 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/chain.main.js +26 -0
- package/dist/chain.main.js.map +1 -0
- package/dist/chain.mjs +21 -0
- package/dist/chain.module.js +21 -0
- package/dist/chain.module.js.map +1 -0
- package/dist/domHelpers.main.js +19 -0
- package/dist/domHelpers.main.js.map +1 -0
- package/dist/domHelpers.mjs +13 -0
- package/dist/domHelpers.module.js +13 -0
- package/dist/domHelpers.module.js.map +1 -0
- package/dist/filterDOMProps.main.js +45 -0
- package/dist/filterDOMProps.main.js.map +1 -0
- package/dist/filterDOMProps.mjs +40 -0
- package/dist/filterDOMProps.module.js +40 -0
- package/dist/filterDOMProps.module.js.map +1 -0
- package/dist/focusWithoutScrolling.main.js +72 -0
- package/dist/focusWithoutScrolling.main.js.map +1 -0
- package/dist/focusWithoutScrolling.mjs +67 -0
- package/dist/focusWithoutScrolling.module.js +67 -0
- package/dist/focusWithoutScrolling.module.js.map +1 -0
- package/dist/getOffset.main.js +24 -0
- package/dist/getOffset.main.js.map +1 -0
- package/dist/getOffset.mjs +19 -0
- package/dist/getOffset.module.js +19 -0
- package/dist/getOffset.module.js.map +1 -0
- package/dist/getScrollParent.main.js +28 -0
- package/dist/getScrollParent.main.js.map +1 -0
- package/dist/getScrollParent.mjs +23 -0
- package/dist/getScrollParent.module.js +23 -0
- package/dist/getScrollParent.module.js.map +1 -0
- package/dist/getScrollParents.main.js +30 -0
- package/dist/getScrollParents.main.js.map +1 -0
- package/dist/getScrollParents.mjs +25 -0
- package/dist/getScrollParents.module.js +25 -0
- package/dist/getScrollParents.module.js.map +1 -0
- package/dist/import.mjs +81 -0
- package/dist/isScrollable.main.js +25 -0
- package/dist/isScrollable.main.js.map +1 -0
- package/dist/isScrollable.mjs +20 -0
- package/dist/isScrollable.module.js +20 -0
- package/dist/isScrollable.module.js.map +1 -0
- package/dist/isVirtualEvent.main.js +41 -0
- package/dist/isVirtualEvent.main.js.map +1 -0
- package/dist/isVirtualEvent.mjs +35 -0
- package/dist/isVirtualEvent.module.js +35 -0
- package/dist/isVirtualEvent.module.js.map +1 -0
- package/dist/main.js +138 -0
- package/dist/main.js.map +1 -0
- package/dist/mergeProps.main.js +51 -0
- package/dist/mergeProps.main.js.map +1 -0
- package/dist/mergeProps.mjs +42 -0
- package/dist/mergeProps.module.js +42 -0
- package/dist/mergeProps.module.js.map +1 -0
- package/dist/mergeRefs.main.js +28 -0
- package/dist/mergeRefs.main.js.map +1 -0
- package/dist/mergeRefs.mjs +23 -0
- package/dist/mergeRefs.module.js +23 -0
- package/dist/mergeRefs.module.js.map +1 -0
- package/dist/module.js +81 -0
- package/dist/module.js.map +1 -0
- package/dist/openLink.main.js +155 -0
- package/dist/openLink.main.js.map +1 -0
- package/dist/openLink.mjs +140 -0
- package/dist/openLink.module.js +140 -0
- package/dist/openLink.module.js.map +1 -0
- package/dist/platform.main.js +71 -0
- package/dist/platform.main.js.map +1 -0
- package/dist/platform.mjs +58 -0
- package/dist/platform.module.js +58 -0
- package/dist/platform.module.js.map +1 -0
- package/dist/runAfterTransition.main.js +82 -0
- package/dist/runAfterTransition.main.js.map +1 -0
- package/dist/runAfterTransition.mjs +77 -0
- package/dist/runAfterTransition.module.js +77 -0
- package/dist/runAfterTransition.module.js.map +1 -0
- package/dist/scrollIntoView.main.js +96 -0
- package/dist/scrollIntoView.main.js.map +1 -0
- package/dist/scrollIntoView.mjs +90 -0
- package/dist/scrollIntoView.module.js +90 -0
- package/dist/scrollIntoView.module.js.map +1 -0
- package/dist/types.d.ts +229 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/useDeepMemo.main.js +30 -0
- package/dist/useDeepMemo.main.js.map +1 -0
- package/dist/useDeepMemo.mjs +25 -0
- package/dist/useDeepMemo.module.js +25 -0
- package/dist/useDeepMemo.module.js.map +1 -0
- package/dist/useDescription.main.js +59 -0
- package/dist/useDescription.main.js.map +1 -0
- package/dist/useDescription.mjs +54 -0
- package/dist/useDescription.module.js +54 -0
- package/dist/useDescription.module.js.map +1 -0
- package/dist/useDrag1D.main.js +139 -0
- package/dist/useDrag1D.main.js.map +1 -0
- package/dist/useDrag1D.mjs +134 -0
- package/dist/useDrag1D.module.js +134 -0
- package/dist/useDrag1D.module.js.map +1 -0
- package/dist/useEffectEvent.main.js +37 -0
- package/dist/useEffectEvent.main.js.map +1 -0
- package/dist/useEffectEvent.mjs +32 -0
- package/dist/useEffectEvent.module.js +32 -0
- package/dist/useEffectEvent.module.js.map +1 -0
- package/dist/useEvent.main.js +42 -0
- package/dist/useEvent.main.js.map +1 -0
- package/dist/useEvent.mjs +37 -0
- package/dist/useEvent.module.js +37 -0
- package/dist/useEvent.module.js.map +1 -0
- package/dist/useFormReset.main.js +41 -0
- package/dist/useFormReset.main.js.map +1 -0
- package/dist/useFormReset.mjs +36 -0
- package/dist/useFormReset.module.js +36 -0
- package/dist/useFormReset.module.js.map +1 -0
- package/dist/useGlobalListeners.main.js +63 -0
- package/dist/useGlobalListeners.main.js.map +1 -0
- package/dist/useGlobalListeners.mjs +58 -0
- package/dist/useGlobalListeners.module.js +58 -0
- package/dist/useGlobalListeners.module.js.map +1 -0
- package/dist/useId.main.js +93 -0
- package/dist/useId.main.js.map +1 -0
- package/dist/useId.mjs +86 -0
- package/dist/useId.module.js +86 -0
- package/dist/useId.module.js.map +1 -0
- package/dist/useLabels.main.js +44 -0
- package/dist/useLabels.main.js.map +1 -0
- package/dist/useLabels.mjs +39 -0
- package/dist/useLabels.module.js +39 -0
- package/dist/useLabels.module.js.map +1 -0
- package/dist/useLayoutEffect.main.js +27 -0
- package/dist/useLayoutEffect.main.js.map +1 -0
- package/dist/useLayoutEffect.mjs +18 -0
- package/dist/useLayoutEffect.module.js +18 -0
- package/dist/useLayoutEffect.module.js.map +1 -0
- package/dist/useLoadMore.main.js +72 -0
- package/dist/useLoadMore.main.js.map +1 -0
- package/dist/useLoadMore.mjs +67 -0
- package/dist/useLoadMore.module.js +67 -0
- package/dist/useLoadMore.module.js.map +1 -0
- package/dist/useObjectRef.main.js +37 -0
- package/dist/useObjectRef.main.js.map +1 -0
- package/dist/useObjectRef.mjs +32 -0
- package/dist/useObjectRef.module.js +32 -0
- package/dist/useObjectRef.module.js.map +1 -0
- package/dist/useResizeObserver.main.js +43 -0
- package/dist/useResizeObserver.main.js.map +1 -0
- package/dist/useResizeObserver.mjs +38 -0
- package/dist/useResizeObserver.module.js +38 -0
- package/dist/useResizeObserver.module.js.map +1 -0
- package/dist/useSyncRef.main.js +32 -0
- package/dist/useSyncRef.main.js.map +1 -0
- package/dist/useSyncRef.mjs +27 -0
- package/dist/useSyncRef.module.js +27 -0
- package/dist/useSyncRef.module.js.map +1 -0
- package/dist/useUpdateEffect.main.js +38 -0
- package/dist/useUpdateEffect.main.js.map +1 -0
- package/dist/useUpdateEffect.mjs +33 -0
- package/dist/useUpdateEffect.module.js +33 -0
- package/dist/useUpdateEffect.module.js.map +1 -0
- package/dist/useValueEffect.main.js +58 -0
- package/dist/useValueEffect.main.js.map +1 -0
- package/dist/useValueEffect.mjs +53 -0
- package/dist/useValueEffect.module.js +53 -0
- package/dist/useValueEffect.module.js.map +1 -0
- package/dist/useViewportSize.main.js +56 -0
- package/dist/useViewportSize.main.js.map +1 -0
- package/dist/useViewportSize.mjs +51 -0
- package/dist/useViewportSize.module.js +51 -0
- package/dist/useViewportSize.module.js.map +1 -0
- package/package.json +38 -0
- package/src/chain.ts +24 -0
- package/src/domHelpers.ts +14 -0
- package/src/filterDOMProps.ts +76 -0
- package/src/focusWithoutScrolling.ts +96 -0
- package/src/getOffset.ts +19 -0
- package/src/getScrollParent.ts +27 -0
- package/src/getScrollParents.ts +26 -0
- package/src/index.ts +44 -0
- package/src/isScrollable.ts +22 -0
- package/src/isVirtualEvent.ts +58 -0
- package/src/mergeProps.ts +75 -0
- package/src/mergeRefs.ts +32 -0
- package/src/openLink.tsx +183 -0
- package/src/platform.ts +79 -0
- package/src/runAfterTransition.ts +105 -0
- package/src/scrollIntoView.ts +109 -0
- package/src/useDeepMemo.ts +27 -0
- package/src/useDescription.ts +56 -0
- package/src/useDrag1D.ts +190 -0
- package/src/useEffectEvent.ts +26 -0
- package/src/useEvent.ts +37 -0
- package/src/useFormReset.ts +36 -0
- package/src/useGlobalListeners.ts +51 -0
- package/src/useId.ts +108 -0
- package/src/useLabels.ts +48 -0
- package/src/useLayoutEffect.ts +20 -0
- package/src/useLoadMore.ts +82 -0
- package/src/useObjectRef.ts +39 -0
- package/src/useResizeObserver.ts +48 -0
- package/src/useSyncRef.ts +33 -0
- package/src/useUpdateEffect.ts +36 -0
- package/src/useValueEffect.ts +64 -0
- package/src/useViewportSize.ts +63 -0
package/src/useDrag1D.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
/* eslint-disable rulesdir/pure-render */
|
|
14
|
+
|
|
15
|
+
import {getOffset} from './getOffset';
|
|
16
|
+
import {Orientation} from '@react-types/shared';
|
|
17
|
+
import React, {HTMLAttributes, MutableRefObject, useRef} from 'react';
|
|
18
|
+
|
|
19
|
+
interface UseDrag1DProps {
|
|
20
|
+
containerRef: MutableRefObject<HTMLElement>,
|
|
21
|
+
reverse?: boolean,
|
|
22
|
+
orientation?: Orientation,
|
|
23
|
+
onHover?: (hovered: boolean) => void,
|
|
24
|
+
onDrag?: (dragging: boolean) => void,
|
|
25
|
+
onPositionChange?: (position: number) => void,
|
|
26
|
+
onIncrement?: () => void,
|
|
27
|
+
onDecrement?: () => void,
|
|
28
|
+
onIncrementToMax?: () => void,
|
|
29
|
+
onDecrementToMin?: () => void,
|
|
30
|
+
onCollapseToggle?: () => void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Keep track of elements that we are currently handling dragging for via useDrag1D.
|
|
34
|
+
// If there's an ancestor and a descendant both using useDrag1D(), and the user starts
|
|
35
|
+
// dragging the descendant, we don't want useDrag1D events to fire for the ancestor.
|
|
36
|
+
const draggingElements: HTMLElement[] = [];
|
|
37
|
+
|
|
38
|
+
// created for splitview, this should be reusable for things like sliders/dials
|
|
39
|
+
// It also handles keyboard events on the target allowing for increment/decrement by a given stepsize as well as minifying/maximizing and toggling between minified and previous size
|
|
40
|
+
// It can also take a 'reverse' param to say if we should measure from the right/bottom instead of the top/left
|
|
41
|
+
// It can also handle either a vertical or horizontal movement, but not both at the same time
|
|
42
|
+
|
|
43
|
+
export function useDrag1D(props: UseDrag1DProps): HTMLAttributes<HTMLElement> {
|
|
44
|
+
console.warn('useDrag1D is deprecated, please use `useMove` instead https://react-spectrum.adobe.com/react-aria/useMove.html');
|
|
45
|
+
let {containerRef, reverse, orientation, onHover, onDrag, onPositionChange, onIncrement, onDecrement, onIncrementToMax, onDecrementToMin, onCollapseToggle} = props;
|
|
46
|
+
let getPosition = (e) => orientation === 'horizontal' ? e.clientX : e.clientY;
|
|
47
|
+
let getNextOffset = (e: MouseEvent) => {
|
|
48
|
+
let containerOffset = getOffset(containerRef.current, reverse, orientation);
|
|
49
|
+
let mouseOffset = getPosition(e);
|
|
50
|
+
let nextOffset = reverse ? containerOffset - mouseOffset : mouseOffset - containerOffset;
|
|
51
|
+
return nextOffset;
|
|
52
|
+
};
|
|
53
|
+
let dragging = useRef(false);
|
|
54
|
+
let prevPosition = useRef(0);
|
|
55
|
+
|
|
56
|
+
// Keep track of the current handlers in a ref so that the events can access them.
|
|
57
|
+
let handlers = useRef({onPositionChange, onDrag});
|
|
58
|
+
handlers.current.onDrag = onDrag;
|
|
59
|
+
handlers.current.onPositionChange = onPositionChange;
|
|
60
|
+
|
|
61
|
+
let onMouseDragged = (e: MouseEvent) => {
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
let nextOffset = getNextOffset(e);
|
|
64
|
+
if (!dragging.current) {
|
|
65
|
+
dragging.current = true;
|
|
66
|
+
if (handlers.current.onDrag) {
|
|
67
|
+
handlers.current.onDrag(true);
|
|
68
|
+
}
|
|
69
|
+
if (handlers.current.onPositionChange) {
|
|
70
|
+
handlers.current.onPositionChange(nextOffset);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (prevPosition.current === nextOffset) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
prevPosition.current = nextOffset;
|
|
77
|
+
if (onPositionChange) {
|
|
78
|
+
onPositionChange(nextOffset);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
let onMouseUp = (e: MouseEvent) => {
|
|
83
|
+
const target = e.target as HTMLElement;
|
|
84
|
+
dragging.current = false;
|
|
85
|
+
let nextOffset = getNextOffset(e);
|
|
86
|
+
if (handlers.current.onDrag) {
|
|
87
|
+
handlers.current.onDrag(false);
|
|
88
|
+
}
|
|
89
|
+
if (handlers.current.onPositionChange) {
|
|
90
|
+
handlers.current.onPositionChange(nextOffset);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
draggingElements.splice(draggingElements.indexOf(target), 1);
|
|
94
|
+
window.removeEventListener('mouseup', onMouseUp, false);
|
|
95
|
+
window.removeEventListener('mousemove', onMouseDragged, false);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
let onMouseDown = (e: React.MouseEvent<HTMLElement>) => {
|
|
99
|
+
const target = e.currentTarget;
|
|
100
|
+
// If we're already handling dragging on a descendant with useDrag1D, then
|
|
101
|
+
// we don't want to handle the drag motion on this target as well.
|
|
102
|
+
if (draggingElements.some(elt => target.contains(elt))) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
draggingElements.push(target);
|
|
106
|
+
window.addEventListener('mousemove', onMouseDragged, false);
|
|
107
|
+
window.addEventListener('mouseup', onMouseUp, false);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
let onMouseEnter = () => {
|
|
111
|
+
if (onHover) {
|
|
112
|
+
onHover(true);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
let onMouseOut = () => {
|
|
117
|
+
if (onHover) {
|
|
118
|
+
onHover(false);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
let onKeyDown = (e) => {
|
|
123
|
+
switch (e.key) {
|
|
124
|
+
case 'Left':
|
|
125
|
+
case 'ArrowLeft':
|
|
126
|
+
if (orientation === 'horizontal') {
|
|
127
|
+
e.preventDefault();
|
|
128
|
+
if (onDecrement && !reverse) {
|
|
129
|
+
onDecrement();
|
|
130
|
+
} else if (onIncrement && reverse) {
|
|
131
|
+
onIncrement();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
case 'Up':
|
|
136
|
+
case 'ArrowUp':
|
|
137
|
+
if (orientation === 'vertical') {
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
if (onDecrement && !reverse) {
|
|
140
|
+
onDecrement();
|
|
141
|
+
} else if (onIncrement && reverse) {
|
|
142
|
+
onIncrement();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
case 'Right':
|
|
147
|
+
case 'ArrowRight':
|
|
148
|
+
if (orientation === 'horizontal') {
|
|
149
|
+
e.preventDefault();
|
|
150
|
+
if (onIncrement && !reverse) {
|
|
151
|
+
onIncrement();
|
|
152
|
+
} else if (onDecrement && reverse) {
|
|
153
|
+
onDecrement();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
case 'Down':
|
|
158
|
+
case 'ArrowDown':
|
|
159
|
+
if (orientation === 'vertical') {
|
|
160
|
+
e.preventDefault();
|
|
161
|
+
if (onIncrement && !reverse) {
|
|
162
|
+
onIncrement();
|
|
163
|
+
} else if (onDecrement && reverse) {
|
|
164
|
+
onDecrement();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
case 'Home':
|
|
169
|
+
e.preventDefault();
|
|
170
|
+
if (onDecrementToMin) {
|
|
171
|
+
onDecrementToMin();
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
case 'End':
|
|
175
|
+
e.preventDefault();
|
|
176
|
+
if (onIncrementToMax) {
|
|
177
|
+
onIncrementToMax();
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
case 'Enter':
|
|
181
|
+
e.preventDefault();
|
|
182
|
+
if (onCollapseToggle) {
|
|
183
|
+
onCollapseToggle();
|
|
184
|
+
}
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
return {onMouseDown, onMouseEnter, onMouseOut, onKeyDown};
|
|
190
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 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 {useCallback, useRef} from 'react';
|
|
14
|
+
import {useLayoutEffect} from './useLayoutEffect';
|
|
15
|
+
|
|
16
|
+
export function useEffectEvent<T extends Function>(fn?: T): T {
|
|
17
|
+
const ref = useRef<T | null | undefined>(null);
|
|
18
|
+
useLayoutEffect(() => {
|
|
19
|
+
ref.current = fn;
|
|
20
|
+
}, [fn]);
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
return useCallback<T>((...args) => {
|
|
23
|
+
const f = ref.current!;
|
|
24
|
+
return f?.(...args);
|
|
25
|
+
}, []);
|
|
26
|
+
}
|
package/src/useEvent.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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 {RefObject} from '@react-types/shared';
|
|
14
|
+
import {useEffect} from 'react';
|
|
15
|
+
import {useEffectEvent} from './useEffectEvent';
|
|
16
|
+
|
|
17
|
+
export function useEvent<K extends keyof GlobalEventHandlersEventMap>(
|
|
18
|
+
ref: RefObject<EventTarget | null>,
|
|
19
|
+
event: K | (string & {}),
|
|
20
|
+
handler?: (this: Document, ev: GlobalEventHandlersEventMap[K]) => any,
|
|
21
|
+
options?: boolean | AddEventListenerOptions
|
|
22
|
+
) {
|
|
23
|
+
let handleEvent = useEffectEvent(handler);
|
|
24
|
+
let isDisabled = handler == null;
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (isDisabled || !ref.current) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let element = ref.current;
|
|
32
|
+
element.addEventListener(event, handleEvent as EventListener, options);
|
|
33
|
+
return () => {
|
|
34
|
+
element.removeEventListener(event, handleEvent as EventListener, options);
|
|
35
|
+
};
|
|
36
|
+
}, [ref, event, options, isDisabled, handleEvent]);
|
|
37
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 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 {useEffect, useRef} from 'react';
|
|
15
|
+
import {useEffectEvent} from './useEffectEvent';
|
|
16
|
+
|
|
17
|
+
export function useFormReset<T>(
|
|
18
|
+
ref: RefObject<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null>,
|
|
19
|
+
initialValue: T,
|
|
20
|
+
onReset: (value: T) => void
|
|
21
|
+
) {
|
|
22
|
+
let resetValue = useRef(initialValue);
|
|
23
|
+
let handleReset = useEffectEvent(() => {
|
|
24
|
+
if (onReset) {
|
|
25
|
+
onReset(resetValue.current);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
let form = ref?.current?.form;
|
|
31
|
+
form?.addEventListener('reset', handleReset);
|
|
32
|
+
return () => {
|
|
33
|
+
form?.removeEventListener('reset', handleReset);
|
|
34
|
+
};
|
|
35
|
+
}, [ref, handleReset]);
|
|
36
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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 {useCallback, useEffect, useRef} from 'react';
|
|
14
|
+
|
|
15
|
+
interface GlobalListeners {
|
|
16
|
+
addGlobalListener<K extends keyof DocumentEventMap>(el: EventTarget, type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void,
|
|
17
|
+
addGlobalListener(el: EventTarget, type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void,
|
|
18
|
+
removeGlobalListener<K extends keyof DocumentEventMap>(el: EventTarget, type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void,
|
|
19
|
+
removeGlobalListener(el: EventTarget, type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void,
|
|
20
|
+
removeAllGlobalListeners(): void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function useGlobalListeners(): GlobalListeners {
|
|
24
|
+
let globalListeners = useRef(new Map());
|
|
25
|
+
let addGlobalListener = useCallback((eventTarget, type, listener, options) => {
|
|
26
|
+
// Make sure we remove the listener after it is called with the `once` option.
|
|
27
|
+
let fn = options?.once ? (...args) => {
|
|
28
|
+
globalListeners.current.delete(listener);
|
|
29
|
+
listener(...args);
|
|
30
|
+
} : listener;
|
|
31
|
+
globalListeners.current.set(listener, {type, eventTarget, fn, options});
|
|
32
|
+
eventTarget.addEventListener(type, listener, options);
|
|
33
|
+
}, []);
|
|
34
|
+
let removeGlobalListener = useCallback((eventTarget, type, listener, options) => {
|
|
35
|
+
let fn = globalListeners.current.get(listener)?.fn || listener;
|
|
36
|
+
eventTarget.removeEventListener(type, fn, options);
|
|
37
|
+
globalListeners.current.delete(listener);
|
|
38
|
+
}, []);
|
|
39
|
+
let removeAllGlobalListeners = useCallback(() => {
|
|
40
|
+
globalListeners.current.forEach((value, key) => {
|
|
41
|
+
removeGlobalListener(value.eventTarget, value.type, key, value.options);
|
|
42
|
+
});
|
|
43
|
+
}, [removeGlobalListener]);
|
|
44
|
+
|
|
45
|
+
// eslint-disable-next-line arrow-body-style
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
return removeAllGlobalListeners;
|
|
48
|
+
}, [removeAllGlobalListeners]);
|
|
49
|
+
|
|
50
|
+
return {addGlobalListener, removeGlobalListener, removeAllGlobalListeners};
|
|
51
|
+
}
|
package/src/useId.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
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 {useCallback, useEffect, useRef, useState} from 'react';
|
|
14
|
+
import {useLayoutEffect} from './useLayoutEffect';
|
|
15
|
+
import {useSSRSafeId} from '@react-aria/ssr';
|
|
16
|
+
import {useValueEffect} from './';
|
|
17
|
+
|
|
18
|
+
// copied from SSRProvider.tsx to reduce exports, if needed again, consider sharing
|
|
19
|
+
let canUseDOM = Boolean(
|
|
20
|
+
typeof window !== 'undefined' &&
|
|
21
|
+
window.document &&
|
|
22
|
+
window.document.createElement
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
let idsUpdaterMap: Map<string, (v: string) => void> = new Map();
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* If a default is not provided, generate an id.
|
|
29
|
+
* @param defaultId - Default component id.
|
|
30
|
+
*/
|
|
31
|
+
export function useId(defaultId?: string): string {
|
|
32
|
+
let [value, setValue] = useState(defaultId);
|
|
33
|
+
let nextId = useRef(null);
|
|
34
|
+
|
|
35
|
+
let res = useSSRSafeId(value);
|
|
36
|
+
|
|
37
|
+
let updateValue = useCallback((val) => {
|
|
38
|
+
nextId.current = val;
|
|
39
|
+
}, []);
|
|
40
|
+
|
|
41
|
+
if (canUseDOM) {
|
|
42
|
+
idsUpdaterMap.set(res, updateValue);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
useLayoutEffect(() => {
|
|
46
|
+
let r = res;
|
|
47
|
+
return () => {
|
|
48
|
+
idsUpdaterMap.delete(r);
|
|
49
|
+
};
|
|
50
|
+
}, [res]);
|
|
51
|
+
|
|
52
|
+
// This cannot cause an infinite loop because the ref is updated first.
|
|
53
|
+
// eslint-disable-next-line
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
let newId = nextId.current;
|
|
56
|
+
if (newId) {
|
|
57
|
+
nextId.current = null;
|
|
58
|
+
setValue(newId);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return res;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Merges two ids.
|
|
67
|
+
* Different ids will trigger a side-effect and re-render components hooked up with `useId`.
|
|
68
|
+
*/
|
|
69
|
+
export function mergeIds(idA: string, idB: string): string {
|
|
70
|
+
if (idA === idB) {
|
|
71
|
+
return idA;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let setIdA = idsUpdaterMap.get(idA);
|
|
75
|
+
if (setIdA) {
|
|
76
|
+
setIdA(idB);
|
|
77
|
+
return idB;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let setIdB = idsUpdaterMap.get(idB);
|
|
81
|
+
if (setIdB) {
|
|
82
|
+
setIdB(idA);
|
|
83
|
+
return idA;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return idB;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Used to generate an id, and after render, check if that id is rendered so we know
|
|
91
|
+
* if we can use it in places such as labelledby.
|
|
92
|
+
* @param depArray - When to recalculate if the id is in the DOM.
|
|
93
|
+
*/
|
|
94
|
+
export function useSlotId(depArray: ReadonlyArray<any> = []): string {
|
|
95
|
+
let id = useId();
|
|
96
|
+
let [resolvedId, setResolvedId] = useValueEffect(id);
|
|
97
|
+
let updateId = useCallback(() => {
|
|
98
|
+
setResolvedId(function *() {
|
|
99
|
+
yield id;
|
|
100
|
+
|
|
101
|
+
yield document.getElementById(id) ? id : undefined;
|
|
102
|
+
});
|
|
103
|
+
}, [id, setResolvedId]);
|
|
104
|
+
|
|
105
|
+
useLayoutEffect(updateId, [id, updateId, ...depArray]);
|
|
106
|
+
|
|
107
|
+
return resolvedId;
|
|
108
|
+
}
|
package/src/useLabels.ts
ADDED
|
@@ -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 {AriaLabelingProps, DOMProps} from '@react-types/shared';
|
|
14
|
+
import {useId} from './useId';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Merges aria-label and aria-labelledby into aria-labelledby when both exist.
|
|
18
|
+
* @param props - Aria label props.
|
|
19
|
+
* @param defaultLabel - Default value for aria-label when not present.
|
|
20
|
+
*/
|
|
21
|
+
export function useLabels(props: DOMProps & AriaLabelingProps, defaultLabel?: string): DOMProps & AriaLabelingProps {
|
|
22
|
+
let {
|
|
23
|
+
id,
|
|
24
|
+
'aria-label': label,
|
|
25
|
+
'aria-labelledby': labelledBy
|
|
26
|
+
} = props;
|
|
27
|
+
|
|
28
|
+
// If there is both an aria-label and aria-labelledby,
|
|
29
|
+
// combine them by pointing to the element itself.
|
|
30
|
+
id = useId(id);
|
|
31
|
+
if (labelledBy && label) {
|
|
32
|
+
let ids = new Set([id, ...labelledBy.trim().split(/\s+/)]);
|
|
33
|
+
labelledBy = [...ids].join(' ');
|
|
34
|
+
} else if (labelledBy) {
|
|
35
|
+
labelledBy = labelledBy.trim().split(/\s+/).join(' ');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// If no labels are provided, use the default
|
|
39
|
+
if (!label && !labelledBy && defaultLabel) {
|
|
40
|
+
label = defaultLabel;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
'aria-label': label,
|
|
46
|
+
'aria-labelledby': labelledBy
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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 React from 'react';
|
|
14
|
+
|
|
15
|
+
// During SSR, React emits a warning when calling useLayoutEffect.
|
|
16
|
+
// Since neither useLayoutEffect nor useEffect run on the server,
|
|
17
|
+
// we can suppress this by replace it with a noop on the server.
|
|
18
|
+
export const useLayoutEffect = typeof document !== 'undefined'
|
|
19
|
+
? React.useLayoutEffect
|
|
20
|
+
: () => {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2024 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, useCallback, useRef} from 'react';
|
|
14
|
+
import {useEvent} from './useEvent';
|
|
15
|
+
// eslint-disable-next-line rulesdir/useLayoutEffectRule
|
|
16
|
+
import {useLayoutEffect} from './useLayoutEffect';
|
|
17
|
+
|
|
18
|
+
export interface LoadMoreProps {
|
|
19
|
+
/** Whether data is currently being loaded. */
|
|
20
|
+
isLoading?: boolean,
|
|
21
|
+
/** Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. */
|
|
22
|
+
onLoadMore?: () => void,
|
|
23
|
+
/**
|
|
24
|
+
* The amount of offset from the bottom of your scrollable region that should trigger load more.
|
|
25
|
+
* Uses a percentage value relative to the scroll body's client height. Load more is then triggered
|
|
26
|
+
* when your current scroll position's distance from the bottom of the currently loaded list of items is less than
|
|
27
|
+
* or equal to the provided value. (e.g. 1 = 100% of the scroll region's height).
|
|
28
|
+
* @default 1
|
|
29
|
+
*/
|
|
30
|
+
scrollOffset?: number,
|
|
31
|
+
/** The data currently loaded. */
|
|
32
|
+
items?: any[]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function useLoadMore(props: LoadMoreProps, ref: RefObject<HTMLElement | null>) {
|
|
36
|
+
let {isLoading, onLoadMore, scrollOffset = 1, items} = props;
|
|
37
|
+
|
|
38
|
+
// Handle scrolling, and call onLoadMore when nearing the bottom.
|
|
39
|
+
let isLoadingRef = useRef(isLoading);
|
|
40
|
+
let prevProps = useRef(props);
|
|
41
|
+
let onScroll = useCallback(() => {
|
|
42
|
+
if (ref.current && !isLoadingRef.current && onLoadMore) {
|
|
43
|
+
let shouldLoadMore = ref.current.scrollHeight - ref.current.scrollTop - ref.current.clientHeight < ref.current.clientHeight * scrollOffset;
|
|
44
|
+
|
|
45
|
+
if (shouldLoadMore) {
|
|
46
|
+
isLoadingRef.current = true;
|
|
47
|
+
onLoadMore();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}, [onLoadMore, ref, scrollOffset]);
|
|
51
|
+
|
|
52
|
+
let lastItems = useRef(items);
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
// Only update isLoadingRef if props object actually changed,
|
|
55
|
+
// not if a local state change occurred.
|
|
56
|
+
if (props !== prevProps.current) {
|
|
57
|
+
isLoadingRef.current = isLoading;
|
|
58
|
+
prevProps.current = props;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// TODO: Eventually this hook will move back into RAC during which we will accept the collection as a option to this hook.
|
|
62
|
+
// We will only load more if the collection has changed after the last load to prevent multiple onLoadMore from being called
|
|
63
|
+
// while the data from the last onLoadMore is being processed by RAC collection.
|
|
64
|
+
let shouldLoadMore = ref?.current
|
|
65
|
+
&& !isLoadingRef.current
|
|
66
|
+
&& onLoadMore
|
|
67
|
+
&& (!items || items !== lastItems.current)
|
|
68
|
+
&& ref.current.clientHeight === ref.current.scrollHeight;
|
|
69
|
+
|
|
70
|
+
if (shouldLoadMore) {
|
|
71
|
+
isLoadingRef.current = true;
|
|
72
|
+
onLoadMore?.();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
lastItems.current = items;
|
|
76
|
+
}, [isLoading, onLoadMore, props, ref]);
|
|
77
|
+
|
|
78
|
+
// TODO: maybe this should still just return scroll props?
|
|
79
|
+
// Test against case where the ref isn't defined when this is called
|
|
80
|
+
// Think this was a problem when trying to attach to the scrollable body of the table in OnLoadMoreTableBodyScroll
|
|
81
|
+
useEvent(ref, 'scroll', onScroll);
|
|
82
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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 {MutableRefObject, useMemo, useRef} from 'react';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Offers an object ref for a given callback ref or an object ref. Especially
|
|
17
|
+
* helfpul when passing forwarded refs (created using `React.forwardRef`) to
|
|
18
|
+
* React Aria hooks.
|
|
19
|
+
*
|
|
20
|
+
* @param forwardedRef The original ref intended to be used.
|
|
21
|
+
* @returns An object ref that updates the given ref.
|
|
22
|
+
* @see https://reactjs.org/docs/forwarding-refs.html
|
|
23
|
+
*/
|
|
24
|
+
export function useObjectRef<T>(forwardedRef?: ((instance: T | null) => void) | MutableRefObject<T | null> | null): MutableRefObject<T | null> {
|
|
25
|
+
const objRef: MutableRefObject<T | null> = useRef<T>(null);
|
|
26
|
+
return useMemo(() => ({
|
|
27
|
+
get current() {
|
|
28
|
+
return objRef.current;
|
|
29
|
+
},
|
|
30
|
+
set current(value) {
|
|
31
|
+
objRef.current = value;
|
|
32
|
+
if (typeof forwardedRef === 'function') {
|
|
33
|
+
forwardedRef(value);
|
|
34
|
+
} else if (forwardedRef) {
|
|
35
|
+
forwardedRef.current = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}), [forwardedRef]);
|
|
39
|
+
}
|