@nucleux/context-menu 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nucleux
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @nucleux/context-menu
2
+
3
+ Right-click context menu — Nucleux agentic UI for React.
4
+
5
+ Part of **[Nucleux](https://github.com/intelligaia-open-labs/nucleux)** — agentic UI components for React. Install a single component, or the full kit via [`@nucleux/react`](https://www.npmjs.com/package/@nucleux/react).
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @nucleux/context-menu @nucleux/tokens
11
+ ```
12
+
13
+ `react` / `react-dom` are peer dependencies.
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import { ContextMenu } from "@nucleux/context-menu";
19
+ ```
20
+
21
+ Requires the theme once in your app: `import "@nucleux/tokens/styles.css";`
22
+ and the Tailwind preset from `@nucleux/tokens/preset`.
23
+
24
+ ## License
25
+
26
+ MIT © Nucleux
package/dist/index.cjs ADDED
@@ -0,0 +1,128 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ var React = require('react');
5
+ var utils = require('@nucleux/utils');
6
+ var jsxRuntime = require('react/jsx-runtime');
7
+
8
+ function _interopNamespace(e) {
9
+ if (e && e.__esModule) return e;
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
27
+
28
+ // src/index.tsx
29
+ var ContextMenuContext = React__namespace.createContext(null);
30
+ var ContextMenu = React__namespace.forwardRef(
31
+ ({ content, children, className, ...props }, ref) => {
32
+ const [pos, setPos] = React__namespace.useState(null);
33
+ const menuRef = React__namespace.useRef(null);
34
+ const close = React__namespace.useCallback(() => setPos(null), []);
35
+ React__namespace.useEffect(() => {
36
+ if (!pos) return;
37
+ const onDown = (e) => {
38
+ if (!menuRef.current?.contains(e.target)) close();
39
+ };
40
+ const onKey = (e) => e.key === "Escape" && close();
41
+ const onScroll = () => close();
42
+ document.addEventListener("mousedown", onDown);
43
+ document.addEventListener("keydown", onKey);
44
+ window.addEventListener("scroll", onScroll, true);
45
+ return () => {
46
+ document.removeEventListener("mousedown", onDown);
47
+ document.removeEventListener("keydown", onKey);
48
+ window.removeEventListener("scroll", onScroll, true);
49
+ };
50
+ }, [pos, close]);
51
+ React__namespace.useEffect(() => {
52
+ if (pos) menuRef.current?.focus();
53
+ }, [pos]);
54
+ return /* @__PURE__ */ jsxRuntime.jsxs(
55
+ "div",
56
+ {
57
+ ref,
58
+ onContextMenu: (e) => {
59
+ e.preventDefault();
60
+ setPos({ x: e.clientX, y: e.clientY });
61
+ },
62
+ className,
63
+ ...props,
64
+ children: [
65
+ children,
66
+ pos && /* @__PURE__ */ jsxRuntime.jsx(
67
+ "div",
68
+ {
69
+ ref: menuRef,
70
+ role: "menu",
71
+ tabIndex: -1,
72
+ style: { top: pos.y, left: pos.x },
73
+ className: utils.cn(
74
+ "fixed z-50 min-w-[12rem] animate-nx-fade-in rounded-lg border border-border bg-background p-1 text-foreground shadow-md outline-none"
75
+ ),
76
+ children: /* @__PURE__ */ jsxRuntime.jsx(ContextMenuContext.Provider, { value: { close }, children: content })
77
+ }
78
+ )
79
+ ]
80
+ }
81
+ );
82
+ }
83
+ );
84
+ ContextMenu.displayName = "ContextMenu";
85
+ var ContextMenuItem = React__namespace.forwardRef(
86
+ ({ icon, trailing, destructive = false, className, children, onClick, ...props }, ref) => {
87
+ const ctx = React__namespace.useContext(ContextMenuContext);
88
+ return /* @__PURE__ */ jsxRuntime.jsxs(
89
+ "button",
90
+ {
91
+ ref,
92
+ type: "button",
93
+ role: "menuitem",
94
+ onClick: (e) => {
95
+ onClick?.(e);
96
+ if (!e.defaultPrevented) ctx?.close();
97
+ },
98
+ className: utils.cn(
99
+ "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:size-4",
100
+ destructive ? "text-destructive hover:bg-destructive/10" : "text-foreground hover:bg-accent",
101
+ className
102
+ ),
103
+ ...props,
104
+ children: [
105
+ icon && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "shrink-0", children: icon }),
106
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "flex-1 truncate", children }),
107
+ trailing && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "shrink-0 text-muted-foreground", children: trailing })
108
+ ]
109
+ }
110
+ );
111
+ }
112
+ );
113
+ ContextMenuItem.displayName = "ContextMenuItem";
114
+ var ContextMenuSeparator = React__namespace.forwardRef(
115
+ ({ className, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx("div", { ref, role: "separator", className: utils.cn("-mx-1 my-1 h-px bg-border", className), ...props })
116
+ );
117
+ ContextMenuSeparator.displayName = "ContextMenuSeparator";
118
+ var ContextMenuLabel = React__namespace.forwardRef(
119
+ ({ className, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx("div", { ref, className: utils.cn("px-2 py-1.5 text-xs font-medium text-muted-foreground", className), ...props })
120
+ );
121
+ ContextMenuLabel.displayName = "ContextMenuLabel";
122
+
123
+ exports.ContextMenu = ContextMenu;
124
+ exports.ContextMenuItem = ContextMenuItem;
125
+ exports.ContextMenuLabel = ContextMenuLabel;
126
+ exports.ContextMenuSeparator = ContextMenuSeparator;
127
+ //# sourceMappingURL=index.cjs.map
128
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.tsx"],"names":["React","jsxs","jsx","cn"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,IAAM,kBAAA,GAA2BA,+BAA8C,IAAI,CAAA;AAU5E,IAAM,WAAA,GAAoBA,gBAAA,CAAA,UAAA;AAAA,EAC/B,CAAC,EAAE,OAAA,EAAS,QAAA,EAAU,WAAW,GAAG,KAAA,IAAS,GAAA,KAAQ;AACnD,IAAA,MAAM,CAAC,GAAA,EAAK,MAAM,CAAA,GAAUA,0BAA0C,IAAI,CAAA;AAC1E,IAAA,MAAM,OAAA,GAAgBA,wBAAuB,IAAI,CAAA;AACjD,IAAA,MAAM,QAAcA,gBAAA,CAAA,WAAA,CAAY,MAAM,OAAO,IAAI,CAAA,EAAG,EAAE,CAAA;AAEtD,IAAMA,2BAAU,MAAM;AACpB,MAAA,IAAI,CAAC,GAAA,EAAK;AACV,MAAA,MAAM,MAAA,GAAS,CAAC,CAAA,KAAk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AAA,EAAU,UAAA,EAAY,QAAA,EAAA,IAAA,EAAK,CAAA;AAAA,0BAC1CA,cAAA,CAAC,MAAA,EAAA,EAAK,SAAA,EAAU,iBAAA,EAAmB,QAAA,EAAS,CAAA;AAAA,UAC3C,QAAA,oBAAYA,cAAA,CAAC,MAAA,EAAA,EAAK,SAAA,EAAU,kCAAkC,QAAA,EAAA,QAAA,EAAS;AAAA;AAAA;AAAA,KAC1E;AAAA,EAEJ;AACF;AACA,eAAA,CAAgB,WAAA,GAAc,iBAAA;AAEvB,IAAM,oBAAA,GAA6BF,gBAAA,CAAA,UAAA;AAAA,EACxC,CAAC,EAAE,SAAA,EAAW,GAAG,KAAA,EAAM,EAAG,wBACxBE,cAAA,CAAC,KAAA,EAAA,EAAI,GAAA,EAAU,IAAA,EAAK,aAAY,SAAA,EAAWC,QAAA,CAAG,6BAA6B,SAAS,CAAA,EAAI,GAAG,KAAA,EAAO;AAEtG;AACA,oBAAA,CAAqB,WAAA,GAAc,sBAAA;AAE5B,IAAM,gBAAA,GAAyBH,gBAAA,CAAA,UAAA;AAAA,EACpC,CAAC,EAAE,SAAA,EAAW,GAAG,KAAA,IAAS,GAAA,qBACxBE,cAAA,CAAC,KAAA,EAAA,EAAI,GAAA,EAAU,WAAWC,QAAA,CAAG,uDAAA,EAAyD,SAAS,CAAA,EAAI,GAAG,KAAA,EAAO;AAEjH;AACA,gBAAA,CAAiB,WAAA,GAAc,kBAAA","file":"index.cjs","sourcesContent":["import * as React from \"react\";\nimport { cn } from \"@nucleux/utils\";\n\ninterface ContextMenuContextValue {\n close: () => void;\n}\nconst ContextMenuContext = React.createContext<ContextMenuContextValue | null>(null);\n\nexport interface ContextMenuProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"content\"> {\n /** Menu items — compose ContextMenuItem / ContextMenuSeparator / ContextMenuLabel. */\n content: React.ReactNode;\n /** The area that opens the menu on right-click. */\n children: React.ReactNode;\n}\n\n/** Wraps a region so right-clicking it opens a menu at the cursor. */\nexport const ContextMenu = React.forwardRef<HTMLDivElement, ContextMenuProps>(\n ({ content, children, className, ...props }, ref) => {\n const [pos, setPos] = React.useState<{ x: number; y: number } | null>(null);\n const menuRef = React.useRef<HTMLDivElement>(null);\n const close = React.useCallback(() => setPos(null), []);\n\n React.useEffect(() => {\n if (!pos) return;\n const onDown = (e: MouseEvent) => {\n if (!menuRef.current?.contains(e.target as Node)) close();\n };\n const onKey = (e: KeyboardEvent) => e.key === \"Escape\" && close();\n const onScroll = () => close();\n document.addEventListener(\"mousedown\", onDown);\n document.addEventListener(\"keydown\", onKey);\n window.addEventListener(\"scroll\", onScroll, true);\n return () => {\n document.removeEventListener(\"mousedown\", onDown);\n document.removeEventListener(\"keydown\", onKey);\n window.removeEventListener(\"scroll\", onScroll, true);\n };\n }, [pos, close]);\n\n React.useEffect(() => {\n if (pos) menuRef.current?.focus();\n }, [pos]);\n\n return (\n <div\n ref={ref}\n onContextMenu={(e) => {\n e.preventDefault();\n setPos({ x: e.clientX, y: e.clientY });\n }}\n className={className}\n {...props}\n >\n {children}\n {pos && (\n <div\n ref={menuRef}\n role=\"menu\"\n tabIndex={-1}\n style={{ top: pos.y, left: pos.x }}\n className={cn(\n \"fixed z-50 min-w-[12rem] animate-nx-fade-in rounded-lg border border-border bg-background p-1 text-foreground shadow-md outline-none\",\n )}\n >\n <ContextMenuContext.Provider value={{ close }}>{content}</ContextMenuContext.Provider>\n </div>\n )}\n </div>\n );\n },\n);\nContextMenu.displayName = \"ContextMenu\";\n\nexport interface ContextMenuItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n icon?: React.ReactNode;\n trailing?: React.ReactNode;\n destructive?: boolean;\n}\n\n/** A selectable item inside a {@link ContextMenu}. Closes the menu on click. */\nexport const ContextMenuItem = React.forwardRef<HTMLButtonElement, ContextMenuItemProps>(\n ({ icon, trailing, destructive = false, className, children, onClick, ...props }, ref) => {\n const ctx = React.useContext(ContextMenuContext);\n return (\n <button\n ref={ref}\n type=\"button\"\n role=\"menuitem\"\n onClick={(e) => {\n onClick?.(e);\n if (!e.defaultPrevented) ctx?.close();\n }}\n className={cn(\n \"flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:size-4\",\n destructive ? \"text-destructive hover:bg-destructive/10\" : \"text-foreground hover:bg-accent\",\n className,\n )}\n {...props}\n >\n {icon && <span className=\"shrink-0\">{icon}</span>}\n <span className=\"flex-1 truncate\">{children}</span>\n {trailing && <span className=\"shrink-0 text-muted-foreground\">{trailing}</span>}\n </button>\n );\n },\n);\nContextMenuItem.displayName = \"ContextMenuItem\";\n\nexport const ContextMenuSeparator = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(\n ({ className, ...props }, ref) => (\n <div ref={ref} role=\"separator\" className={cn(\"-mx-1 my-1 h-px bg-border\", className)} {...props} />\n ),\n);\nContextMenuSeparator.displayName = \"ContextMenuSeparator\";\n\nexport const ContextMenuLabel = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(\n ({ className, ...props }, ref) => (\n <div ref={ref} className={cn(\"px-2 py-1.5 text-xs font-medium text-muted-foreground\", className)} {...props} />\n ),\n);\nContextMenuLabel.displayName = \"ContextMenuLabel\";\n"]}
@@ -0,0 +1,21 @@
1
+ import * as React from 'react';
2
+
3
+ interface ContextMenuProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "content"> {
4
+ /** Menu items — compose ContextMenuItem / ContextMenuSeparator / ContextMenuLabel. */
5
+ content: React.ReactNode;
6
+ /** The area that opens the menu on right-click. */
7
+ children: React.ReactNode;
8
+ }
9
+ /** Wraps a region so right-clicking it opens a menu at the cursor. */
10
+ declare const ContextMenu: React.ForwardRefExoticComponent<ContextMenuProps & React.RefAttributes<HTMLDivElement>>;
11
+ interface ContextMenuItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
12
+ icon?: React.ReactNode;
13
+ trailing?: React.ReactNode;
14
+ destructive?: boolean;
15
+ }
16
+ /** A selectable item inside a {@link ContextMenu}. Closes the menu on click. */
17
+ declare const ContextMenuItem: React.ForwardRefExoticComponent<ContextMenuItemProps & React.RefAttributes<HTMLButtonElement>>;
18
+ declare const ContextMenuSeparator: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLDivElement> & React.RefAttributes<HTMLDivElement>>;
19
+ declare const ContextMenuLabel: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLDivElement> & React.RefAttributes<HTMLDivElement>>;
20
+
21
+ export { ContextMenu, ContextMenuItem, type ContextMenuItemProps, ContextMenuLabel, type ContextMenuProps, ContextMenuSeparator };
@@ -0,0 +1,21 @@
1
+ import * as React from 'react';
2
+
3
+ interface ContextMenuProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "content"> {
4
+ /** Menu items — compose ContextMenuItem / ContextMenuSeparator / ContextMenuLabel. */
5
+ content: React.ReactNode;
6
+ /** The area that opens the menu on right-click. */
7
+ children: React.ReactNode;
8
+ }
9
+ /** Wraps a region so right-clicking it opens a menu at the cursor. */
10
+ declare const ContextMenu: React.ForwardRefExoticComponent<ContextMenuProps & React.RefAttributes<HTMLDivElement>>;
11
+ interface ContextMenuItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
12
+ icon?: React.ReactNode;
13
+ trailing?: React.ReactNode;
14
+ destructive?: boolean;
15
+ }
16
+ /** A selectable item inside a {@link ContextMenu}. Closes the menu on click. */
17
+ declare const ContextMenuItem: React.ForwardRefExoticComponent<ContextMenuItemProps & React.RefAttributes<HTMLButtonElement>>;
18
+ declare const ContextMenuSeparator: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLDivElement> & React.RefAttributes<HTMLDivElement>>;
19
+ declare const ContextMenuLabel: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLDivElement> & React.RefAttributes<HTMLDivElement>>;
20
+
21
+ export { ContextMenu, ContextMenuItem, type ContextMenuItemProps, ContextMenuLabel, type ContextMenuProps, ContextMenuSeparator };
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ "use client";
2
+ import * as React from 'react';
3
+ import { cn } from '@nucleux/utils';
4
+ import { jsxs, jsx } from 'react/jsx-runtime';
5
+
6
+ // src/index.tsx
7
+ var ContextMenuContext = React.createContext(null);
8
+ var ContextMenu = React.forwardRef(
9
+ ({ content, children, className, ...props }, ref) => {
10
+ const [pos, setPos] = React.useState(null);
11
+ const menuRef = React.useRef(null);
12
+ const close = React.useCallback(() => setPos(null), []);
13
+ React.useEffect(() => {
14
+ if (!pos) return;
15
+ const onDown = (e) => {
16
+ if (!menuRef.current?.contains(e.target)) close();
17
+ };
18
+ const onKey = (e) => e.key === "Escape" && close();
19
+ const onScroll = () => close();
20
+ document.addEventListener("mousedown", onDown);
21
+ document.addEventListener("keydown", onKey);
22
+ window.addEventListener("scroll", onScroll, true);
23
+ return () => {
24
+ document.removeEventListener("mousedown", onDown);
25
+ document.removeEventListener("keydown", onKey);
26
+ window.removeEventListener("scroll", onScroll, true);
27
+ };
28
+ }, [pos, close]);
29
+ React.useEffect(() => {
30
+ if (pos) menuRef.current?.focus();
31
+ }, [pos]);
32
+ return /* @__PURE__ */ jsxs(
33
+ "div",
34
+ {
35
+ ref,
36
+ onContextMenu: (e) => {
37
+ e.preventDefault();
38
+ setPos({ x: e.clientX, y: e.clientY });
39
+ },
40
+ className,
41
+ ...props,
42
+ children: [
43
+ children,
44
+ pos && /* @__PURE__ */ jsx(
45
+ "div",
46
+ {
47
+ ref: menuRef,
48
+ role: "menu",
49
+ tabIndex: -1,
50
+ style: { top: pos.y, left: pos.x },
51
+ className: cn(
52
+ "fixed z-50 min-w-[12rem] animate-nx-fade-in rounded-lg border border-border bg-background p-1 text-foreground shadow-md outline-none"
53
+ ),
54
+ children: /* @__PURE__ */ jsx(ContextMenuContext.Provider, { value: { close }, children: content })
55
+ }
56
+ )
57
+ ]
58
+ }
59
+ );
60
+ }
61
+ );
62
+ ContextMenu.displayName = "ContextMenu";
63
+ var ContextMenuItem = React.forwardRef(
64
+ ({ icon, trailing, destructive = false, className, children, onClick, ...props }, ref) => {
65
+ const ctx = React.useContext(ContextMenuContext);
66
+ return /* @__PURE__ */ jsxs(
67
+ "button",
68
+ {
69
+ ref,
70
+ type: "button",
71
+ role: "menuitem",
72
+ onClick: (e) => {
73
+ onClick?.(e);
74
+ if (!e.defaultPrevented) ctx?.close();
75
+ },
76
+ className: cn(
77
+ "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:size-4",
78
+ destructive ? "text-destructive hover:bg-destructive/10" : "text-foreground hover:bg-accent",
79
+ className
80
+ ),
81
+ ...props,
82
+ children: [
83
+ icon && /* @__PURE__ */ jsx("span", { className: "shrink-0", children: icon }),
84
+ /* @__PURE__ */ jsx("span", { className: "flex-1 truncate", children }),
85
+ trailing && /* @__PURE__ */ jsx("span", { className: "shrink-0 text-muted-foreground", children: trailing })
86
+ ]
87
+ }
88
+ );
89
+ }
90
+ );
91
+ ContextMenuItem.displayName = "ContextMenuItem";
92
+ var ContextMenuSeparator = React.forwardRef(
93
+ ({ className, ...props }, ref) => /* @__PURE__ */ jsx("div", { ref, role: "separator", className: cn("-mx-1 my-1 h-px bg-border", className), ...props })
94
+ );
95
+ ContextMenuSeparator.displayName = "ContextMenuSeparator";
96
+ var ContextMenuLabel = React.forwardRef(
97
+ ({ className, ...props }, ref) => /* @__PURE__ */ jsx("div", { ref, className: cn("px-2 py-1.5 text-xs font-medium text-muted-foreground", className), ...props })
98
+ );
99
+ ContextMenuLabel.displayName = "ContextMenuLabel";
100
+
101
+ export { ContextMenu, ContextMenuItem, ContextMenuLabel, ContextMenuSeparator };
102
+ //# sourceMappingURL=index.js.map
103
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.tsx"],"names":[],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CAAC,KAAA,EAAA,EAAI,GAAA,EAAU,WAAW,EAAA,CAAG,uDAAA,EAAyD,SAAS,CAAA,EAAI,GAAG,KAAA,EAAO;AAEjH;AACA,gBAAA,CAAiB,WAAA,GAAc,kBAAA","file":"index.js","sourcesContent":["import * as React from \"react\";\nimport { cn } from \"@nucleux/utils\";\n\ninterface ContextMenuContextValue {\n close: () => void;\n}\nconst ContextMenuContext = React.createContext<ContextMenuContextValue | null>(null);\n\nexport interface ContextMenuProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"content\"> {\n /** Menu items — compose ContextMenuItem / ContextMenuSeparator / ContextMenuLabel. */\n content: React.ReactNode;\n /** The area that opens the menu on right-click. */\n children: React.ReactNode;\n}\n\n/** Wraps a region so right-clicking it opens a menu at the cursor. */\nexport const ContextMenu = React.forwardRef<HTMLDivElement, ContextMenuProps>(\n ({ content, children, className, ...props }, ref) => {\n const [pos, setPos] = React.useState<{ x: number; y: number } | null>(null);\n const menuRef = React.useRef<HTMLDivElement>(null);\n const close = React.useCallback(() => setPos(null), []);\n\n React.useEffect(() => {\n if (!pos) return;\n const onDown = (e: MouseEvent) => {\n if (!menuRef.current?.contains(e.target as Node)) close();\n };\n const onKey = (e: KeyboardEvent) => e.key === \"Escape\" && close();\n const onScroll = () => close();\n document.addEventListener(\"mousedown\", onDown);\n document.addEventListener(\"keydown\", onKey);\n window.addEventListener(\"scroll\", onScroll, true);\n return () => {\n document.removeEventListener(\"mousedown\", onDown);\n document.removeEventListener(\"keydown\", onKey);\n window.removeEventListener(\"scroll\", onScroll, true);\n };\n }, [pos, close]);\n\n React.useEffect(() => {\n if (pos) menuRef.current?.focus();\n }, [pos]);\n\n return (\n <div\n ref={ref}\n onContextMenu={(e) => {\n e.preventDefault();\n setPos({ x: e.clientX, y: e.clientY });\n }}\n className={className}\n {...props}\n >\n {children}\n {pos && (\n <div\n ref={menuRef}\n role=\"menu\"\n tabIndex={-1}\n style={{ top: pos.y, left: pos.x }}\n className={cn(\n \"fixed z-50 min-w-[12rem] animate-nx-fade-in rounded-lg border border-border bg-background p-1 text-foreground shadow-md outline-none\",\n )}\n >\n <ContextMenuContext.Provider value={{ close }}>{content}</ContextMenuContext.Provider>\n </div>\n )}\n </div>\n );\n },\n);\nContextMenu.displayName = \"ContextMenu\";\n\nexport interface ContextMenuItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n icon?: React.ReactNode;\n trailing?: React.ReactNode;\n destructive?: boolean;\n}\n\n/** A selectable item inside a {@link ContextMenu}. Closes the menu on click. */\nexport const ContextMenuItem = React.forwardRef<HTMLButtonElement, ContextMenuItemProps>(\n ({ icon, trailing, destructive = false, className, children, onClick, ...props }, ref) => {\n const ctx = React.useContext(ContextMenuContext);\n return (\n <button\n ref={ref}\n type=\"button\"\n role=\"menuitem\"\n onClick={(e) => {\n onClick?.(e);\n if (!e.defaultPrevented) ctx?.close();\n }}\n className={cn(\n \"flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:size-4\",\n destructive ? \"text-destructive hover:bg-destructive/10\" : \"text-foreground hover:bg-accent\",\n className,\n )}\n {...props}\n >\n {icon && <span className=\"shrink-0\">{icon}</span>}\n <span className=\"flex-1 truncate\">{children}</span>\n {trailing && <span className=\"shrink-0 text-muted-foreground\">{trailing}</span>}\n </button>\n );\n },\n);\nContextMenuItem.displayName = \"ContextMenuItem\";\n\nexport const ContextMenuSeparator = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(\n ({ className, ...props }, ref) => (\n <div ref={ref} role=\"separator\" className={cn(\"-mx-1 my-1 h-px bg-border\", className)} {...props} />\n ),\n);\nContextMenuSeparator.displayName = \"ContextMenuSeparator\";\n\nexport const ContextMenuLabel = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(\n ({ className, ...props }, ref) => (\n <div ref={ref} className={cn(\"px-2 py-1.5 text-xs font-medium text-muted-foreground\", className)} {...props} />\n ),\n);\nContextMenuLabel.displayName = \"ContextMenuLabel\";\n"]}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@nucleux/context-menu",
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "description": "Right-click context menu — Nucleux agentic UI for React.",
7
+ "keywords": [
8
+ "nucleux",
9
+ "react",
10
+ "ui",
11
+ "components",
12
+ "agentic",
13
+ "ai",
14
+ "tailwind",
15
+ "context-menu"
16
+ ],
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
25
+ }
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md"
30
+ ],
31
+ "peerDependencies": {
32
+ "react": ">=18",
33
+ "react-dom": ">=18"
34
+ },
35
+ "dependencies": {
36
+ "lucide-react": "^0.469.0",
37
+ "@nucleux/utils": "0.2.0"
38
+ },
39
+ "devDependencies": {
40
+ "tsup": "^8.3.5",
41
+ "typescript": "^5.7.2",
42
+ "@types/react": "^18.3.18",
43
+ "react": "^18.3.1"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "license": "MIT",
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/intelligaia-open-labs/nucleux.git",
52
+ "directory": "packages/context-menu"
53
+ },
54
+ "homepage": "https://github.com/intelligaia-open-labs/nucleux#readme",
55
+ "scripts": {
56
+ "build": "tsup"
57
+ }
58
+ }