@react-aria/utils 3.33.1 → 3.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/dist/import.mjs +47 -43
  2. package/dist/main.js +120 -115
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +47 -43
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +45 -0
  7. package/package.json +15 -15
  8. package/src/index.ts +45 -43
  9. package/dist/DOMFunctions.main.js +0 -57
  10. package/dist/DOMFunctions.main.js.map +0 -1
  11. package/dist/DOMFunctions.mjs +0 -49
  12. package/dist/DOMFunctions.module.js +0 -49
  13. package/dist/DOMFunctions.module.js.map +0 -1
  14. package/dist/ShadowTreeWalker.main.js +0 -200
  15. package/dist/ShadowTreeWalker.main.js.map +0 -1
  16. package/dist/ShadowTreeWalker.mjs +0 -194
  17. package/dist/ShadowTreeWalker.module.js +0 -194
  18. package/dist/ShadowTreeWalker.module.js.map +0 -1
  19. package/dist/animation.main.js +0 -97
  20. package/dist/animation.main.js.map +0 -1
  21. package/dist/animation.mjs +0 -91
  22. package/dist/animation.module.js +0 -91
  23. package/dist/animation.module.js.map +0 -1
  24. package/dist/chain.main.js +0 -26
  25. package/dist/chain.main.js.map +0 -1
  26. package/dist/chain.mjs +0 -21
  27. package/dist/chain.module.js +0 -21
  28. package/dist/chain.module.js.map +0 -1
  29. package/dist/constants.main.js +0 -23
  30. package/dist/constants.main.js.map +0 -1
  31. package/dist/constants.mjs +0 -17
  32. package/dist/constants.module.js +0 -17
  33. package/dist/constants.module.js.map +0 -1
  34. package/dist/domHelpers.main.js +0 -28
  35. package/dist/domHelpers.main.js.map +0 -1
  36. package/dist/domHelpers.mjs +0 -21
  37. package/dist/domHelpers.module.js +0 -21
  38. package/dist/domHelpers.module.js.map +0 -1
  39. package/dist/filterDOMProps.main.js +0 -88
  40. package/dist/filterDOMProps.main.js.map +0 -1
  41. package/dist/filterDOMProps.mjs +0 -83
  42. package/dist/filterDOMProps.module.js +0 -83
  43. package/dist/filterDOMProps.module.js.map +0 -1
  44. package/dist/focusWithoutScrolling.main.js +0 -72
  45. package/dist/focusWithoutScrolling.main.js.map +0 -1
  46. package/dist/focusWithoutScrolling.mjs +0 -67
  47. package/dist/focusWithoutScrolling.module.js +0 -67
  48. package/dist/focusWithoutScrolling.module.js.map +0 -1
  49. package/dist/getOffset.main.js +0 -24
  50. package/dist/getOffset.main.js.map +0 -1
  51. package/dist/getOffset.mjs +0 -19
  52. package/dist/getOffset.module.js +0 -19
  53. package/dist/getOffset.module.js.map +0 -1
  54. package/dist/getScrollParent.main.js +0 -28
  55. package/dist/getScrollParent.main.js.map +0 -1
  56. package/dist/getScrollParent.mjs +0 -23
  57. package/dist/getScrollParent.module.js +0 -23
  58. package/dist/getScrollParent.module.js.map +0 -1
  59. package/dist/getScrollParents.main.js +0 -31
  60. package/dist/getScrollParents.main.js.map +0 -1
  61. package/dist/getScrollParents.mjs +0 -26
  62. package/dist/getScrollParents.module.js +0 -26
  63. package/dist/getScrollParents.module.js.map +0 -1
  64. package/dist/inertValue.main.js +0 -19
  65. package/dist/inertValue.main.js.map +0 -1
  66. package/dist/inertValue.mjs +0 -14
  67. package/dist/inertValue.module.js +0 -14
  68. package/dist/inertValue.module.js.map +0 -1
  69. package/dist/isElementVisible.main.js +0 -45
  70. package/dist/isElementVisible.main.js.map +0 -1
  71. package/dist/isElementVisible.mjs +0 -40
  72. package/dist/isElementVisible.module.js +0 -40
  73. package/dist/isElementVisible.module.js.map +0 -1
  74. package/dist/isFocusable.main.js +0 -56
  75. package/dist/isFocusable.main.js.map +0 -1
  76. package/dist/isFocusable.mjs +0 -50
  77. package/dist/isFocusable.module.js +0 -50
  78. package/dist/isFocusable.module.js.map +0 -1
  79. package/dist/isScrollable.main.js +0 -29
  80. package/dist/isScrollable.main.js.map +0 -1
  81. package/dist/isScrollable.mjs +0 -24
  82. package/dist/isScrollable.module.js +0 -24
  83. package/dist/isScrollable.module.js.map +0 -1
  84. package/dist/isVirtualEvent.main.js +0 -41
  85. package/dist/isVirtualEvent.main.js.map +0 -1
  86. package/dist/isVirtualEvent.mjs +0 -35
  87. package/dist/isVirtualEvent.module.js +0 -35
  88. package/dist/isVirtualEvent.module.js.map +0 -1
  89. package/dist/keyboard.main.js +0 -42
  90. package/dist/keyboard.main.js.map +0 -1
  91. package/dist/keyboard.mjs +0 -36
  92. package/dist/keyboard.module.js +0 -36
  93. package/dist/keyboard.module.js.map +0 -1
  94. package/dist/mergeProps.main.js +0 -54
  95. package/dist/mergeProps.main.js.map +0 -1
  96. package/dist/mergeProps.mjs +0 -45
  97. package/dist/mergeProps.module.js +0 -45
  98. package/dist/mergeProps.module.js.map +0 -1
  99. package/dist/mergeRefs.main.js +0 -40
  100. package/dist/mergeRefs.main.js.map +0 -1
  101. package/dist/mergeRefs.mjs +0 -35
  102. package/dist/mergeRefs.module.js +0 -35
  103. package/dist/mergeRefs.module.js.map +0 -1
  104. package/dist/openLink.main.js +0 -169
  105. package/dist/openLink.main.js.map +0 -1
  106. package/dist/openLink.mjs +0 -153
  107. package/dist/openLink.module.js +0 -153
  108. package/dist/openLink.module.js.map +0 -1
  109. package/dist/platform.main.js +0 -73
  110. package/dist/platform.main.js.map +0 -1
  111. package/dist/platform.mjs +0 -60
  112. package/dist/platform.module.js +0 -60
  113. package/dist/platform.module.js.map +0 -1
  114. package/dist/runAfterTransition.main.js +0 -97
  115. package/dist/runAfterTransition.main.js.map +0 -1
  116. package/dist/runAfterTransition.mjs +0 -92
  117. package/dist/runAfterTransition.module.js +0 -92
  118. package/dist/runAfterTransition.module.js.map +0 -1
  119. package/dist/scrollIntoView.main.js +0 -138
  120. package/dist/scrollIntoView.main.js.map +0 -1
  121. package/dist/scrollIntoView.mjs +0 -132
  122. package/dist/scrollIntoView.module.js +0 -132
  123. package/dist/scrollIntoView.module.js.map +0 -1
  124. package/dist/types.d.ts +0 -299
  125. package/dist/types.d.ts.map +0 -1
  126. package/dist/useDeepMemo.main.js +0 -30
  127. package/dist/useDeepMemo.main.js.map +0 -1
  128. package/dist/useDeepMemo.mjs +0 -25
  129. package/dist/useDeepMemo.module.js +0 -25
  130. package/dist/useDeepMemo.module.js.map +0 -1
  131. package/dist/useDescription.main.js +0 -59
  132. package/dist/useDescription.main.js.map +0 -1
  133. package/dist/useDescription.mjs +0 -54
  134. package/dist/useDescription.module.js +0 -54
  135. package/dist/useDescription.module.js.map +0 -1
  136. package/dist/useDrag1D.main.js +0 -141
  137. package/dist/useDrag1D.main.js.map +0 -1
  138. package/dist/useDrag1D.mjs +0 -136
  139. package/dist/useDrag1D.module.js +0 -136
  140. package/dist/useDrag1D.module.js.map +0 -1
  141. package/dist/useEffectEvent.main.js +0 -45
  142. package/dist/useEffectEvent.main.js.map +0 -1
  143. package/dist/useEffectEvent.mjs +0 -36
  144. package/dist/useEffectEvent.module.js +0 -36
  145. package/dist/useEffectEvent.module.js.map +0 -1
  146. package/dist/useEvent.main.js +0 -41
  147. package/dist/useEvent.main.js.map +0 -1
  148. package/dist/useEvent.mjs +0 -36
  149. package/dist/useEvent.module.js +0 -36
  150. package/dist/useEvent.module.js.map +0 -1
  151. package/dist/useFormReset.main.js +0 -39
  152. package/dist/useFormReset.main.js.map +0 -1
  153. package/dist/useFormReset.mjs +0 -34
  154. package/dist/useFormReset.module.js +0 -34
  155. package/dist/useFormReset.module.js.map +0 -1
  156. package/dist/useGlobalListeners.main.js +0 -62
  157. package/dist/useGlobalListeners.main.js.map +0 -1
  158. package/dist/useGlobalListeners.mjs +0 -57
  159. package/dist/useGlobalListeners.module.js +0 -57
  160. package/dist/useGlobalListeners.module.js.map +0 -1
  161. package/dist/useId.main.js +0 -108
  162. package/dist/useId.main.js.map +0 -1
  163. package/dist/useId.mjs +0 -101
  164. package/dist/useId.module.js +0 -101
  165. package/dist/useId.module.js.map +0 -1
  166. package/dist/useLabels.main.js +0 -44
  167. package/dist/useLabels.main.js.map +0 -1
  168. package/dist/useLabels.mjs +0 -39
  169. package/dist/useLabels.module.js +0 -39
  170. package/dist/useLabels.module.js.map +0 -1
  171. package/dist/useLayoutEffect.main.js +0 -27
  172. package/dist/useLayoutEffect.main.js.map +0 -1
  173. package/dist/useLayoutEffect.mjs +0 -18
  174. package/dist/useLayoutEffect.module.js +0 -18
  175. package/dist/useLayoutEffect.module.js.map +0 -1
  176. package/dist/useLoadMore.main.js +0 -73
  177. package/dist/useLoadMore.main.js.map +0 -1
  178. package/dist/useLoadMore.mjs +0 -68
  179. package/dist/useLoadMore.module.js +0 -68
  180. package/dist/useLoadMore.module.js.map +0 -1
  181. package/dist/useLoadMoreSentinel.main.js +0 -58
  182. package/dist/useLoadMoreSentinel.main.js.map +0 -1
  183. package/dist/useLoadMoreSentinel.mjs +0 -53
  184. package/dist/useLoadMoreSentinel.module.js +0 -53
  185. package/dist/useLoadMoreSentinel.module.js.map +0 -1
  186. package/dist/useObjectRef.main.js +0 -58
  187. package/dist/useObjectRef.main.js.map +0 -1
  188. package/dist/useObjectRef.mjs +0 -53
  189. package/dist/useObjectRef.module.js +0 -53
  190. package/dist/useObjectRef.module.js.map +0 -1
  191. package/dist/useResizeObserver.main.js +0 -47
  192. package/dist/useResizeObserver.main.js.map +0 -1
  193. package/dist/useResizeObserver.mjs +0 -42
  194. package/dist/useResizeObserver.module.js +0 -42
  195. package/dist/useResizeObserver.module.js.map +0 -1
  196. package/dist/useSyncRef.main.js +0 -32
  197. package/dist/useSyncRef.main.js.map +0 -1
  198. package/dist/useSyncRef.mjs +0 -27
  199. package/dist/useSyncRef.module.js +0 -27
  200. package/dist/useSyncRef.module.js.map +0 -1
  201. package/dist/useUpdateEffect.main.js +0 -42
  202. package/dist/useUpdateEffect.main.js.map +0 -1
  203. package/dist/useUpdateEffect.mjs +0 -37
  204. package/dist/useUpdateEffect.module.js +0 -37
  205. package/dist/useUpdateEffect.module.js.map +0 -1
  206. package/dist/useUpdateLayoutEffect.main.js +0 -40
  207. package/dist/useUpdateLayoutEffect.main.js.map +0 -1
  208. package/dist/useUpdateLayoutEffect.mjs +0 -35
  209. package/dist/useUpdateLayoutEffect.module.js +0 -35
  210. package/dist/useUpdateLayoutEffect.module.js.map +0 -1
  211. package/dist/useValueEffect.main.js +0 -63
  212. package/dist/useValueEffect.main.js.map +0 -1
  213. package/dist/useValueEffect.mjs +0 -58
  214. package/dist/useValueEffect.module.js +0 -58
  215. package/dist/useValueEffect.module.js.map +0 -1
  216. package/dist/useViewportSize.main.js +0 -84
  217. package/dist/useViewportSize.main.js.map +0 -1
  218. package/dist/useViewportSize.mjs +0 -79
  219. package/dist/useViewportSize.module.js +0 -79
  220. package/dist/useViewportSize.module.js.map +0 -1
  221. package/src/animation.ts +0 -103
  222. package/src/chain.ts +0 -24
  223. package/src/constants.ts +0 -15
  224. package/src/domHelpers.ts +0 -33
  225. package/src/filterDOMProps.ts +0 -127
  226. package/src/focusWithoutScrolling.ts +0 -96
  227. package/src/getOffset.ts +0 -21
  228. package/src/getScrollParent.ts +0 -27
  229. package/src/getScrollParents.ts +0 -27
  230. package/src/inertValue.ts +0 -11
  231. package/src/isElementVisible.ts +0 -75
  232. package/src/isFocusable.ts +0 -56
  233. package/src/isScrollable.ts +0 -31
  234. package/src/isVirtualEvent.ts +0 -58
  235. package/src/keyboard.tsx +0 -48
  236. package/src/mergeProps.ts +0 -77
  237. package/src/mergeRefs.ts +0 -52
  238. package/src/openLink.tsx +0 -201
  239. package/src/platform.ts +0 -78
  240. package/src/runAfterTransition.ts +0 -124
  241. package/src/scrollIntoView.ts +0 -168
  242. package/src/shadowdom/DOMFunctions.ts +0 -100
  243. package/src/shadowdom/ShadowTreeWalker.ts +0 -319
  244. package/src/useDeepMemo.ts +0 -27
  245. package/src/useDescription.ts +0 -56
  246. package/src/useDrag1D.ts +0 -191
  247. package/src/useEffectEvent.ts +0 -30
  248. package/src/useEvent.ts +0 -37
  249. package/src/useFormReset.ts +0 -36
  250. package/src/useGlobalListeners.ts +0 -52
  251. package/src/useId.ts +0 -129
  252. package/src/useLabels.ts +0 -48
  253. package/src/useLayoutEffect.ts +0 -20
  254. package/src/useLoadMore.ts +0 -82
  255. package/src/useLoadMoreSentinel.ts +0 -63
  256. package/src/useObjectRef.ts +0 -69
  257. package/src/useResizeObserver.ts +0 -52
  258. package/src/useSyncRef.ts +0 -33
  259. package/src/useUpdateEffect.ts +0 -39
  260. package/src/useUpdateLayoutEffect.ts +0 -37
  261. package/src/useValueEffect.ts +0 -68
  262. package/src/useViewportSize.ts +0 -103
@@ -1,30 +0,0 @@
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 React, {useCallback, useRef} from 'react';
14
- import {useLayoutEffect} from './useLayoutEffect';
15
-
16
- // Use the earliest effect type possible. useInsertionEffect runs during the mutation phase,
17
- // before all layout effects, but is available only in React 18 and later.
18
- const useEarlyEffect = React['useInsertionEffect'] ?? useLayoutEffect;
19
-
20
- export function useEffectEvent<T extends Function>(fn?: T): T {
21
- const ref = useRef<T | null | undefined>(null);
22
- useEarlyEffect(() => {
23
- ref.current = fn;
24
- }, [fn]);
25
- // @ts-ignore
26
- return useCallback<T>((...args) => {
27
- const f = ref.current!;
28
- return f?.(...args);
29
- }, []);
30
- }
package/src/useEvent.ts DELETED
@@ -1,37 +0,0 @@
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
- ): void {
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]);
37
- }
@@ -1,36 +0,0 @@
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} from 'react';
15
- import {useEffectEvent} from './useEffectEvent';
16
-
17
- export function useFormReset<T>(
18
- ref: RefObject<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null> | undefined,
19
- initialValue: T,
20
- onReset: (value: T) => void
21
- ): void {
22
- let handleReset = useEffectEvent(() => {
23
- if (onReset) {
24
- onReset(initialValue);
25
- }
26
- });
27
-
28
- useEffect(() => {
29
- let form = ref?.current?.form;
30
-
31
- form?.addEventListener('reset', handleReset);
32
- return () => {
33
- form?.removeEventListener('reset', handleReset);
34
- };
35
- }, [ref]);
36
- }
@@ -1,52 +0,0 @@
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 WindowEventMap>(el: Window, type: K, listener: (this: Document, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void,
17
- addGlobalListener<K extends keyof DocumentEventMap>(el: EventTarget, type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void,
18
- addGlobalListener(el: EventTarget, type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void,
19
- removeGlobalListener<K extends keyof DocumentEventMap>(el: EventTarget, type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void,
20
- removeGlobalListener(el: EventTarget, type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void,
21
- removeAllGlobalListeners(): void
22
- }
23
-
24
- export function useGlobalListeners(): GlobalListeners {
25
- let globalListeners = useRef(new Map());
26
- let addGlobalListener = useCallback((eventTarget, type, listener, options) => {
27
- // Make sure we remove the listener after it is called with the `once` option.
28
- let fn = options?.once ? (...args) => {
29
- globalListeners.current.delete(listener);
30
- listener(...args);
31
- } : listener;
32
- globalListeners.current.set(listener, {type, eventTarget, fn, options});
33
- eventTarget.addEventListener(type, fn, options);
34
- }, []);
35
- let removeGlobalListener = useCallback((eventTarget, type, listener, options) => {
36
- let fn = globalListeners.current.get(listener)?.fn || listener;
37
- eventTarget.removeEventListener(type, fn, options);
38
- globalListeners.current.delete(listener);
39
- }, []);
40
- let removeAllGlobalListeners = useCallback(() => {
41
- globalListeners.current.forEach((value, key) => {
42
- removeGlobalListener(value.eventTarget, value.type, key, value.options);
43
- });
44
- }, [removeGlobalListener]);
45
-
46
-
47
- useEffect(() => {
48
- return removeAllGlobalListeners;
49
- }, [removeAllGlobalListeners]);
50
-
51
- return {addGlobalListener, removeGlobalListener, removeAllGlobalListeners};
52
- }
package/src/useId.ts DELETED
@@ -1,129 +0,0 @@
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
- export let idsUpdaterMap: Map<string, { current: string | null }[]> = new Map();
26
- // This allows us to clean up the idsUpdaterMap when the id is no longer used.
27
- // Map is a strong reference, so unused ids wouldn't be cleaned up otherwise.
28
- // This can happen in suspended components where mount/unmount is not called.
29
- let registry;
30
- if (typeof FinalizationRegistry !== 'undefined') {
31
- registry = new FinalizationRegistry<string>((heldValue) => {
32
- idsUpdaterMap.delete(heldValue);
33
- });
34
- }
35
-
36
- /**
37
- * If a default is not provided, generate an id.
38
- * @param defaultId - Default component id.
39
- */
40
- export function useId(defaultId?: string): string {
41
- let [value, setValue] = useState(defaultId);
42
- let nextId = useRef(null);
43
-
44
- let res = useSSRSafeId(value);
45
- let cleanupRef = useRef(null);
46
-
47
- if (registry) {
48
- registry.register(cleanupRef, res);
49
- }
50
-
51
- if (canUseDOM) {
52
- const cacheIdRef = idsUpdaterMap.get(res);
53
- if (cacheIdRef && !cacheIdRef.includes(nextId)) {
54
- cacheIdRef.push(nextId);
55
- } else {
56
- idsUpdaterMap.set(res, [nextId]);
57
- }
58
- }
59
-
60
- useLayoutEffect(() => {
61
- let r = res;
62
- return () => {
63
- // In Suspense, the cleanup function may be not called
64
- // when it is though, also remove it from the finalization registry.
65
- if (registry) {
66
- registry.unregister(cleanupRef);
67
- }
68
- idsUpdaterMap.delete(r);
69
- };
70
- }, [res]);
71
-
72
- // This cannot cause an infinite loop because the ref is always cleaned up.
73
- // eslint-disable-next-line
74
- useEffect(() => {
75
- let newId = nextId.current;
76
- if (newId) { setValue(newId); }
77
-
78
- return () => {
79
- if (newId) { nextId.current = null; }
80
- };
81
- });
82
-
83
- return res;
84
- }
85
-
86
- /**
87
- * Merges two ids.
88
- * Different ids will trigger a side-effect and re-render components hooked up with `useId`.
89
- */
90
- export function mergeIds(idA: string, idB: string): string {
91
- if (idA === idB) {
92
- return idA;
93
- }
94
-
95
- let setIdsA = idsUpdaterMap.get(idA);
96
- if (setIdsA) {
97
- setIdsA.forEach(ref => (ref.current = idB));
98
- return idB;
99
- }
100
-
101
- let setIdsB = idsUpdaterMap.get(idB);
102
- if (setIdsB) {
103
- setIdsB.forEach((ref) => (ref.current = idA));
104
- return idA;
105
- }
106
-
107
- return idB;
108
- }
109
-
110
- /**
111
- * Used to generate an id, and after render, check if that id is rendered so we know
112
- * if we can use it in places such as labelledby.
113
- * @param depArray - When to recalculate if the id is in the DOM.
114
- */
115
- export function useSlotId(depArray: ReadonlyArray<any> = []): string {
116
- let id = useId();
117
- let [resolvedId, setResolvedId] = useValueEffect(id);
118
- let updateId = useCallback(() => {
119
- setResolvedId(function *() {
120
- yield id;
121
-
122
- yield document.getElementById(id) ? id : undefined;
123
- });
124
- }, [id, setResolvedId]);
125
-
126
- useLayoutEffect(updateId, [id, updateId, ...depArray]);
127
-
128
- return resolvedId;
129
- }
package/src/useLabels.ts DELETED
@@ -1,48 +0,0 @@
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
- }
@@ -1,20 +0,0 @@
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 React.useLayoutEffect = typeof document !== 'undefined'
19
- ? React.useLayoutEffect
20
- : () => {};
@@ -1,82 +0,0 @@
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
-
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>): void {
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, items]);
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
- }
@@ -1,63 +0,0 @@
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 type {AsyncLoadable, Collection} from '@react-types/shared';
14
- import {getScrollParent} from './getScrollParent';
15
- import {RefObject, useRef} from 'react';
16
- import {useEffectEvent} from './useEffectEvent';
17
- import {useLayoutEffect} from './useLayoutEffect';
18
-
19
- export interface LoadMoreSentinelProps extends Omit<AsyncLoadable, 'isLoading'> {
20
- collection: Collection<any>,
21
- /**
22
- * The amount of offset from the bottom of your scrollable region that should trigger load more.
23
- * Uses a percentage value relative to the scroll body's client height. Load more is then triggered
24
- * when your current scroll position's distance from the bottom of the currently loaded list of items is less than
25
- * or equal to the provided value. (e.g. 1 = 100% of the scroll region's height).
26
- * @default 1
27
- */
28
- scrollOffset?: number
29
- }
30
-
31
- export function useLoadMoreSentinel(props: LoadMoreSentinelProps, ref: RefObject<HTMLElement | null>): void {
32
- let {collection, onLoadMore, scrollOffset = 1} = props;
33
-
34
- let sentinelObserver = useRef<IntersectionObserver>(null);
35
-
36
- let triggerLoadMore = useEffectEvent((entries: IntersectionObserverEntry[]) => {
37
- // Use "isIntersecting" over an equality check of 0 since it seems like there is cases where
38
- // a intersection ratio of 0 can be reported when isIntersecting is actually true
39
- for (let entry of entries) {
40
- // Note that this will be called if the collection changes, even if onLoadMore was already called and is being processed.
41
- // Up to user discretion as to how to handle these multiple onLoadMore calls
42
- if (entry.isIntersecting && onLoadMore) {
43
- onLoadMore();
44
- }
45
- }
46
- });
47
-
48
- useLayoutEffect(() => {
49
- if (ref.current) {
50
- // Tear down and set up a new IntersectionObserver when the collection changes so that we can properly trigger additional loadMores if there is room for more items
51
- // Need to do this tear down and set up since using a large rootMargin will mean the observer's callback isn't called even when scrolling the item into view beause its visibility hasn't actually changed
52
- // https://codesandbox.io/p/sandbox/magical-swanson-dhgp89?file=%2Fsrc%2FApp.js%3A21%2C21
53
- sentinelObserver.current = new IntersectionObserver(triggerLoadMore, {root: getScrollParent(ref?.current) as HTMLElement, rootMargin: `0px ${100 * scrollOffset}% ${100 * scrollOffset}% ${100 * scrollOffset}%`});
54
- sentinelObserver.current.observe(ref.current);
55
- }
56
-
57
- return () => {
58
- if (sentinelObserver.current) {
59
- sentinelObserver.current.disconnect();
60
- }
61
- };
62
- }, [collection, ref, scrollOffset]);
63
- }
@@ -1,69 +0,0 @@
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, useCallback, 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 ref The original ref intended to be used.
21
- * @returns An object ref that updates the given ref.
22
- * @see https://react.dev/reference/react/forwardRef
23
- */
24
- export function useObjectRef<T>(ref?: ((instance: T | null) => (() => void) | void) | MutableRefObject<T | null> | null): MutableRefObject<T | null> {
25
- const objRef: MutableRefObject<T | null> = useRef<T>(null);
26
- const cleanupRef: MutableRefObject<(() => void) | void> = useRef(undefined);
27
-
28
- const refEffect = useCallback(
29
- (instance: T | null) => {
30
- if (typeof ref === 'function') {
31
- const refCallback = ref;
32
- const refCleanup = refCallback(instance);
33
- return () => {
34
- if (typeof refCleanup === 'function') {
35
- refCleanup();
36
- } else {
37
- refCallback(null);
38
- }
39
- };
40
- } else if (ref) {
41
- ref.current = instance;
42
- return () => {
43
- ref.current = null;
44
- };
45
- }
46
- },
47
- [ref]
48
- );
49
-
50
- return useMemo(
51
- () => ({
52
- get current() {
53
- return objRef.current;
54
- },
55
- set current(value) {
56
- objRef.current = value;
57
- if (cleanupRef.current) {
58
- cleanupRef.current();
59
- cleanupRef.current = undefined;
60
- }
61
-
62
- if (value != null) {
63
- cleanupRef.current = refEffect(value);
64
- }
65
- }
66
- }),
67
- [refEffect]
68
- );
69
- }
@@ -1,52 +0,0 @@
1
-
2
- import {RefObject} from '@react-types/shared';
3
- import {useEffect} from 'react';
4
- import {useEffectEvent} from './useEffectEvent';
5
-
6
- function hasResizeObserver() {
7
- return typeof window.ResizeObserver !== 'undefined';
8
- }
9
-
10
- type useResizeObserverOptionsType<T> = {
11
- ref: RefObject<T | undefined | null> | undefined,
12
- box?: ResizeObserverBoxOptions,
13
- onResize: () => void
14
- }
15
-
16
- export function useResizeObserver<T extends Element>(options: useResizeObserverOptionsType<T>): void {
17
- // Only call onResize from inside the effect, otherwise we'll void our assumption that
18
- // useEffectEvents are safe to pass in.
19
- const {ref, box, onResize} = options;
20
- let onResizeEvent = useEffectEvent(onResize);
21
-
22
- useEffect(() => {
23
- let element = ref?.current;
24
- if (!element) {
25
- return;
26
- }
27
-
28
- if (!hasResizeObserver()) {
29
- window.addEventListener('resize', onResizeEvent, false);
30
- return () => {
31
- window.removeEventListener('resize', onResizeEvent, false);
32
- };
33
- } else {
34
-
35
- const resizeObserverInstance = new window.ResizeObserver((entries) => {
36
- if (!entries.length) {
37
- return;
38
- }
39
-
40
- onResizeEvent();
41
- });
42
- resizeObserverInstance.observe(element, {box});
43
-
44
- return () => {
45
- if (element) {
46
- resizeObserverInstance.unobserve(element);
47
- }
48
- };
49
- }
50
-
51
- }, [ref, box]);
52
- }
package/src/useSyncRef.ts DELETED
@@ -1,33 +0,0 @@
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>): void {
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
- }