diff --git a/docs/src/guide/getting-started.md b/docs/src/guide/getting-started.md index 54197206..72aa8dbe 100644 --- a/docs/src/guide/getting-started.md +++ b/docs/src/guide/getting-started.md @@ -37,12 +37,10 @@ $ yarn add @vue-flow/core ## Usage -In Vue Flow, an application structure consists -of [nodes](/typedocs/interfaces/Node) -and [edges](/typedocs/types/Edge), all of which are categorised as -[elements](/typedocs/types/Elements). +In Vue Flow, an application structure consists of [**nodes**](/typedocs/interfaces/Node) +and [**edges**](/typedocs/types/Edge), all of which are categorised as [**elements**](/typedocs/types/Elements). -Each element requires a unique id. +**Each element requires a unique id.** Nodes additionally need an [XY-position](/typedocs/interfaces/XYPosition), while edges require a source and a target, both represented by node ids. diff --git a/docs/src/guide/node.md b/docs/src/guide/node.md index 43490214..2ab9809d 100644 --- a/docs/src/guide/node.md +++ b/docs/src/guide/node.md @@ -1,9 +1,19 @@ -# Nodes +# Introduction to Nodes -Nodes are the building blocks of your graph. They represent any sort of data you want to present in your graph. +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. -They can exist on their own but can be connected to each other with edges to create a map. +Remember, every node is unique and thus **requires a unique id** and **an [XY-position](/typedocs/interfaces/XYPosition)**. -Each node 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). -You can view the full options-list for a node [here](/typedocs/interfaces/Node). +## Adding Nodes to the Graph -## Usage +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. -Generally you create nodes by adding them to the model-value or the nodes prop of the Vue Flow component. +:::code-group -```vue - ``` -For more advanced graphs that require more state access you will want to use the useVueFlow composable. -[useVueFlow](/typedocs/functions/useVueFlow) will provide the [`addNodes`](/typedocs/interfaces/Actions#addnodes) action, -which you can use to add nodes directly to the state. +```vue [] -This action can be even be used outside the component that is rendering the graph, like a Sidebar or a Toolbar. + + + +``` + +::: + +If you are working with more complex graphs that necessitate extensive state access, the `useVueFlow` composable should +be employed. +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 ``` -## [Default Node-Types](/typedocs/types/DefaultNodeTypes) +```vue [] + + + +``` + +::: + +## [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. -A default node comes with two handles. -It represents a branching point in your map. +You have the freedom to determine the location of handles in the node's definition. -You can specify the position of handles in the node definition. +```ts +import { ref } from 'vue' +import { Position } from '@vue-flow/core' -```js{5-6} -const nodes = [ +const nodes = ref([ { id: '1', - label: 'Node 1', + label: 'Default Node', + type: 'default', // You can omit this as it's the fallback type targetHandle: Position.Top, // or Bottom, Left, Right, - sourceHandle: Position.Right, + sourceHandle: Position.Bottom, // or Top, Left, Right, } -] +]) ``` +
+ + + +
+ ### 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', + label: 'Input Node', + type: 'input', + sourceHandle: Position.Bottom, // or Top, Left, Right, + } +]) +``` + +
- +
- -An input node has a single handle, located at the bottom by default. -It represents a starting point of your map. - ### 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', + label: 'Output Node', + type: 'output', + targetHandle: Position.Top, // or Bottom, Left, Right, + } +]) +``` + +
- +
-An output node has a single handle, located at the top by default. -It represents an ending point of your map. -## Custom Nodes +## User-Defined Nodes -In addition to the default node types from the previous chapter, you can define any amount of custom node-types. -Node-types are inferred from your node's definition. +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. -```js{5,11} -const nodes = [ +::: code-group + +```js [nodes ] +import { ref } from 'vue' + +export const nodes = ref([ { id: '1', label: 'Node 1', + // this will create the node-type `custom` type: 'custom', position: { x: 50, y: 50 }, }, { id: '1', label: 'Node 1', + // this will create the node-type `special` type: 'special', position: { x: 150, y: 50 }, } -] +]) ``` -Vue Flow will now try to resolve this node-type to a component. -First and foremost we will look for a definition in the `nodeTypes` object of the state. -After that we will try to resolve the component to a globally registered one that matches the exact name. -Finally, we will check if a template slot has been provided to fill the node-type. +```vue [CustomNode.vue ] + + + +``` + +```ts [nodes ] +import { ref } from 'vue' +import type { Node } from '@vue-flow/core' + +// You can pass 3 optional generic arguments to the Node interface, allowing you to define: +// 1. The data object type +// 2. The events object type +// 3. The possible node types + +export interface CustomData { + hello: string +} + +export interface CustomEvents { + onCustomEvent: (event: MouseEvent) => void +} + +type CustomNodeTypes = 'custom' | 'special' + +type CustomNode = Node + +export const nodes = ref([ + { + id: '1', + label: 'Node 1', + // this will create the node-type `custom` + type: 'custom', + position: { x: 50, y: 50 }, + }, + { + id: '1', + label: 'Node 1', + // this will create the node-type `special` + type: 'special', + position: { x: 150, y: 50 }, + }, + + // this will throw a type error, as the type is not defined in the CustomEdgeTypes + // regardless it would be rendered as a default edge type + { + id: '1', + label: 'Node 1', + type: 'invalid', + position: { x: 150, y: 50 }, + } +]) +``` + +```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 -The easiest way to define custom nodes is, by passing them as template slots. -Your custom node-types are dynamically resolved to slot-names, meaning a node with the type `custom` -will expect a slot to have the name `node-custom`. +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,16} +```vue{9,18} +