I'll help you implement the ghost node approach by modifying your existing code. Here's how we can add the hover points and handle node creation:
import { Position, useReactFlow } from 'reactflow';
import { useState } from 'react';
interface AddNodeButtonProps {
position: Position;
onAdd: (position: Position) => void;
}
const AddNodeButton = ({ position, onAdd }: AddNodeButtonProps) => {
const [isHovered, setIsHovered] = useState(false);
return (
<div
className={`absolute w-6 h-6 rounded-full bg-gray-100 hover:bg-gray-200
flex items-center justify-center cursor-pointer transition-opacity
${position === Position.Top ? 'top-0 left-1/2 -translate-x-1/2 -translate-y-full' : ''}
${position === Position.Right ? 'right-0 top-1/2 translate-x-full -translate-y-1/2' : ''}
${position === Position.Bottom ? 'bottom-0 left-1/2 -translate-x-1/2 translate-y-full' : ''}
${position === Position.Left ? 'left-0 top-1/2 -translate-x-full -translate-y-1/2' : ''}`}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
onClick={() => onAdd(position)}
>
<span className="text-gray-600">+</span>
{isHovered && (
<div className="absolute whitespace-nowrap bg-gray-800 text-white text-xs py-1 px-2 rounded -top-8">
Add connected node
</div>
)}
</div>
);
};
export default function HumanNode({ data, type, selected }: HumanNodeProps) {
const color = NodeRegistry[type].metadata.color;
const [isHovered, setIsHovered] = useState(false);
const reactFlow = useReactFlow();
const handleAddNode = (position: Position) => {
const nodeWidth = 200; // Adjust based on your node size
const nodeHeight = 100;
const spacing = 100; // Space between nodes
// Calculate new node position based on current node and clicked position
const currentNode = reactFlow.getNode(data.id);
if (!currentNode) return;
let newPosition = { x: currentNode.position.x, y: currentNode.position.y };
switch (position) {
case Position.Top:
newPosition.y -= (nodeHeight + spacing);
break;
case Position.Right:
newPosition.x += (nodeWidth + spacing);
break;
case Position.Bottom:
newPosition.y += (nodeHeight + spacing);
break;
case Position.Left:
newPosition.x -= (nodeWidth + spacing);
break;
}
// Create new node
const newNode = {
id: `node-${Math.random()}`,
type: type,
position: newPosition,
data: { name: 'New Node' }
};
// Create edge between nodes
const newEdge = {
id: `edge-${Math.random()}`,
source: data.id,
target: newNode.id,
type: 'smoothstep'
};
reactFlow.addNodes(newNode);
reactFlow.addEdges(newEdge);
};
return (
<div
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
className="relative"
>
<BaseNode className="px-3 py-2" selected={selected}>
<NodeHeader className={`-mx-3 -mt-2 border-b bg-[${color}]`}>
<NodeHeaderIcon>
<DynamicIcon name={NodeRegistry[type].metadata.icon} />
</NodeHeaderIcon>
<NodeHeaderTitle className="text-white">{data.name}</NodeHeaderTitle>
<NodeHeaderActions>
<NodeHeaderDeleteAction className="hover:bg-red-500 text-white hover:text-white" />
</NodeHeaderActions>
</NodeHeader>
</BaseNode>
{isHovered && (
<>
<AddNodeButton position={Position.Top} onAdd={handleAddNode} />
<AddNodeButton position={Position.Right} onAdd={handleAddNode} />
<AddNodeButton position={Position.Bottom} onAdd={handleAddNode} />
<AddNodeButton position={Position.Left} onAdd={handleAddNode} />
</>
)}
</div>
);
}
This implementation:
- Adds hover detection to the entire node
- Shows four "+" buttons when the node is hovered
- Positions the buttons using absolute positioning around the node
- Includes tooltips on button hover
- Handles node creation with proper positioning and edge connection
To make this work well, you'll need to:
- Add appropriate z-index management to ensure the buttons are clickable
- Fine-tune the positioning values (spacing, nodeWidth, nodeHeight) based on your actual node dimensions
- Add appropriate styling to match your design system
- Consider adding node type selection when creating new nodes
You might also want to add a floating action button component to your main flow component:
const FloatingActionButton = () => {
const reactFlow = useReactFlow();
const handleClick = () => {
const position = reactFlow.getViewport();
const newNode = {
id: `node-${Math.random()}`,
type: 'default',
position: { x: -position.x + 100, y: -position.y + 100 },
data: { name: 'New Node' }
};
reactFlow.addNodes(newNode);
};
return (
<button
className="fixed bottom-8 right-8 w-12 h-12 rounded-full bg-blue-500
hover:bg-blue-600 flex items-center justify-center text-white
shadow-lg"
onClick={handleClick}
>
<span className="text-2xl">+</span>
</button>
);
};
Would you like me to explain any part of this implementation in more detail?