@cssints/data

Statistics over runtime values, computed by the browser's layout for cssints: max, sum and count of a list, the dCSS idea as one call member of cssints/plugin scope(). It prints a small global rig and no class; there is no runtime. The browser lays the values out as widths, reads the resulting width back as a number with a container query, and prints it with a counter.

import { data } from "@cssints/data" with { type: "cssints" };

// at a module's top level, once: the rig, and no class
data();
@layer _.k {
  [data-data] {
    display: inline-grid;
    grid-template-columns: auto;
    width: max-content;
    overflow: clip;
  }
  [data-data] > ul {
    margin: 0;
    padding: 0;
    list-style: none;
    height: 0;
  }
  [data-data="max"] > ul {
    display: grid;
  }
  [data-data="max"] > ul > li {
    width: calc(var(--value) * 1px);
    height: 0;
  }
  [data-data="sum"] > ul {
    display: flex;
    width: max-content;
  }
  [data-data="sum"] > ul > li {
    flex: none;
    width: calc(var(--value) * 1px);
    height: 0;
  }
  [data-data="count"] > ul {
    display: flex;
    width: max-content;
  }
  [data-data="count"] > ul > li {
    width: 1px;
    height: 0;
  }
  [data-read] {
    display: block;
    container-type: inline-size;
  }
  [data-read] > * {
    --data-result: round(100cqw / 1px);
  }
  [data-data="max"] [data-read] output::after,
  [data-data="sum"] [data-read] output::after,
  [data-data="count"] [data-read] output::after {
    counter-reset: out var(--data-result);
    content: counter(out);
  }
}

The DOM contract

<figure data-data="max">
  <ul aria-hidden="true">
    <li style="--value: 12"></li>
    <li style="--value: 40"></li>
    <li style="--value: 30"></li>
  </ul>
  <b data-read><output></output></b>
</figure>

A worked example

max of 12, 40 and 30:

sum makes the list a hug-width flex row instead, so the row is 12 + 40 + 30 = 82px and the counter prints 82. count is a row of 1px items, so its width is the number of items: 3.

What layout computes

data-data The layout that is the answer A list of 12, 40, 30
max the widest item (one auto grid column) 40
sum the total (a hug-width flex row) 82
count the number of items (a row of 1px) 3

The reader's inline size is its width in the figure's column. Because it is container-type: inline-size (size containment on the inline axis), its own contents cannot widen it: it is sized by the measuring list, not by the printed answer. round(100cqw / 1px) divides the container width by a pixel, so the result is the width as a unitless number, rounded to an integer.

The rules

Accessibility

The measuring list is aria-hidden="true": it is a ruler, not content, and a reader should not hear three empty items. The printed answer is generated content (output::after): no screen reader reads it as text, find-in-page does not find it, and it is not copied. Do not make it the only place the number exists. Put the number in a <figcaption>, as dCSS does, so the figure has a name and a value a reader and a search can reach:

<figure data-data="max">
  <ul aria-hidden="true">
    <li style="--value: 12"></li>
    <li style="--value: 40"></li>
    <li style="--value: 30"></li>
  </ul>
  <b data-read><output></output></b>
  <figcaption>Busiest hour: 40 requests</figcaption>
</figure>

The caption is text; keep it in step with the same values. When the host can render both (server-side or at the same moment it writes --value), write the number there too.

Limits

cd packages/data && bun run test            # the sheet through the engine: the rig once for two calls, no class, the rules
cd packages/data && bun run test:browsers   # Chromium through playwright-core: 40, 82 and 3, then a changed value
cd packages/data && bun run typecheck
cd packages/data && bun run build

Credits

The idea and the DOM contract are Computing with CSS Layout by Agustin Capeletto and dCSS: an auto grid column is a maximum, a hug-width flex row is a sum, a row of 1px items is a count, and a container query read back through round(100cqw / 1px) is the number, printed by a counter. The rules are written here, as globals of scope(); no code is copied.