@pushwoosh/dumb-components 1.1.129 → 1.1.130
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/Drawer/components/Drawer/Drawer.js +10 -16
- package/DropdownMenu/components/DropdownMenu/DropdownMenu.js +20 -8
- package/Modal/components/Modal/Modal.js +10 -13
- package/hooks/index.d.ts +1 -0
- package/hooks/index.js +2 -1
- package/hooks/useLayer.d.ts +10 -0
- package/hooks/useLayer.js +81 -0
- package/package.json +1 -1
|
@@ -2,13 +2,14 @@ import React, { useRef, useState, useEffect, useCallback } from 'react';
|
|
|
2
2
|
import { createPortal } from 'react-dom';
|
|
3
3
|
import { drawerSizes } from './maps';
|
|
4
4
|
import { DrawerBoxTrans, DrawerBox, LockBody, DrawerInner, DrawerContainer } from './styles';
|
|
5
|
+
import { useLayer } from '../../../hooks';
|
|
5
6
|
function createDivElement() {
|
|
6
7
|
return document.createElement('div');
|
|
7
8
|
}
|
|
8
9
|
export function Drawer({
|
|
9
10
|
size,
|
|
10
11
|
isOpen,
|
|
11
|
-
zIndex
|
|
12
|
+
zIndex,
|
|
12
13
|
onClose,
|
|
13
14
|
children,
|
|
14
15
|
padding,
|
|
@@ -17,25 +18,18 @@ export function Drawer({
|
|
|
17
18
|
}) {
|
|
18
19
|
const ref = useRef();
|
|
19
20
|
const [element] = useState(createDivElement);
|
|
21
|
+
const {
|
|
22
|
+
zIndex: resolvedZIndex
|
|
23
|
+
} = useLayer({
|
|
24
|
+
isOpen,
|
|
25
|
+
onClose,
|
|
26
|
+
zIndex
|
|
27
|
+
});
|
|
20
28
|
useEffect(() => {
|
|
21
29
|
var _document$body$lastCh;
|
|
22
30
|
(_document$body$lastCh = document.body.lastChild) === null || _document$body$lastCh === void 0 || _document$body$lastCh.after(element);
|
|
23
31
|
return () => element.remove();
|
|
24
32
|
}, [element]);
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
if (isOpen && onClose) {
|
|
27
|
-
const onCloseEventHandler = event => {
|
|
28
|
-
if (event.key === 'Escape') {
|
|
29
|
-
onClose();
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
window.addEventListener('keydown', onCloseEventHandler);
|
|
33
|
-
return () => {
|
|
34
|
-
window.removeEventListener('keydown', onCloseEventHandler);
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
return () => {};
|
|
38
|
-
}, [isOpen, onClose]);
|
|
39
33
|
const handleClose = useCallback(event => {
|
|
40
34
|
if (event.target === event.currentTarget) {
|
|
41
35
|
onClose === null || onClose === void 0 || onClose();
|
|
@@ -52,7 +46,7 @@ export function Drawer({
|
|
|
52
46
|
withoutLockScreen: withoutLockScreen
|
|
53
47
|
}, React.createElement(DrawerBox, {
|
|
54
48
|
ref: ref,
|
|
55
|
-
"$zIndex":
|
|
49
|
+
"$zIndex": resolvedZIndex
|
|
56
50
|
}, React.createElement(LockBody, null), React.createElement(DrawerInner, {
|
|
57
51
|
onMouseDown: handleClickOutside,
|
|
58
52
|
"$padding": padding
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { autoPlacement, size, offset, useFloating } from '@floating-ui/react-dom';
|
|
2
|
-
import React, { useState, useEffect } from 'react';
|
|
2
|
+
import React, { useState, useEffect, useCallback } from 'react';
|
|
3
3
|
import { createPortal, flushSync } from 'react-dom';
|
|
4
4
|
import { Place } from './styles';
|
|
5
|
+
import { useLayer } from '../../../hooks';
|
|
5
6
|
import { DropdownContext, DropdownZIndexContext } from '../../context';
|
|
6
7
|
import { renderChildren } from '../../shared/renderChildren';
|
|
7
8
|
const defaultPlacements = ['top-start', 'top-end', 'bottom-start', 'bottom-end'];
|
|
@@ -19,6 +20,17 @@ export const DropdownMenu = ({
|
|
|
19
20
|
}) => {
|
|
20
21
|
const [isOpen, setIsOpen] = useState(openOnMount);
|
|
21
22
|
const [width, setWidth] = useState(windowWidth);
|
|
23
|
+
const handleClose = useCallback(() => {
|
|
24
|
+
setIsOpen(false);
|
|
25
|
+
onClose === null || onClose === void 0 || onClose();
|
|
26
|
+
}, [onClose]);
|
|
27
|
+
const {
|
|
28
|
+
zIndex: resolvedZIndex
|
|
29
|
+
} = useLayer({
|
|
30
|
+
isOpen,
|
|
31
|
+
onClose: handleClose,
|
|
32
|
+
zIndex: menuZIndex
|
|
33
|
+
});
|
|
22
34
|
const {
|
|
23
35
|
refs,
|
|
24
36
|
floatingStyles
|
|
@@ -41,8 +53,7 @@ export const DropdownMenu = ({
|
|
|
41
53
|
const handleClickListener = e => {
|
|
42
54
|
var _refs$floating$curren, _refs$reference$curre;
|
|
43
55
|
if (!((_refs$floating$curren = refs.floating.current) !== null && _refs$floating$curren !== void 0 && _refs$floating$curren.contains(e.target)) && !((_refs$reference$curre = refs.reference.current) !== null && _refs$reference$curre !== void 0 && _refs$reference$curre.contains(e.target))) {
|
|
44
|
-
|
|
45
|
-
onClose === null || onClose === void 0 || onClose();
|
|
56
|
+
handleClose();
|
|
46
57
|
}
|
|
47
58
|
};
|
|
48
59
|
const timer = setTimeout(() => {
|
|
@@ -53,7 +64,7 @@ export const DropdownMenu = ({
|
|
|
53
64
|
document.removeEventListener('click', handleClickListener);
|
|
54
65
|
};
|
|
55
66
|
}
|
|
56
|
-
}, [isOpen, refs.floating, refs.reference,
|
|
67
|
+
}, [isOpen, refs.floating, refs.reference, handleClose]);
|
|
57
68
|
useEffect(() => {
|
|
58
69
|
if (isOpen && isScrollable && refs.floating.current) {
|
|
59
70
|
const activeElement = refs.floating.current.querySelector('[data-active="true"]');
|
|
@@ -68,15 +79,16 @@ export const DropdownMenu = ({
|
|
|
68
79
|
value: setIsOpen
|
|
69
80
|
}, renderHandler({
|
|
70
81
|
onClick: () => {
|
|
71
|
-
setIsOpen(prev => !prev);
|
|
72
82
|
if (isOpen) {
|
|
73
|
-
|
|
83
|
+
handleClose();
|
|
84
|
+
} else {
|
|
85
|
+
setIsOpen(true);
|
|
74
86
|
}
|
|
75
87
|
},
|
|
76
88
|
ref: refs.setReference,
|
|
77
89
|
isOpen
|
|
78
90
|
}), isOpen && createPortal(React.createElement(DropdownZIndexContext.Provider, {
|
|
79
|
-
value:
|
|
91
|
+
value: resolvedZIndex
|
|
80
92
|
}, React.createElement(Place, {
|
|
81
93
|
ref: refs.setFloating,
|
|
82
94
|
style: floatingStyles,
|
|
@@ -84,6 +96,6 @@ export const DropdownMenu = ({
|
|
|
84
96
|
"$width": width,
|
|
85
97
|
"$fitContent": windowWidth === null && !fullWidth,
|
|
86
98
|
"$scrollable": isScrollable,
|
|
87
|
-
"$zIndex":
|
|
99
|
+
"$zIndex": resolvedZIndex
|
|
88
100
|
}, renderChildren(children))), document.body));
|
|
89
101
|
};
|
|
@@ -2,19 +2,27 @@ import React, { useState, useCallback, useEffect, useRef } from 'react';
|
|
|
2
2
|
import { createPortal } from 'react-dom';
|
|
3
3
|
import { modalSizes } from './maps';
|
|
4
4
|
import { LockBody, ModalBox, ModalBoxTrans, ModalContainer, ModalInner } from './styles';
|
|
5
|
+
import { useLayer } from '../../../hooks';
|
|
5
6
|
function createDivElement() {
|
|
6
7
|
return document.createElement('div');
|
|
7
8
|
}
|
|
8
9
|
export function Modal(props) {
|
|
9
10
|
const {
|
|
10
11
|
size,
|
|
11
|
-
zIndex
|
|
12
|
+
zIndex,
|
|
12
13
|
isOpen,
|
|
13
14
|
onClose,
|
|
14
15
|
children
|
|
15
16
|
} = props;
|
|
16
17
|
const ref = useRef();
|
|
17
18
|
const [element] = useState(createDivElement);
|
|
19
|
+
const {
|
|
20
|
+
zIndex: resolvedZIndex
|
|
21
|
+
} = useLayer({
|
|
22
|
+
isOpen,
|
|
23
|
+
onClose,
|
|
24
|
+
zIndex
|
|
25
|
+
});
|
|
18
26
|
const handleClose = useCallback(event => {
|
|
19
27
|
if (event.target === event.currentTarget) {
|
|
20
28
|
onClose === null || onClose === void 0 || onClose();
|
|
@@ -25,23 +33,12 @@ export function Modal(props) {
|
|
|
25
33
|
(_document$body$lastCh = document.body.lastChild) === null || _document$body$lastCh === void 0 || _document$body$lastCh.after(element);
|
|
26
34
|
return () => element.remove();
|
|
27
35
|
}, [element]);
|
|
28
|
-
useEffect(() => {
|
|
29
|
-
if (isOpen && onClose) {
|
|
30
|
-
const onCloseEventHandler = event => {
|
|
31
|
-
if (event.key === 'Escape') {
|
|
32
|
-
onClose();
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
window.addEventListener('keydown', onCloseEventHandler);
|
|
36
|
-
return () => window.removeEventListener('keydown', onCloseEventHandler);
|
|
37
|
-
}
|
|
38
|
-
}, [isOpen, onClose]);
|
|
39
36
|
return createPortal(React.createElement(ModalBoxTrans, {
|
|
40
37
|
nodeRef: ref,
|
|
41
38
|
isOpen: isOpen
|
|
42
39
|
}, React.createElement(ModalBox, {
|
|
43
40
|
ref: ref,
|
|
44
|
-
"$zIndex":
|
|
41
|
+
"$zIndex": resolvedZIndex
|
|
45
42
|
}, React.createElement(LockBody, null), React.createElement(ModalInner, {
|
|
46
43
|
onMouseDown: handleClose
|
|
47
44
|
}, React.createElement(ModalContainer, {
|
package/hooks/index.d.ts
CHANGED
package/hooks/index.js
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
const BASE_Z_INDEX = 10000;
|
|
3
|
+
const Z_INDEX_STEP = 100;
|
|
4
|
+
const stack = [];
|
|
5
|
+
const subscribers = new Set();
|
|
6
|
+
let keydownAttached = false;
|
|
7
|
+
function notify() {
|
|
8
|
+
subscribers.forEach(subscriber => subscriber());
|
|
9
|
+
}
|
|
10
|
+
function handleKeydown(event) {
|
|
11
|
+
var _top$close;
|
|
12
|
+
if (event.key !== 'Escape') {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const top = stack[stack.length - 1];
|
|
16
|
+
top === null || top === void 0 || (_top$close = top.close) === null || _top$close === void 0 || _top$close.call(top);
|
|
17
|
+
}
|
|
18
|
+
function ensureKeydownListener() {
|
|
19
|
+
if (!keydownAttached && stack.length > 0) {
|
|
20
|
+
window.addEventListener('keydown', handleKeydown);
|
|
21
|
+
keydownAttached = true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function teardownKeydownListener() {
|
|
25
|
+
if (keydownAttached && stack.length === 0) {
|
|
26
|
+
window.removeEventListener('keydown', handleKeydown);
|
|
27
|
+
keydownAttached = false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function registerLayer(entry) {
|
|
31
|
+
stack.push(entry);
|
|
32
|
+
ensureKeydownListener();
|
|
33
|
+
notify();
|
|
34
|
+
}
|
|
35
|
+
function unregisterLayer(id) {
|
|
36
|
+
const index = stack.findIndex(entry => entry.id === id);
|
|
37
|
+
if (index !== -1) {
|
|
38
|
+
stack.splice(index, 1);
|
|
39
|
+
}
|
|
40
|
+
teardownKeydownListener();
|
|
41
|
+
notify();
|
|
42
|
+
}
|
|
43
|
+
function getDepth(id) {
|
|
44
|
+
return stack.findIndex(entry => entry.id === id);
|
|
45
|
+
}
|
|
46
|
+
export function useLayer({
|
|
47
|
+
isOpen,
|
|
48
|
+
onClose,
|
|
49
|
+
zIndex
|
|
50
|
+
}) {
|
|
51
|
+
const id = useId();
|
|
52
|
+
const onCloseRef = useRef(onClose);
|
|
53
|
+
onCloseRef.current = onClose;
|
|
54
|
+
const [, forceRender] = useState(0);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const subscriber = () => forceRender(value => value + 1);
|
|
57
|
+
subscribers.add(subscriber);
|
|
58
|
+
return () => {
|
|
59
|
+
subscribers.delete(subscriber);
|
|
60
|
+
};
|
|
61
|
+
}, []);
|
|
62
|
+
useLayoutEffect(() => {
|
|
63
|
+
if (!isOpen) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
registerLayer({
|
|
67
|
+
id,
|
|
68
|
+
close: () => {
|
|
69
|
+
var _onCloseRef$current;
|
|
70
|
+
return (_onCloseRef$current = onCloseRef.current) === null || _onCloseRef$current === void 0 ? void 0 : _onCloseRef$current.call(onCloseRef);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
forceRender(value => value + 1);
|
|
74
|
+
return () => unregisterLayer(id);
|
|
75
|
+
}, [isOpen, id]);
|
|
76
|
+
const depth = getDepth(id);
|
|
77
|
+
const resolvedZIndex = zIndex ?? BASE_Z_INDEX + Math.max(depth, 0) * Z_INDEX_STEP;
|
|
78
|
+
return {
|
|
79
|
+
zIndex: resolvedZIndex
|
|
80
|
+
};
|
|
81
|
+
}
|