Skip to content
Source
Sign inSign up
TanStack/charts

[Feature] Focus strategy context: add the current focus and the trigger (`move`, `click`, `keyboard`) #190

Opentoomingos(GitHub profile, opens in a new tab) opened this issue 1 comment

Version: @tanstack/charts 1.1.0 (57774e1).

A ChartFocusStrategy.resolve gets only (points, { x, y, maxDistance }). It cannot know which points are focused (A), so a custom strategy cannot keep a sticky hit area, for example when a focused mark grows and the pointer sits on its old edge. It also cannot tell a pointer move from a click (B). Both are additive fields on the same context.

A. No current focus in the context

Repro: save as packages/charts-core/src/repro.test.ts, run pnpm vitest run packages/charts-core/src/repro.test.ts.

import { expect, it, vi } from 'vitest'
import { scaleLinear } from 'd3-scale'
import { dot } from './dot'
import { motion } from './motion'
import { mountChartRenderer } from './renderer'
import { defineChart } from './scene'
import type { ChartFocusResolveContext } from './types'

it('A: a focus strategy is told which points are currently focused', () => {
  const seen: ChartFocusResolveContext[] = []
  const rows = [{ x: 1, y: 1 }, { x: 2, y: 2 }]
  const container = document.createElement('div')
  const host = mountChartRenderer(container, {
    definition: defineChart({
      marks: [dot(rows, { x: 'x', y: 'y' })],
      scales: { x: { scale: scaleLinear }, y: { scale: scaleLinear } },
      pointer: false,
      focus: {
        resolve: (points, context) => (seen.push(context), points.slice(0, 1)),
        group: (_points, { point }) => [point],
        navigation: (points) => points,
      },
    }),
    renderer: motion({ initial: false }),
    width: 300, height: 200, ariaLabel: 'Dots',
  })
  vi.spyOn(container.querySelector('svg')!, 'getBoundingClientRect').mockReturnValue(
    DOMRect.fromRect({ width: 300, height: 200 }),
  )
  host.interaction.setControlledFocus(host.interaction.resolvePointer(10, 10))
  host.interaction.resolvePointer(11, 11) // second call, a point is now focused
  expect(seen.at(-1)).toHaveProperty('current')
})

Output on 57774e1: AssertionError: expected { x: 11, y: 11, maxDistance: 48 } to have property "current".

Downstream (bklit migration, Chrome, arc that scales up when focused): focus toggled on and off at the arc's inner edge as it grew and shrank.

B. No trigger in the context (from this comment)

A pointer move and a click both reach strategy.resolve through pointsAtPointer (renderer.ts:644; callers renderer.ts:752 and renderer.ts:858), so a strategy cannot treat them differently.

Example: a sunburst keeps hover on the last sector while the pointer is over the hub or a gap, but a click on the hub must still zoom out and a click on a gap must do nothing. Holding the focus inside resolve would also hold the hub click, because the click re-resolves through the same function. The only workaround is capture-phase pointermove and click listeners on the host.

Proposed

Add two optional fields to ChartFocusResolveContext, filled by the renderer:

  • current?: readonly ChartPoint<TDatum, TXValue, TYValue>[], the focused points;
  • kind?: 'move' | 'click' | 'keyboard', the trigger.

The second part of the comment (the pointer position in onFocusChange) moved to #207, which asks for the focus position on the host.

Related: #203 (pointer move cost).

  1. Two more gaps in what a focus strategy and the focus callbacks can see. Checked against 57774e14a1a86081eddb3a23523724a40da05640 (v1.1.0).

    Focus resolve has no event type

    ChartFocusResolveContext is { x, y, maxDistance } (types.ts:2113-2117). A pointer move and a click both reach strategy.resolve through pointsAtPointer (renderer.ts:644; callers renderer.ts:752 and renderer.ts:858), so a strategy cannot treat them differently.

    Example: a sunburst keeps hover on the last sector while the pointer is over the hub or a gap, but a click on the hub must still zoom out and a click on a gap must do nothing. Holding the focus inside resolve would also hold the hub click, because the click re-resolves through the same function. The only workaround is capture-phase pointermove and click listeners on the host.

    Proposal: add the trigger to the context, for example kind: 'move' | 'click' | 'keyboard', next to current from the main report. Additive.

    Focus callbacks are deduped by datum identity, so a continuous pointer position is never reported

    notifyFocus calls onFocusChange only when the primary point's (key, markId, datumIndex) or Object.is(datum) changes (renderer.ts:561-581, sameFocusObservation at renderer.ts:1404-1416). Moves inside one point's hit region produce no callback, and the callback carries no coordinates.

    Example: a live line chart whose crosshair and pill follow the raw pointer x between samples cannot get that x from the focus pipeline. It needs a separate pointermove listener on the container, and it then has to convert client coordinates to plot space itself.

    Proposal: pass the resolved pointer position (plot-space x/y, or the ChartTooltipPosition already used for tooltips) as a second argument to onFocusChange, or add an opt-in onPointerMove with scene coordinates. This does not conflict with the dedupe in #178; it only adds data to the call that already happens.

  2. toomingos(GitHub profile, opens in a new tab) changed the title

Sign in to join this conversation.