--- title: Nodes --- # Introduction to Nodes Nodes are the underlying components of your graph. They can be any kind of data you want to visualize in your graph, existing independently and being interconnected through edges to create a data map. Remember, every node is unique and thus **requires a unique id** and **an [XY-position](/typedocs/interfaces/XYPosition)**. For the full list of options available for a node, check out the [Node Interface](/typedocs/interfaces/Node). ## Adding Nodes to the Graph Nodes are generally created by adding them to the `mode-value` (using `v-model`) or to the `nodes` prop of the Vue Flow component. This can be done dynamically at any point in your component's lifecycle. :::code-group ```vue [] ``` ```vue [] ``` ::: If you are working with more complex graphs or simply require access to the internal state, the [useVueFlow](/typedocs/functions/useVueFlow) composable will come in handy. The [`addNodes`](/typedocs/interfaces/Actions#addnodes) action is available through [useVueFlow](/typedocs/functions/useVueFlow), allowing you to add nodes straight to the state. What's more, this action isn't limited to the component rendering the graph; it can be utilized elsewhere, like in a Sidebar or Toolbar. ::: code-group ```vue [] ``` ```vue [] ``` ::: ## Removing Nodes from the Graph Similar to adding nodes, nodes can be removed from the graph by removing them from the `mode-value` (using `v-model`) or from the `nodes` prop of the Vue Flow component. ```vue ``` The [`removeNodes`](/typedocs/interfaces/Actions#removeNodes) action is available through [useVueFlow](/typedocs/functions/useVueFlow), allowing you to remove nodes straight from the state. You can also use this action outside the component rendering the graph, like in a Sidebar or Toolbar. ```vue ``` ## Updating Node Data Since nodes are reactive object, you can update their data at any point by simply mutating it. This allows you to disable or enable handles, change the label, or even add new properties to the data object at any point in time. There are multiple ways of achieving this, here are some examples: ::: code-group ```ts [useVueFlow] import { useVueFlow } from '@vue-flow/core' const instance = useVueFlow() // use the `updateNodeData` method to update the data of an edge instance.updateNodeData(edgeId, { hello: 'mona' }) // find the node in the state by its id const node = instance.findNode(nodeId) node.data = { ...node.data, hello: 'world', } // you can also mutate properties like `selectable` or `draggable` node.selectable = false node.draggable = false // or use `updateNode` to update the node directly instance.updateNode(nodeId, { selectable: false, draggable: false }) ``` ```vue [useNode] ``` ```vue [v-model] ``` ::: ## [Predefined Node-Types](/typedocs/types/DefaultNodeTypes) Vue Flow provides several built-in node types that you can leverage immediately. The included node types are `default`, `input`, and `output`. ### Default Node A default node includes two handles and serves as a branching junction in your map. You have the freedom to determine the location of handles in the node's definition. ```ts import { ref } from 'vue' import { Position } from '@vue-flow/core' const nodes = ref([ { id: '1', type: 'default', // You can omit this as it's the fallback type targetPosition: Position.Top, // or Bottom, Left, Right, sourcePosition: Position.Bottom, // or Top, Left, Right, data: { label: 'Default Node' }, } ]) ```
### Input Node An input node features a single handle, which is by default positioned at the bottom. It represents a starting point of your map. ```ts import { ref } from 'vue' import { Position } from '@vue-flow/core' const nodes = ref([ { id: '1', type: 'input', sourcePosition: Position.Bottom, // or Top, Left, Right, data: { label: 'Input Node' }, } ]) ```
### Output Node An output node also possesses a single handle, although it is typically found at the top. This node represents a conclusion point of your map. ```ts import { ref } from 'vue' import { Position } from '@vue-flow/core' const nodes = ref([ { id: '1', type: 'output', targetPosition: Position.Top, // or Bottom, Left, Right, data: { label: 'Output Node' }, } ]) ```
## User-Defined Nodes On top of the default node types mentioned earlier, you can create as many custom node-types as you need. Node-types are determined from your nodes' definitions. ::: code-group ```vue [App.vue ] ``` ```vue [CustomNode.vue ] ``` ```vue{30-31,37-38,45-47} [App.vue ] ``` ```vue [CustomNode.vue ] ``` ::: Vue Flow will then attempt to resolve this node-type to a component. Priority is given to a definition in the nodeTypes object of the state. Next, it tries to match the component to a globally registered one with the same name. Finally, it searches for a provided template slot to fill in the node-type. If no methods produce a result in resolving the component, the default node-type is used as a fallback. ### Template slots One of the easiest ways to define custom nodes is, by passing them as template slots. Dynamic resolution to slot-names is done for your user-defined node-types, meaning a node with the type `custom` is expected to have a slot named `#node-custom`. ```vue{9,18} ``` ### Node-types object Alternatively, node-types can also be defined by passing an object as a prop to the VueFlow component (or as an option to the composable). ::: warning Take precaution to mark your components as raw (utilizing the marked function from the Vue library) to prevent their conversion into reactive objects. Otherwise, Vue will display a warning on the console. ::: ```vue{6-9,26} ``` ::: tip [You can find a working example here](/examples/nodes/). ::: ## [Node Props](/typedocs/interfaces/NodeProps) Your custom nodes are enclosed so that fundamental functions like dragging or selecting operate. But you may wish to expand on these features or implement your business logic inside nodes, thus your nodes receive the following properties: | Prop Name | Description | Type | Optional | |-------------------------------------------------------------|---------------------------------------------------------------------|------------------------------------------------------------|--------------------------------------------| | id | Unique node id | string | | | type | Node Type | string | | | selected | Is node selected | boolean | | | connectable | Can node handles be connected | [HandleConnectable](/typedocs/types/HandleConnectable) | | | position | Node's x, y (relative) position on the graph | [XYPosition](/typedocs/interfaces/XYPosition) | | | dimensions | Dom element dimensions (width, height) | [Dimensions](/typedocs/interfaces/Dimensions) | | | label | Node label, either a string or a VNode. `h('div', props, children)` | string \| VNode \| Component \| Object | | | isValidTargetPos | Called when used as target for new connection | [ValidConnectionFunc](/typedocs/types/ValidConnectionFunc) | | | isValidSourcePos | Called when used as the source for a new connection | [ValidConnectionFunc](/typedocs/types/ValidConnectionFunc) | | | parent | Parent node id | string | | | dragging | Is node currently dragging | boolean | | | resizing | Is node currently resizing | boolean | | | zIndex | Node z-index | number | | | targetPosition | Handle position | [Position](/typedocs/enums/Position) | | | sourcePosition | Handle position | [Position](/typedocs/enums/Position) | | | dragHandle | Drag handle query selector | string | | | data | Additional data of node | any object | | | events | Contextual and custom events of node | [NodeEventsOn](/typedocs/types/NodeEventsOn) | | ## [Node Events](/typedocs/types/NodeEventsHandler) Vue Flow provides two main ways of listening to node events, either by using `useVueFlow` to bind listeners to the event handlers or by binding them to the `` component. ::: code-group ```vue [useVueFlow] ``` ```vue [component] ``` :::

Interact to see events in browser console

## Customizing Appearance ::: tip To override the styles of the default theme, visit the [Theming section](/guide/theming). ::: ### User-Defined Nodes When constructing a new node type, it's necessary for you to add some styling specific to it. User-created nodes don't have any default styles associated and thus need custom styling. ```css .vue-flow__node-custom { background: #9CA8B3; color: #fff; padding: 10px; } ``` ## Implementing Scrolling within Nodes Sometimes, a node might contain a large amount of content, making it difficult for users to view everything without the aid of a scroll function. To facilitate this scrolling ability without invoking zoom or pan behaviors on the node, Vue Flow provides the `noWheelClassName` property. The `noWheelClassName` property allows you to specify a class name that, when applied to a node, will disable the default zoom-on-scroll or pan-on-scroll events on that particular node. By default, the `noWheelClassName` is `nowheel`. ```vue ```
## Preventing Drag Behavior withing Nodes There are certain scenarios where you might need to interact with the contents of a node without triggering a drag action on the node itself. This can be particularly useful when nodes contain interactive elements like input boxes, buttons, or sliders that you want your users to engage with. To accomplish this, Vue Flow provides a `noDragClassName` property. This property allows specification of a class name, which when applied to an element within a node, prevents triggering a drag action on the node when the user interacts with that element. By default, the `noDragClassName` is set as `nodrag`. ```vue ```