Skip to content

Tree ​

The Tree component renders hierarchical data as a text tree with optional icons and per-node styling.

Basic Usage ​

vue
<template>
  <Tree :data="nodes" />
</template>

<script setup>
import { Tree } from 'vuetty';

const nodes = [
  {
    name: 'src',
    children: [
      { name: 'components', children: [{ name: 'Tree.js' }] },
      { name: 'App.vue' }
    ]
  },
  { name: 'package.json' }
];
</script>

Data Structure ​

Tree expects an array of nodes. Each node can include a name, optional children, and an optional color override.

js
[
  {
    name: 'root',
    children: [
      { name: 'child-1' },
      {
        name: 'child-2',
        color: 'cyan',
        children: [{ name: 'grandchild' }]
      }
    ]
  }
]

Props ​

Tree ​

NameTypeDefaultDescription
dataarray[]Tree nodes
branchColorstring'gray'Color for branch characters (the connecting lines)
folderColorstring'blue'Color for nodes with children
fileColorstringnullColor for leaf nodes (falls back to color or theme default)
colorstring-Fallback color used for files when fileColor is not set
bgstring-Reserved for future background styling
showIconsbooleanfalseShow folder/file icons before node names
treeStylestring | object'default'Tree branch character style (see Tree Styles below)

Layout Props (Box Props) ​

NameTypeDefaultDescription
flexnumber | stringnullFlex shorthand when inside a flex container
flexGrownumbernullFlex grow factor
flexShrinknumbernullFlex shrink factor
flexBasisnumber | stringnullFlex basis
alignSelfstringnullSelf alignment: 'auto', 'flex-start', 'flex-end', 'center', 'stretch', 'baseline'
widthnumber | stringnullWidth (chars or %)
heightnumber | stringnullHeight (rows)
minWidthnumbernullMinimum width
maxWidthnumbernullMaximum width
minHeightnumbernullMinimum height
maxHeightnumbernullMaximum height
paddingnumbernullPadding
paddingLeftnumbernullLeft padding
paddingRightnumbernullRight padding
paddingTopnumbernullTop padding
paddingBottomnumbernullBottom padding
marginnumbernullMargin
marginLeftnumbernullLeft margin
marginRightnumbernullRight margin
marginTopnumbernullTop margin
marginBottomnumbernullBottom margin

Tree Styles ​

The treeStyle prop controls the appearance of tree branch characters. You can use predefined styles or create custom ones.

Predefined Styles ​

StyleCharactersDescription
'default' or 'rounded'├ └ │ ──Standard rounded box-drawing characters
'bold'┣ ┗ ┃ ━━Bold/thick box-drawing characters
'double'╠ ╚ ║ ══Double-line box-drawing characters
'classic'+ + | --Simple ASCII characters for maximum compatibility

Custom Styles ​

You can provide a custom object with the following properties:

js
{
  branch: '├',      // Character for intermediate children
  last: '└',        // Character for the last child
  vertical: '│',    // Vertical continuation line
  horizontal: '──'  // Horizontal connecting line
}

Styling Examples ​

Custom Branch and Node Colors ​

vue
<template>
  <Tree
    :data="nodes"
    branch-color="gray"
    folder-color="cyan"
    file-color="green"
  />
</template>

<script setup>
import { Tree } from 'vuetty';

const nodes = [
  {
    name: 'docs',
    children: [
      { name: 'index.md' },
      { name: 'guide.md' }
    ]
  },
  { name: 'package.json' }
];
</script>

Per-Node Color Overrides ​

vue
<template>
  <Tree :data="nodes" />
</template>

<script setup>
import { Tree } from 'vuetty';

const nodes = [
  {
    name: 'src',
    children: [
      { name: 'index.js', color: 'yellow' },
      { name: 'theme.css', color: 'magenta' }
    ]
  }
];
</script>

Icons ​

vue
<template>
  <Tree :data="nodes" show-icons />
</template>

<script setup>
import { Tree } from 'vuetty';

const nodes = [
  {
    name: 'assets',
    children: [
      { name: 'logo.svg' },
      { name: 'banner.png' }
    ]
  }
];
</script>

Different Tree Styles ​

vue
<template>
  <Col :gap="2">
    <!-- Default/Rounded style -->
    <Tree :data="nodes" treeStyle="default" />

    <!-- Bold style -->
    <Tree :data="nodes" treeStyle="bold" branch-color="cyan" />

    <!-- Double style -->
    <Tree :data="nodes" treeStyle="double" branch-color="magenta" />

    <!-- Classic ASCII style -->
    <Tree :data="nodes" treeStyle="classic" branch-color="yellow" />
  </Col>
</template>

<script setup>
import { Col, Tree } from 'vuetty';

const nodes = [
  {
    name: 'project',
    children: [
      { name: 'src', children: [{ name: 'app.js' }] },
      { name: 'README.md' }
    ]
  }
];
</script>

Custom Tree Style ​

vue
<template>
  <Tree :data="nodes" :treeStyle="customStyle" branch-color="green" />
</template>

<script setup>
import { Tree } from 'vuetty';

const nodes = [
  {
    name: 'docs',
    children: [
      { name: 'api', children: [{ name: 'index.md' }] },
      { name: 'guide.md' }
    ]
  }
];

// Define custom tree characters
const customStyle = {
  branch: '▸',
  last: '▹',
  vertical: '┊',
  horizontal: '─'
};
</script>

Notes ​

  • Theme defaults can be provided via theme.components.tree.branchColor, folderColor, fileColor, and treeStyle.

Released under the MIT License.