Skip to content

Commit c1f35f7

Browse files
committed
Add inserting-new-nodes.md
1 parent cd61828 commit c1f35f7

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

docs/.vitepress/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export default defineConfigWithTheme({
4545
}, {
4646
text: 'Adding props',
4747
link: '/guide/adding-props.html'
48+
}, {
49+
text: 'Inserting new nodes',
50+
link: '/guide/inserting-new-nodes.html'
4851
}
4952
]
5053
}, {

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ A new array will be returned and the passed array and its contents should be lef
5858
### See also
5959

6060
* [Example - Inserting between children](/examples.html#inserting-between-children)
61+
* [Guide - Inserting new nodes](/guide/inserting-new-nodes.html)
6162

6263
## eachChild()
6364

docs/guide/inserting-new-nodes.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Inserting new nodes
2+
3+
Adding extra nodes before or after slot contents is relatively easy and doesn't even need a render function. But inserting extra nodes between the slotted nodes is more difficult. Comment nodes from `v-if` typically need to be ignored and fragments coming from `v-for` make it quite difficult to figure out exactly what 'top-level' nodes exist. Further, the fragments tree is an important part of the VNode diffing algorithm, so flattening that tree can lose valuable information.
4+
5+
The `betweenChildren()` helper aims to make inserting nodes between children easier. This example inserts an `<hr>` between 'top-level' children:
6+
7+
```js
8+
import { h } from 'vue'
9+
import { betweenChildren } from '@skirtle/vue-vnode-utils'
10+
11+
// Inside a render function
12+
const children = slots.default?.() || []
13+
14+
const newChildren = betweenChildren(children, (beforeVNode, afterVNode) => {
15+
return h('hr')
16+
})
17+
```
18+
19+
See it on the SFC Playground: [Composition API](https://sfc.vuejs.org/#eNp1lE2P2jAQhv/KKD0kSPmiVS8pH93uuT320lRVIAPxbmJbthNYofz3jp0ESrYgDvbrmWc8Hl4u3pOUcdeil3krvVdMGtBoWrnJO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j6w3P6oHTxsCY0F3Q2uEoGZ0nfXzAJuncQNYWOn6yS/Eb8RPPTBgv3I88DfgcuzoOKSNssSex65R7Hk42V2SS8ig0PRzQYo22ipVE7i4aJ84DvseP4aZ+FoRpddrSVPNSoJVF7oQtxK/Xzhq+sSe7lc4uWhuCNbInbW2lOTnhjiyYyKEs07t7utzNIfdfS+fnwFN/75SOzHaCC48ljnaX6jSXSDlF6Hhbs0+51f9eO3WkvxlgpaQlsrejvH662MAfvhs8OsjwjX/sj5orXV/j3/7pqdT7NfHi+0YVwAlw6X0A1CisTOm/dmJthfovhhHtc1YXlj0ToaaP+uxGyyXspb82PPGnppXaGjAfVXZ9LZbjrDKZ6D1bVooRPaZp+d1tNYTZCRqR0Bt/0/oTto50oqcrgS5qeQF2UpZCbc1Z3Aq8aHN8A8svKIg==)
20+
21+
The callback will be passed the two VNodes, allowing for conditional logic to decide what to insert between them. While the two nodes are considered adjacent from an iteration perspective, they may be in different fragments or have other nodes between them that have been skipped. By default, any comment VNodes will be ignored. The exact insertion position of any returned nodes is not guaranteed, only that it will be somewhere between the two nodes passed to the callback.
22+
23+
If the callback returns an array then the individual items will be inserted, it won't be treated as a fragment. Nested arrays will be treated as fragments. This is similar to passing children to `h()`. If the number of items being inserted is dynamic then wrapping them in a fragment can be a useful technique to ensure that VNodes get paired up correctly across re-renders. A fragment can also make it easier to assign suitable `key` values, as the children of a fragment are only eligible for pairing with each other.
24+
25+
`betweenChildren` takes an optional third argument specifying [iteration options](/api.html#iterationoptions), much like with the [iterators](/guide/iterators.html). Unlike those iterators, the default value for `betweenChildren` is `SKIP_COMMENTS`.

0 commit comments

Comments
 (0)