Skip to content
Application

Tree View

Recursive expand/collapse tree with Motion height reveal and rotating chevrons.

Motionbranch height reveal on tween.quick; chevron rotate on spring.snappy

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { TreeView } from "kineto/tree-view";

Preview

tsx
"use client";

import * as React from "react";
import { ChevronRight, type LucideIcon } from "lucide-react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";

import { cn } from "@/lib/cn";
import { spring, tween } from "@/lib/motion-presets";

export interface TreeNode {
  id: string;
  label: string;
  icon?: LucideIcon;
  children?: TreeNode[];
}

export interface TreeViewProps extends React.HTMLAttributes<HTMLDivElement> {
  data: TreeNode[];
  defaultExpanded?: string[];
}

const ExpandedContext = React.createContext<{
  expanded: Set<string>;
  toggle: (id: string) => void;
  reduced: boolean;
}>({ expanded: new Set(), toggle: () => {}, reduced: false });

function TreeItem({ node, depth }: { node: TreeNode; depth: number }) {
  const { expanded, toggle, reduced } = React.useContext(ExpandedContext);
  const open = expanded.has(node.id);
  const hasChildren = !!node.children?.length;
  const Icon = node.icon;

  return (
    <li>
      <button
        type="button"
        onClick={() => hasChildren && toggle(node.id)}
        style={{ paddingLeft: `${depth * 1.25 + 0.5}rem` }}
        className="flex w-full items-center gap-2 rounded-md py-1.5 pr-2 text-left text-sm text-foreground transition-colors hover:bg-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
      >
        {hasChildren ? (
          <motion.span
            animate={{ rotate: open ? 90 : 0 }}
            transition={reduced ? { duration: 0 } : spring.snappy}
            className="text-muted-foreground"
          >
            <ChevronRight className="size-4" />
          </motion.span>
        ) : (
          <span className="size-4" />
        )}
        {Icon ? <Icon className="size-4 text-muted-foreground" /> : null}
        <span className="truncate">{node.label}</span>
      </button>

      {hasChildren ? (
        <AnimatePresence initial={false}>
          {open ? (
            <motion.ul
              initial={reduced ? { opacity: 0 } : { height: 0, opacity: 0 }}
              animate={reduced ? { opacity: 1 } : { height: "auto", opacity: 1 }}
              exit={reduced ? { opacity: 0 } : { height: 0, opacity: 0 }}
              transition={reduced ? { duration: 0 } : tween.quick}
              className="overflow-hidden"
            >
              {node.children!.map((child) => (
                <TreeItem key={child.id} node={child} depth={depth + 1} />
              ))}
            </motion.ul>
          ) : null}
        </AnimatePresence>
      ) : null}
    </li>
  );
}

const TreeView = React.forwardRef<HTMLDivElement, TreeViewProps>(
  ({ className, data, defaultExpanded = [], ...props }, ref) => {
    const [expanded, setExpanded] = React.useState<Set<string>>(
      () => new Set(defaultExpanded)
    );
    const reduced = useReducedMotion() ?? false;

    const toggle = React.useCallback((id: string) => {
      setExpanded((prev) => {
        const next = new Set(prev);
        if (next.has(id)) next.delete(id);
        else next.add(id);
        return next;
      });
    }, []);

    return (
      <ExpandedContext.Provider value={{ expanded, toggle, reduced }}>
        <div ref={ref} className={cn("w-full", className)} {...props}>
          <ul>
            {data.map((node) => (
              <TreeItem key={node.id} node={node} depth={0} />
            ))}
          </ul>
        </div>
      </ExpandedContext.Provider>
    );
  }
);
TreeView.displayName = "TreeView";

export { TreeView };

src/components/ui/tree-view.tsx

Props

PropTypeDefaultDescription
data{ id; label; icon?; children? }[]—Tree nodes.
defaultExpandedstring[]—Node ids open initially.