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

Abierto
#190 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
62/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
d3, typescript

Línea de trabajo

Start with the ChartFocusResolveContext type in packages/charts-core/src/types.ts (around line 2113), then the context built in packages/charts-core/src/interaction.ts (line 68) and the focusedPoint field in renderer.ts (line 135). Add an optional current array filled from the focused points, and update docs/reference/focus-and-interaction.md near line 771. Done when the repro test packages/charts-core/test-repro/05-pointer-focus.test.ts passes and the context type and docs describe current.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
TypeScript
Estrellas
870
Forks
57
Merge medio
11 h 58 min
PR fusionados (30 d)
18

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TanStack/charts

Todos los issues de TanStack/charts

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.