Skill Tree

A talent tree: skills on a grid, lines for prerequisites, points to spend. Nodes show their state at a glance (locked with a padlock, available with a soft glow, learned in the branch colour, maxed with a gold ring), multi-rank nodes show "2/3", and picking a node opens its details in a side panel (a bottom sheet on phones). Your game owns the ranks and the point balance; the tree tells you what changed. Big trees scale to the box and can be dragged and zoomed.

View as Markdown

A farmer's tree with three branches. Click a node for details, press Learn (or double-click, or Enter on a focused node). Refunds are allowed unless another learned skill needs the node. +1 point and Level up unlock more; the keystone needs level 4 and a skill from each side.

Farmer level 2
Points4
  • Fields
  • Fishing
  • Crafts

Installation

npx shadcn@latest add @cozy/skill-tree

Usage

import { SkillTree, canLearn, canRefund, spentPoints, type SkillNode } from "@/components/ui/skill-tree"

const nodes: SkillNode[] = [
  { id: "till", name: "Tilling", x: 1, y: 0, maxRank: 3, branch: "fields", rankText: (r) => `+${r * 10}% speed` },
  { id: "irrigation", name: "Irrigation", x: 0, y: 1, requires: ["till"], kind: "notable", branch: "fields" },
]

const [ranks, setRanks] = useState({})
const points = totalPoints - spentPoints(ranks, nodes)

<SkillTree
  nodes={nodes}
  branches={{ fields: { label: "Fields", color: "#8DB84A" } }}
  ranks={ranks}
  onRanksChange={(next, change) => setRanks(next)}
  points={points}
  allowRefund
  onReset={() => setRanks({})}
/>

State: each node is locked (prerequisites or level missing), available (rank 0 and unlocked), learned (some ranks) or maxed. It is exposed as data-state; data-learnable marks nodes that can be learned right now (they pulse, except under reduced motion).

Keyboard: arrow keys move to the nearest node in that direction, Space selects, Enter selects and learns, Escape closes the details. Learning, refunding and why a learn was blocked are announced through an aria-live region.

Pan and zoom never take over the page: a plain wheel scrolls the page, Ctrl or ⌘ + wheel zooms. On touch, drags pan only along the axes where the tree overflows, so the page can still scroll.

Examples

Minimum props

Only nodes and a height. No points means unlimited, pannable={false} scales the tree to fit the box, and the tree keeps the ranks itself.

API

Everything else is passed through to the underlying element or Radix primitive.

PropTypeDefaultDescription
nodesSkillNode<T>[]–{ id, name, label?, icon?, description?, x, y, requires?, requiresAny?, maxRank?, cost?, kind?, branch?, rankText?, requiresLevel?, data? }. x and y are grid cells (column, row); the tree is cropped to the cells in use.
requires / requiresAnystring[]–Node ids. `requires`: all must be learned (rank 1 or more). `requiresAny`: at least one. Both can be used on one node.
maxRank / costnumber / number | number[]1 / 1Ranks, and points per rank: one number, or one entry per rank (the last repeats), e.g. [1, 1, 2].
kind"small" | "notable" | "keystone""small"Circle, rounded square, big diamond.
rankText(rank) => ReactNode–Effect at a rank. The details show it for the current and the next rank.
branches / legendRecord<string, { label, color?, icon? }> / boolean- / trueColours by `node.branch` (default: a palette colour per branch). The legend shows when branches are given.
ranks / defaultRanks / onRanksChangeRecord<string, number>{}Ranks per node id. `onRanksChange(next, { id, from, to, node })` fires after each learn or refund; in controlled mode apply `next`.
pointsnumberunlimitedPoints the player can spend. Learning is disabled when the next rank costs more. The tree does not subtract: use `total - spentPoints(ranks, nodes)`. A number shows the counter.
levelnumber–Player level for `requiresLevel` (no level means no check).
allowRefundbooleanfalseAdds a Refund button to the details. Blocked while a learned node depends on the node.
onReset() => void | boolean–Shows a Reset all button (disabled with nothing learned); the game decides the cost. Uncontrolled ranks are cleared too, unless you return false.
selected / defaultSelected / onSelectstring | null / (node | null) => voidnoneSelected node id and callback with the node (null when closed).
cellSize / heightnumber / number | string88 / 480Pixels between cells at zoom 1, and the box height. The tree scales down to the box width, but not below 80%.
pannablebooleantrueDrag to pan and zoom (buttons, Ctrl or ⌘ + wheel) when the tree is bigger than the box. False scales the whole tree to fit.
renderNode / renderDetails(node, state) => ReactNode–Replace a node's icon area or the whole details panel. `state` has status, rank, maxRank, cost, learnable, refundable, learn() and refund().
labelsPartial<SkillTreeLabels>EnglishEvery built-in text, including the aria-live announcements and "{name}", "{rank}", "{max}" placeholders.
canLearn(node, ranks, nodes, { points, level }){ ok, reason? }–Pure helper. reason is "maxed", "requires", "level" or "points" (checked in that order).
canRefund(node, ranks, nodes) / spentPoints(ranks, nodes)boolean / number–Pure helpers. canRefund is false when it is the last rank and a learned node needs it.