Spaces:
Running
Running
Download src/components/ContextMenu.tsx from mrfakename/flow: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/spaces/mrfakename/flow/resolve/main/src/components/ContextMenu.tsx
- Command line
-
hf download hf://spaces/mrfakename/flow/src/components/ContextMenu.tsx
-
curl -L -o ContextMenu.tsx https://huggingface.co/spaces/mrfakename/flow/resolve/main/src/components/ContextMenu.tsx
3.65 kB
| import type { Icon } from "@phosphor-icons/react"; | |
| import { useCallback, useEffect, useLayoutEffect, useRef, useState, type MouseEvent } from "react"; | |
| import { createPortal } from "react-dom"; | |
| export interface MenuItem { | |
| label: string; | |
| icon?: Icon; | |
| danger?: boolean; | |
| disabled?: boolean; | |
| shortcut?: string; | |
| onSelect?: () => void; | |
| /** Renders a divider above this item. */ | |
| separator?: boolean; | |
| href?: string; | |
| /** Custom row content (e.g. colour swatches); `onSelect` and `label` are ignored. */ | |
| render?: React.ReactNode; | |
| } | |
| interface OpenState { | |
| x: number; | |
| y: number; | |
| items: MenuItem[]; | |
| } | |
| /** Right-click menus. Kumo's DropdownMenu needs a trigger element; this one opens at a point. */ | |
| export function useContextMenu() { | |
| const [state, setState] = useState<OpenState | null>(null); | |
| const open = useCallback((e: MouseEvent, items: MenuItem[]) => { | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| setState({ x: e.clientX, y: e.clientY, items }); | |
| }, []); | |
| const close = useCallback(() => setState(null), []); | |
| const menu = state ? <ContextMenu {...state} onClose={close} /> : null; | |
| return { open, close, menu }; | |
| } | |
| function ContextMenu({ x, y, items, onClose }: OpenState & { onClose: () => void }) { | |
| const ref = useRef<HTMLDivElement>(null); | |
| const [pos, setPos] = useState({ x, y }); | |
| useLayoutEffect(() => { | |
| const el = ref.current; | |
| if (!el) return; | |
| const r = el.getBoundingClientRect(); | |
| setPos({ | |
| x: Math.min(x, window.innerWidth - r.width - 8), | |
| y: Math.min(y, window.innerHeight - r.height - 8), | |
| }); | |
| }, [x, y]); | |
| useEffect(() => { | |
| const down = (e: PointerEvent) => { | |
| if (!ref.current?.contains(e.target as Node)) onClose(); | |
| }; | |
| const key = (e: KeyboardEvent) => e.key === "Escape" && onClose(); | |
| window.addEventListener("pointerdown", down, true); | |
| window.addEventListener("keydown", key); | |
| window.addEventListener("blur", onClose); | |
| window.addEventListener("wheel", onClose, { passive: true }); | |
| return () => { | |
| window.removeEventListener("pointerdown", down, true); | |
| window.removeEventListener("keydown", key); | |
| window.removeEventListener("blur", onClose); | |
| window.removeEventListener("wheel", onClose); | |
| }; | |
| }, [onClose]); | |
| return createPortal( | |
| <div ref={ref} className="ctx-menu" role="menu" style={{ left: pos.x, top: pos.y }} onContextMenu={(e) => e.preventDefault()}> | |
| {items.map((it, i) => { | |
| const Icon = it.icon; | |
| const body = ( | |
| <> | |
| {Icon && <Icon size={13} />} | |
| <span className="flex-1 truncate">{it.label}</span> | |
| {it.shortcut && <kbd className="ctx-menu__kbd">{it.shortcut}</kbd>} | |
| </> | |
| ); | |
| const cls = `ctx-menu__item${it.danger ? " is-danger" : ""}`; | |
| return ( | |
| <div key={i}> | |
| {it.separator && i > 0 && <div className="ctx-menu__sep" />} | |
| {it.render ? ( | |
| <div className="ctx-menu__row" role="presentation">{it.render}</div> | |
| ) : it.href ? ( | |
| <a className={cls} role="menuitem" href={it.href} target="_blank" rel="noreferrer" onClick={onClose}> | |
| {body} | |
| </a> | |
| ) : ( | |
| <button | |
| type="button" | |
| className={cls} | |
| role="menuitem" | |
| disabled={it.disabled} | |
| onClick={() => { | |
| onClose(); | |
| it.onSelect?.(); | |
| }} | |
| > | |
| {body} | |
| </button> | |
| )} | |
| </div> | |
| ); | |
| })} | |
| </div>, | |
| document.body, | |
| ); | |
| } | |