@asup/context-menu 1.0.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.
- package/LICENCE +8 -0
- package/README.md +62 -0
- package/dist/cjs/main.css +51 -0
- package/dist/cjs/main.css.map +1 -0
- package/dist/cjs/main.js +129 -0
- package/dist/cjs/main.js.map +1 -0
- package/dist/context-menu.d.ts +10 -0
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/main.css +51 -0
- package/dist/main.css.map +1 -0
- package/dist/main.js +102 -0
- package/dist/main.js.map +1 -0
- package/package.json +93 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright © 2022 <copyright holders>
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
[npm]: https://img.shields.io/npm/v/@asup/context-menu
|
|
2
|
+
[npm-url]: https://www.npmjs.com/package/@asup/context-menu
|
|
3
|
+
[size]: https://packagephobia.now.sh/badge?p=@asup/context-menu
|
|
4
|
+
[size-url]: https://packagephobia.now.sh/result?p=@asup/context-menu
|
|
5
|
+
|
|
6
|
+
[![npm][npm]][npm-url]
|
|
7
|
+
[![size][size]][size-url]
|
|
8
|
+

|
|
9
|
+
[](https://raw.githubusercontent.com/PaulDThomas/context-menu/master/LICENCE)
|
|
10
|
+
|
|
11
|
+
# @asup/context-menu
|
|
12
|
+
|
|
13
|
+
REACT Context menu, because I couldn't quite find what I wanted.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
# with npm
|
|
19
|
+
npm install @asup/context-menu
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Context menu provider, takes a list of available actions and renders a context menu on appropriate click.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
import { ContextMenuProvider, iMenuItem, MenuContext } from '@asup/context-menu';
|
|
28
|
+
|
|
29
|
+
... inside REACT component
|
|
30
|
+
|
|
31
|
+
<ContextMenuProvider>
|
|
32
|
+
|
|
33
|
+
<SomeChild
|
|
34
|
+
|
|
35
|
+
const menuContext = useContext(MenuContext);
|
|
36
|
+
const showMenu = useCallback((e) => {
|
|
37
|
+
e.preventDefault();
|
|
38
|
+
e.stopPropagation();
|
|
39
|
+
const menuItems: iMenuItem[] = [
|
|
40
|
+
{ label: 'Item 1', action: item1Function },
|
|
41
|
+
{ label: 'Item 2', action: item2Function },
|
|
42
|
+
...
|
|
43
|
+
];
|
|
44
|
+
menuContext.set && menuContext.set({
|
|
45
|
+
visible: true,
|
|
46
|
+
y: e.pageY,
|
|
47
|
+
x: e.pageX,
|
|
48
|
+
menuItems: menuItems,
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
[...]);
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div onContextMenu={showMenu}>
|
|
55
|
+
</div>
|
|
56
|
+
);
|
|
57
|
+
>
|
|
58
|
+
|
|
59
|
+
</ContextMenuProvider>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Add an `onContextMenu` action to an element inside the `ContextMenuProvider`, and create a corresponding function that loads the menuItems array and then sets it to visible.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
.context-menu-handler {
|
|
2
|
+
height: -webkit-fit-content;
|
|
3
|
+
height: -moz-fit-content;
|
|
4
|
+
height: fit-content;
|
|
5
|
+
width: -webkit-fit-content;
|
|
6
|
+
width: -moz-fit-content;
|
|
7
|
+
width: fit-content;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.context-menu {
|
|
11
|
+
visibility: hidden;
|
|
12
|
+
opacity: 1;
|
|
13
|
+
z-index: 10000;
|
|
14
|
+
background-color: #fbfdf6;
|
|
15
|
+
border: 1px solid #111418;
|
|
16
|
+
border-radius: 8px;
|
|
17
|
+
position: absolute;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.context-menu.visible {
|
|
21
|
+
visibility: inherit;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.context-menu-item {
|
|
25
|
+
color: #111418;
|
|
26
|
+
cursor: pointer;
|
|
27
|
+
min-width: 80px;
|
|
28
|
+
padding: 0 4px;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.context-menu-item:first-child {
|
|
32
|
+
padding-top: 2px;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.context-menu-item:last-child {
|
|
36
|
+
padding-bottom: 2px;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.context-menu-item:hover {
|
|
40
|
+
background-color: #fbe9e6;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.context-menu-item:hover:first-child {
|
|
44
|
+
border-radius: 6px 6px 0 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.context-menu-item:hover:last-child {
|
|
48
|
+
border-radius: 0 0 6px 6px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*# sourceMappingURL=main.css.map */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA;;;;;;;;;AAKA;;;;;;;;;;AAWA;;;;AAIA;;;;;;;AAOA;;;;AAIA;;;;AAIA;;;;AAIA;;;;AAIA","sources":["src/components/ContextMenu.scss"],"sourcesContent":[".context-menu-handler {\n height: fit-content;\n width: fit-content;\n}\n\n.context-menu {\n position: absolute;\n visibility: hidden;\n border: 1px solid;\n border-color: rgb(17, 20, 24);\n border-radius: 8px;\n opacity: 1;\n background-color: rgb(251, 253, 246);\n z-index: 10000;\n}\n\n.context-menu.visible {\n visibility: inherit;\n}\n\n.context-menu-item {\n color: rgb(17, 20, 24);\n cursor: pointer;\n padding: 0 4px;\n min-width: 80px;\n}\n\n.context-menu-item:first-child {\n padding-top: 2px;\n}\n\n.context-menu-item:last-child {\n padding-bottom: 2px;\n}\n\n.context-menu-item:hover {\n background-color: rgb(251, 233, 230);\n}\n\n.context-menu-item:hover:first-child {\n border-radius: 6px 6px 0 0;\n}\n\n.context-menu-item:hover:last-child {\n border-radius: 0 0 6px 6px;\n}\n"],"names":[],"version":3,"file":"main.css.map"}
|
package/dist/cjs/main.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
require("./main.css");
|
|
2
|
+
var $gTuX4$swchelperslib_sliced_to_arrayjs = require("@swc/helpers/lib/_sliced_to_array.js");
|
|
3
|
+
var $gTuX4$reactjsxruntime = require("react/jsx-runtime");
|
|
4
|
+
var $gTuX4$react = require("react");
|
|
5
|
+
var $gTuX4$reactdom = require("react-dom");
|
|
6
|
+
|
|
7
|
+
function $parcel$exportWildcard(dest, source) {
|
|
8
|
+
Object.keys(source).forEach(function(key) {
|
|
9
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(dest, key, {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function get() {
|
|
16
|
+
return source[key];
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return dest;
|
|
22
|
+
}
|
|
23
|
+
function $parcel$export(e, n, v, s) {
|
|
24
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
25
|
+
}
|
|
26
|
+
function $parcel$interopDefault(a) {
|
|
27
|
+
return a && a.__esModule ? a.default : a;
|
|
28
|
+
}
|
|
29
|
+
var $a68bd8a6c0fd98c2$exports = {};
|
|
30
|
+
|
|
31
|
+
$parcel$export($a68bd8a6c0fd98c2$exports, "ContextMenuHandler", function () { return $3c568ee547c732c3$export$ed4f9641643dc7e4; });
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
var $5150b66b01c99189$export$8dc6765e8be191c7 = /*#__PURE__*/ (0, ($parcel$interopDefault($gTuX4$react))).forwardRef(function(param, ref) {
|
|
39
|
+
var entries = param.entries, xPos = param.xPos, yPos = param.yPos, toClose = param.toClose;
|
|
40
|
+
$5150b66b01c99189$export$8dc6765e8be191c7.displayName = "ContextMenu";
|
|
41
|
+
return /*#__PURE__*/ (0, $gTuX4$reactjsxruntime.jsx)("div", {
|
|
42
|
+
ref: ref,
|
|
43
|
+
className: "context-menu visible",
|
|
44
|
+
style: {
|
|
45
|
+
top: "".concat(yPos, "px"),
|
|
46
|
+
left: "".concat(xPos, "px")
|
|
47
|
+
},
|
|
48
|
+
children: entries.map(function(e, i) {
|
|
49
|
+
return /*#__PURE__*/ (0, $gTuX4$reactjsxruntime.jsx)("div", {
|
|
50
|
+
className: "context-menu-item",
|
|
51
|
+
onClick: function() {
|
|
52
|
+
e.action && e.action();
|
|
53
|
+
toClose();
|
|
54
|
+
},
|
|
55
|
+
children: e.label
|
|
56
|
+
}, i);
|
|
57
|
+
})
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
var $3c568ee547c732c3$export$ed4f9641643dc7e4 = function(param) {
|
|
64
|
+
var children = param.children, menuItems = param.menuItems;
|
|
65
|
+
// Menu resources
|
|
66
|
+
var divRef = (0, $gTuX4$react.useRef)(null);
|
|
67
|
+
var menuRef = (0, $gTuX4$react.useRef)(null);
|
|
68
|
+
var _useState = (0, ($parcel$interopDefault($gTuX4$swchelperslib_sliced_to_arrayjs)))((0, $gTuX4$react.useState)(0), 2), menuXPos = _useState[0], setMenuXPos = _useState[1];
|
|
69
|
+
var _useState1 = (0, ($parcel$interopDefault($gTuX4$swchelperslib_sliced_to_arrayjs)))((0, $gTuX4$react.useState)(0), 2), menuYPos = _useState1[0], setMenuYPos = _useState1[1];
|
|
70
|
+
var _useState2 = (0, ($parcel$interopDefault($gTuX4$swchelperslib_sliced_to_arrayjs)))((0, $gTuX4$react.useState)(false), 2), menuVisible = _useState2[0], setMenuVisible = _useState2[1];
|
|
71
|
+
// Show menu when context is requested
|
|
72
|
+
var showMenu = function(e) {
|
|
73
|
+
e.preventDefault();
|
|
74
|
+
e.stopPropagation();
|
|
75
|
+
setMenuVisible(true);
|
|
76
|
+
setMenuXPos(e.pageX);
|
|
77
|
+
setMenuYPos(e.pageY);
|
|
78
|
+
};
|
|
79
|
+
// Handle click off the menu
|
|
80
|
+
var handleClick = (0, $gTuX4$react.useCallback)(function(e) {
|
|
81
|
+
var _menuRef_current;
|
|
82
|
+
if (menuRef.current && (e.target instanceof Element && !((_menuRef_current = menuRef.current) === null || _menuRef_current === void 0 ? void 0 : _menuRef_current.contains(e.target)) || !(e.target instanceof Element))) setMenuVisible(false);
|
|
83
|
+
}, []);
|
|
84
|
+
// Update the document click handler
|
|
85
|
+
(0, $gTuX4$react.useEffect)(function() {
|
|
86
|
+
if (menuVisible) document.addEventListener("mousedown", handleClick);
|
|
87
|
+
else document.removeEventListener("mousedown", handleClick);
|
|
88
|
+
return function() {
|
|
89
|
+
document.removeEventListener("mousedown", handleClick);
|
|
90
|
+
};
|
|
91
|
+
}, [
|
|
92
|
+
handleClick,
|
|
93
|
+
menuVisible
|
|
94
|
+
]);
|
|
95
|
+
return /*#__PURE__*/ (0, $gTuX4$reactjsxruntime.jsxs)((0, $gTuX4$reactjsxruntime.Fragment), {
|
|
96
|
+
children: [
|
|
97
|
+
/*#__PURE__*/ (0, $gTuX4$reactjsxruntime.jsx)("div", {
|
|
98
|
+
onContextMenu: showMenu,
|
|
99
|
+
className: "context-menu-handler",
|
|
100
|
+
children: children
|
|
101
|
+
}),
|
|
102
|
+
menuVisible && /*#__PURE__*/ (0, $gTuX4$reactdom.createPortal)(/*#__PURE__*/ (0, $gTuX4$reactjsxruntime.jsx)("div", {
|
|
103
|
+
style: {
|
|
104
|
+
position: "absolute",
|
|
105
|
+
top: 0,
|
|
106
|
+
left: 0
|
|
107
|
+
},
|
|
108
|
+
ref: divRef,
|
|
109
|
+
children: /*#__PURE__*/ (0, $gTuX4$reactjsxruntime.jsx)((0, $5150b66b01c99189$export$8dc6765e8be191c7), {
|
|
110
|
+
ref: menuRef,
|
|
111
|
+
entries: menuItems,
|
|
112
|
+
xPos: menuXPos,
|
|
113
|
+
yPos: menuYPos,
|
|
114
|
+
toClose: function() {
|
|
115
|
+
return setMenuVisible(false);
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
}), document.body)
|
|
119
|
+
]
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
$parcel$exportWildcard(module.exports, $a68bd8a6c0fd98c2$exports);
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AEAA;;;;ACAA;;AAUO,IAAM,0DAAc,CAAA,GAAA,sCAAI,EAAE,UAAU,CACzC,gBAAmC,KAAqB;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B,GAAG,EAAE;IAEL,oCAAoC;IACpC,CAAA,GAAA,sBAAS,AAAD,EAAE,WAAM;QACd,IAAI,aAAa,SAAS,gBAAgB,CAAC,aAAa;aACnD,SAAS,mBAAmB,CAAC,aAAa;QAC/C,OAAO,WAAM;YACX,SAAS,mBAAmB,CAAC,aAAa;QAC5C;IACF,GAAG;QAAC;QAAa;KAAY;IAE7B,qBACE;;0BACE,gCAAC;gBACC,eAAe;gBACf,WAAU;0BAET;;YAEF,6BACC,CAAA,GAAA,4BAAY,AAAD,gBACT,gCAAC;gBACC,OAAO;oBAAE,UAAU;oBAAY,KAAK;oBAAG,MAAM;gBAAE;gBAC/C,KAAK;0BAEL,cAAA,gCAAC,CAAA,GAAA,yCAAW,AAAD;oBACT,KAAK;oBACL,SAAS;oBACT,MAAM;oBACN,MAAM;oBACN,SAAS;+BAAM,eAAe,KAAK;;;gBAGvC,SAAS,IAAI;;;AAIvB;;AD3EA;;ADAA","sources":["src/main.ts","src/components/index.ts","src/components/ContextMenuHandler.tsx","src/components/ContextMenu.tsx"],"sourcesContent":["export * from './components';\n","import { ContextMenuHandler } from './ContextMenuHandler';\nimport { iMenuItem } from './interface';\n\nexport { ContextMenuHandler };\nexport type { iMenuItem };\n","import { MouseEvent, useCallback, useEffect, useRef, useState } from 'react';\nimport { createPortal } from 'react-dom';\nimport { ContextMenu } from './ContextMenu';\nimport { iMenuItem } from './interface';\nimport './ContextMenu.scss';\n\nexport const ContextMenuHandler = ({\n children,\n menuItems,\n}: {\n children: JSX.Element[] | JSX.Element;\n menuItems: iMenuItem[];\n}): JSX.Element => {\n // Menu resources\n const divRef = useRef<HTMLDivElement | null>(null);\n const menuRef = useRef<HTMLDivElement | null>(null);\n const [menuXPos, setMenuXPos] = useState<number>(0);\n const [menuYPos, setMenuYPos] = useState<number>(0);\n const [menuVisible, setMenuVisible] = useState<boolean>(false);\n\n // Show menu when context is requested\n const showMenu = (e: MouseEvent<HTMLDivElement>) => {\n e.preventDefault();\n e.stopPropagation();\n setMenuVisible(true);\n setMenuXPos(e.pageX);\n setMenuYPos(e.pageY);\n };\n\n // Handle click off the menu\n const handleClick = useCallback((e: globalThis.MouseEvent) => {\n if (\n menuRef.current &&\n ((e.target instanceof Element && !menuRef.current?.contains(e.target)) ||\n !(e.target instanceof Element))\n ) {\n setMenuVisible(false);\n }\n }, []);\n\n // Update the document click handler\n useEffect(() => {\n if (menuVisible) document.addEventListener('mousedown', handleClick);\n else document.removeEventListener('mousedown', handleClick);\n return () => {\n document.removeEventListener('mousedown', handleClick);\n };\n }, [handleClick, menuVisible]);\n\n return (\n <>\n <div\n onContextMenu={showMenu}\n className='context-menu-handler'\n >\n {children}\n </div>\n {menuVisible &&\n createPortal(\n <div\n style={{ position: 'absolute', top: 0, left: 0 }}\n ref={divRef}\n >\n <ContextMenu\n ref={menuRef}\n entries={menuItems}\n xPos={menuXPos}\n yPos={menuYPos}\n toClose={() => setMenuVisible(false)}\n />\n </div>,\n document.body,\n )}\n </>\n );\n};\n","import React, { useContext } from 'react';\nimport { iMenuItem } from './interface';\n\nexport interface contextMenuProps {\n entries: iMenuItem[];\n xPos: number;\n yPos: number;\n toClose: () => void;\n}\n\nexport const ContextMenu = React.forwardRef<HTMLDivElement, contextMenuProps>(\n ({ entries, xPos, yPos, toClose }, ref): JSX.Element => {\n ContextMenu.displayName = 'ContextMenu';\n\n return (\n <div\n ref={ref}\n className='context-menu visible'\n style={{\n top: `${yPos}px`,\n left: `${xPos}px`,\n }}\n >\n {entries.map((e, i) => (\n <div\n key={i}\n className='context-menu-item'\n onClick={() => {\n e.action && e.action();\n toClose();\n }}\n >\n {e.label}\n </div>\n ))}\n </div>\n );\n },\n);\n"],"names":[],"version":3,"file":"main.js.map"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface iMenuItem {
|
|
2
|
+
label: string;
|
|
3
|
+
action?: () => void;
|
|
4
|
+
}
|
|
5
|
+
export const ContextMenuHandler: ({ children, menuItems, }: {
|
|
6
|
+
children: JSX.Element[] | JSX.Element;
|
|
7
|
+
menuItems: iMenuItem[];
|
|
8
|
+
}) => JSX.Element;
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=context-menu.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA;IACE,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;CACrB;AEGD,OAAO,MAAM;cAID,WAAW,EAAE,GAAG,WAAW;eAC1B,SAAS,EAAE;MACpB,WA+DH,CAAC","sources":["src/src/components/interface.ts","src/src/components/ContextMenu.tsx","src/src/components/ContextMenuHandler.tsx","src/src/components/index.ts","src/src/main.ts","src/main.ts"],"sourcesContent":[null,null,null,null,null,"export * from './components';\n"],"names":[],"version":3,"file":"context-menu.d.ts.map"}
|
package/dist/main.css
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
.context-menu-handler {
|
|
2
|
+
height: -webkit-fit-content;
|
|
3
|
+
height: -moz-fit-content;
|
|
4
|
+
height: fit-content;
|
|
5
|
+
width: -webkit-fit-content;
|
|
6
|
+
width: -moz-fit-content;
|
|
7
|
+
width: fit-content;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.context-menu {
|
|
11
|
+
visibility: hidden;
|
|
12
|
+
opacity: 1;
|
|
13
|
+
z-index: 10000;
|
|
14
|
+
background-color: #fbfdf6;
|
|
15
|
+
border: 1px solid #111418;
|
|
16
|
+
border-radius: 8px;
|
|
17
|
+
position: absolute;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.context-menu.visible {
|
|
21
|
+
visibility: inherit;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.context-menu-item {
|
|
25
|
+
color: #111418;
|
|
26
|
+
cursor: pointer;
|
|
27
|
+
min-width: 80px;
|
|
28
|
+
padding: 0 4px;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.context-menu-item:first-child {
|
|
32
|
+
padding-top: 2px;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.context-menu-item:last-child {
|
|
36
|
+
padding-bottom: 2px;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.context-menu-item:hover {
|
|
40
|
+
background-color: #fbe9e6;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.context-menu-item:hover:first-child {
|
|
44
|
+
border-radius: 6px 6px 0 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.context-menu-item:hover:last-child {
|
|
48
|
+
border-radius: 0 0 6px 6px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*# sourceMappingURL=main.css.map */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA;;;;;;;;;AAKA;;;;;;;;;;AAWA;;;;AAIA;;;;;;;AAOA;;;;AAIA;;;;AAIA;;;;AAIA;;;;AAIA","sources":["src/components/ContextMenu.scss"],"sourcesContent":[".context-menu-handler {\n height: fit-content;\n width: fit-content;\n}\n\n.context-menu {\n position: absolute;\n visibility: hidden;\n border: 1px solid;\n border-color: rgb(17, 20, 24);\n border-radius: 8px;\n opacity: 1;\n background-color: rgb(251, 253, 246);\n z-index: 10000;\n}\n\n.context-menu.visible {\n visibility: inherit;\n}\n\n.context-menu-item {\n color: rgb(17, 20, 24);\n cursor: pointer;\n padding: 0 4px;\n min-width: 80px;\n}\n\n.context-menu-item:first-child {\n padding-top: 2px;\n}\n\n.context-menu-item:last-child {\n padding-bottom: 2px;\n}\n\n.context-menu-item:hover {\n background-color: rgb(251, 233, 230);\n}\n\n.context-menu-item:hover:first-child {\n border-radius: 6px 6px 0 0;\n}\n\n.context-menu-item:hover:last-child {\n border-radius: 0 0 6px 6px;\n}\n"],"names":[],"version":3,"file":"main.css.map"}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import "./main.css";
|
|
2
|
+
import {jsxs as $duWW8$jsxs, Fragment as $duWW8$Fragment, jsx as $duWW8$jsx} from "react/jsx-runtime";
|
|
3
|
+
import $duWW8$react, {useRef as $duWW8$useRef, useState as $duWW8$useState, useCallback as $duWW8$useCallback, useEffect as $duWW8$useEffect} from "react";
|
|
4
|
+
import {createPortal as $duWW8$createPortal} from "react-dom";
|
|
5
|
+
|
|
6
|
+
function $parcel$export(e, n, v, s) {
|
|
7
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
8
|
+
}
|
|
9
|
+
var $b65191f6d0a0a991$exports = {};
|
|
10
|
+
|
|
11
|
+
$parcel$export($b65191f6d0a0a991$exports, "ContextMenuHandler", function () { return $1e1c1e9e0b943830$export$ed4f9641643dc7e4; });
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const $567ed433af94513f$export$8dc6765e8be191c7 = /*#__PURE__*/ (0, $duWW8$react).forwardRef(({ entries: entries , xPos: xPos , yPos: yPos , toClose: toClose }, ref)=>{
|
|
18
|
+
$567ed433af94513f$export$8dc6765e8be191c7.displayName = "ContextMenu";
|
|
19
|
+
return /*#__PURE__*/ (0, $duWW8$jsx)("div", {
|
|
20
|
+
ref: ref,
|
|
21
|
+
className: "context-menu visible",
|
|
22
|
+
style: {
|
|
23
|
+
top: `${yPos}px`,
|
|
24
|
+
left: `${xPos}px`
|
|
25
|
+
},
|
|
26
|
+
children: entries.map((e, i)=>/*#__PURE__*/ (0, $duWW8$jsx)("div", {
|
|
27
|
+
className: "context-menu-item",
|
|
28
|
+
onClick: ()=>{
|
|
29
|
+
e.action && e.action();
|
|
30
|
+
toClose();
|
|
31
|
+
},
|
|
32
|
+
children: e.label
|
|
33
|
+
}, i))
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
const $1e1c1e9e0b943830$export$ed4f9641643dc7e4 = ({ children: children , menuItems: menuItems })=>{
|
|
40
|
+
// Menu resources
|
|
41
|
+
const divRef = (0, $duWW8$useRef)(null);
|
|
42
|
+
const menuRef = (0, $duWW8$useRef)(null);
|
|
43
|
+
const [menuXPos, setMenuXPos] = (0, $duWW8$useState)(0);
|
|
44
|
+
const [menuYPos, setMenuYPos] = (0, $duWW8$useState)(0);
|
|
45
|
+
const [menuVisible, setMenuVisible] = (0, $duWW8$useState)(false);
|
|
46
|
+
// Show menu when context is requested
|
|
47
|
+
const showMenu = (e)=>{
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
e.stopPropagation();
|
|
50
|
+
setMenuVisible(true);
|
|
51
|
+
setMenuXPos(e.pageX);
|
|
52
|
+
setMenuYPos(e.pageY);
|
|
53
|
+
};
|
|
54
|
+
// Handle click off the menu
|
|
55
|
+
const handleClick = (0, $duWW8$useCallback)((e)=>{
|
|
56
|
+
var _menuRef_current;
|
|
57
|
+
if (menuRef.current && (e.target instanceof Element && !((_menuRef_current = menuRef.current) === null || _menuRef_current === void 0 ? void 0 : _menuRef_current.contains(e.target)) || !(e.target instanceof Element))) setMenuVisible(false);
|
|
58
|
+
}, []);
|
|
59
|
+
// Update the document click handler
|
|
60
|
+
(0, $duWW8$useEffect)(()=>{
|
|
61
|
+
if (menuVisible) document.addEventListener("mousedown", handleClick);
|
|
62
|
+
else document.removeEventListener("mousedown", handleClick);
|
|
63
|
+
return ()=>{
|
|
64
|
+
document.removeEventListener("mousedown", handleClick);
|
|
65
|
+
};
|
|
66
|
+
}, [
|
|
67
|
+
handleClick,
|
|
68
|
+
menuVisible
|
|
69
|
+
]);
|
|
70
|
+
return /*#__PURE__*/ (0, $duWW8$jsxs)((0, $duWW8$Fragment), {
|
|
71
|
+
children: [
|
|
72
|
+
/*#__PURE__*/ (0, $duWW8$jsx)("div", {
|
|
73
|
+
onContextMenu: showMenu,
|
|
74
|
+
className: "context-menu-handler",
|
|
75
|
+
children: children
|
|
76
|
+
}),
|
|
77
|
+
menuVisible && /*#__PURE__*/ (0, $duWW8$createPortal)(/*#__PURE__*/ (0, $duWW8$jsx)("div", {
|
|
78
|
+
style: {
|
|
79
|
+
position: "absolute",
|
|
80
|
+
top: 0,
|
|
81
|
+
left: 0
|
|
82
|
+
},
|
|
83
|
+
ref: divRef,
|
|
84
|
+
children: /*#__PURE__*/ (0, $duWW8$jsx)((0, $567ed433af94513f$export$8dc6765e8be191c7), {
|
|
85
|
+
ref: menuRef,
|
|
86
|
+
entries: menuItems,
|
|
87
|
+
xPos: menuXPos,
|
|
88
|
+
yPos: menuYPos,
|
|
89
|
+
toClose: ()=>setMenuVisible(false)
|
|
90
|
+
})
|
|
91
|
+
}), document.body)
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
export {$1e1c1e9e0b943830$export$ed4f9641643dc7e4 as ContextMenuHandler};
|
|
102
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;AEAA;;;ACAA;;AAUO,MAAM,0DAAc,CAAA,GAAA,YAAK,AAAD,EAAE,UAAU,CACzC,CAAC,WAAE,QAAO,QAAE,KAAI,QAAE,KAAI,WAAE,QAAO,EAAE,EAAE,MAAqB;IACtD,0CAAY,WAAW,GAAG;IAE1B,qBACE,gBAAC;QACC,KAAK;QACL,WAAU;QACV,OAAO;YACL,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC;YAChB,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;QACnB;kBAEC,QAAQ,GAAG,CAAC,CAAC,GAAG,kBACf,gBAAC;gBAEC,WAAU;gBACV,SAAS,IAAM;oBACb,EAAE,MAAM,IAAI,EAAE,MAAM;oBACpB;gBACF;0BAEC,EAAE,KAAK;eAPH;;AAYf;;;;AD/BK,MAAM,4CAAqB,CAAC,YACjC,SAAQ,aACR,UAAS,EAIV,GAAkB;IACjB,iBAAiB;IACjB,MAAM,SAAS,CAAA,GAAA,aAA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,MAAM;oBACN,SAAS,IAAM,eAAe,KAAK;;gBAGvC,SAAS,IAAI;;;AAIvB;;AD3EA;;ADAA","sources":["src/main.ts","src/components/index.ts","src/components/ContextMenuHandler.tsx","src/components/ContextMenu.tsx"],"sourcesContent":["export * from './components';\n","import { ContextMenuHandler } from './ContextMenuHandler';\nimport { iMenuItem } from './interface';\n\nexport { ContextMenuHandler };\nexport type { iMenuItem };\n","import { MouseEvent, useCallback, useEffect, useRef, useState } from 'react';\nimport { createPortal } from 'react-dom';\nimport { ContextMenu } from './ContextMenu';\nimport { iMenuItem } from './interface';\nimport './ContextMenu.scss';\n\nexport const ContextMenuHandler = ({\n children,\n menuItems,\n}: {\n children: JSX.Element[] | JSX.Element;\n menuItems: iMenuItem[];\n}): JSX.Element => {\n // Menu resources\n const divRef = useRef<HTMLDivElement | null>(null);\n const menuRef = useRef<HTMLDivElement | null>(null);\n const [menuXPos, setMenuXPos] = useState<number>(0);\n const [menuYPos, setMenuYPos] = useState<number>(0);\n const [menuVisible, setMenuVisible] = useState<boolean>(false);\n\n // Show menu when context is requested\n const showMenu = (e: MouseEvent<HTMLDivElement>) => {\n e.preventDefault();\n e.stopPropagation();\n setMenuVisible(true);\n setMenuXPos(e.pageX);\n setMenuYPos(e.pageY);\n };\n\n // Handle click off the menu\n const handleClick = useCallback((e: globalThis.MouseEvent) => {\n if (\n menuRef.current &&\n ((e.target instanceof Element && !menuRef.current?.contains(e.target)) ||\n !(e.target instanceof Element))\n ) {\n setMenuVisible(false);\n }\n }, []);\n\n // Update the document click handler\n useEffect(() => {\n if (menuVisible) document.addEventListener('mousedown', handleClick);\n else document.removeEventListener('mousedown', handleClick);\n return () => {\n document.removeEventListener('mousedown', handleClick);\n };\n }, [handleClick, menuVisible]);\n\n return (\n <>\n <div\n onContextMenu={showMenu}\n className='context-menu-handler'\n >\n {children}\n </div>\n {menuVisible &&\n createPortal(\n <div\n style={{ position: 'absolute', top: 0, left: 0 }}\n ref={divRef}\n >\n <ContextMenu\n ref={menuRef}\n entries={menuItems}\n xPos={menuXPos}\n yPos={menuYPos}\n toClose={() => setMenuVisible(false)}\n />\n </div>,\n document.body,\n )}\n </>\n );\n};\n","import React, { useContext } from 'react';\nimport { iMenuItem } from './interface';\n\nexport interface contextMenuProps {\n entries: iMenuItem[];\n xPos: number;\n yPos: number;\n toClose: () => void;\n}\n\nexport const ContextMenu = React.forwardRef<HTMLDivElement, contextMenuProps>(\n ({ entries, xPos, yPos, toClose }, ref): JSX.Element => {\n ContextMenu.displayName = 'ContextMenu';\n\n return (\n <div\n ref={ref}\n className='context-menu visible'\n style={{\n top: `${yPos}px`,\n left: `${xPos}px`,\n }}\n >\n {entries.map((e, i) => (\n <div\n key={i}\n className='context-menu-item'\n onClick={() => {\n e.action && e.action();\n toClose();\n }}\n >\n {e.label}\n </div>\n ))}\n </div>\n );\n },\n);\n"],"names":[],"version":3,"file":"main.js.map"}
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@asup/context-menu",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "REACT Typescript Context menu component",
|
|
5
|
+
"author": "Paul Thomas <@PaulDThomas>",
|
|
6
|
+
"private": false,
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/PaulDThomas/context-menu.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"react",
|
|
14
|
+
"typescript",
|
|
15
|
+
"simple",
|
|
16
|
+
"treeview"
|
|
17
|
+
],
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/PaulDThomas/context-menu/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/PaulDThomas/context-menu#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@swc/helpers": "^0.4.14",
|
|
24
|
+
"react": "^18.0.0",
|
|
25
|
+
"react-dom": "^18.0.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": "^18.0.0",
|
|
29
|
+
"react-dom": "^18.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@parcel/packager-raw-url": "^2.7.0",
|
|
33
|
+
"@parcel/packager-ts": "^2.8.3",
|
|
34
|
+
"@parcel/transformer-sass": "^2.7.0",
|
|
35
|
+
"@parcel/transformer-typescript-types": "^2.8.0",
|
|
36
|
+
"@parcel/transformer-webmanifest": "^2.7.0",
|
|
37
|
+
"@testing-library/jest-dom": "^5.16.5",
|
|
38
|
+
"@testing-library/react": "^13.4.0",
|
|
39
|
+
"@testing-library/user-event": "^14.4.3",
|
|
40
|
+
"@types/jest": "^29.2.2",
|
|
41
|
+
"@types/node": "^18.11.9",
|
|
42
|
+
"@types/react": "^18.0.25",
|
|
43
|
+
"@types/react-dom": "^18.0.8",
|
|
44
|
+
"eslint-config-prettier": "^8.5.0",
|
|
45
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
46
|
+
"eslint-plugin-react": "^7.31.10",
|
|
47
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
48
|
+
"husky": "^8.0.1",
|
|
49
|
+
"jest": "^29.2.2",
|
|
50
|
+
"jest-environment-jsdom": "^29.3.1",
|
|
51
|
+
"lint-staged": "^13.0.3",
|
|
52
|
+
"parcel": "^2.7.0",
|
|
53
|
+
"postcss": "^8.4.18",
|
|
54
|
+
"prettier": "^2.7.1",
|
|
55
|
+
"process": "^0.11.10",
|
|
56
|
+
"ts-jest": "^29.0.3",
|
|
57
|
+
"ts-node": "^10.9.1",
|
|
58
|
+
"typescript": "^4.9.3"
|
|
59
|
+
},
|
|
60
|
+
"lint-staged": {
|
|
61
|
+
"**/*": "prettier --write --ignore-unknown"
|
|
62
|
+
},
|
|
63
|
+
"main": "dist/cjs/main.js",
|
|
64
|
+
"module": "dist/main.js",
|
|
65
|
+
"types": "dist/context-menu.d.ts",
|
|
66
|
+
"files": [
|
|
67
|
+
"dist"
|
|
68
|
+
],
|
|
69
|
+
"eslintConfig": {
|
|
70
|
+
"extends": [
|
|
71
|
+
"react-app",
|
|
72
|
+
"react-app/jest"
|
|
73
|
+
]
|
|
74
|
+
},
|
|
75
|
+
"browserslist": {
|
|
76
|
+
"production": [
|
|
77
|
+
">0.2%",
|
|
78
|
+
"not dead",
|
|
79
|
+
"not op_mini all"
|
|
80
|
+
],
|
|
81
|
+
"development": [
|
|
82
|
+
"last 1 chrome version",
|
|
83
|
+
"last 1 firefox version",
|
|
84
|
+
"last 1 safari version"
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
"scripts": {
|
|
88
|
+
"build": "parcel build src/main.ts",
|
|
89
|
+
"start": "parcel demo/index.html --dist-dir demo/dist",
|
|
90
|
+
"test": "jest --watch",
|
|
91
|
+
"test:coverage-report": "npm test -- --coverage --watchAll=false"
|
|
92
|
+
}
|
|
93
|
+
}
|