Skip to Content

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:

NameTypeDefault
refRefObject<GraphCanvasRef | null>

Required ref for the graph.

selectionsstring[]

Current selections.

Contains both nodes and edges ids.

[]
activesstring[]

Default active selections.

[]
nodesGraphNode[]

Node datas.

[]
edgesGraphEdge[]

Edge datas.

disabledboolean

Disabled or not.

focusOnSelectboolean | "singleOnly"

Whether to focus on select or not.

true
typeSelectionTypes

Type of selection.

'single'
pathSelectionTypePathSelectionTypes

Type of path selection.

'direct'
pathHoverTypePathSelectionTypes

Whether it should active on hover or not.

'out'
onSelection(selectionIds: string[]) => void

On selection change.

and returns SelectionResult:

NameTypeDefault
selectionsstring[]

Selections id array (of nodes and edges).

activesstring[]

The nodes/edges around the selections to highlight.

clearSelections(value?: string[] | undefined) => void

Clear selections method.

addSelection(value: string) => void

A selection method.

selectNodePaths(source: string, target: string) => void

Get the paths between two nodes.

removeSelection(value: string) => void

Remove selection method.

toggleSelection(value: string) => void

Toggle existing selection on/off method.

setSelections(value: string[]) => void

Set internal selections.

onNodeClick(data: GraphNode) => void

On click event pass through.

onCanvasClick(event: MouseEvent) => void

On canvas click event pass through.

onLasso(selections: string[]) => void

When the lasso happened.

onLassoEnd(selections: string[]) => void

When the lasso ended.

onNodePointerOver(node: GraphNode) => void

When node got a pointer over.

onNodePointerOut(node: GraphNode) => void

When node lost pointer over.

Hotkeys

The hotkeys that are bound via this hook are:

  • ctrl/meta + a: Select all nodes
  • escape: Defoucs selections
  • ctrl/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