File size: 5,525 Bytes
57f0b3e 8f72426 90bf6ec 8f72426 57f0b3e 8f72426 90bf6ec 8f72426 90bf6ec 8f72426 90bf6ec 8f72426 90bf6ec 8f72426 57f0b3e 8f72426 90bf6ec 8f72426 db325d1 8f72426 db325d1 8f72426 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import { useCallback, useEffect, useRef, useState } from "react";
import {
Code2,
Link as LinkIcon,
MoreHorizontal,
Trash2,
Wand2,
} from "lucide-react";
import { useDismissOn } from "../lib/useDismissOn";
/**
* Mobile-only overflow menu for the canvas toolbar.
*
* Bundles Auto-connect / Share / Clear into a single "⋮" dropdown so
* narrow viewports (< md, i.e. <768px) keep the toolbar to three visible
* controls (Load preset, More, Run pipeline) instead of five. Desktop
* renders each action as its own toolbar button and hides this
* component entirely via `md:hidden` on the wrapper `className`.
*
* The component closes itself on:
* - outside click
* - Escape keypress
* - viewport growing past `md` (i.e. the menu's wrapper flips to
* `display:none`; without this guard the React `open` state would
* silently stay `true` and the menu would pop back open if the
* user shrinks the viewport again)
*/
export function MoreMenu({
className = "",
canAutoConnect,
hasEdgesToReplace,
canClear,
canExport,
onAutoConnect,
onShare,
onExport,
onClear,
}: {
className?: string;
canAutoConnect: boolean;
hasEdgesToReplace: boolean;
canClear: boolean;
canExport: boolean;
onAutoConnect: () => void;
onShare: () => void;
onExport: () => void;
onClear: () => void;
}) {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
useDismissOn(open, rootRef, useCallback(() => setOpen(false), []));
// Responsive guard: if the viewport grows past the `md` breakpoint
// while the menu is open, the wrapper becomes `display:none` but the
// React `open` state is unchanged. Resizing back to mobile would then
// re-reveal the menu in an "open" state the user never asked for.
// `matchMedia` fires the moment the breakpoint is crossed so we can
// close the menu eagerly.
useEffect(() => {
if (typeof window === "undefined") return;
const mq = window.matchMedia("(min-width: 768px)");
const onChange = () => {
if (mq.matches) setOpen(false);
};
mq.addEventListener("change", onChange);
return () => mq.removeEventListener("change", onChange);
}, []);
const items: {
key: string;
icon: React.ReactNode;
label: string;
sub?: string;
disabled?: boolean;
tone?: "default" | "danger";
onClick: () => void;
}[] = [
{
key: "auto",
icon: <Wand2 className="w-4 h-4" />,
label: "Auto-connect",
sub: hasEdgesToReplace
? "Re-wire all blocks (replaces existing links)"
: "Wire all blocks into a sensible chain",
disabled: !canAutoConnect,
onClick: onAutoConnect,
},
{
key: "share",
icon: <LinkIcon className="w-4 h-4" />,
label: "Copy share link",
sub: "Copies a URL that restores this pipeline",
onClick: onShare,
},
{
key: "export",
icon: <Code2 className="w-4 h-4" />,
label: "Export .py",
sub: "Download pipeline as a Python script",
disabled: !canExport,
onClick: onExport,
},
{
key: "clear",
icon: <Trash2 className="w-4 h-4" />,
label: "Clear canvas",
sub: "Remove every block and link",
disabled: !canClear,
tone: "danger",
onClick: onClear,
},
];
return (
<div ref={rootRef} className={`relative ${className}`}>
<button
type="button"
onClick={() => setOpen((v) => !v)}
className="btn"
title="More actions"
aria-label="More actions"
aria-haspopup="menu"
aria-expanded={open}
>
<MoreHorizontal className="w-4 h-4" />
</button>
{open && (
// Mobile (<sm): viewport-pinned (canvas toolbar can scroll
// horizontally on tiny screens, so an absolute-anchored menu
// could end up off-edge).
// Desktop (≥sm): anchored to the button.
<div
role="menu"
className="fixed right-3 top-24 sm:absolute sm:right-0 sm:top-full sm:mt-1 rounded-lg border border-edge bg-surface shadow-xl z-30 p-1.5 flex flex-col gap-0.5 w-[min(18rem,calc(100vw-1.5rem))]"
>
{items.map((it) => (
<button
key={it.key}
type="button"
role="menuitem"
disabled={it.disabled}
onClick={() => {
if (it.disabled) return;
it.onClick();
setOpen(false);
}}
className={`flex items-start gap-3 px-3 py-2 rounded-md text-left transition-colors border border-transparent disabled:opacity-40 disabled:cursor-not-allowed ${
it.tone === "danger"
? "hover:bg-danger/10 hover:border-danger/40 text-ink"
: "hover:bg-surfaceAlt hover:border-edge/60 text-ink"
}`}
>
<span
className={`shrink-0 mt-0.5 ${
it.tone === "danger" ? "text-danger" : "text-mute"
}`}
>
{it.icon}
</span>
<span className="min-w-0">
<span className="block text-sm font-medium">{it.label}</span>
{it.sub && (
<span className="block text-[11px] text-mute leading-snug mt-0.5">
{it.sub}
</span>
)}
</span>
</button>
))}
</div>
)}
</div>
);
}
|