Selection
The selection feature in ngDiagram allows users to select nodes on the diagram canvas. Selected nodes are visually highlighted, making it easier to identify and interact with them.
How Selection Works
Section titled “How Selection Works”- Nodes can be selected by clicking on them.
- When a node is selected, it will be visually highlighted according to the styles defined in your application.
- The selected state is managed by ngDiagram and can be accessed via the node’s
selected
property. - Default selection style can be enabled per node using a directive provided by ngDiagram.
Adding Selection Styles to a Custom Node
Section titled “Adding Selection Styles to a Custom Node”Selection can be fully customized by using the selected
property from the node to control its state.
You can assign any CSS class you prefer based on this property and define your own styles for selected nodes.
This allows you to tailor the selection appearance and behavior to fit your application’s needs.
6 collapsed lines
import { Component, input } from '@angular/core';import { type NgDiagramNodeTemplate, type Node } from 'ng-diagram';
@Component({ selector: 'node', template: ` <div class="node"> <div class="node-header">Header {{ node().data.label }}</div> <div class="node-content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </div> </div> `, host: { '[class.custom-selected]': 'isSelected', }, styleUrls: ['./node.component.scss'],})export class CustomNodeComponent implements NgDiagramNodeTemplate { node = input.required<Node>();
get isSelected(): boolean { return this.node().selected ?? false; }}
:host { width: 100%; height: 100%; display: flex; border: 1px solid #ccc;
&.custom-selected { box-shadow: 0px 0px 5px 3px orangered; }
8 collapsed lines
.node-header { font-size: 20px; padding: 3px 10px; } .node-content { font-size: 14px; padding: 3px 10px; }}
Using Default Selection for a Custom Node
Section titled “Using Default Selection for a Custom Node”To make a custom node selectable with default functionality, add the NgDiagramNodeSelectedDirective
as a host directive in your node component.
There is no need to define anything else in your node or to provide custom selection styles.
6 collapsed lines
import { Component, input } from '@angular/core';import { NgDiagramNodeSelectedDirective, type NgDiagramNodeTemplate, type Node,} from 'ng-diagram';
@Component({ selector: 'node', template: ` <div class="node"> <div class="node-header">Header {{ node().data.label }}</div> <div class="node-content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </div> </div> `, hostDirectives: [ { directive: NgDiagramNodeSelectedDirective, inputs: ['node'] }, ], styleUrls: ['./node.component.scss'],})export class DefaultNodeComponent implements NgDiagramNodeTemplate { node = input.required<Node>();}
Customizing Default Selection Styles
Section titled “Customizing Default Selection Styles”If you want to use the default selection functionality, you can still customize its styles.
Here is a CSS variable you can use to style the selection highlight:
--ngd-selected-node-box-shadow // Box shadow for selected node
You can override this variable in your styles to customize the appearance of selected nodes.