Selection
Out of the box, reagraph supports selection handled either manually or via the useSelection hook.
useSelection
The useSelection hook will automatically manage selection state and bind some hotkeys for you.
Interfaces
The following selection types are used below:
export type HotkeyTypes = 'selectAll' | 'deselect' | 'delete';
export type PathSelectionTypes = 'direct' | 'out' | 'in' | 'all';
export type SelectionTypes = 'single' | 'multi' | 'multiModifier';The hook accepts SelectionProps:
and returns SelectionResult:
Hotkeys
The hotkeys that are bound via this hook are:
ctrl/meta + a: Select all nodesescape: Defoucs selectionsctrl/meta + click: Toggle node selection
Example
A typical example might look like:
import { GraphCanvas, GraphCanvasRef, useSelection } from 'reagraph';
export const App = () => {
const graphRef = useRef<GraphCanvasRef | null>(null);
const { selections, onNodeClick, onCanvasClick } = useSelection({
ref: graphRef,
nodes: myNodes,
edges: myEdges
});
return (
<GraphCanvas
ref={graphRef}
nodes={myNodes}
edges={myEdges}
selections={selections}
onCanvasClick={onCanvasClick}
onNodeClick={onNodeClick}
/>
);
};Manual Selection
If you donβt wish to use the useSelection hook you can handle the selections yourself manually via
passing down a string[] of ids to the selections prop.
export const App = () => (
<GraphCanvas
nodes={complexNodes}
edges={complexEdges}
selections={['node-id-1']}
/>
);Last updated on