Repository navigation
feat(mosaic): add the headless Chart primitive with stacked bars - #10953
Draft
austincalvelage wants to merge 39 commits into
Draft
austincalvelage wants to merge 39 commits into
austincalvelage wants to merge 39 commits into
Conversation
…nd tooltip placement in Chromium
…chart object useBarChart measures the container, projects the layout and owns the selection, keyboard handling and announcements. Root takes `chart`, BarSelection spreads `chart.getInteractionProps()`, and `selection` / `onSelectionChange` control the selection. useStackedBarLayout and BarSelection's onActiveChange are removed.
…trolled selection
…d a bar SelectionModel The reducer, controlled reconcile, pointer coalescing, keyboard, activation and announcements now live in useChartInstance, generic over the selection type. The bar section rules (hit test near the zero line, Up and Down, resolving a section) move into barSelectionModel. The pointer snaps to the nearest category center instead of dividing by the band step.
Chart.Root, Grid, YLine, YAxis, YTick, XAxis, XTick, Selection, Cursor and Tooltip live in primitives/chart and read only the shared ChartInstance, so a second chart type can reuse them. BarChart keeps useBarChart, StackedBars, Bar, ProjectionOutline and CanvasRenderError; its marks read their layout through a module-private symbol on the instance. - GridLine and ZeroLine become Chart.YLine value. BarSelection becomes Chart.Selection. Categories carry x and width instead of bandX and bandWidth, and chart.step is gone. - The selection engine moves to chart/internal, fenced by lint to the *-chart folders. - The tooltip anchor carries a y, and calendar ticks interpolate between category centers instead of assuming evenly split bands. - A type-level line chart fixture proves the contract fits points.
primitives/chart/README.md documents the shared parts, the chart object, selection, keyboard, tooltip and paint order. The bar-chart README keeps what bars own: useBarChart, the marks, sections, Canvas styling and layout. The Swingset page moves to Chart.* names and stays one raw Default story.
…ce unmounts Unmounting Chart.Selection while a period was selected left the selection stuck: no pointerleave or blur fires on a removed element, so onSelectionChange never reported null and Chart.XTick kept data-active. The interaction props now carry a ref whose detach clears a pointer or keyboard selection. A selection set through selection or select stays.
…tio changes Browser tests change the class on <html> and the device pixel ratio, then read the painted Canvas: the new theme color, and a backing buffer at the new ratio capped at 2 with the bars repainted. Chromium's device emulation does not fire the resolution query's change event, so the test sends it.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 262c951 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
|
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/electron
@clerk/electron-passkeys
@clerk/eslint-plugin
@clerk/expo
@clerk/expo-biometrics
@clerk/expo-google-signin
@clerk/expo-native-components
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/hono
@clerk/localizations
@clerk/mosaic
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
… changes The bars Canvas read colors from BarChart.Bar's computed style but only repainted on <html> attribute changes. A light-dark() color under `color-scheme: light dark` stayed stale when the OS color scheme flipped, and a theme class or color-scheme set on a wrapper never repainted. Watch class, style and data-theme on every ancestor of BarChart.Bar and the prefers-color-scheme media query instead of <html> alone.
The bar-chart entry re-exports CanvasRenderError before any 'use client' module, so its module must import nothing but relative modules. Its old home in bar-chart-canvas reached d3-scale, which fails check:bundle once the entry is published.
Add the @clerk/mosaic/primitives/chart and @clerk/mosaic/primitives/bar-chart exports, their tsdown entries and their agent docs, and describe the new entries in the changeset.
The marks part paints whatever arrangement useBarChart lays out. A single series is a plain bar chart, and a later grouped mode would render through the same part, so the name no longer says "stacked". The stack layout functions keep their names, since they compute the stacked arrangement itself.
- Take the screen reader text through a `messages` option with English defaults (`ChartMessages`, and `BarChartMessages` for the Up and Down sections), so a styled layer can pass localized strings. - Drop `data-chart-interaction` from the inner interaction surface: primitives emit no slot attributes. Find the surface by its role. - Move the tests that cover the whole chart from bars/__tests__ to chart/__tests__, with their screenshots, and add axe checks.
…the value axis A pointer over the empty plot above or below the bars selected the nearest period, so clicking or tapping off a bar left its tooltip open. The bar model now hit-tests the point itself and selects only on a section's bars, or within a 24px minimum target of the zero line for a short one. Keyboard announcements read the selected period's nonzero values, such as `Feb, Sign-ups 20`, and the projection. They name the section only when some value in the chart is negative, and the keyboard instructions leave Up and Down out otherwise. Series and projections take an optional `label`, and every chart type takes `formatValue`. The selection model now takes a chart point in `hitTest`, returns the tooltip `anchor` as a point, and describes the resolved selection. `ChartInstance` no longer carries `scaleY` and `yTicks`; `CartesianChartInstance` adds them, and `Chart.Grid`, `Chart.YAxis` and `Chart.YLine` throw for a chart without them, so a chart type with no value axis, such as a donut, fits `Chart.Root`.
…ound out the chart options `Chart.Tooltip` now renders no element. It positions against the selected category and provides the Tooltip primitive's context, so the chart renders its tooltip with `Chart.TooltipPortal`, `Chart.TooltipPositioner`, `Chart.TooltipPopup` and `Chart.TooltipArrow`, the same parts as Mosaic's Tooltip. Its fixed offsets become `side`, `sideOffset`, `collisionPadding`, `collisionBoundary` (was `boundaryRef`) and `pinned`. The chart options gain `defaultSelection` and `disabled`, `onActivate` receives the mouse or keyboard event that activated the period, and `getInteractionProps` takes the surface's own props and runs their handlers after the chart's.
…d overflow The first and last category labels were always pinned to the plot's edges, so with a few wide bars they sat away from their bars. Category labels now use the calendar labels' rule: centered under the category, and moved to the edge only where centered they would leave the plot.
…tegory The chart is one tab stop with arrow keys inside, so the focus ring belongs on the selected category rather than around the whole chart. `selectionSource` on the chart says what made the selection, and `Chart.Cursor` carries `data-focus-visible` while it was the keyboard.
The chart tooltip used none of Mosaic's Tooltip behavior: no trigger, no hover or focus wiring, no description link. Composing it from the Tooltip parts meant faking their context and nesting three parts to show content. `Chart.Tooltip` is one positioned `div` again that renders its children's content, takes `div` props and `ref`, and hides when the content is null. It keeps `side`, `sideOffset`, `collisionPadding`, `collisionBoundary` and `pinned`. `Chart.TooltipPortal`, `TooltipPositioner`, `TooltipPopup` and `TooltipArrow` are removed.
…e column A ring around the full column said which period the keyboard was on but not which side of a stacked bar. The selection model now gives a `focusRect`, the selected section from the zero line to its far edge, at least 24px tall like the hit test and at least 12px wide so a ring stays findable on dense data. `Chart.Cursor` covers it while the keyboard made the selection; a mouse hover keeps the full-height column.
The hover band covers the full column, but only the bar and a 24px minimum answered the pointer, so the empty band above a short bar showed a target that did not respond. A pointer selects the nearest column anywhere in the plot again, and the section on its side of the zero line, as Dashboard's chart does. Leaving the plot, tapping outside the chart and Escape still close the tooltip. The keyboard focus ring keeps outlining just the section.
The focus box followed the column band, so it was wider than the bars and brought the band's fill with it. `Chart.FocusOutline` is a `rect` in the `Chart.Bars` SVG around the selected section's painted bars, the projection included, `offset` px outside them and at least `minSize` px each way. It renders only for a keyboard selection. `focusRect` is removed, and `Chart.Cursor` is the column band again.
Reverts bb31b51 (`Chart.FocusOutline`), 0e9a749 (`focusRect`) and 5245c6e (`selectionSource` and the cursor's `data-focus-visible`). A keyboard selection shows the same band and tooltip as a pointer again, and the chart-wide ring marks focus. The column hit test from 8a23d9e stays, and the section measurement only the focus box used is removed.
…sizeObserver callback The chart learned its size only from its first ResizeObserver callback, at the next rendering step after hydration, so any effect the page ran first held the canvas blank. In a Swingset production build, syntax highlighting did that on 3 of 11 warm reloads, leaving the chart blank 186 to 212ms against 63 to 72ms otherwise. Measuring the element as it attaches removed those slow reloads (none of 11, measured with `offsetWidth`). The chart now reads `getBoundingClientRect()` when `rootRef` attaches, which keeps fractional widths, so React paints the canvas in the commit that mounts or hydrates it. The ResizeObserver still commits every later size, and corrects the first one under a scaled ancestor.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a headless
Chartprimitive to Mosaic at@clerk/mosaic/primitives/chart. Its first chart type is stacked bars, with values above and below zero. A single series is a plain bar chart. A line chart will addChart.useLineChartand line marks to the same namespace in a later PR.Root,Grid,YLine,YAxis,YTick,XAxis,XTick,Selection,CursorandTooltip.useBarChart,Bars,Bar,ProjectionOutlineandCanvasRenderError.Chart.Barelement. Consumers target that element withdata-series,data-inactiveanddata-projection.class,style,data-theme), whenprefers-color-schemeflips, and when the pixel ratio changes.chart/internal/. A lint rule allows only the chart-type folders underchart/to import it. Each chart type supplies its own hit test from a chart point, its Up/Down rules and its tooltip anchor.selection,defaultSelection,onSelectionChange,onActivate(active, event)anddisabled. A disabled chart ignores the pointer and keyboard, leaves the tab order and carriesaria-disabled.getInteractionProps(props)returns the surface props for a custom surface. Handlers passed in run after the chart's.rootRefattaches, so React paints the canvas in the commit that mounts or hydrates it. AResizeObservercommits every later size.Chart.Tooltipis onediv, portaled and positioned with Floating UI against the selected category. Its children render the content, and returning null hides it. It takesside,sideOffset,collisionPadding,collisionBoundaryandpinned, and thediv's own props andref.ChartInstancehas no Y scale.CartesianChartInstanceaddsscaleYandyTicks, anduseBarChartreturns one.Chart.Grid,Chart.YAxisandChart.YLinethrow with the part's name for a chart without a value axis, so a chart type such as a donut fitsChart.Rootwithout a fake or nullable scale.Nov 24, 2026, Active agents 53, plus the projection.negative values, …) only when some value in the chart is negative. The keyboard instructions leave Up and Down out otherwise.label. Every chart type takesformatValue.messagesoption (ChartMessages, andBarChartMessagesfor the sections and the projection), with English defaults. A styled layer passes localized strings.chart/line-chart-contract.test-d.tscompiles a type-only line chart and a chart without a value axis against the shared contract.chart.doc.ts. The changeset is minor.Ported from the Dashboard's stacked bar chart, with the same layout and keyboard model. A styled
Chartbuilt on these parts is stacked on this PR in #10960.For review
offsetWidth; this PR usesgetBoundingClientRect(), which keeps fractional widths).Tooltip. Mosaic'sTooltipopens from a trigger on hover and focus, and describes that trigger. The chart opens its tooltip from the selection and announces it through thestatusregion, so it uses none of that. Building it from the Tooltip parts meant faking their context and nesting three parts to show content, soChart.Tooltippositions one element itself.side, flips when that side does not fit, and keeps the side it chose last so it does not cross the selection twice near an edge.Chart.Bars, notChart.StackedBars. The marks part is named for bars in general. A latermode: 'grouped'onuseBarChartcan render through the same part without a breaking rename.Chartconsumer bundles the bar code and d3, about 19KB gzip, even one that uses only the shared parts.__exportAllgetter object. I measured it with webpack 5, rollup and esbuild.export * asre-export lets webpack and rollup drop the unused members, but not esbuild. That fix is deferred to the line chart PR, where the cost first applies.Chart.useBarChart) is new for Mosaic primitives. React Aria'suseListBoxwithListBoxis the closest precedent.d3-scale4.0.2 andd3-shape3.2.0. Neither is indist/index.js.getInteractionProps()returns aref. It lets the chart end a pointer or keyboard selection when its interaction surface unmounts.onSelectionChangepasses only the selection, as Mosaic's other primitives pass only their value. Passing{ reason, event }would be a Mosaic-wide change, so it is left out here.How to test
pnpm --filter @clerk/mosaic testandtest:browser. The browser tests cover painting, resize, pointer and touch selection, selecting the column above a short bar, the section on each side of the zero line, a first paint without any ResizeObserver callback, tooltip placement andside, a tooltip hidden by null content, and repaint after theme, color scheme and pixel ratio changes.pnpm --filter @clerk/mosaic test:visualruns the 4 chart screenshots.pnpm dev:swingset, then open/primitives/chart. Hover a bar, then move above a short one: the tooltip closes. Focus the chart and use the arrow keys, Home and End, and read thestatusregion.Verification
On
262c951b20:lint,format:check,test(3,584 passed, 2 todo),test:browser(160) andbuildpass. The build includestypecheck, the client-boundary check and the docs-export check.pointer-events: none.statusregion readsNov 24, 2026, Active agents 53for one series, andJan 12, 2026, negative values, Retained churned -25, Reactivated churned -11, New churned -18after Down on the stacked example.On an earlier commit of this PR, and not rerun since:
bundlerandnodenext) and runtime for@clerk/mosaic/primitives/chart.react-doctor0.9.23 reported one deliberateno-flush-syncwarning: the resize observer commits the new size in the same frame, so Canvas never paints at the old size.Not verified
stacked-bars-*baselines to change.statusregion's text.ticks='calendar') have no screenshot test.CanvasRenderErrorend to end in this PR. The stacked styled PR covers error and retry.Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
🤖 Generated with Claude Code