Vue ApexTree

Installation

npm install vue-apextree apextree

Note: apextree is a peer dependency and must be installed alongside vue-apextree.

Demo

View Demo Project created using vue-apextree

Basic Usage

<script setup lang="ts">
import { ApexTreeChart, type NestedNode, type TreeOptions } from 'vue-apextree';

const data: NestedNode = {
  id: '1',
  name: 'CEO',
  children: [
    {
      id: '2',
      name: 'CTO',
      children: [
        { id: '3', name: 'Dev Lead', children: [] },
        { id: '4', name: 'QA Lead', children: [] },
      ],
    },
    {
      id: '5',
      name: 'CFO',
      children: [],
    },
  ],
};

// a new options object rebuilds the tree, so declare it once
const options: Partial<TreeOptions> = {
  width: 800,
  height: 600,
  direction: 'top',
  nodeWidth: 120,
  nodeHeight: 80,
};
</script>

<template>
  <ApexTreeChart :data="data" :options="options" />
</template>

Setting License Key

If you have a commercial license, set it once at app initialization:

// main.ts
import { setApexTreeLicense } from 'vue-apextree';

setApexTreeLicense('your-license-key');

Using Template Ref Methods

Access methods like changeLayout, collapse, expand, and fitScreen via template ref:

<script setup lang="ts">
import { ref } from 'vue';
import { ApexTreeChart, type ApexTreeExpose } from 'vue-apextree';

const treeRef = ref<ApexTreeExpose | null>(null);
const options = { width: 800, height: 600 };

const handleChangeLayout = () => {
  treeRef.value?.changeLayout('left');
};

const handleCollapse = (nodeId: string) => {
  treeRef.value?.collapse(nodeId);
};

const handleExpand = (nodeId: string) => {
  treeRef.value?.expand(nodeId);
};

const handleFitScreen = () => {
  treeRef.value?.fitScreen();
};
</script>

<template>
  <div>
    <button @click="handleChangeLayout">Change Layout</button>
    <button @click="handleFitScreen">Fit Screen</button>

    <ApexTreeChart ref="treeRef" :data="data" :options="options" />
  </div>
</template>

Custom Node Templates

<script setup lang="ts">
import { ApexTreeChart } from 'vue-apextree';

interface PersonData {
  name: string;
  imageURL: string;
}

const nodeTemplate = (content: unknown): string => {
  const person = content as PersonData;
  return `
    <div style="display: flex; flex-direction: column; align-items: center; height: 100%;">
      <img 
        src="${person.imageURL}" 
        style="width: 50px; height: 50px; border-radius: 50%;" 
      />
      <div style="font-weight: bold;">${person.name}</div>
    </div>
  `;
};

const options = {
  width: 800,
  height: 600,
  contentKey: 'data',
  nodeWidth: 150,
  nodeHeight: 100,
  nodeTemplate,
};
</script>

<template>
  <ApexTreeChart :data="data" :options="options" />
</template>

Reactivity

A data change is reconciled into the live tree rather than rebuilding it: surviving nodes spring to their new positions, new ids grow in, departed ones retract, and collapse state, selection, focus and expanded cards all survive. A change to options rebuilds the instance, since options are read at construction.

The component watches data deeply, so mutating a nested array works as well as replacing the object:

<script setup lang="ts">
import { ref } from 'vue';
import { ApexTreeChart, type NestedNode } from 'vue-apextree';

const data = ref<NestedNode>({
  id: '1',
  name: 'Root',
  children: [],
});

const options = { width: 800, height: 600 };

// this will trigger a re-render
const addChild = () => {
  data.value.children.push({
    id: String(Date.now()),
    name: 'New Node',
    children: [],
  });
};
</script>

<template>
  <button @click="addChild">Add Child</button>
  <ApexTreeChart :data="data" :options="options" />
</template>

See Live Data Updates for why stable node ids matter to the diff.

Events

<script setup lang="ts">
import { ApexTreeChart } from 'vue-apextree';

const options = { width: 800, height: 600 };

// the payload is the raw node data object, typed unknown
const handleNodeClick = (node: unknown) => {
  console.log('Node clicked:', node);
};
</script>

<template>
  <ApexTreeChart
    :data="data"
    :options="options"
    @node-click="handleNodeClick"
  />
</template>

Props

PropTypeDescription
dataNestedNodeTree data structure (required)
optionsEvery core apextree option: size, direction, node and edge styling, templates, toolbar, tooltip, theme and the rest. Use the node-click event for clicks
classstringCSS class for the container
styleCSSPropertiesInline styles for the container

Layout and styling go in options, not as separate props. See Options for every key and its default.

Events

EventPayloadDescription
node-clickunknownEmitted when a node is clicked, with the raw node data object

Exposed Methods

MethodDescription
changeLayout(direction?)Change tree direction
collapse(nodeId)Collapse a node
expand(nodeId)Expand a node
expandAll()Expand every node in one reflow
collapseAll()Collapse every node, leaving the root visible
expandToDepth(depth)Show the tree down to a depth (root = 0)
updateData(data)Diff against a new dataset and spring to it
focus(nodeId)Spotlight a node's lineage and subtree
clearFocus()Remove the spotlight
setActivePath(nodeIds)Flow a marching dash along the root-to-node path
clearActivePath()Clear the active-path flow
toggleCard(nodeId)Toggle a node's expanded card
zoom(factor)Zoom by a fraction of the current scale (positive in, negative out)
centerOnNode(nodeId)Center the camera on a node
fitScreen()Fit tree to screen
refreshTokens()Re-read the tree's CSS variables, the family tokens, the registered theme and the Ignite UI theme, and repaint (apextree 2.3.0 or later)
getGraph()Get the underlying graph instance for the full API

getGraph() returns the core Graph, so anything not exposed here (expandSubtree, getExpandedCards, getActivePath, exportToSvg, selection and search) is reachable through it. See Methods.

Theming and Ignite UI

options is typed off the core apextree package you have installed, so with apextree 2.3.0 it takes theme, tokens and designSystem as well as every color option. An option you pass beats the --apex-tree-* CSS variables and the --apx-* tokens, so leave out any option a stylesheet should drive. See Theming and Customization.

To follow an Ignite UI theme on the page, import the add-on once, in main.ts or anywhere before the first tree renders. It ships in the core apextree package and needs no extra licence key:

import 'apextree/ignite-ui'
<script setup lang="ts">
// per tree: designSystem: false ignores the Ignite UI theme, tokens: false ignores every token.
// Declare the options once: a new object on every render rebuilds the tree.
const noIgnite = { designSystem: false }
</script>

<template>
  <ApexTreeChart :data="data" :options="noIgnite" />
</template>

The tree reads its theme when it renders. When the theme changes without a prop change, for example after a stylesheet swap for an Ignite UI light/dark switch, a class toggle that sets the variables, or the add-on loading after the first render, call refreshTokens() on the template ref (the useApexTree() composable returns it too). Nodes, expanded cards, selection, focus and the active path are kept:

<script setup lang="ts">
const treeRef = ref<ApexTreeExpose | null>(null)

themeLink.onload = () => treeRef.value?.refreshTokens()
</script>

<template>
  <ApexTreeChart ref="treeRef" :data="data" />
</template>

refreshTokens() needs vue-apextree 2.2.0 and apextree 2.3.0; on an older core it does nothing. See Using with Ignite UI.

Data Structure

NestedNode is the core apextree type, re-exported by vue-apextree:

interface NestedNode<T = undefined> {
  id: string;                // unique, stable across renders
  name: string;              // display label
  data: T;                   // custom payload for templates and callbacks
  children: NestedNode<T>[]; // [] for a leaf
  hasChildren?: boolean;     // children not loaded yet, fetched by the loadChildren option
  options?: Partial<FontOptions & NodeOptions & TooltipOptions>; // per-node overrides
}

Per-node overrides go in options, for example nodeBGColor, borderColor and fontColor. FontOptions, NodeOptions and TooltipOptions are exported by apextree, not by the wrapper.

TypeScript Support

Full TypeScript support with exported types:

import type {
  ApexTreeProps,
  ApexTreeExpose,
  GraphInstance,
  NestedNode,
  TreeOptions,
  TreeDirection,
} from 'vue-apextree';

NestedNode, TreeOptions and TreeDirection are re-exported from the core apextree package, so they always match the core you have installed.