Recently, I've been developing a window manager component using Vue 3. I began this project by creating a proof of concept (POC) using a standard component approach, relying on props and for loops.
Once the POC reached a certain stage, and I had a clear grasp of the basic cases and resizing logic, I shifted my focus to managing windows and nesting. Initially, the POC only facilitated window arrangement along the x-axis. However, for long-term viability, we required support for both x and y axes, along with the ability to handle deeply nested windows.
This seems to be an ideal scenario for utilizing a binary tree. Each node in the tree can accommodate a left and right branch, allowing for recursive traversal down the tree.
So, the question arises: how can we achieve this using Vue single file components?
Thankfully, Vue already supports this. You can find more information in the documentation. All we need to do is reference the component's filename within the component.
Building It Out
Let's define a basic binary tree that we will pass to our component as a prop called
node. If you're unfamiliar with this data structure, you can find more information on its wiki.
<script setup>
import TreeNode from './components/TreeNode.vue'
const tree = {
value: 1,
left: {
value: 2,
left: {
value: 4,
left: null,
right: null,
},
right: {
value: 5,
left: null,
right: null,
},
},
right: {
value: 3,
left: null,
right: null,
},
};
</script>
<template>
<ul>
<TreeNode :node="tree" />
</ul>
</template>
Now let's set up the TreeNode component.
<script setup>
defineProps({
node: { type: Object, required: true },
depth: { type: Number, default: 0 },
})
</script>
<template>
<li :style="{ '--opacity': 0.15 + depth * 0.2 }">
{{ node.value }}
<ul v-if="node.left || node.right">
<TreeNode v-if="node.left" :node="node.left" :depth="depth + 1" />
<TreeNode v-if="node.right" :node="node.right" :depth="depth + 1" />
</ul>
</li>
</template>
<style scoped>
li {
background: rgba(255, 0, 0, var(--opacity, 0.15));
}
</style>
The component renders one node as a list item, then renders itself again for each child, using its own filename, TreeNode. Both children go in a single nested <ul>, so the markup is a valid nested list that screen readers announce with the right levels.
Every recursive function needs a base case, a point where it stops calling itself. Here it's the v-if: a node with no children renders no nested list, and the recursion ends.
The depth prop shows the other half of recursion: each level passes information down to the next. Every child gets depth + 1, and I've used it to darken the background a little at each level so you can see the nesting.
A note on naming: Vue's style guide reserves the The prefix for components that only appear once on a page. A recursive component is the opposite, since it renders many copies of itself, so TreeNode describes it better.
Demo
- 1
- 2
- 4
- 5
- 3
- 2
Back to the Window Manager
The same shape works for the window layout. Each leaf node is a window. Each node with children is a split: it divides its space between the left and right branches, either side by side (the x-axis) or stacked (the y-axis). Nesting a split inside another split is how a layout supports both axes at any depth.
The component barely changes. A split node renders a flex container whose flex-direction comes from the split's axis, and puts a TreeNode in each half. A window node renders the window. The recursion, the base case and the depth all work exactly as they do in the demo above.
Conclusion
Building a recursive component in Vue is surprisingly straightforward. There are two things to get right: a base case, so the component knows when to stop rendering itself, and the props each level passes down to the next.
To learn more, see Vue's documentation on recursive components.