Skip to content

feat(mosaic): add the styled BarChart with a categorical palette - #10960

Draft
austincalvelage wants to merge 46 commits into
austin/mosaic-bar-chartfrom
austin/mosaic-bar-chart-component
Draft

austincalvelage wants to merge 46 commits into
austin/mosaic-bar-chartfrom
austin/mosaic-bar-chart-component

Conversation

@austincalvelage

@austincalvelage austincalvelage commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Description

Stacked on #10953. Review that PR first; this diff contains only the styled layer.

Adds a styled bar chart to Mosaic at @clerk/mosaic/components/chart, built on the headless Chart primitive, with a categorical color palette. BarChart.Root takes the data and, with no children, draws the whole chart: the legend, a grid, the bars, both axes and a tooltip.

import { BarChart, type ChartSeries } from '@clerk/mosaic/components/chart';

const series: ChartSeries<Day>[] = [{ key: 'agents', label: 'Active agents', color: 1, value: day => day.agents }];

<BarChart.Root aria-label='Active agents by day' data={days} index={day => day.date} series={series} />;

Children replace the layout. BarChart.Legend renders wherever it is placed, and BarChart.Plot holds the layers drawn on the plot, in paint order:

<BarChart.Root aria-label='Weekly active users' data={weeks} index={getWeek} series={series} onActivate={openPeriod}>
  <header>
    <BarChart.Legend />
    <ExportButton />
  </header>
  <BarChart.Plot>
    <BarChart.Grid />
    <BarChart.Bars />
    <BarChart.XAxis ticks='calendar' />
    <BarChart.Tooltip />
  </BarChart.Plot>
</BarChart.Root>

The chart's styles and palette ship in their own sheet, since Clerk's components do not render a chart. Import it after styles.css, into the same layer:

@import '@clerk/mosaic/styles.css' layer(components);
@import '@clerk/mosaic/chart.css' layer(components);
  • Parts. Root, Plot, Legend, Grid, YLine, YAxis, YTick, XAxis, XTick, Bars, Bar, ProjectionOutline, Tooltip and TooltipContent.
    • Each part has a .cl-chart-<slot> theme slot and takes xstyle.
    • Leave a part out to hide it; there are no show* props.
    • Without children, Root renders the legend from two entries, then Plot. Plot renders the grid, bars, both axes and the tooltip.
  • Data on the root. BarChart.Root takes data, index, series, highlight, projection, tooltip, formatValue, formatCategory, categoryGap, margin, selection, onSelectionChange, onActivate, height, renderEmptyState and onRenderError. It is generic over the row type, so tooltip and onActivate receive a typed ActiveBar<Row> with no type argument.
  • Axis gutters. Each axis reserves its room beside the plot while it is mounted, registering in a layout effect so the margin settles before the first paint. Leaving out BarChart.YAxis frees its gutter without a margin. margin remains as an override.
  • Tooltip. BarChart.Tooltip mounts the pointer and keyboard selection surface, the hover band (cursor={false} drops it) and the .cl-chart-tooltip surface, which shows what the root's tooltip prop renders. Leaving it out gives a chart that takes no focus, such as a preview. The surface fades in on first open, then slides between bars once the selection moves, while it carries data-moving. Under reduced motion it only fades. It hides while the selected bar is scrolled out of view, carrying data-anchor-hidden. It takes the primitive's side, sideOffset, collisionPadding, collisionBoundary and pinned.
  • From feat(mosaic): slide the chart tooltip between bars #11006 (Alex). The slide, the off-screen hiding, and a selection update that skips re-rendering while the pointer stays on one bar. In Alex's Swingset measurement, React commits over three sweeps fell from 321 to 71.
  • Projection. projection is { category, value, label?, description? }, naming its row by category key, as highlight does. A label adds it to the legend, the tooltip and the announcement. A category that is not in the data draws nothing and leaves the legend alone.
  • Palette. Six new public tokens, --cl-color-chart-1 through --cl-color-chart-6, keep the original hue order: violet, sky, orange, cyan, yellow and slate.
    • Light and dark values are tuned separately so every color passes the dataviz contrast and color-vision checks against the surface. Yellow becomes a gold and slate a steel blue.
    • chart-palette.test.ts reruns those checks against the token values.
    • Pairs to keep apart in one stack: 2 with 4, 2 with 6, and 3 with 5.
    • The palette is defined beside the chart in chart.vars.stylex.ts and ships in chart.css. The grid, labels and tooltip read --cl-color-border, --cl-color-foreground-secondary and --cl-color-surface.
  • Series colors. color is required on every series, as a slot 1 to 6 or any CSS color, so filtering a series never recolors the others.
    • Series reach the Canvas through 8 static [data-series] rules, which caps a chart at 8 series.
  • Highlight. highlight dims the other bars with an opaque color-mix(in oklab, <series> 40%, var(--cl-color-surface)), so grid lines don't show through.
    • Plain hover and keyboard selection don't dim anything.
    • The hover band sits behind the bars, so bar colors stay exact.
  • States. Root sets data-state to ready, empty or error. Plot shows an empty state, and when Canvas fails it shows a negative Banner (role='alert') with a retry. Loading belongs to the owner, who renders a skeleton.
  • Focus ring. The selection surface rings the whole chart while it has keyboard focus. A keyboard selection shows the same band and tooltip as a hover.
  • Screen reader text. Each series' label reaches the primitive's announcement, and values use the tooltip's number format, so screen readers hear Nov 24, 2026, Active agents 53. The series bridge keys series by position for the Canvas, so it passes the label through.
  • Localization. Empty and error copy and the screen reader text, including the new projected message, come from the chart messages, passed to the primitive's messages option.
  • Swingset. The page leads with a single series: daily active agents, a few monthly bars, and a projection, each a one-line <BarChart.Root />. The six-series growth chart is the Stacked example. Highlight pins on click, as Dashboard's overview chart does. Custom layout puts the legend beside an Export button, leaves out the Y axis and adds a total to the tooltip.

For review

  • A namespace per chart type. The root is BarChart.Root, not Chart.Root with a chart type. A later LineChart.Root or DonutChart.Root takes its own options and infers its own tooltip type, and its namespace carries only the parts that apply, so a donut has no XAxis to misuse. The legend, grid, axes and tooltip are shared components re-exported onto each namespace. This shape assumes no combined charts (bars and a line on one plot); those would want a mark-based root.
  • No styled hook. The styled API has no useBarChart. Placing the legend elsewhere is a child of Root, reading the hover from outside is selection and onSelectionChange, and drawing a custom chart is the headless Chart.useBarChart in @clerk/mosaic/primitives/chart. A styled hook can be added later without a breaking change.
  • A root that draws everything. No other Mosaic compound root renders its parts when given no children. The chart's parts already did (Bars renders the bars, the projection outline and the zero line), and it keeps the common case to one line.
  • Separate sheet. chart.css holds the rules the chart reaches that styles.css does not already hold, so the atoms the chart shares with Tooltip and Button stay in styles.css. The build emits an empty rule at every StyleX priority level in both sheets, so priorityN names the same priority in each file and the sublayer order holds whichever sheet loads first. Within a layer, chart rules now follow core rules. That only matters for two atoms on one chart part that set the same property at equal specificity, which none do today. docs/theming.md documents the rule.
  • Legend placement. BarChart.Legend renders in place, so it is in the server render and stays visible in the error state. Outside a chart, items={…} renders a legend beside a skeleton.
  • Legend order. The legend lists the series top to bottom as the bars draw: the stack above zero from its top, then negative series from the zero line down, then a labelled projection. The tooltip already listed a period's rows this way. A stack with nothing below zero lists top first, so the palette story reads Slot 6 to Slot 1.
    • The styled chart's light and dark screenshots change in legend order. They also pick up the current x-axis tick placement, which the old baselines predated.
    • Running test:visual:update also rewrote the switcher, organization switcher, user profile and headless chart baselines, which this change does not touch. I left those out, so they may still differ in the advisory visual job.
  • Zero values in the tooltip. A zero lists with the section its series is drawn in: above zero by default, below zero for a negative series. A missing value still lists nothing, and a period with nothing to show still hides the tooltip.
  • Tooltip footer width. The footer sits in a cl-chart-tooltip-footer box that takes the width the heading and rows set, so a Banner there wraps instead of widening the tooltip.
  • Default date formatting. The default formatCategory formats UTC date keys (YYYY-MM-DD, or ISO ending in Z) as dates, for example "Jan 5, 2026" in tooltips and announcements, and "Jan 5" on ticks. Pass formatCategory for anything else, such as a month.
  • CSS budget. styles.css keeps main's 20KB budget, since the chart's rules moved to chart.css, which gets its own 2KB budget.
  • Bundle. The JS budgets do not change.

How to test

  • pnpm --filter @clerk/mosaic test and test:browser. The styled browser tests cover:
    • palette pixels
    • the opaque dim over a grid line
    • the band behind the bars
    • the tooltip and a custom tooltip prop
    • the axis gutters with and without each axis
    • a chart without BarChart.Tooltip taking no focus
    • the legend in a row beside a button
    • the announcement with each series label and value
    • color-scheme and series-color repaint
    • error and retry
    • empty states and the legend
  • chart.test-d.tsx checks that tooltip and onActivate infer the row type and that series must read the chart's rows.
  • pnpm --filter @clerk/mosaic test:visual runs the light and dark styled screenshots.
  • pnpm dev:swingset, then open /components/chart. Hover the Few bars example, then move above the short September bar: the tooltip closes. Tab into a chart: the ring sits on the selected bar's band as you use the arrow keys, and moves to the whole chart after Escape. Read the status region as you move. Custom layout shows the legend beside the button and no Y axis gutter.

Verification

On 259f06982a, rebased on main at 49334306d2:

  • test (4,213 passed, 24 skipped, 2 todo) and build pass, including typecheck, the client-boundary check and the docs-export check. Mosaic's lint (oxlint -c oxlint.config.mjs) and pnpm dedupe --check pass. The styled chart's 25 browser tests pass on this commit.
  • The chart browser tests: 56 of 57 pass. The failure is the headless primitive's "hides the tooltip while its chart is scrolled out of view" test, which fails the same way before this change (in a full test:browser run on b5a1463e08) and passed 3 of 3 runs on its own. It also failed once in this PR's CI on e5977cc944, so it is intermittent in CI too and belongs to feat(mosaic): add the headless Chart primitive with stacked bars #10953.
  • Bundlewatch passes: the eager ui entry is 50.94KB of 54KB, the total 342.45KB of 347KB, styles.css 19.69KB of 20KB and chart.css 1.3KB of 2KB.
  • Before the rebase, the styled chart rendered pixel-identical to the previous API, light and dark: screenshots taken from b5a1463e08's Chart.Root composition match the BarChart.Root composition on macOS.

Before the rebase, on the commit that split out chart.css:

  • The two sheets together hold every declaration the single sheet held, 1,410 of 1,410, each in the same layer and selector. Only the six palette variables moved, from styles.css to chart.css.
  • Without a runtime import of the palette, 7 of the 22 styled browser tests fail on palette pixels; chart-series.ts reads the slot from chartColorVars, which keeps the palette in the extracted sheet.

On a20724fb4f, and not rerun since:

  • The announcement test fails when the series bridge drops the label: it reads A, 0 10, 1 10, 2 10.
  • On the real Swingset page:
    • The styled tooltip is one .cl-chart-tooltip element that opens beside the bar on the side that fits, with position: fixed, z-index: 10 and pointer-events: none.
    • The slide matches feat(mosaic): slide the chart tooltip between bars #11006's original commit in an identical keyboard run: opacity, top, left at 0.1s, 0.25s and 0.25s on the enter curve, the same first position, and the same path between bars.
    • Hovering the empty top of the short September column opens its tooltip.
    • Highlight pins on click: clicking a section keeps it at full strength and dims the rest, the dimming stays after the pointer leaves, clicking it again clears it, and Enter pins the same way. Hovering alone dims nothing.
    • The few-bars labels sit centered under their bars, and the daily chart keeps its first and last dates at the plot edges.
  • Two mutations each failed exactly one test: a bar z-index of 0 failed the band test, and a dim mixed with transparent failed the opaque-dim test.

Not verified

  • The Linux styled screenshots have not run since the slide and the focus ring. Docker was not available; the pixel comparison above ran on macOS.
  • Swingset has not been opened in a browser since BarChart.Root.
  • Dashboard's overview chart is moving to BarChart.Root on the snapshot @clerk/mosaic@0.15.0-snapshot.v20261203204906, built before the rebase with the same chart API.
  • The hover state is still called selection (selection, onSelectionChange), as in the primitive. Renaming it active, so selection can mean a committed choice, is a separate change.
  • feat(mosaic): slide the chart tooltip between bars #11006's changeset stays separate from the chart's. The chart is unreleased, so it could fold into mosaic-chart-component.md.
  • The styled screenshots have not run on React 19.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

🤖 Generated with Claude Code

@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
swingset Ready Ready Preview Dec 4, 2026 4:51am UTC
2 Skipped Deployments
Project Deployment Actions Updated
clerk-js-sandbox Skipped Skipped Dec 4, 2026 4:51am UTC
mosaic-docs Skipped Skipped Dec 4, 2026 4:51am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2a390ed

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@clerk/mosaic Minor
@clerk/mosaic-docs Patch
@clerk/nextjs Patch
@clerk/swingset Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@10960

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@10960

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@10960

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@10960

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@10960

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@10960

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@10960

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@10960

@clerk/expo-biometrics

npm i https://pkg.pr.new/@clerk/expo-biometrics@10960

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@10960

@clerk/expo-native-components

npm i https://pkg.pr.new/@clerk/expo-native-components@10960

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@10960

@clerk/express

npm i https://pkg.pr.new/@clerk/express@10960

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@10960

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@10960

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@10960

@clerk/mosaic

npm i https://pkg.pr.new/@clerk/mosaic@10960

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@10960

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@10960

@clerk/react

npm i https://pkg.pr.new/@clerk/react@10960

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@10960

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@10960

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@10960

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@10960

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@10960

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@10960

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@10960

commit: 2a390ed

@vercel
vercel Bot temporarily deployed to Preview – clerk-js-sandbox November 23, 2026 23:19 Inactive
austincalvelage and others added 19 commits December 3, 2026 13:59
Pass the keyboard instructions and announcements to the primitive's new
`messages` option from the `chart` messages, so a localization override
reaches them. The styled hook takes no `messages` of its own: the
localization is their one owner. Find the interaction surface in tests
by its role now that the primitive drops `data-chart-interaction`.
…examples with one series

The styled chart keys series by position for the Canvas, so it now passes
each series' label and the full value format to the primitive, and screen
readers hear `Nov 24, 2026, Active agents 53` rather than position keys. A new
`projected` chart message names an unlabelled projection.

The examples now start from a single series, the common case: daily active
agents, a few monthly bars, and a projection. The six-series growth chart
moves to Stacked. Highlight sets a fixed highlight instead of a selection
that ignored clears, which left its tooltip open after the pointer left. The
primitive page gets the same single-series Default.
The styled `Chart.Tooltip` composes the primitive's portal, positioner and
popup: the popup keeps the `cl-chart-tooltip` surface and fade, and a new
`cl-chart-tooltip-positioner` holds the z-index on the fixed element. It
passes `side`, `sideOffset`, `collisionPadding`, `collisionBoundary` and
`pinned` through.

The primitive Swingset examples and docs return the tooltip parts from
`Chart.Tooltip`, since content returned on its own renders unpositioned.
The chart is one tab stop with arrow keys inside. While the keyboard has a
bar selected, the focus ring sits on its band, and the chart-wide ring shows
only while keyboard focus has no band, as after Escape. A mouse hover still
shows the band without a ring.
Follows the primitive back to a single tooltip element. The styled
`Chart.Tooltip` again takes the element's props and `ref`, styles it as
`.cl-chart-tooltip` with the z-index, and hides when its children return
null. The `.cl-chart-tooltip-positioner` class is gone, and the Swingset
examples and docs return content from `Chart.Tooltip` directly.
Hovering anywhere in a column selects it again, so the examples no longer
say that moving off the bars closes the tooltip.
The styled `Chart.FocusOutline` draws Mosaic's focus ring, in the ring color
and width, 2px clear of the selected section's bars, with corners concentric
with theirs. `Chart.Bars` renders it by default after the zero line. The
cursor no longer draws a ring and shows no band under the keyboard, so the
ring around the bars is the only focus mark.
Reverts 188db2b (the styled `Chart.FocusOutline`) and 38c3ee2 (the
ring on the keyboard-selected band). `Chart.Selection` rings the whole chart
while it has keyboard focus again, and the cursor shows the same band for
the keyboard as for a pointer.
The Highlight example dimmed on hover, so the highlight and the hover band
said the same thing and the dimming left with the pointer. It now follows
Dashboard's overview chart: `onActivate` pins the clicked section, or the one
Enter is pressed on, the dimming stays after the pointer leaves, and
clicking the same section again clears it.
The styled bar chart listed its legend in stack order, bottom first, while
its tooltip listed a period's rows from the top. A chart with churn below
zero had to pass its own legend items to read in drawing order.

A series now takes `negative: true` when it is drawn below zero. The legend
lists the stack above zero from its top, then the negative series from the
zero line down, then a labelled projection, matching the tooltip.
`Chart.useBarChart` returns these entries as `chart.legend`, so a legend
placed outside `Chart.Root`, such as beside an Export button, reads
`<Chart.Legend items={chart.legend} />` instead of repeating the order.
The default bar tooltip dropped a series whose value was zero, so a period
where nothing churned or nobody returned showed fewer rows than the chart
tracks, and Dashboard rebuilt the rows to keep them.

A zero now lists with the section its series is drawn in: above zero by
default, below zero for a `negative` series. A missing or non-finite value
still lists nothing, and a period with nothing to show still hides the
tooltip.
A tooltip sizes to its content, so a `Banner` in `Chart.TooltipContent`'s
footer stretched the tooltip to the length of its sentence. The footer now
sits in a `cl-chart-tooltip-footer` box that takes the width the heading and
rows set, so its text wraps there instead.
The six `--cl-color-chart-*` tokens move from `tokens.stylex.ts` to
`components/chart/chart.vars.stylex.ts`, so a separate chart stylesheet can
carry them. Only the chart reads them.

`resolveChartColor` now reads a slot from `chartColorVars` instead of
building the `var()` string. The output is the same string, and the runtime
import keeps the palette in the module graph, so a StyleX build that follows
imports, such as the Vitest browser project, still emits it.
Clerk's components do not render a chart, so `styles.css` should not carry
its rules or palette. The CSS build now has two entries: `styles/index.ts`
for `styles.css` and `styles/chart.ts` for `chart.css`. A sheet holds the
StyleX rules its entry reaches that an earlier sheet does not, so the atoms
the chart shares with Tooltip or Button stay in `styles.css`.

StyleX names a layer `priorityN` by its position among the levels a sheet
contains, so two sheets built apart would give one name two priorities. The
build gives each sheet every level: both files name all ten layers in order,
and the sublayer order holds whichever sheet loads first.

Every declaration of the old `styles.css` is in exactly one of the two
sheets, in the same layer and selector. `styles.css` drops to 19.69KB gzip,
so its budget returns to 20KB; `chart.css` is 1.3KB under a 2KB budget.

Apps that render the chart import both sheets into the same layer:

  @import '@clerk/mosaic/styles.css' layer(components);
  @import '@clerk/mosaic/chart.css' layer(components);
`<BarChart.Root aria-label data index series />` now draws the whole
chart. Children replace the layout: `BarChart.Legend` renders in place,
anywhere in the column, and `BarChart.Plot` holds the drawn layers.
Each chart type gets its own namespace and root, and shares the legend,
grid, axes and tooltip parts.

- Each axis reserves its gutter while mounted; `margin` only overrides.
- The `tooltip` prop is typed from `data`, so no type argument is needed.
- `BarChart.Tooltip` mounts the selection surface and the hover band.
  Leaving it out gives a chart that takes no focus.
- A projection names its row by category key, and the legend lists it
  only when it draws.
- The default layout shows the legend from two entries.

The styled `Chart` namespace, with `Chart.useBarChart`, `Chart.Root`,
`Chart.Selection` and `Chart.Cursor`, is gone. The headless primitive is
unchanged.
ChartFrame takes the computed chart style as `chartStyle`, since a `style` prop reads as an inline style to the xstyle rule. The custom tooltip test renders through a module-level function.

This branch was successfully deployed

1 active and 1 inactive deployments
Preview – swingset — 2a390ed0 Deployed Dec 4, 2026 by vercel[bot]
Preview – clerk-js-sandbox — 2a390ed0 Deployed Dec 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants