Terminal
Terminal/Skill
Covers making TanStack Charts fit narrow or resized containers, with label priority, SSR initial width and stable comparisons.
By TanStack
Works with
npx skills add tanstack/charts --skill design-responsive-chartsUse trigger → inspect → decide → build → verify. Adapt to the chart container and information priority; do not equate responsive design with stretching geometry.
Omit host width, choose height deliberately, and use the responsive definition only for surface-dependent decisions:
import { barX, defineChart, mountChart } from '@tanstack/charts'
import { scaleBand } from '@tanstack/charts/scales/band'
import { scaleLinear } from '@tanstack/charts/scales/linear'
const rows = [
{ feature: 'Account recovery', requests: 128 },
{ feature: 'Saved searches', requests: 95 },
{ feature: 'Audit export', requests: 72 },
]
const definition = defineChart(({ width }) => ({
marks: [barX(rows, { x: 'requests', y: 'feature' })],
scales: {
x: {
scale: scaleLinear,
nice: true,
axis: { ticks: { count: width < 420 ? 3 : 6 } },
},
y: { scale: () => scaleBand<string>().padding(0.1) },
},
}))
const element = document.querySelector<HTMLElement>('#feature-chart')
if (!element) throw new Error('Missing #feature-chart')
export const host = mountChart(element, {
definition,
height: 320,
initialWidth: 640,
ariaLabel: 'Weekly feature requests',
})
The container's grid or flex item must be allowed to shrink, usually with min-width: 0.
At narrower containers:
Keep domains, thresholds, units, and category-color assignments stable when readers compare the same chart across sizes.
The responsive builder receives full surface width and height. Axes, legends, and measured text later determine scene.chart, the final inner plot. Use:
host.getScene().chart for application overlays after render.Do not duplicate margin math in application code.
Waffle packing, treemaps, Sankey columns, facets, Delaunay links, hexbins, density contours, and label fit can change membership or arrangement when final bounds change. Verify semantic keys and interaction state after topology changes, not only node dimensions.
Supply the same initialWidth for equivalent server renders. The client adopts the measured container width after hydration. Use fixed width only for exports, benchmarks, or another application-owned frame.
Wrong:
const compact = window.innerWidth < 640
Correct:
defineChart(({ width }) => ({
marks,
scales: {
x: {
scale: scaleLinear,
axis: { ticks: { count: width < 420 ? 4 : 8 } },
},
y: { scale: scaleLinear },
},
}))
Dashboard panels and embeds can be narrow inside a wide viewport.
Source: docs/guides/responsive-charts.md
Wrong: compute bins, collisions, or overlay positions directly from responsive builder width.
Correct: perform exact pixel work from resolved custom-mark bounds or scene.chart.
Guides and legends reduce the final plot after the builder returns.
Source: API-FRICTION.md F-116, F-219; docs/guides/responsive-charts.md
Wrong: preserve old treemap, facet, waffle, Delaunay, hexbin, or label topology and scale its pixels.
Correct: let the owning resolved-layout primitive recompute from final bounds and preserve semantic keys through the change.
Many final-pixel layouts change topology, not only size.
Source: waffle, treemap, Delaunay, and hexbin mark references
Wrong:
mountChart(element, { definition, width: 640, height: 320, ariaLabel })
Correct:
mountChart(element, {
definition,
height: 320,
initialWidth: 640,
ariaLabel,
})
Fixed width opts out of container measurement; missing initialWidth or shrink constraints causes different SSR or overflow failures.
Source: docs/guides/responsive-charts.md; API-FRICTION.md F-111
Wrong:
x: {
scale: scaleLinear().range([0, 640])
}
Correct:
x: {
scale: scaleLinear
}
The range must follow the final plot after container measurement and guide margins resolve.
Source: API-FRICTION.md F-002; docs/concepts/scales-and-d3.md
Wrong: expect automatic margins to make every long tick label readable.
Correct: choose responsive candidate spacing, thinning, rotation, abbreviation, or a different composition.
Margins contain guides inside the surface; they do not resolve every label-label collision.
Source: API-FRICTION.md F-023, F-160; docs/guides/responsive-charts.md
Reduce guide density and change composition without silently changing what position, color, or a threshold means.
See also: configure-scales-guides-color/SKILL.md § Common Mistakes
Responsive relayout should commit immediately by default. Animating every observed resize can leave geometry behind the actual panel size and repeatedly interrupt transitions.
See also: update-and-animate-charts/SKILL.md and debug-and-verify-charts/SKILL.md
See also: ship-accessible-charts/SKILL.md and update-and-animate-charts/SKILL.md — initial size, label priority, and resize policy affect hydration, accessibility, and motion.