@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.
Files changed (202) hide show
  1. package/README.md +3 -0
  2. package/dist/chain.main.js +26 -0
  3. package/dist/chain.main.js.map +1 -0
  4. package/dist/chain.mjs +21 -0
  5. package/dist/chain.module.js +21 -0
  6. package/dist/chain.module.js.map +1 -0
  7. package/dist/domHelpers.main.js +19 -0
  8. package/dist/domHelpers.main.js.map +1 -0
  9. package/dist/domHelpers.mjs +13 -0
  10. package/dist/domHelpers.module.js +13 -0
  11. package/dist/domHelpers.module.js.map +1 -0
  12. package/dist/filterDOMProps.main.js +45 -0
  13. package/dist/filterDOMProps.main.js.map +1 -0
  14. package/dist/filterDOMProps.mjs +40 -0
  15. package/dist/filterDOMProps.module.js +40 -0
  16. package/dist/filterDOMProps.module.js.map +1 -0
  17. package/dist/focusWithoutScrolling.main.js +72 -0
  18. package/dist/focusWithoutScrolling.main.js.map +1 -0
  19. package/dist/focusWithoutScrolling.mjs +67 -0
  20. package/dist/focusWithoutScrolling.module.js +67 -0
  21. package/dist/focusWithoutScrolling.module.js.map +1 -0
  22. package/dist/getOffset.main.js +24 -0
  23. package/dist/getOffset.main.js.map +1 -0
  24. package/dist/getOffset.mjs +19 -0
  25. package/dist/getOffset.module.js +19 -0
  26. package/dist/getOffset.module.js.map +1 -0
  27. package/dist/getScrollParent.main.js +28 -0
  28. package/dist/getScrollParent.main.js.map +1 -0
  29. package/dist/getScrollParent.mjs +23 -0
  30. package/dist/getScrollParent.module.js +23 -0
  31. package/dist/getScrollParent.module.js.map +1 -0
  32. package/dist/getScrollParents.main.js +30 -0
  33. package/dist/getScrollParents.main.js.map +1 -0
  34. package/dist/getScrollParents.mjs +25 -0
  35. package/dist/getScrollParents.module.js +25 -0
  36. package/dist/getScrollParents.module.js.map +1 -0
  37. package/dist/import.mjs +81 -0
  38. package/dist/isScrollable.main.js +25 -0
  39. package/dist/isScrollable.main.js.map +1 -0
  40. package/dist/isScrollable.mjs +20 -0
  41. package/dist/isScrollable.module.js +20 -0
  42. package/dist/isScrollable.module.js.map +1 -0
  43. package/dist/isVirtualEvent.main.js +41 -0
  44. package/dist/isVirtualEvent.main.js.map +1 -0
  45. package/dist/isVirtualEvent.mjs +35 -0
  46. package/dist/isVirtualEvent.module.js +35 -0
  47. package/dist/isVirtualEvent.module.js.map +1 -0
  48. package/dist/main.js +138 -0
  49. package/dist/main.js.map +1 -0
  50. package/dist/mergeProps.main.js +51 -0
  51. package/dist/mergeProps.main.js.map +1 -0
  52. package/dist/mergeProps.mjs +42 -0
  53. package/dist/mergeProps.module.js +42 -0
  54. package/dist/mergeProps.module.js.map +1 -0
  55. package/dist/mergeRefs.main.js +28 -0
  56. package/dist/mergeRefs.main.js.map +1 -0
  57. package/dist/mergeRefs.mjs +23 -0
  58. package/dist/mergeRefs.module.js +23 -0
  59. package/dist/mergeRefs.module.js.map +1 -0
  60. package/dist/module.js +81 -0
  61. package/dist/module.js.map +1 -0
  62. package/dist/openLink.main.js +155 -0
  63. package/dist/openLink.main.js.map +1 -0
  64. package/dist/openLink.mjs +140 -0
  65. package/dist/openLink.module.js +140 -0
  66. package/dist/openLink.module.js.map +1 -0
  67. package/dist/platform.main.js +71 -0
  68. package/dist/platform.main.js.map +1 -0
  69. package/dist/platform.mjs +58 -0
  70. package/dist/platform.module.js +58 -0
  71. package/dist/platform.module.js.map +1 -0
  72. package/dist/runAfterTransition.main.js +82 -0
  73. package/dist/runAfterTransition.main.js.map +1 -0
  74. package/dist/runAfterTransition.mjs +77 -0
  75. package/dist/runAfterTransition.module.js +77 -0
  76. package/dist/runAfterTransition.module.js.map +1 -0
  77. package/dist/scrollIntoView.main.js +96 -0
  78. package/dist/scrollIntoView.main.js.map +1 -0
  79. package/dist/scrollIntoView.mjs +90 -0
  80. package/dist/scrollIntoView.module.js +90 -0
  81. package/dist/scrollIntoView.module.js.map +1 -0
  82. package/dist/types.d.ts +229 -0
  83. package/dist/types.d.ts.map +1 -0
  84. package/dist/useDeepMemo.main.js +30 -0
  85. package/dist/useDeepMemo.main.js.map +1 -0
  86. package/dist/useDeepMemo.mjs +25 -0
  87. package/dist/useDeepMemo.module.js +25 -0
  88. package/dist/useDeepMemo.module.js.map +1 -0
  89. package/dist/useDescription.main.js +59 -0
  90. package/dist/useDescription.main.js.map +1 -0
  91. package/dist/useDescription.mjs +54 -0
  92. package/dist/useDescription.module.js +54 -0
  93. package/dist/useDescription.module.js.map +1 -0
  94. package/dist/useDrag1D.main.js +139 -0
  95. package/dist/useDrag1D.main.js.map +1 -0
  96. package/dist/useDrag1D.mjs +134 -0
  97. package/dist/useDrag1D.module.js +134 -0
  98. package/dist/useDrag1D.module.js.map +1 -0
  99. package/dist/useEffectEvent.main.js +37 -0
  100. package/dist/useEffectEvent.main.js.map +1 -0
  101. package/dist/useEffectEvent.mjs +32 -0
  102. package/dist/useEffectEvent.module.js +32 -0
  103. package/dist/useEffectEvent.module.js.map +1 -0
  104. package/dist/useEvent.main.js +42 -0
  105. package/dist/useEvent.main.js.map +1 -0
  106. package/dist/useEvent.mjs +37 -0
  107. package/dist/useEvent.module.js +37 -0
  108. package/dist/useEvent.module.js.map +1 -0
  109. package/dist/useFormReset.main.js +41 -0
  110. package/dist/useFormReset.main.js.map +1 -0
  111. package/dist/useFormReset.mjs +36 -0
  112. package/dist/useFormReset.module.js +36 -0
  113. package/dist/useFormReset.module.js.map +1 -0
  114. package/dist/useGlobalListeners.main.js +63 -0
  115. package/dist/useGlobalListeners.main.js.map +1 -0
  116. package/dist/useGlobalListeners.mjs +58 -0
  117. package/dist/useGlobalListeners.module.js +58 -0
  118. package/dist/useGlobalListeners.module.js.map +1 -0
  119. package/dist/useId.main.js +93 -0
  120. package/dist/useId.main.js.map +1 -0
  121. package/dist/useId.mjs +86 -0
  122. package/dist/useId.module.js +86 -0
  123. package/dist/useId.module.js.map +1 -0
  124. package/dist/useLabels.main.js +44 -0
  125. package/dist/useLabels.main.js.map +1 -0
  126. package/dist/useLabels.mjs +39 -0
  127. package/dist/useLabels.module.js +39 -0
  128. package/dist/useLabels.module.js.map +1 -0
  129. package/dist/useLayoutEffect.main.js +27 -0
  130. package/dist/useLayoutEffect.main.js.map +1 -0
  131. package/dist/useLayoutEffect.mjs +18 -0
  132. package/dist/useLayoutEffect.module.js +18 -0
  133. package/dist/useLayoutEffect.module.js.map +1 -0
  134. package/dist/useLoadMore.main.js +72 -0
  135. package/dist/useLoadMore.main.js.map +1 -0
  136. package/dist/useLoadMore.mjs +67 -0
  137. package/dist/useLoadMore.module.js +67 -0
  138. package/dist/useLoadMore.module.js.map +1 -0
  139. package/dist/useObjectRef.main.js +37 -0
  140. package/dist/useObjectRef.main.js.map +1 -0
  141. package/dist/useObjectRef.mjs +32 -0
  142. package/dist/useObjectRef.module.js +32 -0
  143. package/dist/useObjectRef.module.js.map +1 -0
  144. package/dist/useResizeObserver.main.js +43 -0
  145. package/dist/useResizeObserver.main.js.map +1 -0
  146. package/dist/useResizeObserver.mjs +38 -0
  147. package/dist/useResizeObserver.module.js +38 -0
  148. package/dist/useResizeObserver.module.js.map +1 -0
  149. package/dist/useSyncRef.main.js +32 -0
  150. package/dist/useSyncRef.main.js.map +1 -0
  151. package/dist/useSyncRef.mjs +27 -0
  152. package/dist/useSyncRef.module.js +27 -0
  153. package/dist/useSyncRef.module.js.map +1 -0
  154. package/dist/useUpdateEffect.main.js +38 -0
  155. package/dist/useUpdateEffect.main.js.map +1 -0
  156. package/dist/useUpdateEffect.mjs +33 -0
  157. package/dist/useUpdateEffect.module.js +33 -0
  158. package/dist/useUpdateEffect.module.js.map +1 -0
  159. package/dist/useValueEffect.main.js +58 -0
  160. package/dist/useValueEffect.main.js.map +1 -0
  161. package/dist/useValueEffect.mjs +53 -0
  162. package/dist/useValueEffect.module.js +53 -0
  163. package/dist/useValueEffect.module.js.map +1 -0
  164. package/dist/useViewportSize.main.js +56 -0
  165. package/dist/useViewportSize.main.js.map +1 -0
  166. package/dist/useViewportSize.mjs +51 -0
  167. package/dist/useViewportSize.module.js +51 -0
  168. package/dist/useViewportSize.module.js.map +1 -0
  169. package/package.json +38 -0
  170. package/src/chain.ts +24 -0
  171. package/src/domHelpers.ts +14 -0
  172. package/src/filterDOMProps.ts +76 -0
  173. package/src/focusWithoutScrolling.ts +96 -0
  174. package/src/getOffset.ts +19 -0
  175. package/src/getScrollParent.ts +27 -0
  176. package/src/getScrollParents.ts +26 -0
  177. package/src/index.ts +44 -0
  178. package/src/isScrollable.ts +22 -0
  179. package/src/isVirtualEvent.ts +58 -0
  180. package/src/mergeProps.ts +75 -0
  181. package/src/mergeRefs.ts +32 -0
  182. package/src/openLink.tsx +183 -0
  183. package/src/platform.ts +79 -0
  184. package/src/runAfterTransition.ts +105 -0
  185. package/src/scrollIntoView.ts +109 -0
  186. package/src/useDeepMemo.ts +27 -0
  187. package/src/useDescription.ts +56 -0
  188. package/src/useDrag1D.ts +190 -0
  189. package/src/useEffectEvent.ts +26 -0
  190. package/src/useEvent.ts +37 -0
  191. package/src/useFormReset.ts +36 -0
  192. package/src/useGlobalListeners.ts +51 -0
  193. package/src/useId.ts +108 -0
  194. package/src/useLabels.ts +48 -0
  195. package/src/useLayoutEffect.ts +20 -0
  196. package/src/useLoadMore.ts +82 -0
  197. package/src/useObjectRef.ts +39 -0
  198. package/src/useResizeObserver.ts +48 -0
  199. package/src/useSyncRef.ts +33 -0
  200. package/src/useUpdateEffect.ts +36 -0
  201. package/src/useValueEffect.ts +64 -0
  202. package/src/useViewportSize.ts +63 -0
@@ -0,0 +1,48 @@
1
+
2
+ import {RefObject} from '@react-types/shared';
3
+ import {useEffect} from 'react';
4
+
5
+ function hasResizeObserver() {
6
+ return typeof window.ResizeObserver !== 'undefined';
7
+ }
8
+
9
+ type useResizeObserverOptionsType<T> = {
10
+ ref: RefObject<T | undefined | null> | undefined,
11
+ box?: ResizeObserverBoxOptions,
12
+ onResize: () => void
13
+ }
14
+
15
+ export function useResizeObserver<T extends Element>(options: useResizeObserverOptionsType<T>) {
16
+ const {ref, box, onResize} = options;
17
+
18
+ useEffect(() => {
19
+ let element = ref?.current;
20
+ if (!element) {
21
+ return;
22
+ }
23
+
24
+ if (!hasResizeObserver()) {
25
+ window.addEventListener('resize', onResize, false);
26
+ return () => {
27
+ window.removeEventListener('resize', onResize, false);
28
+ };
29
+ } else {
30
+
31
+ const resizeObserverInstance = new window.ResizeObserver((entries) => {
32
+ if (!entries.length) {
33
+ return;
34
+ }
35
+
36
+ onResize();
37
+ });
38
+ resizeObserverInstance.observe(element, {box});
39
+
40
+ return () => {
41
+ if (element) {
42
+ resizeObserverInstance.unobserve(element);
43
+ }
44
+ };
45
+ }
46
+
47
+ }, [onResize, ref, box]);
48
+ }
@@ -0,0 +1,33 @@
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 {MutableRefObject} from 'react';
14
+ import {RefObject} from '@react-types/shared';
15
+ import {useLayoutEffect} from './';
16
+
17
+ interface ContextValue<T> {
18
+ ref?: MutableRefObject<T | null>
19
+ }
20
+
21
+ // Syncs ref from context with ref passed to hook
22
+ export function useSyncRef<T>(context?: ContextValue<T> | null, ref?: RefObject<T | null>) {
23
+ useLayoutEffect(() => {
24
+ if (context && context.ref && ref) {
25
+ context.ref.current = ref.current;
26
+ return () => {
27
+ if (context.ref) {
28
+ context.ref.current = null;
29
+ }
30
+ };
31
+ }
32
+ });
33
+ }
@@ -0,0 +1,36 @@
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 {EffectCallback, useEffect, useRef} from 'react';
14
+
15
+ // Like useEffect, but only called for updates after the initial render.
16
+ export function useUpdateEffect(effect: EffectCallback, dependencies: any[]) {
17
+ const isInitialMount = useRef(true);
18
+ const lastDeps = useRef<any[] | null>(null);
19
+
20
+ useEffect(() => {
21
+ isInitialMount.current = true;
22
+ return () => {
23
+ isInitialMount.current = false;
24
+ };
25
+ }, []);
26
+
27
+ useEffect(() => {
28
+ if (isInitialMount.current) {
29
+ isInitialMount.current = false;
30
+ } else if (!lastDeps.current || dependencies.some((dep, i) => !Object.is(dep, lastDeps[i]))) {
31
+ effect();
32
+ }
33
+ lastDeps.current = dependencies;
34
+ // eslint-disable-next-line react-hooks/exhaustive-deps
35
+ }, dependencies);
36
+ }
@@ -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 {Dispatch, MutableRefObject, useRef, useState} from 'react';
14
+ import {useEffectEvent, useLayoutEffect} from './';
15
+
16
+ type SetValueAction<S> = (prev: S) => Generator<any, void, unknown>;
17
+
18
+ // This hook works like `useState`, but when setting the value, you pass a generator function
19
+ // that can yield multiple values. Each yielded value updates the state and waits for the next
20
+ // layout effect, then continues the generator. This allows sequential updates to state to be
21
+ // written linearly.
22
+ export function useValueEffect<S>(defaultValue: S | (() => S)): [S, Dispatch<SetValueAction<S>>] {
23
+ let [value, setValue] = useState(defaultValue);
24
+ let effect: MutableRefObject<Generator<S> | null> = useRef<Generator<S> | null>(null);
25
+
26
+ // Store the function in a ref so we can always access the current version
27
+ // which has the proper `value` in scope.
28
+ let nextRef = useEffectEvent(() => {
29
+ if (!effect.current) {
30
+ return;
31
+ }
32
+ // Run the generator to the next yield.
33
+ let newValue = effect.current.next();
34
+
35
+ // If the generator is done, reset the effect.
36
+ if (newValue.done) {
37
+ effect.current = null;
38
+ return;
39
+ }
40
+
41
+ // If the value is the same as the current value,
42
+ // then continue to the next yield. Otherwise,
43
+ // set the value in state and wait for the next layout effect.
44
+ if (value === newValue.value) {
45
+ nextRef();
46
+ } else {
47
+ setValue(newValue.value);
48
+ }
49
+ });
50
+
51
+ useLayoutEffect(() => {
52
+ // If there is an effect currently running, continue to the next yield.
53
+ if (effect.current) {
54
+ nextRef();
55
+ }
56
+ });
57
+
58
+ let queue = useEffectEvent(fn => {
59
+ effect.current = fn(value);
60
+ nextRef();
61
+ });
62
+
63
+ return [value, queue];
64
+ }
@@ -0,0 +1,63 @@
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 {useEffect, useState} from 'react';
14
+ import {useIsSSR} from '@react-aria/ssr';
15
+
16
+ interface ViewportSize {
17
+ width: number,
18
+ height: number
19
+ }
20
+
21
+ // @ts-ignore
22
+ let visualViewport = typeof document !== 'undefined' && window.visualViewport;
23
+
24
+ export function useViewportSize(): ViewportSize {
25
+ let isSSR = useIsSSR();
26
+ let [size, setSize] = useState(() => isSSR ? {width: 0, height: 0} : getViewportSize());
27
+
28
+ useEffect(() => {
29
+ // Use visualViewport api to track available height even on iOS virtual keyboard opening
30
+ let onResize = () => {
31
+ setSize(size => {
32
+ let newSize = getViewportSize();
33
+ if (newSize.width === size.width && newSize.height === size.height) {
34
+ return size;
35
+ }
36
+ return newSize;
37
+ });
38
+ };
39
+
40
+ if (!visualViewport) {
41
+ window.addEventListener('resize', onResize);
42
+ } else {
43
+ visualViewport.addEventListener('resize', onResize);
44
+ }
45
+
46
+ return () => {
47
+ if (!visualViewport) {
48
+ window.removeEventListener('resize', onResize);
49
+ } else {
50
+ visualViewport.removeEventListener('resize', onResize);
51
+ }
52
+ };
53
+ }, []);
54
+
55
+ return size;
56
+ }
57
+
58
+ function getViewportSize(): ViewportSize {
59
+ return {
60
+ width: (visualViewport && visualViewport?.width) || window.innerWidth,
61
+ height: (visualViewport && visualViewport?.height) || window.innerHeight
62
+ };
63
+ }