|
| 1 | +import React, { useState, useRef, useEffect } from "react"; |
| 2 | +import { createPortal } from "react-dom"; |
| 3 | +import styled from "@emotion/styled"; |
| 4 | +import { TooltipWrapper, Tooltip } from "./Tooltip"; |
| 5 | + |
| 6 | +const KebabButton = styled.button<{ active?: boolean }>` |
| 7 | + background: ${(props) => (props.active ? "rgba(255, 255, 255, 0.1)" : "none")}; |
| 8 | + border: 1px solid rgba(255, 255, 255, 0.2); |
| 9 | + color: #cccccc; |
| 10 | + font-size: 10px; |
| 11 | + padding: 2px 8px; |
| 12 | + border-radius: 3px; |
| 13 | + cursor: pointer; |
| 14 | + transition: all 0.2s ease; |
| 15 | + font-family: var(--font-primary); |
| 16 | + display: flex; |
| 17 | + align-items: center; |
| 18 | + justify-content: center; |
| 19 | + white-space: nowrap; |
| 20 | +
|
| 21 | + &:hover { |
| 22 | + background: rgba(255, 255, 255, 0.1); |
| 23 | + border-color: rgba(255, 255, 255, 0.3); |
| 24 | + } |
| 25 | +
|
| 26 | + &:disabled { |
| 27 | + opacity: 0.5; |
| 28 | + cursor: not-allowed; |
| 29 | + } |
| 30 | +`; |
| 31 | + |
| 32 | +const DropdownMenu = styled.div` |
| 33 | + position: fixed; |
| 34 | + background: #1e1e1e; |
| 35 | + border: 1px solid #3e3e42; |
| 36 | + border-radius: 3px; |
| 37 | + box-shadow: 0 4px 16px rgba(0, 0, 0, 0.8); |
| 38 | + z-index: 10000; |
| 39 | + min-width: 160px; |
| 40 | + overflow: hidden; |
| 41 | +`; |
| 42 | + |
| 43 | +const MenuItem = styled.button<{ active?: boolean; disabled?: boolean }>` |
| 44 | + width: 100%; |
| 45 | + background: ${(props) => (props.active ? "rgba(255, 255, 255, 0.15)" : "#1e1e1e")}; |
| 46 | + border: none; |
| 47 | + border-bottom: 1px solid #2d2d30; |
| 48 | + color: ${(props) => (props.disabled ? "#808080" : "#cccccc")}; |
| 49 | + font-size: 11px; |
| 50 | + padding: 8px 12px; |
| 51 | + text-align: left; |
| 52 | + cursor: ${(props) => (props.disabled ? "not-allowed" : "pointer")}; |
| 53 | + transition: all 0.15s ease; |
| 54 | + font-family: var(--font-primary); |
| 55 | + display: flex; |
| 56 | + align-items: center; |
| 57 | + gap: 8px; |
| 58 | + opacity: ${(props) => (props.disabled ? 0.5 : 1)}; |
| 59 | +
|
| 60 | + &:last-child { |
| 61 | + border-bottom: none; |
| 62 | + } |
| 63 | +
|
| 64 | + &:hover { |
| 65 | + background: ${(props) => (props.disabled ? "#1e1e1e" : "rgba(255, 255, 255, 0.15)")}; |
| 66 | + color: ${(props) => (props.disabled ? "#808080" : "#ffffff")}; |
| 67 | + } |
| 68 | +`; |
| 69 | + |
| 70 | +const MenuItemEmoji = styled.span` |
| 71 | + font-size: 13px; |
| 72 | + width: 16px; |
| 73 | + text-align: center; |
| 74 | + flex-shrink: 0; |
| 75 | +`; |
| 76 | + |
| 77 | +const MenuItemLabel = styled.span` |
| 78 | + flex: 1; |
| 79 | +`; |
| 80 | + |
| 81 | +const MenuContainer = styled.div` |
| 82 | + position: relative; |
| 83 | +`; |
| 84 | + |
| 85 | +export interface KebabMenuItem { |
| 86 | + label: string; |
| 87 | + onClick: () => void; |
| 88 | + active?: boolean; |
| 89 | + disabled?: boolean; |
| 90 | + emoji?: string; |
| 91 | + tooltip?: string; |
| 92 | +} |
| 93 | + |
| 94 | +interface KebabMenuProps { |
| 95 | + items: KebabMenuItem[]; |
| 96 | + className?: string; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * A kebab menu (three vertical dots) that displays a dropdown of menu items. |
| 101 | + * |
| 102 | + * Reduces header clutter by collapsing multiple actions into a single button, |
| 103 | + * saving significant horizontal space compared to individual buttons. |
| 104 | + * |
| 105 | + * Uses React Portal to render dropdown at document.body, preventing clipping |
| 106 | + * by parent containers with overflow constraints. |
| 107 | + */ |
| 108 | +export const KebabMenu: React.FC<KebabMenuProps> = ({ items, className }) => { |
| 109 | + const [isOpen, setIsOpen] = useState(false); |
| 110 | + const [dropdownPosition, setDropdownPosition] = useState({ top: 0, left: 0 }); |
| 111 | + const buttonRef = useRef<HTMLButtonElement>(null); |
| 112 | + const menuRef = useRef<HTMLDivElement>(null); |
| 113 | + |
| 114 | + // Calculate dropdown position when menu opens |
| 115 | + useEffect(() => { |
| 116 | + if (isOpen && buttonRef.current) { |
| 117 | + const rect = buttonRef.current.getBoundingClientRect(); |
| 118 | + setDropdownPosition({ |
| 119 | + top: rect.bottom + 4, // 4px gap below button |
| 120 | + left: rect.right - 160, // Align right edge (160px = min-width) |
| 121 | + }); |
| 122 | + } |
| 123 | + }, [isOpen]); |
| 124 | + |
| 125 | + // Close menu when clicking outside |
| 126 | + useEffect(() => { |
| 127 | + if (!isOpen) return; |
| 128 | + |
| 129 | + const handleClickOutside = (e: MouseEvent) => { |
| 130 | + // Check both button and dropdown (which is now in portal) |
| 131 | + if ( |
| 132 | + buttonRef.current && |
| 133 | + !buttonRef.current.contains(e.target as Node) && |
| 134 | + menuRef.current && |
| 135 | + !menuRef.current.contains(e.target as Node) |
| 136 | + ) { |
| 137 | + setIsOpen(false); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + document.addEventListener("mousedown", handleClickOutside); |
| 142 | + return () => document.removeEventListener("mousedown", handleClickOutside); |
| 143 | + }, [isOpen]); |
| 144 | + |
| 145 | + const handleItemClick = (item: KebabMenuItem) => { |
| 146 | + if (item.disabled) return; |
| 147 | + item.onClick(); |
| 148 | + setIsOpen(false); |
| 149 | + }; |
| 150 | + |
| 151 | + const button = ( |
| 152 | + <KebabButton |
| 153 | + ref={buttonRef} |
| 154 | + active={isOpen} |
| 155 | + onClick={() => setIsOpen(!isOpen)} |
| 156 | + className={className} |
| 157 | + > |
| 158 | + ⋮ |
| 159 | + </KebabButton> |
| 160 | + ); |
| 161 | + |
| 162 | + return ( |
| 163 | + <> |
| 164 | + <MenuContainer> |
| 165 | + <TooltipWrapper inline> |
| 166 | + {button} |
| 167 | + <Tooltip align="center">More actions</Tooltip> |
| 168 | + </TooltipWrapper> |
| 169 | + </MenuContainer> |
| 170 | + |
| 171 | + {isOpen && |
| 172 | + createPortal( |
| 173 | + <DropdownMenu |
| 174 | + ref={menuRef} |
| 175 | + style={{ |
| 176 | + top: `${dropdownPosition.top}px`, |
| 177 | + left: `${dropdownPosition.left}px`, |
| 178 | + }} |
| 179 | + > |
| 180 | + {items.map((item, index) => ( |
| 181 | + <MenuItem |
| 182 | + key={index} |
| 183 | + active={item.active} |
| 184 | + disabled={item.disabled} |
| 185 | + onClick={() => handleItemClick(item)} |
| 186 | + title={item.tooltip} |
| 187 | + > |
| 188 | + {item.emoji && <MenuItemEmoji>{item.emoji}</MenuItemEmoji>} |
| 189 | + <MenuItemLabel>{item.label}</MenuItemLabel> |
| 190 | + </MenuItem> |
| 191 | + ))} |
| 192 | + </DropdownMenu>, |
| 193 | + document.body |
| 194 | + )} |
| 195 | + </> |
| 196 | + ); |
| 197 | +}; |
0 commit comments