Skip to content
WunderUI

useChartInteraction

Pro

Pointer and keyboard behaviour of a chart: the active point, arrow keys, range selection and the live announcement.

import { useChartInteraction } from "@wunderui/react/charts"

Playground

Installation

useChartInteraction comes with WunderUI Core and Pro, in the entry of the package from your plan. See the plans. Then import it:

tsx
import { useChartInteraction } from "@wunderui/react/charts"

When to use

Use useChartInteraction for

  • A chart you draw yourself, with SVG, canvas or HTML marks, that needs a hover point, arrow-key navigation and a live announcement like the WunderUI charts.
  • A plot where people select a range by dragging, or with Shift and the arrow keys.
  • A chart whose active point or range belongs to the page, for example to link it to a table row or a filter: control it with and .

Use something else for

  • Line Chart for a finished chart. It runs this hook inside and reports the same values through and .
  • Bar Chart for the same for bars, vertical or horizontal.
  • useGlide for the movement of hover marks. It takes the index of this hook and glides the marks to it; this hook does not animate.
  • Native controls for a set of options or a row of buttons. A radio group, a listbox or Tabs already handle the arrow keys, Home and End and the focus.

Signature

The behaviour the WunderUI cartesian charts share, for a chart you draw yourself. Pointer movement sets the active point and leaving clears it; the arrow keys (up and down on horizontal charts), Home and End walk the points and announce each one; Escape clears point and range. With , dragging across the plot or Shift with the arrow keys selects a range. Spread on the focusable plot element and render in a polite live region.

ts
useChartInteraction(o: ChartInteractionOptions): { active: number | null; range: ChartRange | null; announcement: string; handlers: { onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => void; onPointerMove: (e: React.PointerEvent<HTMLDivElement>) => void; onPointerDown: (e: React.Poin…

Parameters

ParameterTypeDescription
oChartInteractionOptionsThe options object. Only , and are required.

Returns

An object with the state to draw and the handlers to spread on the plot element.

FieldTypeDescription
activenumber | nullThe index of the active point, or . Set by the pointer and by the keyboard. A value outside is returned as .
rangeChartRange | nullThe selected range as , clamped to the data, or when nothing is selected, the range is empty after clamping or is off.
announcementstringThe sentence for the live region. Only the keyboard writes it, with or ; Escape empties it. The pointer never announces.
handlers{ onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => void; onPointerMove: (e: React.PointerEvent<HTMLDivElement>) => void; onPointerDown: (e: React.PointerEvent<HTMLDivElement>) => void; onPointerUp: (e: React.PointerEvent<HTMLDivEleme…Six event handlers: , , , , and . Spread them on the one focusable element that is the plot.

Keyboard interactions

The plot is one tab stop. Once it has focus, the hook handles these keys on the element that carries .

ArrowRightorArrowLeft
Moves the active point to the next or previous one and announces it. With nothing active, Right lands on the first point and Left on the last. The movement stops at the ends; it does not wrap.
ArrowDownorArrowUp
The same for a chart with : Down is next, Up is previous. Left and Right do nothing there.
HomeorEnd
Moves the active point to the first or the last one.
ShiftArrowRightorShiftArrowLeft
Only with : selects a range from the point where the Shift selection started to the new active point. The live region reads the range. With use Shift with Up and Down. Shift with Home or End extends to the first or last point.
Escape
Clears the active point and the range and empties the announcement. When nothing is active or selected the key is left alone, so a dialog around the chart still closes on Escape.

A plot without has no keyboard path at all. Give it a role and a label, a visible focus ring, and render in a polite live region.

Behavior and motion

The hook turns pointer and key events into state. Drawing the marks and speaking the announcement are up to your markup.

State
The active point and the range are each controllable. Given or (anything but , also ) the hook is controlled and only reports changes through its callbacks; otherwise it keeps its own state, starting at and . The callbacks are called in both modes. The returned and are cleaned up: an index outside the data becomes , a range is clamped to the data and dropped when nothing is left, so shrinking data cannot leave a stale mark.
Pointer
Moving the pointer sets the active point, for mouse, touch and pen alike. Leaving the element, or the element losing focus, clears it; a range stays. Positions are read in layout pixels, so a scaled or zoomed preview does not shift the hit test. With the point is hit-tested in two dimensions and means outside every mark.
Range
With , pressing the primary button starts a drag at the point under the pointer and captures the pointer. Moving to another point selects the ordered range; releasing keeps it. A press and release without moving clears an existing range. While a drag runs, leaving the element does not clear the active point. Range dragging does not run together with .
Announcement
Only the keyboard announces: after an arrow key, Home or End the live region gets , after a Shift selection . Escape empties it. Pointer movement does not announce, so the live region stays quiet while someone moves the mouse over the chart.
Server rendering
Rendering touches no browser API: only state and refs. The handlers read the element (, ) when an event fires. The hook renders the same on the server and the client.
Cleanup
The hook registers no timers and no listeners of its own, only React event handlers. The pointer capture of a drag is released on pointer up. If the plot unmounts during a drag, nothing is left behind.
Motion
None. The hook only reports state. Animate what you draw with the active point, for example with .
Reduced motion
Nothing to reduce in the hook. The marks you draw on the active point follow the reduced-motion rule of their own.

Best practices

  • Do

    Spread on the one element that is exactly the plot. Pointer positions are relative to it, so padding, an axis or a legend inside the element shifts every hit.

  • Do

    Make the plot focusable: , , an that names the chart and mentions the keys, and a visible focus ring.

  • Do

    Render in an region next to the plot, and write as a full sentence with the name and the value, such as "Wednesday: 40 sign-ups".

  • Do

    Draw the active mark from , not from your own pointer state, so the pointer and the keyboard show the same thing.

  • Do

    Give a plot with range selection (), as the finished charts do. A horizontal drag then selects and a vertical one still scrolls the page.

  • Don’t

    Do not pass to leave the state to the hook. It means controlled with nothing active, and the plot would stay empty. Leave the prop out or pass .

  • Don’t

    Do not let return an index outside the data. It is not clamped for you: an index outside shows as no active point.

  • Don’t

    Do not use the hover point as the only way to read a value. People on touch devices and with a keyboard need the announcement, a tooltip or a table.

Notes for AI

Give your coding assistant the page or the MCP call instead of screenshots.

MCPget_component("useChartInteraction")
Importimport { useChartInteraction } from "@wunderui/react/charts"

The full library index for assistants is at /llms.txt.