@dktunited-techoff/techoff-suite-ui 1.13.4 → 1.13.6
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/esm/components/TsMenuButton/TsMenuButton.js +24 -5
- package/esm/components/TsMenuButton/TsMenuButton.js.map +1 -1
- package/lib/components/TsMenuButton/TsMenuButton.js +23 -4
- package/lib/components/TsMenuButton/TsMenuButton.js.map +1 -1
- package/package.json +1 -1
- package/src/components/TsMenuButton/TsMenuButton.tsx +31 -5
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { useState } from 'react';
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
3
4
|
import { TsIcon } from '../TsIcon/TsIcon';
|
|
4
5
|
import { useClickOutside } from '../../hooks/use-click-outside';
|
|
5
6
|
import './TsMenuButton.css';
|
|
@@ -18,17 +19,35 @@ export const TsMenuItem = ({ icon, label, onClick }) => {
|
|
|
18
19
|
};
|
|
19
20
|
export const TsMenuButton = ({ menuAlignment = 'bottom-start', mainButton, items }) => {
|
|
20
21
|
const [showMenu, setShowMenu] = useState(false);
|
|
21
|
-
const
|
|
22
|
+
const containerRef = useClickOutside(() => setShowMenu(false));
|
|
23
|
+
const buttonRef = useRef(null);
|
|
24
|
+
const menuRef = useRef(null);
|
|
25
|
+
const [menuStyle, setMenuStyle] = useState({});
|
|
22
26
|
// ########
|
|
23
27
|
// Hanlders
|
|
24
28
|
const handleShowMenu = (e) => {
|
|
25
29
|
e.stopPropagation();
|
|
26
30
|
setShowMenu(true);
|
|
27
31
|
};
|
|
32
|
+
// ########
|
|
33
|
+
// Watchers
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (showMenu && buttonRef.current && menuRef.current) {
|
|
36
|
+
const buttonRect = buttonRef.current.getBoundingClientRect();
|
|
37
|
+
const viewportHeight = window.innerHeight;
|
|
38
|
+
const menuHeight = menuRef.current.getBoundingClientRect().height;
|
|
39
|
+
const spaceBelow = viewportHeight - buttonRect.bottom;
|
|
40
|
+
setMenuStyle({
|
|
41
|
+
top: spaceBelow >= menuHeight ? `${buttonRect.bottom + 4}px` : `${buttonRect.top - menuHeight - 4}px`,
|
|
42
|
+
left: `${buttonRect.left}px`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}, [showMenu]);
|
|
46
|
+
const menu = showMenu && (React.createElement("div", { className: `ts-menu-button-menu ts-menu-button-menu--${menuAlignment}`, style: menuStyle, ref: menuRef }, items));
|
|
28
47
|
// #########
|
|
29
48
|
// Rendering
|
|
30
|
-
return (React.createElement("div", { className: "ts-menu-button", ref:
|
|
31
|
-
React.cloneElement(mainButton, { onClick: handleShowMenu }),
|
|
32
|
-
|
|
49
|
+
return (React.createElement("div", { className: "ts-menu-button", ref: containerRef },
|
|
50
|
+
React.createElement("div", { ref: buttonRef }, React.cloneElement(mainButton, { onClick: handleShowMenu })),
|
|
51
|
+
createPortal(menu, document.body)));
|
|
33
52
|
};
|
|
34
53
|
//# sourceMappingURL=TsMenuButton.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TsMenuButton.js","sourceRoot":"","sources":["../../../src/components/TsMenuButton/TsMenuButton.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,
|
|
1
|
+
{"version":3,"file":"TsMenuButton.js","sourceRoot":"","sources":["../../../src/components/TsMenuButton/TsMenuButton.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAA6B,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,C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exports.TsMenuItem = exports.TsMenuDivider = void 0;
|
|
4
4
|
const React = require("react");
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
+
const react_dom_1 = require("react-dom");
|
|
6
7
|
const TsIcon_1 = require("../TsIcon/TsIcon");
|
|
7
8
|
const use_click_outside_1 = require("../../hooks/use-click-outside");
|
|
8
9
|
require("./TsMenuButton.css");
|
|
@@ -23,18 +24,36 @@ const TsMenuItem = ({ icon, label, onClick }) => {
|
|
|
23
24
|
exports.TsMenuItem = TsMenuItem;
|
|
24
25
|
const TsMenuButton = ({ menuAlignment = 'bottom-start', mainButton, items }) => {
|
|
25
26
|
const [showMenu, setShowMenu] = (0, react_1.useState)(false);
|
|
26
|
-
const
|
|
27
|
+
const containerRef = (0, use_click_outside_1.useClickOutside)(() => setShowMenu(false));
|
|
28
|
+
const buttonRef = (0, react_1.useRef)(null);
|
|
29
|
+
const menuRef = (0, react_1.useRef)(null);
|
|
30
|
+
const [menuStyle, setMenuStyle] = (0, react_1.useState)({});
|
|
27
31
|
// ########
|
|
28
32
|
// Hanlders
|
|
29
33
|
const handleShowMenu = (e) => {
|
|
30
34
|
e.stopPropagation();
|
|
31
35
|
setShowMenu(true);
|
|
32
36
|
};
|
|
37
|
+
// ########
|
|
38
|
+
// Watchers
|
|
39
|
+
(0, react_1.useEffect)(() => {
|
|
40
|
+
if (showMenu && buttonRef.current && menuRef.current) {
|
|
41
|
+
const buttonRect = buttonRef.current.getBoundingClientRect();
|
|
42
|
+
const viewportHeight = window.innerHeight;
|
|
43
|
+
const menuHeight = menuRef.current.getBoundingClientRect().height;
|
|
44
|
+
const spaceBelow = viewportHeight - buttonRect.bottom;
|
|
45
|
+
setMenuStyle({
|
|
46
|
+
top: spaceBelow >= menuHeight ? `${buttonRect.bottom + 4}px` : `${buttonRect.top - menuHeight - 4}px`,
|
|
47
|
+
left: `${buttonRect.left}px`,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}, [showMenu]);
|
|
51
|
+
const menu = showMenu && (React.createElement("div", { className: `ts-menu-button-menu ts-menu-button-menu--${menuAlignment}`, style: menuStyle, ref: menuRef }, items));
|
|
33
52
|
// #########
|
|
34
53
|
// Rendering
|
|
35
|
-
return (React.createElement("div", { className: "ts-menu-button", ref:
|
|
36
|
-
React.cloneElement(mainButton, { onClick: handleShowMenu }),
|
|
37
|
-
|
|
54
|
+
return (React.createElement("div", { className: "ts-menu-button", ref: containerRef },
|
|
55
|
+
React.createElement("div", { ref: buttonRef }, React.cloneElement(mainButton, { onClick: handleShowMenu })),
|
|
56
|
+
(0, react_dom_1.createPortal)(menu, document.body)));
|
|
38
57
|
};
|
|
39
58
|
exports.TsMenuButton = TsMenuButton;
|
|
40
59
|
//# sourceMappingURL=TsMenuButton.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TsMenuButton.js","sourceRoot":"","sources":["../../../src/components/TsMenuButton/TsMenuButton.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,
|
|
1
|
+
{"version":3,"file":"TsMenuButton.js","sourceRoot":"","sources":["../../../src/components/TsMenuButton/TsMenuButton.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,iCAA+E;AAC/E,yCAAyC;AAEzC,6CAA0C;AAC1C,qEAAgE;AAChE,8BAA4B;AAErB,MAAM,aAAa,GAAG,GAAG,EAAE;IAChC,YAAY;IACZ,YAAY;IACZ,OAAO,6BAAK,SAAS,EAAC,iBAAiB,GAAG,CAAC;AAC7C,CAAC,CAAC;AAJW,QAAA,aAAa,iBAIxB;AAEK,MAAM,UAAU,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAmB,EAAE,EAAE;IACtE,YAAY;IACZ,YAAY;IACZ,OAAO,CACL,gCAAQ,SAAS,EAAC,cAAc,EAAC,OAAO,EAAE,OAAO;QAC9C,IAAI,IAAI,CACP,6BAAK,SAAS,EAAC,mBAAmB;YAChC,oBAAC,eAAM,IAAC,IAAI,EAAE,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BACX,GAAG,EAAE,UAAU,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,GAAG,GAAG,UAAU,GAAG,CAAC,IAAI;gBACrG,IAAI,EAAE,GAAG,UAAU,CAAC,IAAI,IAAI;aAC7B,CAAC,CAAC;SACJ;IACH,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,MAAM,IAAI,GAAG,QAAQ,IAAI,CACvB,6BAAK,SAAS,EAAE,4CAA4C,aAAa,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,OAAO,IACxG,KAAK,CACF,CACP,CAAC;IAEF,YAAY;IACZ,YAAY;IACZ,OAAO,CACL,6BAAK,SAAS,EAAC,gBAAgB,EAAC,GAAG,EAAE,YAAY;QAC/C,6BAAK,GAAG,EAAE,SAAS,IAAG,KAAK,CAAC,YAAY,CAAC,UAAU,EAAE,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC,CAAO;QACvF,IAAA,wBAAY,EAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,CAC9B,CACP,CAAC;AACJ,CAAC,CAAC;AA5CW,QAAA,YAAY,gBA4CvB"}
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { MouseEvent, useState } from 'react';
|
|
2
|
+
import { CSSProperties, MouseEvent, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
3
4
|
import { TsMenuButtonProps, TsMenuItemProps } from './TsMenuButton.types';
|
|
4
5
|
import { TsIcon } from '../TsIcon/TsIcon';
|
|
5
6
|
import { useClickOutside } from '../../hooks/use-click-outside';
|
|
@@ -29,7 +30,10 @@ export const TsMenuItem = ({ icon, label, onClick }: TsMenuItemProps) => {
|
|
|
29
30
|
|
|
30
31
|
export const TsMenuButton = ({ menuAlignment = 'bottom-start', mainButton, items }: TsMenuButtonProps) => {
|
|
31
32
|
const [showMenu, setShowMenu] = useState<boolean>(false);
|
|
32
|
-
const
|
|
33
|
+
const containerRef = useClickOutside(() => setShowMenu(false));
|
|
34
|
+
const buttonRef = useRef<HTMLDivElement>(null);
|
|
35
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
36
|
+
const [menuStyle, setMenuStyle] = useState<CSSProperties>({});
|
|
33
37
|
|
|
34
38
|
// ########
|
|
35
39
|
// Hanlders
|
|
@@ -38,12 +42,34 @@ export const TsMenuButton = ({ menuAlignment = 'bottom-start', mainButton, items
|
|
|
38
42
|
setShowMenu(true);
|
|
39
43
|
};
|
|
40
44
|
|
|
45
|
+
// ########
|
|
46
|
+
// Watchers
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (showMenu && buttonRef.current && menuRef.current) {
|
|
49
|
+
const buttonRect = buttonRef.current.getBoundingClientRect();
|
|
50
|
+
const viewportHeight = window.innerHeight;
|
|
51
|
+
const menuHeight = menuRef.current.getBoundingClientRect().height;
|
|
52
|
+
const spaceBelow = viewportHeight - buttonRect.bottom;
|
|
53
|
+
|
|
54
|
+
setMenuStyle({
|
|
55
|
+
top: spaceBelow >= menuHeight ? `${buttonRect.bottom + 4}px` : `${buttonRect.top - menuHeight - 4}px`,
|
|
56
|
+
left: `${buttonRect.left}px`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}, [showMenu]);
|
|
60
|
+
|
|
61
|
+
const menu = showMenu && (
|
|
62
|
+
<div className={`ts-menu-button-menu ts-menu-button-menu--${menuAlignment}`} style={menuStyle} ref={menuRef}>
|
|
63
|
+
{items}
|
|
64
|
+
</div>
|
|
65
|
+
);
|
|
66
|
+
|
|
41
67
|
// #########
|
|
42
68
|
// Rendering
|
|
43
69
|
return (
|
|
44
|
-
<div className="ts-menu-button" ref={
|
|
45
|
-
{React.cloneElement(mainButton, { onClick: handleShowMenu })}
|
|
46
|
-
{
|
|
70
|
+
<div className="ts-menu-button" ref={containerRef}>
|
|
71
|
+
<div ref={buttonRef}>{React.cloneElement(mainButton, { onClick: handleShowMenu })}</div>
|
|
72
|
+
{createPortal(menu, document.body)}
|
|
47
73
|
</div>
|
|
48
74
|
);
|
|
49
75
|
};
|