@react-aria/utils 3.26.0 → 3.27.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 (45) hide show
  1. package/dist/animation.main.js +97 -0
  2. package/dist/animation.main.js.map +1 -0
  3. package/dist/animation.mjs +91 -0
  4. package/dist/animation.module.js +91 -0
  5. package/dist/animation.module.js.map +1 -0
  6. package/dist/constants.main.js +25 -0
  7. package/dist/constants.main.js.map +1 -0
  8. package/dist/constants.mjs +18 -0
  9. package/dist/constants.module.js +18 -0
  10. package/dist/constants.module.js.map +1 -0
  11. package/dist/import.mjs +9 -1
  12. package/dist/keyboard.main.js +26 -0
  13. package/dist/keyboard.main.js.map +1 -0
  14. package/dist/keyboard.mjs +21 -0
  15. package/dist/keyboard.module.js +21 -0
  16. package/dist/keyboard.module.js.map +1 -0
  17. package/dist/main.js +15 -0
  18. package/dist/main.js.map +1 -1
  19. package/dist/module.js +9 -1
  20. package/dist/module.js.map +1 -1
  21. package/dist/scrollIntoView.main.js +14 -8
  22. package/dist/scrollIntoView.main.js.map +1 -1
  23. package/dist/scrollIntoView.mjs +14 -8
  24. package/dist/scrollIntoView.module.js +14 -8
  25. package/dist/scrollIntoView.module.js.map +1 -1
  26. package/dist/types.d.ts +12 -0
  27. package/dist/types.d.ts.map +1 -1
  28. package/dist/useGlobalListeners.main.js +1 -1
  29. package/dist/useGlobalListeners.main.js.map +1 -1
  30. package/dist/useGlobalListeners.mjs +1 -1
  31. package/dist/useGlobalListeners.module.js +1 -1
  32. package/dist/useGlobalListeners.module.js.map +1 -1
  33. package/dist/useUpdateLayoutEffect.main.js +40 -0
  34. package/dist/useUpdateLayoutEffect.main.js.map +1 -0
  35. package/dist/useUpdateLayoutEffect.mjs +35 -0
  36. package/dist/useUpdateLayoutEffect.module.js +35 -0
  37. package/dist/useUpdateLayoutEffect.module.js.map +1 -0
  38. package/package.json +5 -4
  39. package/src/animation.ts +103 -0
  40. package/src/constants.ts +16 -0
  41. package/src/index.ts +4 -0
  42. package/src/keyboard.tsx +27 -0
  43. package/src/scrollIntoView.ts +28 -12
  44. package/src/useGlobalListeners.ts +1 -1
  45. package/src/useUpdateLayoutEffect.ts +37 -0
@@ -0,0 +1,103 @@
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 {flushSync} from 'react-dom';
14
+ import {RefObject, useCallback, useState} from 'react';
15
+ import {useLayoutEffect} from './useLayoutEffect';
16
+
17
+ export function useEnterAnimation(ref: RefObject<HTMLElement | null>, isReady: boolean = true) {
18
+ let [isEntering, setEntering] = useState(true);
19
+ let isAnimationReady = isEntering && isReady;
20
+
21
+ // There are two cases for entry animations:
22
+ // 1. CSS @keyframes. The `animation` property is set during the isEntering state, and it is removed after the animation finishes.
23
+ // 2. CSS transitions. The initial styles are applied during the isEntering state, and removed immediately, causing the transition to occur.
24
+ //
25
+ // In the second case, cancel any transitions that were triggered prior to the isEntering = false state (when the transition is supposed to start).
26
+ // This can happen when isReady starts as false (e.g. popovers prior to placement calculation).
27
+ useLayoutEffect(() => {
28
+ if (isAnimationReady && ref.current && 'getAnimations' in ref.current) {
29
+ for (let animation of ref.current.getAnimations()) {
30
+ if (animation instanceof CSSTransition) {
31
+ animation.cancel();
32
+ }
33
+ }
34
+ }
35
+ }, [ref, isAnimationReady]);
36
+
37
+ useAnimation(ref, isAnimationReady, useCallback(() => setEntering(false), []));
38
+ return isAnimationReady;
39
+ }
40
+
41
+ export function useExitAnimation(ref: RefObject<HTMLElement | null>, isOpen: boolean) {
42
+ let [exitState, setExitState] = useState<'closed' | 'open' | 'exiting'>(isOpen ? 'open' : 'closed');
43
+
44
+ switch (exitState) {
45
+ case 'open':
46
+ // If isOpen becomes false, set the state to exiting.
47
+ if (!isOpen) {
48
+ setExitState('exiting');
49
+ }
50
+ break;
51
+ case 'closed':
52
+ case 'exiting':
53
+ // If we are exiting and isOpen becomes true, the animation was interrupted.
54
+ // Reset the state to open.
55
+ if (isOpen) {
56
+ setExitState('open');
57
+ }
58
+ break;
59
+ }
60
+
61
+ let isExiting = exitState === 'exiting';
62
+ useAnimation(
63
+ ref,
64
+ isExiting,
65
+ useCallback(() => {
66
+ // Set the state to closed, which will cause the element to be unmounted.
67
+ setExitState(state => state === 'exiting' ? 'closed' : state);
68
+ }, [])
69
+ );
70
+
71
+ return isExiting;
72
+ }
73
+
74
+ function useAnimation(ref: RefObject<HTMLElement | null>, isActive: boolean, onEnd: () => void) {
75
+ useLayoutEffect(() => {
76
+ if (isActive && ref.current) {
77
+ if (!('getAnimations' in ref.current)) {
78
+ // JSDOM
79
+ onEnd();
80
+ return;
81
+ }
82
+
83
+ let animations = ref.current.getAnimations();
84
+ if (animations.length === 0) {
85
+ onEnd();
86
+ return;
87
+ }
88
+
89
+ let canceled = false;
90
+ Promise.all(animations.map(a => a.finished)).then(() => {
91
+ if (!canceled) {
92
+ flushSync(() => {
93
+ onEnd();
94
+ });
95
+ }
96
+ }).catch(() => {});
97
+
98
+ return () => {
99
+ canceled = true;
100
+ };
101
+ }
102
+ }, [ref, isActive, onEnd]);
103
+ }
@@ -0,0 +1,16 @@
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
+ // Custom event names for updating the autocomplete's aria-activedecendant.
14
+ export const CLEAR_FOCUS_EVENT = 'react-aria-clear-focus';
15
+ export const FOCUS_EVENT = 'react-aria-focus';
16
+ export const UPDATE_ACTIVEDESCENDANT = 'react-aria-update-activedescendant';
package/src/index.ts CHANGED
@@ -24,6 +24,7 @@ export {useGlobalListeners} from './useGlobalListeners';
24
24
  export {useLabels} from './useLabels';
25
25
  export {useObjectRef} from './useObjectRef';
26
26
  export {useUpdateEffect} from './useUpdateEffect';
27
+ export {useUpdateLayoutEffect} from './useUpdateLayoutEffect';
27
28
  export {useLayoutEffect} from './useLayoutEffect';
28
29
  export {useResizeObserver} from './useResizeObserver';
29
30
  export {useSyncRef} from './useSyncRef';
@@ -42,3 +43,6 @@ export {useEffectEvent} from './useEffectEvent';
42
43
  export {useDeepMemo} from './useDeepMemo';
43
44
  export {useFormReset} from './useFormReset';
44
45
  export {useLoadMore} from './useLoadMore';
46
+ export {CLEAR_FOCUS_EVENT, FOCUS_EVENT, UPDATE_ACTIVEDESCENDANT} from './constants';
47
+ export {isCtrlKeyPressed} from './keyboard';
48
+ export {useEnterAnimation, useExitAnimation} from './animation';
@@ -0,0 +1,27 @@
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 {isMac} from './platform';
14
+
15
+ interface Event {
16
+ altKey: boolean,
17
+ ctrlKey: boolean,
18
+ metaKey: boolean
19
+ }
20
+
21
+ export function isCtrlKeyPressed(e: Event) {
22
+ if (isMac()) {
23
+ return e.metaKey;
24
+ }
25
+
26
+ return e.ctrlKey;
27
+ }
@@ -30,24 +30,40 @@ export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement) {
30
30
  let x = scrollView.scrollLeft;
31
31
  let y = scrollView.scrollTop;
32
32
 
33
- // Account for top/left border offsetting the scroll top/Left
34
- let {borderTopWidth, borderLeftWidth} = getComputedStyle(scrollView);
35
- let borderAdjustedX = scrollView.scrollLeft + parseInt(borderLeftWidth, 10);
36
- let borderAdjustedY = scrollView.scrollTop + parseInt(borderTopWidth, 10);
33
+ // Account for top/left border offsetting the scroll top/Left + scroll padding
34
+ let {
35
+ borderTopWidth,
36
+ borderLeftWidth,
37
+ scrollPaddingTop,
38
+ scrollPaddingRight,
39
+ scrollPaddingBottom,
40
+ scrollPaddingLeft
41
+ } = getComputedStyle(scrollView);
42
+
43
+ let borderAdjustedX = x + parseInt(borderLeftWidth, 10);
44
+ let borderAdjustedY = y + parseInt(borderTopWidth, 10);
37
45
  // Ignore end/bottom border via clientHeight/Width instead of offsetHeight/Width
38
46
  let maxX = borderAdjustedX + scrollView.clientWidth;
39
47
  let maxY = borderAdjustedY + scrollView.clientHeight;
40
48
 
41
- if (offsetX <= x) {
42
- x = offsetX - parseInt(borderLeftWidth, 10);
43
- } else if (offsetX + width > maxX) {
44
- x += offsetX + width - maxX;
49
+ // Get scroll padding values as pixels - defaults to 0 if no scroll padding
50
+ // is used.
51
+ let scrollPaddingTopNumber = parseInt(scrollPaddingTop, 10) || 0;
52
+ let scrollPaddingBottomNumber = parseInt(scrollPaddingBottom, 10) || 0;
53
+ let scrollPaddingRightNumber = parseInt(scrollPaddingRight, 10) || 0;
54
+ let scrollPaddingLeftNumber = parseInt(scrollPaddingLeft, 10) || 0;
55
+
56
+ if (offsetX <= x + scrollPaddingLeftNumber) {
57
+ x = offsetX - parseInt(borderLeftWidth, 10) - scrollPaddingLeftNumber;
58
+ } else if (offsetX + width > maxX - scrollPaddingRightNumber) {
59
+ x += offsetX + width - maxX + scrollPaddingRightNumber;
45
60
  }
46
- if (offsetY <= borderAdjustedY) {
47
- y = offsetY - parseInt(borderTopWidth, 10);
48
- } else if (offsetY + height > maxY) {
49
- y += offsetY + height - maxY;
61
+ if (offsetY <= borderAdjustedY + scrollPaddingTopNumber) {
62
+ y = offsetY - parseInt(borderTopWidth, 10) - scrollPaddingTopNumber;
63
+ } else if (offsetY + height > maxY - scrollPaddingBottomNumber) {
64
+ y += offsetY + height - maxY + scrollPaddingBottomNumber;
50
65
  }
66
+
51
67
  scrollView.scrollLeft = x;
52
68
  scrollView.scrollTop = y;
53
69
  }
@@ -29,7 +29,7 @@ export function useGlobalListeners(): GlobalListeners {
29
29
  listener(...args);
30
30
  } : listener;
31
31
  globalListeners.current.set(listener, {type, eventTarget, fn, options});
32
- eventTarget.addEventListener(type, listener, options);
32
+ eventTarget.addEventListener(type, fn, options);
33
33
  }, []);
34
34
  let removeGlobalListener = useCallback((eventTarget, type, listener, options) => {
35
35
  let fn = globalListeners.current.get(listener)?.fn || listener;
@@ -0,0 +1,37 @@
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 {EffectCallback, useRef} from 'react';
14
+ import {useLayoutEffect} from './useLayoutEffect';
15
+
16
+ // Like useLayoutEffect, but only called for updates after the initial render.
17
+ export function useUpdateLayoutEffect(effect: EffectCallback, dependencies: any[]) {
18
+ const isInitialMount = useRef(true);
19
+ const lastDeps = useRef<any[] | null>(null);
20
+
21
+ useLayoutEffect(() => {
22
+ isInitialMount.current = true;
23
+ return () => {
24
+ isInitialMount.current = false;
25
+ };
26
+ }, []);
27
+
28
+ useLayoutEffect(() => {
29
+ if (isInitialMount.current) {
30
+ isInitialMount.current = false;
31
+ } else if (!lastDeps.current || dependencies.some((dep, i) => !Object.is(dep, lastDeps[i]))) {
32
+ effect();
33
+ }
34
+ lastDeps.current = dependencies;
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
36
+ }, dependencies);
37
+ }