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 It Works
Section titled “How It 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
selectedproperty. - Default selection style can be enabled per node using a directive provided by ngDiagram.
Custom Node Styling
Section titled “Custom Node Styling”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.
import { Component, input } from '@angular/core';import { type NgDiagramNodeTemplate, type Node } from 'ng-diagram';
@Component({ template: ` <div class="custom-node"> <div class="custom-node__header">Node title</div> <div class="custom-node__content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </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; }}@use '../../../../../../styles/shared.scss' as *;
:host {1 collapsed line
@include custom-node__host;
&:not(.custom-selected):hover { border-color: orangered; } &.custom-selected { box-shadow: 0px 0px 5px 3px orangered; }
9 collapsed lines
.custom-node { &__header { @include custom-node__header; }
&__content { @include custom-node__content; } }}Using Default Styles
Section titled “Using Default Styles”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.
import { Component, input } from '@angular/core';import { NgDiagramNodeSelectedDirective, type NgDiagramNodeTemplate, type Node,} from 'ng-diagram';
@Component({ template: ` <div class="custom-node"> <div class="custom-node__header">Node title</div> <div class="custom-node__content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </div> </div> `, hostDirectives: [ { directive: NgDiagramNodeSelectedDirective, inputs: ['node'] }, ], styleUrls: ['./node.component.scss'],})export class DefaultNodeComponent implements NgDiagramNodeTemplate { node = input.required<Node>();}Customizing Defaults
Section titled “Customizing Defaults”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 nodeYou can override this variable in your styles to customize the appearance of selected nodes.