From 91416c9898145e570bdf99e621c432059f02f488 Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Mon, 4 Apr 2022 10:19:20 +0200 Subject: [PATCH] feat(docs): Add state page --- docs/src/.vuepress/config.ts | 1 + docs/src/guide/getting-started.md | 14 --- docs/src/guide/node/index.md | 3 +- docs/src/guide/state.md | 147 ++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 16 deletions(-) create mode 100644 docs/src/guide/state.md diff --git a/docs/src/.vuepress/config.ts b/docs/src/.vuepress/config.ts index f17ae003..13f42853 100644 --- a/docs/src/.vuepress/config.ts +++ b/docs/src/.vuepress/config.ts @@ -72,6 +72,7 @@ export default defineUserConfig({ '/guide/', '/guide/getting-started', '/guide/theming', + '/guide/state', { text: 'Nodes', link: '/guide/node/', diff --git a/docs/src/guide/getting-started.md b/docs/src/guide/getting-started.md index 7b0b5140..2da192ce 100644 --- a/docs/src/guide/getting-started.md +++ b/docs/src/guide/getting-started.md @@ -85,20 +85,6 @@ A basic setup would look like this: -### State updates - -State updates like removing elements or updating positions are done by default. If you want to strictly control state -changes you can disable this behavior by setting the `applyDefault` option/prop to `false`. - -```vue:no-line-numbers -
- -
-``` - -To take control of state changes you can implement your own state update handlers or use the state helper functions that -come with the library to mix it up. - ## TypeScript Vue Flow is fully written in [TypeScript](https://www.typescriptlang.org/), so it is highly recommended to use TypeScript to have the best possible DX and diff --git a/docs/src/guide/node/index.md b/docs/src/guide/node/index.md index f11d1bd4..2a070032 100644 --- a/docs/src/guide/node/index.md +++ b/docs/src/guide/node/index.md @@ -8,8 +8,7 @@ Each node requires a unique id and a [xy-position](https://types.vueflow.dev/interfaces/XYPosition.html). Anything else is optional. -You can check the full options for a node element in the TypeDocs [here](https://types.vueflow.dev/interfaces/Node.html) -. +You can check the full options for a node element in the TypeDocs [here](https://types.vueflow.dev/interfaces/Node.html). ## Usage diff --git a/docs/src/guide/state.md b/docs/src/guide/state.md new file mode 100644 index 00000000..1834fc9d --- /dev/null +++ b/docs/src/guide/state.md @@ -0,0 +1,147 @@ +# State + +Under the hood Vue Flow uses [Provide/Inject](https://v3.vuejs.org/guide/component-provide-inject.html) +to pass around it's state between components. +You can access the internal state through the `useVueFlow` composable. + +`useVueFlow` can be used to either create a new state instance and inject it into the current component tree or inject +an already existing store from the current context. +Internal state can be manipulated, for example by adding new elements to the state. The +state is reactive and changes will be reflected on the graph. + +```vue:no-line-numbers{4-6} + +``` + +## Accessing internal state + +Using the composition API also allows us to pass the state around outside the current component context, thus we have a lot more flexibility when it comes +to reading, writing and updating the state. + +Consider this example, where we want to create a Sidebar that allows us to select all nodes. + +```vue:no-line-numbers + + +``` + +We could pass all necessary info as props to the Sidebar, which could become either tedious or result in prop drilling, which we want to avoid. +In this example it wouldn't be a big issue but if our destination was 3 components deep, it would become hard to track the flow of information. + +Instead, we can initialize a Vue Flow store instance __before__ the Sidebar is initialized, thus the instance becomes available as an injection in the component tree. + +```vue:no-line-numbers{5-6} + +``` + +Now we can easily access our current state instance from our Sidebar without passing them as props. + +```vue:no-line-numbers + + +``` + +::: tip +If you have multiple store instances in the same context, make sure to give them a unique id in order to guarantee access to the correct instance. +Otherwise `useVueFlow` will try to inject the first instance it can find in the current context, which would usually be the last one that has been injected. +::: + +## Updates + +State updates like removing elements or updating positions are applied by default. +If you want to strictly control state changes you can disable this behavior by setting the `applyDefault` option/prop to `false`. + +```vue:no-line-numbers +
+ +
+``` + +State changes are emitted by the `onNodesChange` or `onEdgesChange` events, which will provide an array of changes that have been triggered. +To take control of state changes you can implement your own state update handlers or use the state helper functions that +come with the library to mix it up. + +## Access state in options API + +`useVueFlow` was designed to be used in the composition API, __but__ it is still possible to use it in the options API. +Though it is necessary to pass a unique id for your Vue Flow state instance, otherwise a look-up will fail and Vue Flow will create a new state instance +when mounted. + +```vue:no-line-numbers{4,32} + + +```