| 1 | "use client"; |
| 2 | |
| 3 | import { useCallback, useEffect |
| 4 | import { createPortal } |
| 5 | import type { LucideIcon |
| 6 | import { MoreHorizontal } |
| 7 | import { |
| 8 | DropdownMenu, |
| 9 | DropdownMenuContent, |
| 10 | DropdownMenuItem, |
| 11 | DropdownMenuSeparator, |
| 12 | DropdownMenuTrigger, |
| 13 | } from "@struxa/ui/components/dropdown-menu"; |
| 14 | |
| 15 | export type ActionItem |
| 16 | | { |
| 17 | label: string |
| 18 | icon?: LucideIcon |
| 19 | onClick: () |
| 20 | destructive?: boolean |
| 21 | } |
| 22 | | "separator" |
| 23 | |
| 24 | // ─── Right-click context menu ──────────────────────────────────────────────── |
| 25 | |
| 26 | interface ContextMenuProps { |
| 27 | items: ActionItem |
| 28 | children: (bind |
| 29 | } |
| 30 | |
| 31 | export function ContextMenu({ |
| 32 | const [pos, |
| 33 | const menuRef = |
| 34 | |
| 35 | const close = |
| 36 | |
| 37 | function handleContextMenu(e |
| 38 | e.preventDefault(); |
| 39 | setPos({ x: |
| 40 | } |
| 41 | |
| 42 | useEffect(() => |
| 43 | if (!pos) |
| 44 | function onPointerDown(e |
| 45 | if (menuRef.current |
| 46 | } |
| 47 | function onKeyDown(e |
| 48 | if (e.key |
| 49 | } |
| 50 | document.addEventListener("pointerdown |
| 51 | document.addEventListener("keydown |
| 52 | return () => |
| 53 | document.removeEventListener("pointerdown |
| 54 | document.removeEventListener("keydown |
| 55 | }; |
| 56 | }, [pos, |
| 57 | |
| 58 | // Push menu inside viewport after it renders |
| 59 | useLayoutEffect(() => |
| 60 | if (!pos || |
| 61 | const rect = |
| 62 | let { x, |
| 63 | if (x + |
| 64 | if (y + |
| 65 | if (x !== |
| 66 | }, [pos]); |
| 67 | |
| 68 | return ( |
| 69 | <> |
| 70 | {children({ onContextMenu: |
| 71 | {pos && |
| 72 | typeof document !== " |
| 73 | createPortal( |
| 74 | <div |
| 75 | ref={menuRef} |
| 76 | style={{ position: |
| 77 | className="min-w-[160px] rounded-xl border border-border bg-card p-1 shadow-lg" |
| 78 | > |
| 79 | {items.map(( |
| 80 | item === "separator" |
| 81 | <div key={i |
| 82 | ) : ( |
| 83 | <button |
| 84 | key={i} |
| 85 | type="button" |
| 86 | onClick={() => |
| 87 | close(); |
| 88 | item.onClick(); |
| 89 | }} |
| 90 | className={`flex w-full items-center gap-2.5 rounded-lg px-3 py-1.5 text-xs transition-colors ${ |
| 91 | item.destructive |
| 92 | ? "text-destructive hover:bg-destructive/10" |
| 93 | : "text-muted-foreground hover:bg-muted hover:text-foreground" |
| 94 | }`} |
| 95 | > |
| 96 | {item.icon && |
| 97 | {item.label} |
| 98 | </button> |
| 99 | ), |
| 100 | )} |
| 101 | </div>, |
| 102 | document.body, |
| 103 | )} |
| 104 | </> |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | // ─── ⋯ row action button ────────────────────────────────────────────────────── |
| 109 | |
| 110 | interface RowMenuProps { |
| 111 | items: ActionItem |
| 112 | } |
| 113 | |
| 114 | export function RowMenu({ |
| 115 | return ( |
| 116 | <DropdownMenu> |
| 117 | <DropdownMenuTrigger |
| 118 | className="flex h-6 w-6 items-center justify-center rounded-md text-muted-foreground/50 outline-none transition-colors hover:bg-muted hover:text-foreground" |
| 119 | onClick={(e) |
| 120 | > |
| 121 | <MoreHorizontal className=" |
| 122 | </DropdownMenuTrigger> |
| 123 | <DropdownMenuContent align=" |
| 124 | {items.map(( |
| 125 | item === "separator" |
| 126 | <DropdownMenuSeparator key={ |
| 127 | ) : ( |
| 128 | <DropdownMenuItem |
| 129 | key={i} |
| 130 | onClick={item.onClick |
| 131 | className={`flex cursor-pointer items-center gap-2.5 px-3 py-2 text-xs ${ |
| 132 | item.destructive ? |
| 133 | }`} |
| 134 | > |
| 135 | {item.icon && |
| 136 | {item.label} |
| 137 | </DropdownMenuItem> |
| 138 | ), |
| 139 | )} |
| 140 | </DropdownMenuContent> |
| 141 | </DropdownMenu> |
| 142 | ); |
| 143 | } |