---
title: "Tree View · Ark UI — StyleX Blocks"
description: "Nested hierarchies with expandable branches, indent guides, and depth-aware padding."
url: "https://stylexblocks.com/components/ark-ui/tree-view/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Tree View

Nested hierarchies with expandable branches, indent guides, and depth-aware padding.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/tree-view.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/tree-view.md)
- Category: Display
- Variants: subtle, solid
- Sizes: xs, sm, md
- HTML page: https://stylexblocks.com/components/ark-ui/tree-view/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init
```

Then add the item as source you own:

```sh
npx stylexblocks add tree-view
```

Equivalent: `npx shadcn add @stylexblocks/tree-view`. Registry item JSON: https://stylexblocks.com/r/ark-ui/tree-view.json

## Usage

```tsx
import { TreeView } from "#components/ui/ark-ui/tree-view.tsx";
import { createTreeCollection } from "@ark-ui/react/tree-view";

const collection = createTreeCollection({
  rootNode: {
    value: "ROOT",
    label: "",
    children: [
      {
        value: "src",
        label: "src",
        children: [{ value: "index.ts", label: "index.ts" }],
      },
    ],
  },
});

function TreeNode({ node, indexPath }) {
  return (
    <TreeView.NodeProvider node={node} indexPath={indexPath}>
      {node.children ? (
        <TreeView.Branch>
          {/* indicator rotation and selected styling resolve from the
              machine's node state */}
          <TreeView.BranchControl>
            <TreeView.BranchIndicator>
              <TreeView.Icon viewBox="0 0 16 16">{/* chevron */}</TreeView.Icon>
            </TreeView.BranchIndicator>
            <TreeView.BranchText>{node.label}</TreeView.BranchText>
          </TreeView.BranchControl>
          <TreeView.BranchContent>
            <TreeView.BranchIndentGuide />
            {node.children.map((child, index) => (
              <TreeNode key={child.value} node={child} indexPath={[...indexPath, index]} />
            ))}
          </TreeView.BranchContent>
        </TreeView.Branch>
      ) : (
        <TreeView.Item>
          <TreeView.ItemText>{node.label}</TreeView.ItemText>
        </TreeView.Item>
      )}
    </TreeView.NodeProvider>
  );
}

export function Example() {
  return (
    // variant/size resolve once on Root; expansion, selection, typeahead,
    // and depth-aware indentation come from the machine
    <TreeView.Root collection={collection} defaultExpandedValue={["src"]} size="md">
      <TreeView.Tree>
        {collection.rootNode.children.map((node, index) => (
          <TreeNode key={node.value} node={node} indexPath={[index]} />
        ))}
      </TreeView.Tree>
    </TreeView.Root>
  );
}
```

## Source

`components/ui/ark-ui/tree-view.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import {
  TreeView as ArkTreeView,
  useTreeViewNodeContext,
  type TreeNode,
  type TreeViewRootProps as ArkTreeViewRootProps,
} from "@ark-ui/react/tree-view";
import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  fromMap,
  resolveSlotStyles,
  type SlotRecipeVariants,
  type WithStyleOverrides,
} from "#lib/recipe-props.ts";
import { treeViewTreeIconVars } from '@stylexblocks/ui/slot-recipes/tree-view.stylex.ts'

import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'


const treeViewRoot = stylex.create({
  base: {
    width: sizes.full,
    display: 'flex',
    flexDirection: 'column',
    gap: spacing._2,
  },
})

const treeViewTree = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    '--tree-item-gap': spacing._2,
    '--tree-indentation':
      'calc(var(--tree-indent-size) + var(--tree-icon-size) * 0.5)',
    [treeViewTreeIconVars.width]: 'var(--tree-icon-size)',
    [treeViewTreeIconVars.height]: 'var(--tree-icon-size)',
  },
})

const treeViewTreeSizes = stylex.create({
  md: {
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    '--tree-indent-size': spacing._4,
    '--tree-icon-size': spacing._4,
    '--tree-padding-inline': spacing._3,
    '--tree-padding-block': spacing._1_5,
  },
  sm: {
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    '--tree-indent-size': spacing._4,
    '--tree-icon-size': spacing._3,
    '--tree-padding-inline': spacing._3,
    '--tree-padding-block': spacing._1,
  },
  xs: {
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    '--tree-indent-size': spacing._4,
    '--tree-icon-size': spacing._3,
    '--tree-padding-inline': spacing._2,
    '--tree-padding-block': spacing._1,
  },
})

const treeViewTreeIcon = stylex.create({
  base: {
    width: treeViewTreeIconVars.width,
    height: treeViewTreeIconVars.height,
  },
})

const treeViewLabel = stylex.create({
  base: {
    fontWeight: fontWeights.medium,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
})

const treeViewBranch = stylex.create({
  base: {
    position: 'relative',
  },
})

const treeViewBranchContent = stylex.create({
  base: {
    position: 'relative',
  },
})

const treeViewBranchContentAnimateContents = stylex.create({
  true: {
    overflow: 'hidden',
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'expand-height, fade-in',
      ':is([data-closed], [data-state="closed"])': 'collapse-height, fade-out',
    },
    animationDuration: {
      default: null,
      ':is([data-open], [data-state="open"])': durations.moderate,
      ':is([data-closed], [data-state="closed"])': durations.moderate,
    },
  },
})

const treeViewBranchIndentGuide = stylex.create({
  base: {
    height: '100%',
    width: '1px',
    backgroundColor: semanticColors.border,
    position: 'absolute',
    '--tree-depth': 'calc(var(--depth) - 1)',
    '--tree-indentation-offset':
      'calc(var(--tree-indentation) * var(--tree-depth))',
    '--tree-offset':
      'calc(var(--tree-padding-inline) + var(--tree-indentation-offset))',
    insetInlineStart: 'calc(var(--tree-offset) + var(--tree-icon-size) * 0.5)',
    zIndex: 1,
  },
})

const treeViewBranchIndicator = stylex.create({
  base: {
    color: semanticColors.fgMuted,
    transformOrigin: 'center',
    transitionDuration: durations.moderate,
    transitionProperty: 'transform',
    transitionTimingFunction: 'ease-in-out',
  },
})

const treeViewBranchTrigger = stylex.create({
  base: {
    display: 'inline-flex',
    alignItems: 'center',
    justifyContent: 'center',
  },
})

const treeViewBranchControl = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    gap: 'var(--tree-item-gap)',
    borderRadius: semanticRadii.controlMd,
    userSelect: 'none',
    position: 'relative',
    '--tree-depth': 'calc(var(--depth) - 1)',
    '--tree-indentation-offset':
      'calc(var(--tree-indentation) * var(--tree-depth))',
    '--tree-offset':
      'calc(var(--tree-padding-inline) + var(--tree-indentation-offset))',
    paddingInlineStart: 'var(--tree-offset)',
    paddingInlineEnd: 'var(--tree-padding-inline)',
    paddingBlock: 'var(--tree-padding-block)',
    outlineWidth: {
      default: null,
      ':focus-visible': 'var(--focus-ring-width, 0)',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
    '--focus-ring-color': semanticColors.borderEmphasized,
    '--focus-ring-width': '2px',
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
    cursor: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
    },
  },
})

const treeViewBranchControlVariants = stylex.create({
  subtle: {},
  solid: {},
})

const treeViewItem = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    gap: 'var(--tree-item-gap)',
    borderRadius: semanticRadii.controlMd,
    userSelect: 'none',
    position: 'relative',
    '--tree-depth': 'calc(var(--depth) - 1)',
    '--tree-indentation-offset':
      'calc(var(--tree-indentation) * var(--tree-depth))',
    '--tree-offset':
      'calc(var(--tree-padding-inline) + var(--tree-indentation-offset))',
    paddingInlineStart: 'var(--tree-offset)',
    paddingInlineEnd: 'var(--tree-padding-inline)',
    paddingBlock: 'var(--tree-padding-block)',
    outlineWidth: {
      default: null,
      ':focus-visible': 'var(--focus-ring-width, 0)',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
    '--focus-ring-color': semanticColors.borderEmphasized,
    '--focus-ring-width': '2px',
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
    cursor: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
    },
  },
})

const treeViewItemVariants = stylex.create({
  subtle: {},
  solid: {},
})

const treeViewItemText = stylex.create({
  base: {
    flex: 1,
  },
})

const treeViewBranchText = stylex.create({
  base: {
    flex: 1,
  },
})

const treeViewNodeCheckbox = stylex.create({
  base: {
    display: 'inline-flex',
  },
})

export type TreeViewSize = keyof typeof treeViewTreeSizes

export type TreeViewVariant = keyof typeof treeViewBranchControlVariants

export type TreeViewAnimateContent =
  keyof typeof treeViewBranchContentAnimateContents

// What the generated recipe doesn't carry: interactive row affordances and
// the selected/expanded treatment (palette-aware). StyleX keys those off
// `data-selected` / `data-state` that the node wrappers copy from the
// machine's node context.
const treeStatic = stylex.create({
  row: {
    cursor: "pointer",
    color: semanticColors.fg,
    backgroundColor: { default: "transparent", ":hover": semanticColors.bgMuted },
  },
  indicatorExpanded: {
    transform: {
      default: null,
      ':is([data-state="open"])': "rotate(90deg)",
    },
  },
});

const treeRowSelected = stylex.create({
  subtle: {
    backgroundColor: {
      default: null,
      ":is([data-selected])": colorPalette.muted,
    },
    color: {
      default: null,
      ":is([data-selected])": colorPalette.fg,
    },
  },
  solid: {
    backgroundColor: {
      default: null,
      ":is([data-selected])": colorPalette.solid,
    },
    color: {
      default: null,
      ":is([data-selected])": colorPalette.contrast,
    },
  },
});

const treeViewRecipe = defineSlotRecipe({
  slots: {
    root: { styles: treeViewRoot },
    tree: { styles: treeViewTree, sizes: treeViewTreeSizes },
    label: { styles: treeViewLabel },
    branch: { styles: treeViewBranch },
    branchContent: { styles: treeViewBranchContent },
    branchIndentGuide: { styles: treeViewBranchIndentGuide },
    branchIndicator: { styles: treeViewBranchIndicator },
    branchTrigger: { styles: treeViewBranchTrigger },
    branchControl: {
      styles: treeViewBranchControl,
      variants: treeViewBranchControlVariants,
    },
    branchText: { styles: treeViewBranchText },
    item: { styles: treeViewItem, variants: treeViewItemVariants },
    itemText: { styles: treeViewItemText },
    nodeCheckbox: { styles: treeViewNodeCheckbox },
  },
  defaultVariants: { size: "md", variant: "subtle" },
});

const { withProvider, withContext } = createSlotRecipeContext(treeViewRecipe);

const TreeViewRoot = withProvider(ArkTreeView.Root, "root", "TreeViewRoot");

type TreeViewVariants = SlotRecipeVariants<typeof treeViewRecipe>;

function Root<T extends TreeNode>(
  props: WithStyleOverrides<ArkTreeViewRootProps<T>> & Partial<TreeViewVariants>,
) {
  return <TreeViewRoot {...(props as React.ComponentProps<typeof TreeViewRoot>)} />;
}
Root.displayName = "TreeViewRoot";

function BranchControlNode(
  props: React.ComponentProps<typeof ArkTreeView.BranchControl>,
) {
  const node = useTreeViewNodeContext();
  return (
    <ArkTreeView.BranchControl
      {...props}
      data-selected={node.selected ? "" : undefined}
    />
  );
}

function BranchIndicatorNode(
  props: React.ComponentProps<typeof ArkTreeView.BranchIndicator>,
) {
  const node = useTreeViewNodeContext();
  return (
    <ArkTreeView.BranchIndicator
      {...props}
      data-state={node.expanded ? "open" : "closed"}
    />
  );
}

function ItemNode(props: React.ComponentProps<typeof ArkTreeView.Item>) {
  const node = useTreeViewNodeContext();
  return (
    <ArkTreeView.Item {...props} data-selected={node.selected ? "" : undefined} />
  );
}

/**
 * TreeView — Ark UI's tree-view machine wearing the generated recipe.
 * `<TreeView.Root collection variant size>` resolves the recipe once and
 * every part inherits it through context; behavior (expand/collapse,
 * selection, typeahead, `--depth` indentation) is the machine's. Iterate
 * nodes with `TreeView.NodeProvider` and `createTreeCollection` from
 * `@ark-ui/react/tree-view`. Composition per part via `asChild`.
 */
export const TreeView = {
  Root,
  Tree: withContext(ArkTreeView.Tree, "tree", "TreeViewTree"),
  Label: withContext(ArkTreeView.Label, "label", "TreeViewLabel"),
  NodeProvider: ArkTreeView.NodeProvider,
  Branch: withContext(ArkTreeView.Branch, "branch", "TreeViewBranch"),
  BranchControl: withContext(
    BranchControlNode,
    (v) => [
      ...resolveSlotStyles(treeViewRecipe.slots.branchControl, v),
      treeStatic.row,
      fromMap(treeRowSelected, v.variant),
    ],
    "TreeViewBranchControl",
  ),
  // Ark's branch content is a collapsible under the hood: it sets `--height`
  // and `data-state`, so the recipe's animateContent styles apply cleanly.
  BranchContent: withContext(
    ArkTreeView.BranchContent,
    () => [treeViewBranchContent.base, treeViewBranchContentAnimateContents.true],
    "TreeViewBranchContent",
  ),
  BranchIndentGuide: withContext(
    ArkTreeView.BranchIndentGuide,
    "branchIndentGuide",
    "TreeViewBranchIndentGuide",
  ),
  BranchIndicator: withContext(
    BranchIndicatorNode,
    () => [treeViewBranchIndicator.base, treeStatic.indicatorExpanded],
    "TreeViewBranchIndicator",
  ),
  BranchTrigger: withContext(
    ArkTreeView.BranchTrigger,
    "branchTrigger",
    "TreeViewBranchTrigger",
  ),
  BranchText: withContext(ArkTreeView.BranchText, "branchText", "TreeViewBranchText"),
  Item: withContext(
    ItemNode,
    (v) => [
      ...resolveSlotStyles(treeViewRecipe.slots.item, v),
      treeStatic.row,
      fromMap(treeRowSelected, v.variant),
    ],
    "TreeViewItem",
  ),
  ItemText: withContext(ArkTreeView.ItemText, "itemText", "TreeViewItemText"),
  NodeCheckbox: withContext(
    ArkTreeView.NodeCheckbox,
    "nodeCheckbox",
    "TreeViewNodeCheckbox",
  ),
  Icon: withContext("svg", () => [treeViewTreeIcon.base], "TreeViewIcon"),
};
```
