Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(quill-charts): reveal pie tooltip on first tap and skip overlay clicks (#125293)

Sam Penningtone05719434a3f
diff --git a/packages/quill/packages/charts/src/charts/PieChart/PieChart.test.tsx b/packages/quill/packages/charts/src/charts/PieChart/PieChart.test.tsx
index 16fca09eb97b..a2131aa28d24 100644
--- a/packages/quill/packages/charts/src/charts/PieChart/PieChart.test.tsx
+++ b/packages/quill/packages/charts/src/charts/PieChart/PieChart.test.tsx
@@ -167,6 +167,46 @@ describe('PieChart', () => {
fireEvent.click(chart.element)
expect(onSliceClick).not.toHaveBeenCalled()
})
+
+ it('shows the tooltip on the first tap and fires onSliceClick on a second tap', async () => {
+ const onSliceClick = jest.fn<void, [RadialSlicePayload]>()
+ // An excluded series makes slice indexes differ from series indexes.
+ const series: Series[] = [
+ { key: 'x', label: 'Safari', data: [10], visibility: { excluded: true } },
+ ...SERIES,
+ ]
+ const { chart } = renderChart(<PieChart series={series} theme={THEME} onSliceClick={onSliceClick} />)
+ const layout = layoutFor(series)
+ // jsdom has no PointerEvent, so the pointer type rides on a MouseEvent React reads it from.
+ const tap = (at: { clientX: number; clientY: number }): void => {
+ const down = Object.assign(new MouseEvent('pointerdown', { bubbles: true }), { pointerType: 'touch' })
+ fireEvent(chart.element, down)
+ fireEvent.click(chart.element, at)
+ }
+
+ tap(pointInSlice(layout, 0))
+ await waitFor(() => expect(getChartTooltip()?.textContent).toContain('Chrome'))
+ expect(onSliceClick).not.toHaveBeenCalled()
+
+ tap(pointInSlice(layout, 0))
+ expect(onSliceClick).toHaveBeenCalledWith(expect.objectContaining({ sliceIndex: 0 }))
+ })
+
+ it('ignores hover and clicks from an interactive overlay child', () => {
+ const onSliceClick = jest.fn()
+ const { chart, getByRole } = renderChart(
+ <PieChart series={SERIES} theme={THEME} onSliceClick={onSliceClick}>
+ <button data-hog-charts-interactive-overlay="">overlay</button>
+ </PieChart>
+ )
+ const at = pointInSlice(layoutFor(SERIES), 0)
+ fireEvent.mouseMove(chart.element, at)
+ fireEvent.mouseMove(getByRole('button'), at)
+ fireEvent.click(getByRole('button'), at)
+
+ expect(onSliceClick).not.toHaveBeenCalled()
+ expect(getChartTooltip()).toBeNull()
+ })
})
describe('interactive legend', () => {
diff --git a/packages/quill/packages/charts/src/core/hooks/useRadialInteraction.ts b/packages/quill/packages/charts/src/core/hooks/useRadialInteraction.ts
index 16176122217e..0cb64276f32a 100644
--- a/packages/quill/packages/charts/src/core/hooks/useRadialInteraction.ts
+++ b/packages/quill/packages/charts/src/core/hooks/useRadialInteraction.ts
@@ -1,9 +1,11 @@
import React, { useCallback, useMemo } from 'react'
+import { originatesInInteractiveOverlay } from '../dom-events'
import { sliceAt } from '../radial-layout'
import type { PieLayout, PieSlice } from '../radial-layout'
import type { ResolvedSeries, TooltipContext } from '../types'
import { useLatest } from './useLatest'
+import { useTapTracking } from './useTapTracking'
import { useTooltipLifecycle } from './useTooltipLifecycle'
export interface RadialSlicePayload<Meta = unknown> {
@@ -31,7 +33,8 @@ interface UseRadialInteractionResult<Meta> {
handlers: {
onMouseMove: (e: React.MouseEvent<HTMLDivElement>) => void
onMouseLeave: () => void
- onClick: () => void
+ onClick: (e: React.MouseEvent<HTMLDivElement>) => void
+ onPointerDown: (e: React.PointerEvent<HTMLDivElement>) => void
}
}
@@ -96,6 +99,18 @@ export function useRadialInteraction<Meta = unknown>({
const hoverIndexRef = useLatest(hoverIndex)
+ const showSlice = useCallback(
+ (idx: number, cursor: { x: number; y: number }) => {
+ setHover(idx, cursor)
+ const lo = layoutRef.current
+ if (showTooltip && lo) {
+ const canvasBounds = canvasRef.current?.getBoundingClientRect() ?? new DOMRect()
+ setTooltipCtx(buildPieTooltipCtx(lo.slices[idx], lo, cursor, canvasBounds))
+ }
+ },
+ [layoutRef, showTooltip, setHover, setTooltipCtx, canvasRef]
+ )
+
const onMouseMove = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
if (isPinned) {
@@ -105,22 +120,20 @@ export function useRadialInteraction<Meta = unknown>({
if (!lo || lo.slices.length === 0) {
return
}
+ if (originatesInInteractiveOverlay(e)) {
+ clearTooltip()
+ return
+ }
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect()
- const mouseX = e.clientX - rect.left
- const mouseY = e.clientY - rect.top
- const cursor = { x: mouseX, y: mouseY }
+ const cursor = { x: e.clientX - rect.left, y: e.clientY - rect.top }
const idx = sliceAt(lo, cursor, { outerSlack: hitOuterSlack })
if (idx < 0) {
clearTooltip()
return
}
- setHover(idx, cursor)
- if (showTooltip) {
- const canvasBounds = canvasRef.current?.getBoundingClientRect() ?? new DOMRect()
- setTooltipCtx(buildPieTooltipCtx(lo.slices[idx], lo, cursor, canvasBounds))
- }
+ showSlice(idx, cursor)
},
- [isPinned, layoutRef, hitOuterSlack, showTooltip, setHover, setTooltipCtx, clearTooltip, canvasRef]
+ [isPinned, layoutRef, hitOuterSlack, showSlice, clearTooltip]
)
const onMouseLeave = useCallback(() => {
@@ -130,28 +143,69 @@ export function useRadialInteraction<Meta = unknown>({
clearTooltip()
}, [isPinned, clearTooltip])
- const onClick = useCallback(() => {
- const idx = hoverIndexRef.current
- if (idx < 0 || !onSliceClick) {
- return
- }
- const lo = layoutRef.current
- if (!lo) {
- return
- }
- const slice = lo.slices[idx]
- if (!slice) {
- return
- }
- onSliceClick({
- sliceIndex: idx,
- series: slice.series,
- value: slice.value,
- fraction: slice.fraction,
- })
- }, [hoverIndexRef, layoutRef, onSliceClick])
+ // As on the cartesian charts, the first tap on a slice shows its tooltip and only a tap on
+ // the slice already showing one fires the click handler.
+ const { lastPointerTypeRef, tapDownTooltipIndexRef, onPointerDown } = useTapTracking(tooltipCtx)
+
+ const onClick = useCallback(
+ (e: React.MouseEvent<HTMLDivElement>) => {
+ const isTouch = lastPointerTypeRef.current === 'touch'
+ // A click or tap meant for an overlay control must not act on the slice behind it.
+ if (originatesInInteractiveOverlay(e)) {
+ if (isTouch) {
+ clearTooltip()
+ }
+ return
+ }
+ const lo = layoutRef.current
+ if (!lo) {
+ return
+ }
+ let idx = hoverIndexRef.current
+ if (isTouch) {
+ const rect = e.currentTarget.getBoundingClientRect()
+ const cursor = { x: e.clientX - rect.left, y: e.clientY - rect.top }
+ idx = sliceAt(lo, cursor, { outerSlack: hitOuterSlack })
+ if (idx < 0) {
+ clearTooltip()
+ return
+ }
+ // The tooltip's `dataIndex` is the slice's series index, not its slice index.
+ if (lo.slices[idx].seriesIndex !== tapDownTooltipIndexRef.current) {
+ showSlice(idx, cursor)
+ if (showTooltip) {
+ return
+ }
+ }
+ }
+ const slice = lo.slices[idx]
+ if (!slice || !onSliceClick) {
+ return
+ }
+ onSliceClick({
+ sliceIndex: idx,
+ series: slice.series,
+ value: slice.value,
+ fraction: slice.fraction,
+ })
+ },
+ [
+ lastPointerTypeRef,
+ tapDownTooltipIndexRef,
+ layoutRef,
+ hoverIndexRef,
+ hitOuterSlack,
+ showSlice,
+ showTooltip,
+ clearTooltip,
+ onSliceClick,
+ ]
+ )
- const handlers = useMemo(() => ({ onMouseMove, onMouseLeave, onClick }), [onMouseMove, onMouseLeave, onClick])
+ const handlers = useMemo(
+ () => ({ onMouseMove, onMouseLeave, onClick, onPointerDown }),
+ [onMouseMove, onMouseLeave, onClick, onPointerDown]
+ )
return { hoverIndex, hoverPosition, tooltipCtx, handlers }
}