Blog · Frontend · · 4 min read
Folding deep trees into a single gutter
A taxonomic path from Life to a species commonly crosses fifteen to twenty ranks, while a readable screen holds three or four columns of names. Panning a wide canvas horizontally hides the ancestors that explain where you are; wrapping or indenting a vertical list destroys the horizontal tree structure. Ptree keeps the active columns readable and folds every older generation into one narrow strip along the left edge.
Column budget
The viewer lets CSS decide how many live columns fit in the current viewport. On .tree > main, two custom properties state the acceptable width of a column: --fit-min at 14ex + 6.6ex and --fit-max at 21ex + 6.6ex, where 6.6ex accounts for padding, icon, and chevron chrome. CSS subtracts the folded gutter (--snakew) and trailing margin (--edge-space) from 100cqi, then computes an integer column count with round():
.tree > main > * {
--live-width: calc(100cqi - var(--snakew) - var(--edge-space) + var(--inset));
--ncols: max(1, min(round(down, calc(var(--live-width) / var(--fit-min)), 1),
round(up, calc(var(--live-width) / var(--fit-max)), 1)));
--colw: calc(var(--live-width) / var(--ncols));
}
Between resize events, CSS adjusts --colw continuously so columns track the window width smoothly. JavaScript reads --ncols only after resize settles, then re-runs the tree layout against that integer count.
How columns fold
When the expanded tree is maxDepth columns deep and the screen fits cols live columns, the Tree view computes fold = Math.max(0, maxDepth - cols). Every node at depth d gets a virtual column index vc = d - fold. Columns with vc >= 0 render normally at width --colw. Columns with vc < 0 belong to the folded gutter on the left.
A folded column is not removed from the DOM. Its <ul> keeps the same <li> tiles, and each surviving tile rotates 90 degrees counter-clockwise so its name reads vertically inside the --snakew gutter. Moving deeper shifts --fold by one, and .tree .plane and .tree > main > header both translate horizontally by:
transform: translateX(calc((1 + min(1, var(--fold))) * var(--snakew) / 2 - var(--fold) * var(--colw)));
When --fold is 0, half of --snakew centers the unfolded tree inside the viewport. As soon as --fold reaches 1, the other half shifts to the left edge to open the gutter, and every further step slides the plane left by one --colw. The tiles that cross from column 0 into column -1 rotate into the strip in place using CSS transitions on the same elements.
Pruning and packing the strip
A deep lineage can have dozens of siblings at each folded rank. Showing every sibling from six folded columns inside one narrow strip would turn the gutter into an unreadable stack. Folded columns therefore enforce a reach limit of one: only the selected ancestor and its immediate neighbors within one step of the spine remain visible, and any lateral disclosures opened off those folded columns are pruned from the projection.
Because folded tiles rotate 90 degrees, a tile's horizontal width (--colw) becomes its vertical height inside the gutter. Two folded tiles from different ranks would overlap vertically if they simply kept their unfolded row numbers. The packer solves this with two coupled row counts:
colwRowsconverts one tile's visual width into row pitches (Math.ceil(colw / pitch)), so the packer knows how many vertical rows a rotated tile consumes.stripRowstracks the combined vertical intervals occupied by folded nodes inoccupied. Each folded node is placed at the nearest free slot to its descendants' midpoint, nudging out of any interval already claimed by another folded rank.
Each folded node stores its strip offset in two CSS custom properties: --i for its base row and --sp for its strip position in multiples of var(--colw) - var(--colw-0). When the window resizes and --colw changes, both the rotated tiles and the SVG <g> fans attached to them track the new vertical strip pitch through calc() without recomputing paths until resize finishes.
Connectors across the fold
An unfolded parent sends cubic curves from its right edge to its children's left edges. A folded parent sits rotated inside the left gutter, so its outgoing fan leaves from the right edge of the vertical strip instead, marked with the .fold class on its <g> element.
When a folded ancestor's children live in column 0 directly beside the strip, the connector routes cleanly across the gutter gap. When a folded ancestor connects to another folded ancestor inside the same strip, _detour() routes the link around the strip edge with rounded elbows of radius pitch / 2 so lineage paths stay traceable all the way back to the root.
Drilling five or six levels deep in the Tree view folds older ranks into the left gutter automatically, and clicking any rotated ancestor unfolds back to it.