Blog · Frontend · · 3 min read
Instant tree expansion with page masks
Clicking an expandable taxon in Ptree fans out its children immediately, before the HTTP request for that level returns. The placeholder fan already has the exact number of rows, the exact vertical layout, and the exact fossil filter state the real children will occupy. When the JSON arrives, the placeholders swap their text and property colors in place without moving a single tile or connector.
Why child count is not enough
A simple integer childCount lets a client draw placeholder rows, but it breaks as soon as fossils are hidden. Consider a genus with 18 children in the database where 14 are extinct fossils and 4 are living species. If the visitor has fossils toggled off and the client only knows childCount = 18, clicking the genus fans out 18 blank rows, shifts the surrounding tree down by 18 pitches, and then violently collapses 14 of them when the response arrives.
Paging adds a second edge case. A node with more than 50 children returns a page of up to 50 items plus up to two pagination arrows (previous page and next page). The placeholder fan needs to know how many total slots appear on the first page and which of those slots are extinct.
Packing the first page into one integer
When the API builds a level response, it computes a pageMask for every expandable child on that page:
const pageMask = slots.reduce((mask, slot) => mask * 2 + Number(!!slot.fossil), 1);
The leading 1 is a sentinel bit that encodes the length of the slot list. Each subsequent bit is 1 if that slot is extinct and 0 if it is extant (or a pagination arrow). Because a page holds at most 50 children plus 2 arrows, the mask takes at most 53 bits, which matches the exact precision of a JavaScript IEEE-754 safe integer (Number.MAX_SAFE_INTEGER = 2^53 - 1), so it serializes as an ordinary JSON number with no string conversion or BigInt overhead. Decoding must use toString(2) rather than 32-bit bitwise operators, which silently truncate anything past 31 bits.
On the client, the skeleton builder decodes the mask in one line:
[...mask.toString(2).slice(1)].map((bit, i) => ({
id: `skel-${node.id}-${i}`,
extinct: bit === "1",
// ...
}))
Dropping the leading "1" yields an array whose length is the exact slot count and whose elements already carry extinct. The tree's normal fossil classifier runs over the skeleton array using the same _away() predicate it uses for real taxa. When fossils are hidden, every extinct placeholder is immediately classified .away and takes zero vertical rows.
Deterministic placeholder bars in CSS
Skeleton tiles reuse the exact same <li> structure as real taxa. While a tile's data-id starts with skel-, its scientific-name link is empty, and CSS draws a shimmer bar using its row index --i:
main li[data-id^="skel-"] > a::before {
content: "";
grid-area: 1 / 1;
inline-size: calc(38% + mod(var(--i), 4) * 11%);
block-size: 0.62em;
border-radius: 1em;
background: color-mix(in srgb, CanvasText 12%, transparent);
}
Using mod(var(--i), 4) gives each row a varied bar length (38%, 49%, 60%, 71%) without random numbers or inline styles, and removing the placeholder state requires no extra attribute: replacing data-id with the real taxon ID and filling the name spans turns the skeleton into the final tile.
1 to toggle whether that child slot is extinct, then toggle Hide fossils to watch extinct skeleton rows collapse in pure CSS.Skipping skeletons when cached
When a visitor clicks a chevron, the disclosure handler checks the client cache before marking the control busy:
let items = routePage({ taxon: t, offset });
if (!Array.isArray(items)) {
pin.ariaBusy = "true";
pin.parentElement.classList.add("busy");
this._relayout();
items = await items;
// ...
}
If the page is already in the client cache, routePage() returns the array synchronously and no skeleton is created; the real children fan out in the first layout pass. Only an unresolved Promise sets aria-busy="true", triggering a skeleton layout that holds the exact geometry until the fetch resolves. If the visitor collapses the node or navigates away before the fetch completes, the stale promise sees that the pin is unchecked or detached and discards the result without touching the DOM.