Chart tooltip
The readout box a chart shows at its cursor, as parts for people who draw their own charts: the box, a title, rows with a swatch and a total. The box stays inside its container wherever the cursor is. The charts in this library draw their own readout; use these parts for a chart of yours.
Install
npx shadcn@latest add https://thrumui.dev/r/chart-tooltip.jsonAdds components/ui/chart-tooltip.tsx and installs motion. Nothing else in your project changes.
import { ChartTooltip } from "@/components/ui/chart-tooltip";Props
| Prop | Type | Default | Description |
|---|---|---|---|
ChartTooltip: x | number | Required | Horizontal position of the cursor, in pixels from the left edge of the container. |
ChartTooltip: y | number | Required | Vertical position of the cursor, in pixels from the top edge of the container. |
ChartTooltip: width | number | Required | Width of the container in pixels, usually from useSize(). Nothing is drawn while it is 0. |
ChartTooltip: open | boolean | Default true | Whether the box is drawn. Pass false when no point is active. |
ChartTooltip: side | "top" | "bottom" | Default "top" | Whether the box sits 8 px above or 8 px below the cursor. |
ChartTooltip: children | ReactNode | – | The title, rows and total. |
ChartTooltip: className | string | – | Merged onto the box. |
ChartTooltipTitle: children | ReactNode | – | The name of the point, such as a date. Drawn muted. |
ChartTooltipRow: color | string | – | Colour of the square swatch. Leave out for a row with no swatch. |
ChartTooltipRow: label | ReactNode | Required | Name of the value, such as the series name. |
ChartTooltipRow: value | ReactNode | Required | The value, already formatted. It is aligned to the end of the row. |
ChartTooltipRow: muted | boolean | Default false | Draw the value muted and not bold, for a missing reading. |
ChartTooltipTotal: label | ReactNode | Default "Total" | Name of the sum. |
ChartTooltipTotal: value | ReactNode | Required | The sum, already formatted. Drawn under a rule. |
className | string | – | Every part takes one, merged with its own classes. |
Motion and sound
Motion
- The box appears in 150 ms and follows the cursor on a stiff spring, so it feels attached but never jitters.
- Across the container it slides from left-aligned at the left edge to right-aligned at the right edge, so it never leaves the container.
- Reduced motion: the box appears and moves at once.
Sound
Silent. This component makes no sound.
Accessibility
- The box is hidden from screen readers. The chart's own cursor speaks the same reading as its value text, so it is not read twice.
- It carries data-surface="floating" with a solid bg-popover. Under frosted glass it turns translucent and keeps its text at 4.5:1; the text sits 8 px inside its edge.
- Swatches carry the colour; names and values stay in the text colour, so every row keeps its contrast.
- The demo chart is one tab stop, exposed as a slider, with a visually hidden table of every value.
Source
"use client";
import type { ReactNode } from "react";
import { motion } from "motion/react";
import { cn } from "@/lib/utils";
import { FAST, INSTANT, SPRING, useReducedMotion } from "@/lib/thrum-motion";
export type ChartTooltipSide = "top" | "bottom";
export type ChartTooltipProps = {
/** Horizontal position of the cursor, in pixels from the left edge of the container. */
x: number;
/** Vertical position of the cursor, in pixels from the top edge of the container. */
y: number;
/** Width of the container in pixels, usually from `useSize()`. The box slides along it so it never leaves it. */
width: number;
/** Whether the box is drawn. Hide it when no point is active. */
open?: boolean;
/** Whether the box sits above or below the cursor. */
side?: ChartTooltipSide;
children?: ReactNode;
className?: string;
};
const r = (n: number) => Math.round(n * 100) / 100;
// Space between the cursor and the box, in pixels.
const OFFSET = 8;
/**
* The readout box a chart shows at its cursor. Place it inside a `relative` container that holds the plot.
* Over the left edge it is left-aligned to the cursor, over the right edge right-aligned, and in between it slides
* smoothly, so it stays inside the container however wide it is. It follows the cursor on a stiff spring.
* It is hidden from screen readers: the chart's cursor already speaks the same reading.
*/
export function ChartTooltip({ x, y, width, open = true, side = "top", children, className }: ChartTooltipProps) {
const reduced = useReducedMotion();
if (!open || width <= 0) return null;
const at = Math.max(0, Math.min(width, x));
const follow = reduced ? INSTANT : SPRING.follow;
return (
<motion.div
data-slot="chart-tooltip"
data-surface="floating"
aria-hidden="true"
className={cn(
"pointer-events-none absolute top-0 left-0 z-10 flex max-w-full flex-col gap-0.5 rounded-md border border-foreground/15 bg-popover p-2 text-xs leading-4 whitespace-nowrap text-popover-foreground tabular-nums shadow-md",
className,
)}
initial={reduced ? false : { opacity: 0, x: at, y }}
animate={{ opacity: 1, x: at, y }}
transition={reduced ? INSTANT : { ...follow, opacity: FAST }}
style={{ translate: `${r((-100 * at) / width)}% ${side === "top" ? `calc(-100% - ${OFFSET}px)` : `${OFFSET}px`}` }}
>
{children}
</motion.div>
);
}
export type ChartTooltipTitleProps = { children?: ReactNode; className?: string };
/** The first line of the box: the name of the point, such as a date or a category. */
export function ChartTooltipTitle({ children, className }: ChartTooltipTitleProps) {
return (
<div data-slot="chart-tooltip-title" className={cn("text-muted-foreground", className)}>
{children}
</div>
);
}
export type ChartTooltipRowProps = {
/** Colour of the swatch, usually the series colour. Leave out for a row with no swatch. */
color?: string;
/** Name of the value, such as the series name. */
label: ReactNode;
/** The value, already formatted. */
value: ReactNode;
/** Draw the value muted, for a missing reading. */
muted?: boolean;
className?: string;
};
/** One reading: a swatch, a name and a value. The swatch carries the colour; the text stays in the text colour. */
export function ChartTooltipRow({ color, label, value, muted = false, className }: ChartTooltipRowProps) {
return (
<div data-slot="chart-tooltip-row" className={cn("flex items-center gap-1.5", className)}>
{color && <span aria-hidden="true" className="size-2 flex-none rounded-[min(var(--radius),2px)]" style={{ backgroundColor: color }} />}
<span>{label}</span>
<span className={cn("ms-auto ps-3", muted ? "text-muted-foreground" : "font-medium")}>{value}</span>
</div>
);
}
export type ChartTooltipTotalProps = {
/** Name of the sum. */
label?: ReactNode;
/** The sum, already formatted. */
value: ReactNode;
className?: string;
};
/** A last line under a rule, for the sum of the rows above. */
export function ChartTooltipTotal({ label = "Total", value, className }: ChartTooltipTotalProps) {
return (
<div data-slot="chart-tooltip-total" className={cn("mt-1 flex border-t pt-1", className)}>
<span>{label}</span>
<span className="ms-auto ps-3 font-medium">{value}</span>
</div>
);
}Files and dependencies
- Adds
components/ui/chart-tooltip.tsx- Packages
motion- Brings along
- Motion tokens
Changelog
- 1.0.0 · 2026-12-03First release.