toolbar.tsx
Overview
The toolbar.tsx file defines a reusable React component named ToolBar designed to provide a small contextual toolbar with common node actions such as run, duplicate, and delete within a graph or workflow editor interface. The component is part of a larger system handling node-based graph manipulation, likely related to a UI flow or visual programming environment.
This toolbar appears as a tooltip attached to nodes and offers interactive icons with tooltips, enabling users to perform node operations efficiently. The file also includes a small helper component IconWrapper to style the toolbar icons consistently.
Detailed Explanation
Imports
Tooltip components (
TooltipContent,TooltipNode,TooltipTrigger) from a local XYFlow tooltip-node component set.Position enum for tooltip positioning from
@xyflow/react.Icon components (
Copy,Play,Trash2) from thelucide-reacticon set.React types and hooks (
HTMLAttributes,MouseEventHandler,PropsWithChildren,useCallback).Constants and hooks related to node operations (
Operator,useDuplicateNode).Global state store hook
useGraphStoremanaging graph nodes.
Component: IconWrapper
A simple presentational component to wrap icons with consistent styling and pointer cursor behavior.
Signature
function IconWrapper(props: HTMLAttributes<HTMLDivElement>): JSX.Element
Parameters
children: React nodes (usually an icon SVG).Other HTML
divattributes are spread onto the wrapper<div>.
Returns
A styled <div> wrapping the icon children with padding, background color, rounded corners, and cursor styling.
Usage Example
<IconWrapper onClick={handleClick}>
<Copy className="size-3.5" />
</IconWrapper>
Component: ToolBar
The main component providing a tooltip toolbar with node action icons.
Signature
function ToolBar(props: ToolBarProps): JSX.Element
Props (ToolBarProps)
Prop | Type | Description | Optional | Default |
|---|---|---|---|---|
| `boolean \ | undefined` | Indicates if the node is currently selected | Yes |
|
| The label/type of the node (used to handle specific logic like iteration nodes) | No | |
|
| Unique identifier of the node | No | |
|
| Whether to show the run/play icon | Yes |
|
|
| The content that triggers the tooltip (usually the node UI element) | Yes |
Behavior and Implementation Details
Uses the global graph store hooks
useGraphStoreto get methods to delete nodes.On delete icon click:
If the node label matches
Operator.Iteration, it callsdeleteIterationNodeById.Otherwise, it calls
deleteNodeById.
On duplicate icon click:
Calls
duplicateNodeobtained fromuseDuplicateNodehook with the nodeidandlabel.
The run/play icon is conditionally rendered based on
showRunprop.The toolbar is wrapped inside a
TooltipNodecomponent withTooltipTriggeras the clickable/hoverable element, andTooltipContentshows the icon actions.The icons use a consistent wrapper
IconWrapperfor styling and interactivity.Events use
useCallbackto memoize handlers and prevent unwanted rerenders.
Returns
A tooltip-enabled toolbar component that displays icons for actions relevant to a node in a graph editor interface.
Usage Example
<ToolBar id="node-123" label="CustomOperator" selected={true} showRun={false}>
{/* Node visual representation */}
<div>Node Content Here</div>
</ToolBar>
Important Implementation Notes
Conditional Deletion Logic: The toolbar differentiates between normal nodes and "iteration" nodes when deleting, reflecting domain-specific behavior.
Global Store Integration: Uses a centralized store (
useGraphStore) for state management, ensuring all graph modifications are consistent.Tooltip Composition: Leverages composable tooltip components for clean separation of trigger and content, improving accessibility and UX.
Icon Consistency: Icons are wrapped in a small reusable styled container (
IconWrapper) for uniform padding, color, and cursor style.Event Handling: Click handlers call
stopPropagationto prevent triggering underlying node or graph events when interacting with the toolbar.
Interaction with Other Parts of the System
Graph State Store (
useGraphStore): The component interacts closely with the application's graph state, modifying nodes throughdeleteNodeByIdanddeleteIterationNodeById.Node Duplication Hook (
useDuplicateNode): Handles node duplication logic abstracted into a custom hook.Tooltip Components (
TooltipNode,TooltipTrigger,TooltipContent): Utilizes these components to present the toolbar as a contextual tooltip linked to nodes.Operator Constants: Uses the
Operatorenum or constant to identify node types for conditional logic.Icon Library (
lucide-react): Uses SVG icon components for common UI actions (copy, play, delete).
Visual Diagram
componentDiagram
component ToolBar {
+selected: boolean | undefined
+label: string
+id: string
+showRun: boolean
+children: ReactNode
+deleteNode()
+handleDuplicate()
}
component IconWrapper {
+children: ReactNode
+HTMLAttributes<HTMLDivElement>
}
ToolBar --> IconWrapper : uses for icons
ToolBar --> TooltipNode : wraps toolbar
TooltipNode --> TooltipTrigger : trigger element (children)
TooltipNode --> TooltipContent : displays icons
ToolBar --> useGraphStore : deleteNodeById, deleteIterationNodeById
ToolBar --> useDuplicateNode : duplicateNode
Summary
The toolbar.tsx file provides a compact, reusable React toolbar component that integrates with a graph editor system to offer node-specific actions including running, duplicating, and deleting nodes. It uses advanced React patterns such as hooks and context/state stores alongside composable tooltip components and iconography to enhance the user experience in managing complex graph-based workflows.