import React, { memo, useEffect, useState } from "react"; import { PopoverInteractionKind as BlueprintPopoverInteractionKind } from "@blueprintjs/core"; import { ValidIconName } from "../../../components/Icon/canonicalIconNames"; import IconButton from "../../../components/Icon/IconButton"; import { CLASSPREFIX as eccgui } from "../../../configuration/constants"; import ContextOverlay, { ContextOverlayProps } from "./../../../components/ContextOverlay/ContextOverlay"; // Functions regarding the menu that can be called from the outside export interface NodeToolsMenuFunctions { /** Closes the menu if its open. */ closeMenu: () => void; } export interface NodeToolsProps extends Omit { children: string | React.JSX.Element; togglerElement?: ValidIconName | React.JSX.Element; togglerText?: string; menuButtonDataTestId?: string; /** If defined this function will be called with the menu API object to be used externally. */ menuFunctionsCallback?: (menuFunctions: NodeToolsMenuFunctions) => any; } export const NodeTools = memo( ({ children, togglerElement = "item-moremenu", togglerText = "Show more options", menuButtonDataTestId, menuFunctionsCallback, ...otherOverlayProps }: NodeToolsProps) => { const [isOpen, setIsOpen] = useState(false); useEffect(() => { menuFunctionsCallback && menuFunctionsCallback({ closeMenu(): void { setIsOpen(false); }, }); }, [menuFunctionsCallback]); return ( setIsOpen(false)} hoverCloseDelay={500} interactionKind={isOpen ? BlueprintPopoverInteractionKind.HOVER : BlueprintPopoverInteractionKind.CLICK} {...otherOverlayProps} content={
{children}
} > {typeof togglerElement === "string" ? ( setIsOpen((previous) => !previous)} /> ) : ( <>{togglerElement} )}
); }, );