@lumx/react 3.6.5-alpha.4 → 3.6.5
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/index.js +20 -18
- package/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/popover/useRestoreFocusOnClose.tsx +18 -16
- package/src/utils/OnBeforeUnmount.tsx +2 -2
package/package.json
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@juggle/resize-observer": "^3.2.0",
|
|
10
|
-
"@lumx/core": "^3.6.5
|
|
11
|
-
"@lumx/icons": "^3.6.5
|
|
10
|
+
"@lumx/core": "^3.6.5",
|
|
11
|
+
"@lumx/icons": "^3.6.5",
|
|
12
12
|
"@popperjs/core": "^2.5.4",
|
|
13
13
|
"body-scroll-lock": "^3.1.5",
|
|
14
14
|
"classnames": "^2.3.2",
|
|
@@ -112,5 +112,5 @@
|
|
|
112
112
|
"build:storybook": "storybook build"
|
|
113
113
|
},
|
|
114
114
|
"sideEffects": false,
|
|
115
|
-
"version": "3.6.5
|
|
115
|
+
"version": "3.6.5"
|
|
116
116
|
}
|
|
@@ -15,31 +15,33 @@ export function useRestoreFocusOnClose(
|
|
|
15
15
|
}: Pick<PopoverProps, 'focusAnchorOnClose' | 'anchorRef' | 'parentElement'>,
|
|
16
16
|
popoverElement?: HTMLElement | null,
|
|
17
17
|
) {
|
|
18
|
-
|
|
19
|
-
const onBeforeUnmountRef = React.useRef<() => void>(() => {});
|
|
20
|
-
|
|
21
|
-
const anchor = anchorRef.current;
|
|
22
|
-
const elementToFocus =
|
|
23
|
-
// Provided parent element
|
|
24
|
-
parentElement?.current ||
|
|
25
|
-
// Or first focusable element in anchor
|
|
26
|
-
(anchor ? getFirstAndLastFocusable(anchor).first : undefined) ||
|
|
27
|
-
// Fallback to anchor
|
|
28
|
-
anchor;
|
|
18
|
+
const onBeforeUnmountRef = React.useRef<(() => void) | undefined>();
|
|
29
19
|
|
|
30
20
|
React.useEffect(() => {
|
|
31
|
-
if (!popoverElement || !focusAnchorOnClose
|
|
21
|
+
if (!popoverElement || !focusAnchorOnClose) {
|
|
22
|
+
onBeforeUnmountRef.current = undefined;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
32
25
|
|
|
33
26
|
onBeforeUnmountRef.current = () => {
|
|
34
27
|
const isFocusWithin = popoverElement?.contains(document.activeElement);
|
|
35
28
|
if (!isFocusWithin) return;
|
|
36
29
|
|
|
37
|
-
//
|
|
30
|
+
// On next render
|
|
38
31
|
setTimeout(() => {
|
|
39
|
-
|
|
32
|
+
const anchor = anchorRef.current;
|
|
33
|
+
const elementToFocus =
|
|
34
|
+
// Provided parent element
|
|
35
|
+
parentElement?.current ||
|
|
36
|
+
// Or first focusable element in anchor
|
|
37
|
+
(anchor ? getFirstAndLastFocusable(anchor).first : undefined) ||
|
|
38
|
+
// Fallback to anchor
|
|
39
|
+
anchor;
|
|
40
|
+
|
|
41
|
+
elementToFocus?.focus({ preventScroll: true });
|
|
40
42
|
}, 0);
|
|
41
43
|
};
|
|
42
|
-
}, [
|
|
44
|
+
}, [anchorRef, focusAnchorOnClose, parentElement, popoverElement]);
|
|
43
45
|
|
|
44
|
-
return <OnBeforeUnmount
|
|
46
|
+
return <OnBeforeUnmount callbackRef={onBeforeUnmountRef} />;
|
|
45
47
|
}
|
|
@@ -5,13 +5,13 @@ import React, { useLayoutEffect } from 'react';
|
|
|
5
5
|
*
|
|
6
6
|
* The callback must be wrapped in a React ref to avoid updating the `useLayoutEffect` before the un-mount
|
|
7
7
|
*/
|
|
8
|
-
export const OnBeforeUnmount = ({
|
|
8
|
+
export const OnBeforeUnmount = ({ callbackRef }: { callbackRef: React.RefObject<(() => void) | undefined> }) => {
|
|
9
9
|
useLayoutEffect(
|
|
10
10
|
() => {
|
|
11
11
|
return () => {
|
|
12
12
|
// On unmount
|
|
13
13
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
14
|
-
|
|
14
|
+
callbackRef.current?.();
|
|
15
15
|
};
|
|
16
16
|
}, // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
17
17
|
[],
|