diff --git a/benchmarks/conformance/cases/132-shadcn-tooltip-advanced/example.tsx b/benchmarks/conformance/cases/132-shadcn-tooltip-advanced/example.tsx index f8e0dd86..18e59254 100644 --- a/benchmarks/conformance/cases/132-shadcn-tooltip-advanced/example.tsx +++ b/benchmarks/conformance/cases/132-shadcn-tooltip-advanced/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - Advanced" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/190-shadcn-tooltip-default/example.tsx b/benchmarks/conformance/cases/190-shadcn-tooltip-default/example.tsx index 813db47d..2eff2b92 100644 --- a/benchmarks/conformance/cases/190-shadcn-tooltip-default/example.tsx +++ b/benchmarks/conformance/cases/190-shadcn-tooltip-default/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - Default" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/191-shadcn-tooltip-formatter/example.tsx b/benchmarks/conformance/cases/191-shadcn-tooltip-formatter/example.tsx index 96c1e176..6c4e3716 100644 --- a/benchmarks/conformance/cases/191-shadcn-tooltip-formatter/example.tsx +++ b/benchmarks/conformance/cases/191-shadcn-tooltip-formatter/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - Formatter" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/192-shadcn-tooltip-icons/example.tsx b/benchmarks/conformance/cases/192-shadcn-tooltip-icons/example.tsx index b726c880..4dc38644 100644 --- a/benchmarks/conformance/cases/192-shadcn-tooltip-icons/example.tsx +++ b/benchmarks/conformance/cases/192-shadcn-tooltip-icons/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 - 27 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - Icons" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/193-shadcn-tooltip-indicator-line/example.tsx b/benchmarks/conformance/cases/193-shadcn-tooltip-indicator-line/example.tsx index dd6423f3..ce131ac2 100644 --- a/benchmarks/conformance/cases/193-shadcn-tooltip-indicator-line/example.tsx +++ b/benchmarks/conformance/cases/193-shadcn-tooltip-indicator-line/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - Line Indicator" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/194-shadcn-tooltip-indicator-none/example.tsx b/benchmarks/conformance/cases/194-shadcn-tooltip-indicator-none/example.tsx index d747f000..6135cc9e 100644 --- a/benchmarks/conformance/cases/194-shadcn-tooltip-indicator-none/example.tsx +++ b/benchmarks/conformance/cases/194-shadcn-tooltip-indicator-none/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - No Indicator" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/195-shadcn-tooltip-label-custom/example.tsx b/benchmarks/conformance/cases/195-shadcn-tooltip-label-custom/example.tsx index 7aba5688..22982547 100644 --- a/benchmarks/conformance/cases/195-shadcn-tooltip-label-custom/example.tsx +++ b/benchmarks/conformance/cases/195-shadcn-tooltip-label-custom/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - Custom label" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/196-shadcn-tooltip-label-formatter/example.tsx b/benchmarks/conformance/cases/196-shadcn-tooltip-label-formatter/example.tsx index 5141beee..92cf6c96 100644 --- a/benchmarks/conformance/cases/196-shadcn-tooltip-label-formatter/example.tsx +++ b/benchmarks/conformance/cases/196-shadcn-tooltip-label-formatter/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - Label Formatter" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/cases/197-shadcn-tooltip-label-none/example.tsx b/benchmarks/conformance/cases/197-shadcn-tooltip-label-none/example.tsx index 38afbfff..b8fc0363 100644 --- a/benchmarks/conformance/cases/197-shadcn-tooltip-label-none/example.tsx +++ b/benchmarks/conformance/cases/197-shadcn-tooltip-label-none/example.tsx @@ -1,4 +1,3 @@ -import { useRef } from 'react' import { barY, defineChart, stack, type ChartPoint } from '@tanstack/charts' import { RendererChart } from '@tanstack/charts/react/tooltip' import { tooltip } from '@tanstack/charts/tooltip' @@ -47,12 +46,9 @@ export function createExampleChart() { tooltip: { use: tooltip, className: 'sc-chart-tooltip', - anchor: (_points, context) => ({ - x: context.surface.width * 0.271, - y: context.surface.height * 0.554, - }), + anchor: 'pointer', placement: 'bottom-right', - offset: 0, + offset: 12, sort: 'color-domain', content: () => ({ rows: [] }), }, @@ -165,7 +161,6 @@ export interface ExampleProps { height?: number } export default function Example({ width = 640, height = 600 }: ExampleProps) { - const seededTooltip = useRef(false) const contentWidth = Math.max(1, width - 50) const chartWidth = contentWidth const chartHeight = (contentWidth * 9) / 16 @@ -189,19 +184,6 @@ export default function Example({ width = 640, height = 600 }: ExampleProps) { initialWidth={chartWidth} height={chartHeight} ariaLabel="Tooltip - No Label" - onRender={({ scene, interaction }) => { - if (seededTooltip.current) return - const point = scene.points.find( - (candidate) => - (candidate.datum as ShadcnActivityDatum).date === - '2024-07-16', - ) - if (!point) return - seededTooltip.current = true - interaction.setControlledFocus(point, { - source: 'programmatic', - }) - }} renderTooltipBody={({ points }) => ( []} diff --git a/benchmarks/conformance/previews/manifest.json b/benchmarks/conformance/previews/manifest.json index c418d2e8..417ca257 100644 --- a/benchmarks/conformance/previews/manifest.json +++ b/benchmarks/conformance/previews/manifest.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "width": 288, "height": 192, - "sourceHash": "f5af2e197556a55efcceca495db0cc1906d648f24a543378691eb418747def1b", + "sourceHash": "cd4c907c1e897fbb571ce825769c9a5c26cb065f40a031cbb9a4c16c4988f6b2", "assets": [ { "id": "01-line-gaps", diff --git a/benchmarks/conformance/shared/shadcn-tooltip-interaction.test.ts b/benchmarks/conformance/shared/shadcn-tooltip-interaction.test.ts new file mode 100644 index 00000000..d335a8ff --- /dev/null +++ b/benchmarks/conformance/shared/shadcn-tooltip-interaction.test.ts @@ -0,0 +1,109 @@ +import { act, createElement } from 'react' +import { createRoot } from 'react-dom/client' +import { createChartScene } from '@tanstack/charts' +import { describe, expect, it, vi } from 'vitest' +import Advanced, { + definition as advanced, +} from '../cases/132-shadcn-tooltip-advanced/example' +import Default, { + definition as standard, +} from '../cases/190-shadcn-tooltip-default/example' +import Formatter, { + definition as formatter, +} from '../cases/191-shadcn-tooltip-formatter/example' +import Icons, { + definition as icons, +} from '../cases/192-shadcn-tooltip-icons/example' +import Line, { + definition as line, +} from '../cases/193-shadcn-tooltip-indicator-line/example' +import NoIndicator, { + definition as noIndicator, +} from '../cases/194-shadcn-tooltip-indicator-none/example' +import CustomLabel, { + definition as customLabel, +} from '../cases/195-shadcn-tooltip-label-custom/example' +import LabelFormatter, { + definition as labelFormatter, +} from '../cases/196-shadcn-tooltip-label-formatter/example' +import NoLabel, { + definition as noLabel, +} from '../cases/197-shadcn-tooltip-label-none/example' + +const examples = [ + ['advanced', Advanced, advanced], + ['default', Default, standard], + ['formatter', Formatter, formatter], + ['icons', Icons, icons], + ['line indicator', Line, line], + ['no indicator', NoIndicator, noIndicator], + ['custom label', CustomLabel, customLabel], + ['label formatter', LabelFormatter, labelFormatter], + ['no label', NoLabel, noLabel], +] as const + +describe('ShadCN tooltip examples', () => { + it.each(examples)( + 'waits for interaction and follows the pointer in %s', + async (_name, Example, definition) => { + const container = document.createElement('div') + document.body.append(container) + const root = createRoot(container) + const width = 590 + const height = (width * 9) / 16 + try { + await act(async () => + root.render(createElement(Example, { width: 640 })), + ) + const svg = container.querySelector('svg') + if (!svg) throw new Error('Expected the example chart') + expect( + container.querySelector('.sc-chart-tooltip:not([hidden])'), + ).toBeNull() + vi.spyOn(svg, 'getBoundingClientRect').mockReturnValue({ + x: 0, + y: 0, + top: 0, + left: 0, + right: width, + bottom: height, + width, + height, + toJSON: () => ({}), + }) + const point = createChartScene(definition, { width, height }).points[0] + if (!point) throw new Error('Expected an activity bar') + const move = async (y: number) => { + await act(async () => { + svg.dispatchEvent( + new MouseEvent('pointermove', { + bubbles: true, + clientX: point.x, + clientY: y, + }), + ) + }) + } + await move(80) + await vi.waitFor(() => + expect( + container.querySelector('.sc-chart-tooltip:not([hidden])'), + ).not.toBeNull(), + ) + const tooltip = + container.querySelector('.sc-chart-tooltip') + if (!tooltip) throw new Error('Expected the active tooltip') + const firstTop = tooltip.style.top + await move(120) + await vi.waitFor(() => expect(tooltip.style.top).not.toBe(firstTop)) + await act(async () => + svg.parentElement?.dispatchEvent(new MouseEvent('mouseleave')), + ) + await vi.waitFor(() => expect(tooltip.hidden).toBe(true)) + } finally { + await act(async () => root.unmount()) + container.remove() + } + }, + ) +})