Skip to content
Thrum

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.json

Adds components/ui/chart-tooltip.tsx and installs motion. Nothing else in your project changes.

import { ChartTooltip } from "@/components/ui/chart-tooltip";

Props

PropTypeDefaultDescription
ChartTooltip: xnumberRequiredHorizontal position of the cursor, in pixels from the left edge of the container.
ChartTooltip: ynumberRequiredVertical position of the cursor, in pixels from the top edge of the container.
ChartTooltip: widthnumberRequiredWidth of the container in pixels, usually from useSize(). Nothing is drawn while it is 0.
ChartTooltip: openbooleanDefault trueWhether 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: childrenReactNode–The title, rows and total.
ChartTooltip: classNamestring–Merged onto the box.
ChartTooltipTitle: childrenReactNode–The name of the point, such as a date. Drawn muted.
ChartTooltipRow: colorstring–Colour of the square swatch. Leave out for a row with no swatch.
ChartTooltipRow: labelReactNodeRequiredName of the value, such as the series name.
ChartTooltipRow: valueReactNodeRequiredThe value, already formatted. It is aligned to the end of the row.
ChartTooltipRow: mutedbooleanDefault falseDraw the value muted and not bold, for a missing reading.
ChartTooltipTotal: labelReactNodeDefault "Total"Name of the sum.
ChartTooltipTotal: valueReactNodeRequiredThe sum, already formatted. Drawn under a rule.
classNamestring–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

components/ui/chart-tooltip.tsx
"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. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor