@page "/custom-nodes" Custom Nodes ยท Blow

Custom Nodes

A NodeTemplate renders your own markup inside each node: here a colour node with a working colour picker and two labelled source handles, while every other node falls back to the built-in look through BlowDefaultNode.

@if (node.Type == "color" && node.Data is ColorData data) {
@* nodrag: a press on the picker opens it, rather than selecting and dragging the node. *@
@node.Label
@data.Color
@* Two source handles on one side, moved along it by their style; the ids are what an edge's SourceHandle names, and each letter sits at the height of its handle. *@ } else { }
  • Click the swatch on Crimson or Emerald and pick another colour: the swatch and the hex code under the name follow as you choose.
  • Hover the two handles on the right of a colour node: the tooltips name them Output A and Output B.
  • Drag from either handle to another node to connect it; each edge remembers which of the two it leaves from.
  • Drag a colour node by its name to move it - a press on the swatch opens the picker instead.
  • Tab to Crimson and press C: a connection starts from Output A and is announced by that name, and C again switches it to Output B. Keep pressing Tab until Palette has focus, then Enter connects and Escape gives up.
  • NodeTemplate is a RenderFragment<BlowNode> that replaces the content of every node; it switches on node.Type and reads its own payload from node.Data.
  • The template is part of the page, so @@bind on the picker writes straight into the node's ColorData, and the re-render that follows redraws the node.
  • The picker carries the nodrag class (the canvas's NoDragClass), so pressing it neither selects nor drags the node.
  • BlowHandle goes anywhere in the template: Type and Position say what and on which side, Style="top:35%" moves it along that side, and an Id tells handles of the same type apart. The starting edges pick theirs with SourceHandle = "a" and "b".
  • Ports on the node model declares the two outputs as BlowPorts; a port's Label becomes its handle's tooltip and the name a screen reader hears when connecting from the keyboard.
  • Every other type is handed back to BlowDefaultNode, which draws the built-in content; Mixer's TargetPosition and SourcePosition put its handles on the left and right.
  • The template renders inside the node's own chrome - padding, border, background - and Width = 180 fixes the colour nodes' width. With a template set, the add-ons go in an explicit <ChildContent>.
  • @code { public sealed class ColorData { public required string Color { get; set; } } private List _nodes = new(); private List _edges = new(); protected override void OnInitialized() { _nodes = [ ColorNode("1", "Crimson", "#dc2626", new(80, 80)), ColorNode("2", "Emerald", "#10b981", new(80, 220)), new BlowNode { Id = "3", Label = "Mixer", Position = new(400, 150), TargetPosition = BlowPosition.Left, SourcePosition = BlowPosition.Right }, new BlowNode { Id = "4", Type = "output", Label = "Palette", Position = new(600, 150), TargetPosition = BlowPosition.Left }, ]; _edges = [ new BlowEdge { Id = "e1-3", Source = "1", Target = "3", SourceHandle = "a", Type = BlowEdgeType.SmoothStep }, new BlowEdge { Id = "e2-3", Source = "2", Target = "3", SourceHandle = "b", Type = BlowEdgeType.SmoothStep }, new BlowEdge { Id = "e3-4", Source = "3", Target = "4", Type = BlowEdgeType.SmoothStep }, ]; } // The ports are the model of the two source handles the template draws: their labels are what the handles' // tooltips show and what a screen reader hears while connecting from the keyboard. private static BlowNode ColorNode(string id, string label, string color, BlowXYPosition position) => new() { Id = id, Type = "color", Label = label, Position = position, Width = 180, Data = new ColorData { Color = color }, Ports = [ new BlowPort { Id = "a", Type = BlowHandleType.Source, Label = "Output A" }, new BlowPort { Id = "b", Type = BlowHandleType.Source, Label = "Output B" }, ], }; }