@nucleux/popover 0.1.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 +21 -0
- package/dist/index.cjs +111 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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 PopoverContext = React__namespace.createContext(null);
|
|
30
|
+
function usePopover(component) {
|
|
31
|
+
const ctx = React__namespace.useContext(PopoverContext);
|
|
32
|
+
if (!ctx) throw new Error(`<${component}> must be used inside <Popover>`);
|
|
33
|
+
return ctx;
|
|
34
|
+
}
|
|
35
|
+
function Popover({ open, defaultOpen = false, onOpenChange, children }) {
|
|
36
|
+
const [internal, setInternal] = React__namespace.useState(defaultOpen);
|
|
37
|
+
const isControlled = open != null;
|
|
38
|
+
const value = isControlled ? open : internal;
|
|
39
|
+
const contentId = React__namespace.useId();
|
|
40
|
+
const containerRef = React__namespace.useRef(null);
|
|
41
|
+
const setOpen = React__namespace.useCallback(
|
|
42
|
+
(next) => {
|
|
43
|
+
if (!isControlled) setInternal(next);
|
|
44
|
+
onOpenChange?.(next);
|
|
45
|
+
},
|
|
46
|
+
[isControlled, onOpenChange]
|
|
47
|
+
);
|
|
48
|
+
React__namespace.useEffect(() => {
|
|
49
|
+
if (!value) return;
|
|
50
|
+
const onDown = (e) => {
|
|
51
|
+
if (!containerRef.current?.contains(e.target)) setOpen(false);
|
|
52
|
+
};
|
|
53
|
+
const onKey = (e) => {
|
|
54
|
+
if (e.key === "Escape") setOpen(false);
|
|
55
|
+
};
|
|
56
|
+
document.addEventListener("mousedown", onDown);
|
|
57
|
+
document.addEventListener("keydown", onKey);
|
|
58
|
+
return () => {
|
|
59
|
+
document.removeEventListener("mousedown", onDown);
|
|
60
|
+
document.removeEventListener("keydown", onKey);
|
|
61
|
+
};
|
|
62
|
+
}, [value, setOpen]);
|
|
63
|
+
return /* @__PURE__ */ jsxRuntime.jsx(PopoverContext.Provider, { value: { open: value, setOpen, contentId }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className: "relative inline-block", children }) });
|
|
64
|
+
}
|
|
65
|
+
var PopoverTrigger = React__namespace.forwardRef(
|
|
66
|
+
({ onClick, type = "button", ...props }, ref) => {
|
|
67
|
+
const { open, setOpen, contentId } = usePopover("PopoverTrigger");
|
|
68
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
69
|
+
"button",
|
|
70
|
+
{
|
|
71
|
+
ref,
|
|
72
|
+
type,
|
|
73
|
+
"aria-haspopup": "dialog",
|
|
74
|
+
"aria-expanded": open,
|
|
75
|
+
"aria-controls": open ? contentId : void 0,
|
|
76
|
+
onClick: (e) => {
|
|
77
|
+
onClick?.(e);
|
|
78
|
+
setOpen(!open);
|
|
79
|
+
},
|
|
80
|
+
...props
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
PopoverTrigger.displayName = "PopoverTrigger";
|
|
86
|
+
var PopoverContent = React__namespace.forwardRef(
|
|
87
|
+
({ align = "start", className, ...props }, ref) => {
|
|
88
|
+
const { open, contentId } = usePopover("PopoverContent");
|
|
89
|
+
if (!open) return null;
|
|
90
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
91
|
+
"div",
|
|
92
|
+
{
|
|
93
|
+
ref,
|
|
94
|
+
id: contentId,
|
|
95
|
+
className: utils.cn(
|
|
96
|
+
"absolute top-full z-50 mt-2 min-w-[12rem] rounded-lg border border-border bg-background p-4 text-foreground shadow-md outline-none animate-nx-fade-in",
|
|
97
|
+
align === "end" ? "right-0" : "left-0",
|
|
98
|
+
className
|
|
99
|
+
),
|
|
100
|
+
...props
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
PopoverContent.displayName = "PopoverContent";
|
|
106
|
+
|
|
107
|
+
exports.Popover = Popover;
|
|
108
|
+
exports.PopoverContent = PopoverContent;
|
|
109
|
+
exports.PopoverTrigger = PopoverTrigger;
|
|
110
|
+
//# sourceMappingURL=index.cjs.map
|
|
111
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":["React","jsx","cn"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,IAAM,cAAA,GAAuBA,+BAA0C,IAAI,CAAA;AAE3E,SAAS,WAAW,SAAA,EAAmB;AACrC,EAAA,MAAM,GAAA,GAAYA,4BAAW,cAAc,CAAA;AAC3C,EAAA,IAAI,CAAC,GAAA,EAAK,MAAM,IAAI,KAAA,CAAM,CAAA,CAAA,EAAI,SAAS,CAAA,+BAAA,CAAiC,CAAA;AACxE,EAAA,OAAO,GAAA;AACT;AAaO,SAAS,QAAQ,EAAE,IAAA,EAAM,cAAc,KAAA,EAAO,YAAA,EAAc,UAAS,EAAiB;AAC3F,EAAA,MAAM,CAAC,QAAA,EAAU,WAAW,CAAA,GAAUA,0BAAS,WAAW,CAAA;AAC1D,EAAA,MAAM,eAAe,IAAA,IAAQ,IAAA;AAC7B,EAAA,MAAM,KAAA,GAAQ,eAAe,IAAA,GAAO,QAAA;AACpC,EAAA,MAAM,YAAkBA,gBAAA,CAAA,KAAA,EAAM;AAC9B,EAAA,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Value {\n open: boolean;\n setOpen: (open: boolean) => void;\n contentId: string;\n}\nconst PopoverContext = React.createContext<PopoverContextValue | null>(null);\n\nfunction usePopover(component: string) {\n const ctx = React.useContext(PopoverContext);\n if (!ctx) throw new Error(`<${component}> must be used inside <Popover>`);\n return ctx;\n}\n\nexport interface PopoverProps {\n open?: boolean;\n defaultOpen?: boolean;\n onOpenChange?: (open: boolean) => void;\n children: React.ReactNode;\n}\n\n/**\n * A click-triggered popover. Compose PopoverTrigger + PopoverContent. Closes on\n * outside click or Escape. Content is positioned relative to the trigger.\n */\nexport function Popover({ open, defaultOpen = false, onOpenChange, children }: PopoverProps) {\n const [internal, setInternal] = React.useState(defaultOpen);\n const isControlled = open != null;\n const value = isControlled ? open : internal;\n const contentId = React.useId();\n const containerRef = React.useRef<HTMLDivElement>(null);\n\n const setOpen = React.useCallback(\n (next: boolean) => {\n if (!isControlled) setInternal(next);\n onOpenChange?.(next);\n },\n [isControlled, onOpenChange],\n );\n\n React.useEffect(() => {\n if (!value) return;\n const onDown = (e: MouseEvent) => {\n if (!containerRef.current?.contains(e.target as Node)) setOpen(false);\n };\n const onKey = (e: KeyboardEvent) => {\n if (e.key === \"Escape\") setOpen(false);\n };\n document.addEventListener(\"mousedown\", onDown);\n document.addEventListener(\"keydown\", onKey);\n return () => {\n document.removeEventListener(\"mousedown\", onDown);\n document.removeEventListener(\"keydown\", onKey);\n };\n }, [value, setOpen]);\n\n return (\n <PopoverContext.Provider value={{ open: value, setOpen, contentId }}>\n <div ref={containerRef} className=\"relative inline-block\">\n {children}\n </div>\n </PopoverContext.Provider>\n );\n}\n\nexport const PopoverTrigger = React.forwardRef<HTMLButtonElement, React.ButtonHTMLAttributes<HTMLButtonElement>>(\n ({ onClick, type = \"button\", ...props }, ref) => {\n const { open, setOpen, contentId } = usePopover(\"PopoverTrigger\");\n return (\n <button\n ref={ref}\n type={type}\n aria-haspopup=\"dialog\"\n aria-expanded={open}\n aria-controls={open ? contentId : undefined}\n onClick={(e) => {\n onClick?.(e);\n setOpen(!open);\n }}\n {...props}\n />\n );\n },\n);\nPopoverTrigger.displayName = \"PopoverTrigger\";\n\nexport interface PopoverContentProps extends React.HTMLAttributes<HTMLDivElement> {\n /** Horizontal alignment relative to the trigger. */\n align?: \"start\" | \"end\";\n}\n\nexport const PopoverContent = React.forwardRef<HTMLDivElement, PopoverContentProps>(\n ({ align = \"start\", className, ...props }, ref) => {\n const { open, contentId } = usePopover(\"PopoverContent\");\n if (!open) return null;\n return (\n <div\n ref={ref}\n id={contentId}\n className={cn(\n \"absolute top-full z-50 mt-2 min-w-[12rem] rounded-lg border border-border bg-background p-4 text-foreground shadow-md outline-none animate-nx-fade-in\",\n align === \"end\" ? \"right-0\" : \"left-0\",\n className,\n )}\n {...props}\n />\n );\n },\n);\nPopoverContent.displayName = \"PopoverContent\";\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
interface PopoverProps {
|
|
4
|
+
open?: boolean;
|
|
5
|
+
defaultOpen?: boolean;
|
|
6
|
+
onOpenChange?: (open: boolean) => void;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A click-triggered popover. Compose PopoverTrigger + PopoverContent. Closes on
|
|
11
|
+
* outside click or Escape. Content is positioned relative to the trigger.
|
|
12
|
+
*/
|
|
13
|
+
declare function Popover({ open, defaultOpen, onOpenChange, children }: PopoverProps): React.JSX.Element;
|
|
14
|
+
declare const PopoverTrigger: React.ForwardRefExoticComponent<React.ButtonHTMLAttributes<HTMLButtonElement> & React.RefAttributes<HTMLButtonElement>>;
|
|
15
|
+
interface PopoverContentProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
16
|
+
/** Horizontal alignment relative to the trigger. */
|
|
17
|
+
align?: "start" | "end";
|
|
18
|
+
}
|
|
19
|
+
declare const PopoverContent: React.ForwardRefExoticComponent<PopoverContentProps & React.RefAttributes<HTMLDivElement>>;
|
|
20
|
+
|
|
21
|
+
export { Popover, PopoverContent, type PopoverContentProps, type PopoverProps, PopoverTrigger };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
interface PopoverProps {
|
|
4
|
+
open?: boolean;
|
|
5
|
+
defaultOpen?: boolean;
|
|
6
|
+
onOpenChange?: (open: boolean) => void;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A click-triggered popover. Compose PopoverTrigger + PopoverContent. Closes on
|
|
11
|
+
* outside click or Escape. Content is positioned relative to the trigger.
|
|
12
|
+
*/
|
|
13
|
+
declare function Popover({ open, defaultOpen, onOpenChange, children }: PopoverProps): React.JSX.Element;
|
|
14
|
+
declare const PopoverTrigger: React.ForwardRefExoticComponent<React.ButtonHTMLAttributes<HTMLButtonElement> & React.RefAttributes<HTMLButtonElement>>;
|
|
15
|
+
interface PopoverContentProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
16
|
+
/** Horizontal alignment relative to the trigger. */
|
|
17
|
+
align?: "start" | "end";
|
|
18
|
+
}
|
|
19
|
+
declare const PopoverContent: React.ForwardRefExoticComponent<PopoverContentProps & React.RefAttributes<HTMLDivElement>>;
|
|
20
|
+
|
|
21
|
+
export { Popover, PopoverContent, type PopoverContentProps, type PopoverProps, PopoverTrigger };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { cn } from '@nucleux/utils';
|
|
4
|
+
import { jsx } from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
// src/index.tsx
|
|
7
|
+
var PopoverContext = React.createContext(null);
|
|
8
|
+
function usePopover(component) {
|
|
9
|
+
const ctx = React.useContext(PopoverContext);
|
|
10
|
+
if (!ctx) throw new Error(`<${component}> must be used inside <Popover>`);
|
|
11
|
+
return ctx;
|
|
12
|
+
}
|
|
13
|
+
function Popover({ open, defaultOpen = false, onOpenChange, children }) {
|
|
14
|
+
const [internal, setInternal] = React.useState(defaultOpen);
|
|
15
|
+
const isControlled = open != null;
|
|
16
|
+
const value = isControlled ? open : internal;
|
|
17
|
+
const contentId = React.useId();
|
|
18
|
+
const containerRef = React.useRef(null);
|
|
19
|
+
const setOpen = React.useCallback(
|
|
20
|
+
(next) => {
|
|
21
|
+
if (!isControlled) setInternal(next);
|
|
22
|
+
onOpenChange?.(next);
|
|
23
|
+
},
|
|
24
|
+
[isControlled, onOpenChange]
|
|
25
|
+
);
|
|
26
|
+
React.useEffect(() => {
|
|
27
|
+
if (!value) return;
|
|
28
|
+
const onDown = (e) => {
|
|
29
|
+
if (!containerRef.current?.contains(e.target)) setOpen(false);
|
|
30
|
+
};
|
|
31
|
+
const onKey = (e) => {
|
|
32
|
+
if (e.key === "Escape") setOpen(false);
|
|
33
|
+
};
|
|
34
|
+
document.addEventListener("mousedown", onDown);
|
|
35
|
+
document.addEventListener("keydown", onKey);
|
|
36
|
+
return () => {
|
|
37
|
+
document.removeEventListener("mousedown", onDown);
|
|
38
|
+
document.removeEventListener("keydown", onKey);
|
|
39
|
+
};
|
|
40
|
+
}, [value, setOpen]);
|
|
41
|
+
return /* @__PURE__ */ jsx(PopoverContext.Provider, { value: { open: value, setOpen, contentId }, children: /* @__PURE__ */ jsx("div", { ref: containerRef, className: "relative inline-block", children }) });
|
|
42
|
+
}
|
|
43
|
+
var PopoverTrigger = React.forwardRef(
|
|
44
|
+
({ onClick, type = "button", ...props }, ref) => {
|
|
45
|
+
const { open, setOpen, contentId } = usePopover("PopoverTrigger");
|
|
46
|
+
return /* @__PURE__ */ jsx(
|
|
47
|
+
"button",
|
|
48
|
+
{
|
|
49
|
+
ref,
|
|
50
|
+
type,
|
|
51
|
+
"aria-haspopup": "dialog",
|
|
52
|
+
"aria-expanded": open,
|
|
53
|
+
"aria-controls": open ? contentId : void 0,
|
|
54
|
+
onClick: (e) => {
|
|
55
|
+
onClick?.(e);
|
|
56
|
+
setOpen(!open);
|
|
57
|
+
},
|
|
58
|
+
...props
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
PopoverTrigger.displayName = "PopoverTrigger";
|
|
64
|
+
var PopoverContent = React.forwardRef(
|
|
65
|
+
({ align = "start", className, ...props }, ref) => {
|
|
66
|
+
const { open, contentId } = usePopover("PopoverContent");
|
|
67
|
+
if (!open) return null;
|
|
68
|
+
return /* @__PURE__ */ jsx(
|
|
69
|
+
"div",
|
|
70
|
+
{
|
|
71
|
+
ref,
|
|
72
|
+
id: contentId,
|
|
73
|
+
className: cn(
|
|
74
|
+
"absolute top-full z-50 mt-2 min-w-[12rem] rounded-lg border border-border bg-background p-4 text-foreground shadow-md outline-none animate-nx-fade-in",
|
|
75
|
+
align === "end" ? "right-0" : "left-0",
|
|
76
|
+
className
|
|
77
|
+
),
|
|
78
|
+
...props
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
PopoverContent.displayName = "PopoverContent";
|
|
84
|
+
|
|
85
|
+
export { Popover, PopoverContent, PopoverTrigger };
|
|
86
|
+
//# sourceMappingURL=index.js.map
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":[],"mappings":";;;;;AAQA,IAAM,cAAA,GAAuB,oBAA0C,IAAI,CAAA;AAE3E,SAAS,WAAW,SAAA,EAAmB;AACrC,EAAA,MAAM,GAAA,GAAY,iBAAW,cAAc,CAAA;AAC3C,EAAA,IAAI,CAAC,GAAA,EAAK,MAAM,IAAI,KAAA,CAAM,CAAA,CAAA,EAAI,SAAS,CAAA,+BAAA,CAAiC,CAAA;AACxE,EAAA,OAAO,GAAA;AACT;AAaO,SAAS,QAAQ,EAAE,IAAA,EAAM,cAAc,KAAA,EA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AAAA,UACT,uJAAA;AAAA,UACA,KAAA,KAAU,QAAQ,SAAA,GAAY,QAAA;AAAA,UAC9B;AAAA,SACF;AAAA,QACC,GAAG;AAAA;AAAA,KACN;AAAA,EAEJ;AACF;AACA,cAAA,CAAe,WAAA,GAAc,gBAAA","file":"index.js","sourcesContent":["import * as React from \"react\";\nimport { cn } from \"@nucleux/utils\";\n\ninterface PopoverContextValue {\n open: boolean;\n setOpen: (open: boolean) => void;\n contentId: string;\n}\nconst PopoverContext = React.createContext<PopoverContextValue | null>(null);\n\nfunction usePopover(component: string) {\n const ctx = React.useContext(PopoverContext);\n if (!ctx) throw new Error(`<${component}> must be used inside <Popover>`);\n return ctx;\n}\n\nexport interface PopoverProps {\n open?: boolean;\n defaultOpen?: boolean;\n onOpenChange?: (open: boolean) => void;\n children: React.ReactNode;\n}\n\n/**\n * A click-triggered popover. Compose PopoverTrigger + PopoverContent. Closes on\n * outside click or Escape. Content is positioned relative to the trigger.\n */\nexport function Popover({ open, defaultOpen = false, onOpenChange, children }: PopoverProps) {\n const [internal, setInternal] = React.useState(defaultOpen);\n const isControlled = open != null;\n const value = isControlled ? open : internal;\n const contentId = React.useId();\n const containerRef = React.useRef<HTMLDivElement>(null);\n\n const setOpen = React.useCallback(\n (next: boolean) => {\n if (!isControlled) setInternal(next);\n onOpenChange?.(next);\n },\n [isControlled, onOpenChange],\n );\n\n React.useEffect(() => {\n if (!value) return;\n const onDown = (e: MouseEvent) => {\n if (!containerRef.current?.contains(e.target as Node)) setOpen(false);\n };\n const onKey = (e: KeyboardEvent) => {\n if (e.key === \"Escape\") setOpen(false);\n };\n document.addEventListener(\"mousedown\", onDown);\n document.addEventListener(\"keydown\", onKey);\n return () => {\n document.removeEventListener(\"mousedown\", onDown);\n document.removeEventListener(\"keydown\", onKey);\n };\n }, [value, setOpen]);\n\n return (\n <PopoverContext.Provider value={{ open: value, setOpen, contentId }}>\n <div ref={containerRef} className=\"relative inline-block\">\n {children}\n </div>\n </PopoverContext.Provider>\n );\n}\n\nexport const PopoverTrigger = React.forwardRef<HTMLButtonElement, React.ButtonHTMLAttributes<HTMLButtonElement>>(\n ({ onClick, type = \"button\", ...props }, ref) => {\n const { open, setOpen, contentId } = usePopover(\"PopoverTrigger\");\n return (\n <button\n ref={ref}\n type={type}\n aria-haspopup=\"dialog\"\n aria-expanded={open}\n aria-controls={open ? contentId : undefined}\n onClick={(e) => {\n onClick?.(e);\n setOpen(!open);\n }}\n {...props}\n />\n );\n },\n);\nPopoverTrigger.displayName = \"PopoverTrigger\";\n\nexport interface PopoverContentProps extends React.HTMLAttributes<HTMLDivElement> {\n /** Horizontal alignment relative to the trigger. */\n align?: \"start\" | \"end\";\n}\n\nexport const PopoverContent = React.forwardRef<HTMLDivElement, PopoverContentProps>(\n ({ align = \"start\", className, ...props }, ref) => {\n const { open, contentId } = usePopover(\"PopoverContent\");\n if (!open) return null;\n return (\n <div\n ref={ref}\n id={contentId}\n className={cn(\n \"absolute top-full z-50 mt-2 min-w-[12rem] rounded-lg border border-border bg-background p-4 text-foreground shadow-md outline-none animate-nx-fade-in\",\n align === \"end\" ? \"right-0\" : \"left-0\",\n className,\n )}\n {...props}\n />\n );\n },\n);\nPopoverContent.displayName = \"PopoverContent\";\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nucleux/popover",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"react": ">=18",
|
|
21
|
+
"react-dom": ">=18"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@nucleux/utils": "0.1.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"tsup": "^8.3.5",
|
|
28
|
+
"typescript": "^5.7.2",
|
|
29
|
+
"@types/react": "^18.3.18",
|
|
30
|
+
"react": "^18.3.1",
|
|
31
|
+
"lucide-react": "^0.469.0"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/intelligaia-open-labs/nucleux.git",
|
|
40
|
+
"directory": "packages/popover"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/intelligaia-open-labs/nucleux#readme",
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup"
|
|
45
|
+
}
|
|
46
|
+
}
|