Download src/components/ui/popover.jsx from guisousa/BaseAdmin: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/spaces/guisousa/BaseAdmin/resolve/main/src/components/ui/popover.jsx
- Command line
-
hf download hf://spaces/guisousa/BaseAdmin/src/components/ui/popover.jsx
-
curl -L -o popover.jsx https://huggingface.co/spaces/guisousa/BaseAdmin/resolve/main/src/components/ui/popover.jsx
1.55 kB
| import React, { createContext, useContext, useState, useRef, useEffect } from "react"; | |
| const Ctx = createContext(null); | |
| export function Popover({ children }) { | |
| const [open, setOpen] = useState(false); | |
| const triggerRef = useRef(null); | |
| const value = { open, setOpen, triggerRef }; | |
| useEffect(() => { | |
| function onDocClick(e) { | |
| if (!triggerRef.current) return; | |
| const content = document.getElementById("__popover-content"); | |
| if (content && (content.contains(e.target) || triggerRef.current.contains(e.target))) return; | |
| setOpen(false); | |
| } | |
| document.addEventListener("mousedown", onDocClick); | |
| return () => document.removeEventListener("mousedown", onDocClick); | |
| }, []); | |
| return <Ctx.Provider value={value}>{children}</Ctx.Provider>; | |
| } | |
| export function PopoverTrigger({ asChild, children }) { | |
| const { setOpen, triggerRef } = useContext(Ctx); | |
| if (asChild && React.isValidElement(children)) { | |
| return React.cloneElement(children, { | |
| ref: triggerRef, | |
| onClick: (e) => { | |
| children.props.onClick && children.props.onClick(e); | |
| setOpen((o) => !o); | |
| }, | |
| }); | |
| } | |
| return ( | |
| <button ref={triggerRef} onClick={() => setOpen((o) => !o)}> | |
| {children} | |
| </button> | |
| ); | |
| } | |
| export function PopoverContent({ className = "", children }) { | |
| const { open } = useContext(Ctx); | |
| if (!open) return null; | |
| return ( | |
| <div | |
| id="__popover-content" | |
| className={`absolute z-50 mt-2 rounded-xl border bg-white shadow-lg ${className}`} | |
| > | |
| {children} | |
| </div> | |
| ); | |
| } | |