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(paths): stop the path hover from rebuilding the chart (#99861)

willwearingef8f2eee4f69
diff --git a/products/product_analytics/frontend/insights/paths/PathNodeCard.tsx b/products/product_analytics/frontend/insights/paths/PathNodeCard.tsx
index ddc3483bf6e2..d0ac79e97599 100644
--- a/products/product_analytics/frontend/insights/paths/PathNodeCard.tsx
+++ b/products/product_analytics/frontend/insights/paths/PathNodeCard.tsx
@@ -1,5 +1,5 @@
import { useActions, useValues } from 'kea'
-import { CSSProperties, useState } from 'react'
+import { CSSProperties } from 'react'
import { Popover } from 'lib/lemon-ui/Popover'
@@ -16,8 +16,9 @@ export type PathNodeCardProps = {
insightProps: InsightLogicProps
node: PathNodeData
canvasHeight: number
- onMouseEnter?: () => void
- onMouseLeave?: () => void
+ popoverVisible: boolean
+ onMouseEnter: () => void
+ onMouseLeave: () => void
}
function getCardStyle(
@@ -43,10 +44,10 @@ export function PathNodeCard({
insightProps,
node,
canvasHeight,
+ popoverVisible,
onMouseEnter,
onMouseLeave,
}: PathNodeCardProps): JSX.Element | null {
- const [popoverVisible, setPopoverVisible] = useState(false)
const { pathsFilter: _pathsFilter, funnelPathsFilter: _funnelPathsFilter } = useValues(pathsDataLogic(insightProps))
const { updateInsightFilter, openPersonsModal, viewPathToFunnel } = useActions(pathsDataLogic(insightProps))
@@ -67,16 +68,6 @@ export function PathNodeCard({
node.targetLinks.length
: null
- const handleMouseEnter = (): void => {
- setPopoverVisible(true)
- onMouseEnter?.()
- }
-
- const handleMouseLeave = (): void => {
- setPopoverVisible(false)
- onMouseLeave?.()
- }
-
return (
<Popover
visible={popoverVisible}
@@ -95,7 +86,11 @@ export function PathNodeCard({
placement="bottom"
padded={false}
matchWidth
- onMouseLeaveInside={handleMouseLeave}
+ // The overlay is portaled, so it is not a DOM child of the card: moving the pointer
+ // onto it fires the card's mouseleave. Without the enter handler below, the popover
+ // closes the moment the pointer touches it, and then opens again on the card.
+ onMouseEnterInside={onMouseEnter}
+ onMouseLeaveInside={onMouseLeave}
>
<div
className={`PathNodeCard absolute rounded bg-surface-primary p-1${node.active ? ' PathNodeCard--active' : ''}`}
@@ -107,8 +102,8 @@ export function PathNodeCard({
canvasHeight
)}
data-attr="path-node-card-button"
- onMouseEnter={handleMouseEnter}
- onMouseLeave={handleMouseLeave}
+ onMouseEnter={onMouseEnter}
+ onMouseLeave={onMouseLeave}
>
<PathNodeCardButton
name={node.name}
diff --git a/products/product_analytics/frontend/insights/paths/Paths.tsx b/products/product_analytics/frontend/insights/paths/Paths.tsx
index 360aa02ee66f..46b977d96761 100644
--- a/products/product_analytics/frontend/insights/paths/Paths.tsx
+++ b/products/product_analytics/frontend/insights/paths/Paths.tsx
@@ -50,20 +50,25 @@ export function Paths(): JSX.Element {
const { loadData } = useActions(insightDataLogic(insightProps))
const interactionLogic = pathsInteractionLogic(insightProps)
- const { resolvedNodeCards, activeIndices } = useValues(interactionLogic)
- const { setNodes, hoverNode, hoverLink, clearHover, requestClearHover, setCardHovered } =
+ const { resolvedNodeCards, activeIndices, cardPopoverIndex } = useValues(interactionLogic)
+ const { setNodes, hoverNode, hoverLink, clearHover, requestClearHover, hoverCard, unhoverCard } =
useActions(interactionLogic)
- // eslint-disable-next-line react-hooks/exhaustive-deps -- kea action creators are stable references
+ // The handlers must keep their identity for the lifetime of the component. They are a
+ // dependency of the render effect below, so a new object there tears the SVG out from under
+ // the pointer and clears the hover that had just been set. Anything that changes is read
+ // when the handler runs, not when it is built.
+ const canvasHeightRef = useRef(canvasHeight)
+ canvasHeightRef.current = canvasHeight
const hoverHandlers = useMemo<PathsHoverHandlers>(
() => ({
- onNodesReady: (nodes: PathNodeData[]) => setNodes(nodes, canvasHeight),
+ onNodesReady: (nodes: PathNodeData[]) => setNodes(nodes, canvasHeightRef.current),
onNodeHover: hoverNode,
onLinkHover: hoverLink,
onHoverClear: requestClearHover,
isCardHovered: () => interactionLogic.values.cardHovered,
}),
- [canvasHeight, interactionLogic.values.cardHovered, setNodes, hoverNode, hoverLink, requestClearHover]
+ [interactionLogic, setNodes, hoverNode, hoverLink, requestClearHover]
)
useLayoutEffect(() => {
@@ -118,20 +123,6 @@ export function Paths(): JSX.Element {
clearHover,
])
- const handleCardMouseEnter = (node: PathNodeData): void => {
- setCardHovered(true)
- hoverNode(node.index)
- }
-
- const handleCardMouseLeave = (): void => {
- // Setting cardHovered=false immediately re-enables SVG handlers. If the mouse
- // lands on an SVG element within the 30ms debounce window, that handler fires
- // a new hoverNode/hoverLink which cancels the pending clearHover — so the
- // transition from card→SVG hover is seamless.
- setCardHovered(false)
- requestClearHover()
- }
-
if (insightDataError) {
return (
<InsightErrorState
@@ -169,8 +160,9 @@ export function Paths(): JSX.Element {
node={node}
insightProps={insightProps}
canvasHeight={canvasHeight}
- onMouseEnter={() => handleCardMouseEnter(node)}
- onMouseLeave={handleCardMouseLeave}
+ popoverVisible={cardPopoverIndex === node.index}
+ onMouseEnter={() => hoverCard(node.index)}
+ onMouseLeave={unhoverCard}
/>
))}
</div>
diff --git a/products/product_analytics/frontend/insights/paths/pathUtils.test.ts b/products/product_analytics/frontend/insights/paths/pathUtils.test.ts
index 4f326f9972ed..5e9e53035943 100644
--- a/products/product_analytics/frontend/insights/paths/pathUtils.test.ts
+++ b/products/product_analytics/frontend/insights/paths/pathUtils.test.ts
@@ -1,9 +1,12 @@
import { RGBColor } from 'd3'
+import { PATH_NODE_CARD_HEIGHT, PATH_NODE_CARD_OVERLAP_GAP } from './constants'
import {
+ MAXIMUM_CARD_NUDGE,
PathNodeData,
PathTargetLink,
activateNodes,
+ calculatePathNodeCardTop,
deactivateNodes,
getForwardConnectedIndices,
pageUrl,
@@ -447,38 +450,55 @@ describe('resolveCardOverlaps', () => {
const { nodes } = buildPathGraph(['/', '/about'])
// Two nodes in different layers — won't overlap each other
// Override to put them in the same layer with close y positions
- nodes[0] = { ...nodes[0], layer: 0, y0: 0, y1: 100, visible: true }
- nodes[1] = { ...nodes[1], layer: 0, y0: 5, y1: 105, visible: true }
+ nodes[0] = { ...nodes[0], layer: 0, y0: 0, y1: 100 }
+ nodes[1] = { ...nodes[1], layer: 0, y0: 5, y1: 105 }
- const result = resolveCardOverlaps(nodes, 720)
+ const tops = resolveCardOverlaps(nodes, 720)
- expect(result[0].resolvedTop).not.toBeUndefined()
- expect(result[1].resolvedTop).not.toBeUndefined()
- expect(result[1].resolvedTop!).toBeGreaterThan(result[0].resolvedTop!)
- // Gap should be at least CARD_HEIGHT + OVERLAP_GAP
- expect(result[1].resolvedTop! - result[0].resolvedTop!).toBeGreaterThanOrEqual(42) // 38 + 4
+ expect(tops.get(1)).not.toBeUndefined()
+ expect(tops.get(1)!).toBeGreaterThan(tops.get(0)!)
+ expect(tops.get(1)! - tops.get(0)!).toBeGreaterThanOrEqual(PATH_NODE_CARD_HEIGHT + PATH_NODE_CARD_OVERLAP_GAP)
})
it('does not adjust cards in different layers', () => {
const { nodes } = buildPathGraph(['/', '/about'])
- nodes[0] = { ...nodes[0], layer: 0, y0: 0, y1: 100, visible: true }
- nodes[1] = { ...nodes[1], layer: 1, y0: 5, y1: 105, visible: true }
+ nodes[0] = { ...nodes[0], layer: 0, y0: 0, y1: 100 }
+ nodes[1] = { ...nodes[1], layer: 1, y0: 5, y1: 105 }
- const result = resolveCardOverlaps(nodes, 720)
+ const tops = resolveCardOverlaps(nodes, 720)
// Each card gets its own layer group — no nudging needed
- expect(result[0].resolvedTop).not.toBeUndefined()
- expect(result[1].resolvedTop).not.toBeUndefined()
+ expect(tops.get(0)).toBe(calculatePathNodeCardTop(nodes[0], 720))
+ expect(tops.get(1)).toBe(calculatePathNodeCardTop(nodes[1], 720))
})
- it('leaves hidden nodes without resolvedTop', () => {
- const { nodes } = buildPathGraph(['/', '/about'])
- nodes[0] = { ...nodes[0], visible: true }
- nodes[1] = { ...nodes[1], visible: false }
+ it('uses the closest free gap for a hover-only card', () => {
+ const { nodes } = buildPathGraph(['/', '/about', '/pricing'])
+ nodes[0] = { ...nodes[0], layer: 0, y0: 80, y1: 95 }
+ nodes[1] = { ...nodes[1], layer: 0, y0: 100, y1: 200 }
+
+ const tops = resolveCardOverlaps(nodes, 720)
+
+ expect(tops.get(1)).toBe(calculatePathNodeCardTop(nodes[1], 720))
+ expect(tops.get(0)).toBe(
+ calculatePathNodeCardTop(nodes[1], 720) - PATH_NODE_CARD_HEIGHT - PATH_NODE_CARD_OVERLAP_GAP
+ )
+ })
+
+ it('keeps every card near its node when the layer has no room left', () => {
+ const nodeHeight = 22
+ const { nodes } = buildPathGraph(Array.from({ length: 24 }, (_, i) => `/page-${i}`))
+ // One layer packed with nodes too short to show a card, which is the shape of a path end
+ for (let i = 0; i < nodes.length; i++) {
+ const y0 = i * (nodeHeight + 8)
+ nodes[i] = { ...nodes[i], layer: 0, y0, y1: y0 + nodeHeight }
+ }
- const result = resolveCardOverlaps(nodes, 720)
+ const tops = resolveCardOverlaps(nodes, 720)
- expect(result[0].resolvedTop).not.toBeUndefined()
- expect(result[1].resolvedTop).toBeUndefined()
+ for (const node of nodes) {
+ const distanceFromNode = Math.abs(tops.get(node.index)! - calculatePathNodeCardTop(node, 720))
+ expect(distanceFromNode).toBeLessThanOrEqual(MAXIMUM_CARD_NUDGE)
+ }
})
})
diff --git a/products/product_analytics/frontend/insights/paths/pathUtils.ts b/products/product_analytics/frontend/insights/paths/pathUtils.ts
index 966aa07f28a9..45f58648df4b 100644
--- a/products/product_analytics/frontend/insights/paths/pathUtils.ts
+++ b/products/product_analytics/frontend/insights/paths/pathUtils.ts
@@ -64,33 +64,59 @@ export function getForwardConnectedIndices(startNode: PathNodeData): {
return { nodeIndices, linkIndices }
}
+/** A node tall enough to show its card at all times, with no hover. */
+export const isCardAlwaysVisible = (node: PathNodeData): boolean => node.y1 - node.y0 > HIDE_PATH_CARD_HEIGHT
+
export const activateNodes = (nodes: PathNodeData[], activeIndices: Set<number>): PathNodeData[] =>
nodes.map((node) => ({
...node,
- visible: activeIndices.has(node.index) || node.y1 - node.y0 > HIDE_PATH_CARD_HEIGHT,
+ visible: activeIndices.has(node.index) || isCardAlwaysVisible(node),
active: activeIndices.has(node.index),
}))
export const deactivateNodes = (nodes: PathNodeData[]): PathNodeData[] =>
nodes.map((node) => ({
...node,
- visible: node.y1 - node.y0 > HIDE_PATH_CARD_HEIGHT,
+ visible: isCardAlwaysVisible(node),
active: false,
}))
-export function resolveCardOverlaps(nodes: PathNodeData[], canvasHeight: number): PathNodeData[] {
- const visibleNodes = nodes.filter((n) => n.visible)
- if (visibleNodes.length === 0) {
- return nodes
- }
+/**
+ * How far a card may move from its node to reach a free gap. A layer holds about 17 cards at the
+ * minimum spacing, so a dense layer of short nodes runs out of gaps. Searching the whole canvas
+ * then lands a card hundreds of pixels from the node it describes, which reads as belonging to a
+ * different node. Past this distance the card keeps its own position instead.
+ */
+export const MAXIMUM_CARD_NUDGE = 2 * (PATH_NODE_CARD_HEIGHT + PATH_NODE_CARD_OVERLAP_GAP)
- const topByIndex = new Map<number, number>()
- for (const node of visibleNodes) {
- topByIndex.set(node.index, calculatePathNodeCardTop(node, canvasHeight))
- }
+const findClosestAvailableCardTop = (
+ naturalTop: number,
+ occupiedTops: number[],
+ canvasHeight: number
+): { top: number; foundGap: boolean } => {
+ const minimumDistance = PATH_NODE_CARD_HEIGHT + PATH_NODE_CARD_OVERLAP_GAP
+ const maximumTop = Math.max(0, canvasHeight - PATH_NODE_CARD_HEIGHT)
+ const clamp = (top: number): number => Math.min(Math.max(top, 0), maximumTop)
+ const candidateTops = [
+ clamp(naturalTop),
+ ...occupiedTops.flatMap((occupiedTop) => [occupiedTop - minimumDistance, occupiedTop + minimumDistance]),
+ ].filter((top) => top >= 0 && top <= maximumTop && Math.abs(top - naturalTop) <= MAXIMUM_CARD_NUDGE)
+
+ const freeTop = candidateTops
+ .filter((top) => occupiedTops.every((occupiedTop) => Math.abs(top - occupiedTop) >= minimumDistance))
+ .sort((a, b) => Math.abs(a - naturalTop) - Math.abs(b - naturalTop) || a - b)[0]
+
+ return freeTop === undefined ? { top: clamp(naturalTop), foundGap: false } : { top: freeTop, foundGap: true }
+}
+/**
+ * Card positions per node index. Positions use every node, so hover changes do not move cards.
+ */
+export function resolveCardOverlaps(nodes: PathNodeData[], canvasHeight: number): Map<number, number> {
const byLayer = new Map<number, PathNodeData[]>()
- for (const node of visibleNodes) {
+ const topByIndex = new Map<number, number>()
+ for (const node of nodes) {
+ topByIndex.set(node.index, calculatePathNodeCardTop(node, canvasHeight))
const group = byLayer.get(node.layer) ?? []
group.push(node)
byLayer.set(node.layer, group)
@@ -99,18 +125,36 @@ export function resolveCardOverlaps(nodes: PathNodeData[], canvasHeight: number)
const resolvedTops = new Map<number, number>()
for (const group of byLayer.values()) {
group.sort((a, b) => topByIndex.get(a.index)! - topByIndex.get(b.index)!)
+ const alwaysVisibleNodes = group.filter(isCardAlwaysVisible)
+ const occupiedTops: number[] = []
let prevBottom = -Infinity
- for (const node of group) {
+ for (const node of alwaysVisibleNodes) {
const naturalTop = topByIndex.get(node.index)!
const resolvedTop = Math.max(naturalTop, prevBottom + PATH_NODE_CARD_OVERLAP_GAP)
resolvedTops.set(node.index, resolvedTop)
+ occupiedTops.push(resolvedTop)
prevBottom = resolvedTop + PATH_NODE_CARD_HEIGHT
}
+
+ occupiedTops.sort((a, b) => a - b)
+ for (const node of group.filter((node) => !isCardAlwaysVisible(node))) {
+ const { top, foundGap } = findClosestAvailableCardTop(
+ topByIndex.get(node.index)!,
+ occupiedTops,
+ canvasHeight
+ )
+ resolvedTops.set(node.index, top)
+ // A card that found no gap near its node keeps the position it wants and accepts the
+ // overlap. It must not claim that position, because the cards after it would then be
+ // pushed further from their own nodes to avoid a card that is already overlapping.
+ if (foundGap) {
+ occupiedTops.push(top)
+ occupiedTops.sort((a, b) => a - b)
+ }
+ }
}
- return nodes.map((node) =>
- resolvedTops.has(node.index) ? { ...node, resolvedTop: resolvedTops.get(node.index) } : node
- )
+ return resolvedTops
}
export function roundedRect(
diff --git a/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.test.ts b/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.test.ts
index 7f27ca080f0f..d84ed11954f2 100644
--- a/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.test.ts
+++ b/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.test.ts
@@ -1,5 +1,7 @@
import { expectLogic } from 'kea-test-utils'
+import { delay } from 'lib/utils/async'
+
import { initKeaTests } from '~/test/init'
import { InsightLogicProps } from '~/types'
@@ -150,19 +152,47 @@ describe('pathsInteractionLogic', () => {
})
})
- describe('cardHovered', () => {
- it('tracks card hover state independently', () => {
- logic.actions.setCardHovered(true)
+ describe('card hover', () => {
+ it('highlights the hovered node and opens only that card popover', () => {
+ logic.actions.setNodes(buildTestNodes(), 720)
+ logic.actions.hoverCard(1)
+
expect(logic.values.cardHovered).toBe(true)
+ expect(logic.values.hoverTarget).toEqual({ type: 'node', nodeIndex: 1 })
+ expect(logic.values.cardPopoverIndex).toBe(1)
+ })
- logic.actions.setCardHovered(false)
- expect(logic.values.cardHovered).toBe(false)
+ it('keeps the popover open when the pointer crosses onto it', async () => {
+ logic.actions.setNodes(buildTestNodes(), 720)
+ logic.actions.hoverCard(1)
+ // The popover is portaled, so entering it leaves the card first
+ logic.actions.unhoverCard()
+ logic.actions.hoverCard(1)
+
+ await delay(60)
+
+ expect(logic.values.cardPopoverIndex).toBe(1)
+ expect(logic.values.hoverTarget).toEqual({ type: 'node', nodeIndex: 1 })
})
- it('is reset by clearHover', () => {
- logic.actions.setCardHovered(true)
- logic.actions.clearHover()
+ it('closes the popover once the pointer has left for good', async () => {
+ logic.actions.setNodes(buildTestNodes(), 720)
+ logic.actions.hoverCard(1)
+ logic.actions.unhoverCard()
+
+ // The SVG handlers are live again right away, so a hover that lands there is not lost
expect(logic.values.cardHovered).toBe(false)
+
+ await expectLogic(logic).toDispatchActions(['clearHover'])
+ expect(logic.values.cardPopoverIndex).toBeNull()
+ })
+
+ it('closes the popover when the hover moves to the SVG', () => {
+ logic.actions.setNodes(buildTestNodes(), 720)
+ logic.actions.hoverCard(1)
+ logic.actions.hoverLink(0, 1, 0)
+
+ expect(logic.values.cardPopoverIndex).toBeNull()
})
})
@@ -185,6 +215,26 @@ describe('pathsInteractionLogic', () => {
expect(afterCards.find((n) => n.index === 3)?.active).toBe(true)
})
+ it('keeps card positions fixed when hover reveals a card in the same layer', () => {
+ const nodes = buildTestNodes()
+ // Nodes 2 and 3 share a layer. Node 3 is too short to show its card until it is
+ // hovered, and its card would sit above the card of node 2.
+ nodes[2] = { ...nodes[2], y0: 10, y1: 70 }
+ nodes[3] = { ...nodes[3], y0: 0, y1: 15 }
+ logic.actions.setNodes(nodes, 720)
+
+ const topOfCard2 = (): number | undefined =>
+ logic.values.resolvedNodeCards.find((n) => n.index === 2)?.resolvedTop
+ const before = topOfCard2()
+ expect(before).toBe(40) // its own natural top, with no room made for node 3
+
+ // Hovering node 1 reveals node 3, which is in its forward chain
+ logic.actions.hoverNode(1)
+
+ expect(logic.values.resolvedNodeCards.find((n) => n.index === 3)?.visible).toBe(true)
+ expect(topOfCard2()).toBe(before)
+ })
+
it('deactivates all nodes when hover is cleared', () => {
logic.actions.setNodes(buildTestNodes(), 720)
logic.actions.hoverNode(0)
diff --git a/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.ts b/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.ts
index a35d64588f19..6959aa336f3d 100644
--- a/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.ts
+++ b/products/product_analytics/frontend/insights/paths/pathsInteractionLogic.ts
@@ -18,6 +18,7 @@ export type HoverTarget =
const DEFAULT_KEY = 'default_paths_interaction_key'
const CLEAR_HOVER_DEBOUNCE_MS = 30
+const CLEAR_HOVER_TIMER = 'clearHoverTimer'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pathsInteractionLogicValues {
@@ -27,6 +28,8 @@ export interface pathsInteractionLogicValues {
}
canvasHeight: number
cardHovered: boolean
+ cardPopoverIndex: number | null
+ cardTops: Map<number, number>
hoverTarget: HoverTarget | null
nodes: PathNodeData[]
resolvedNodeCards: PathNodeData[]
@@ -37,6 +40,9 @@ export interface pathsInteractionLogicActions {
clearHover: () => {
value: true
}
+ hoverCard: (nodeIndex: number) => {
+ nodeIndex: number
+ }
hoverLink: (
sourceIndex: number,
targetIndex: number,
@@ -52,9 +58,6 @@ export interface pathsInteractionLogicActions {
requestClearHover: () => {
value: true
}
- setCardHovered: (hovered: boolean) => {
- hovered: boolean
- }
setNodes: (
nodes: PathNodeData[],
canvasHeight: number
@@ -62,6 +65,9 @@ export interface pathsInteractionLogicActions {
canvasHeight: number
nodes: PathNodeData[]
}
+ unhoverCard: () => {
+ value: true
+ }
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -75,13 +81,14 @@ export interface pathsInteractionLogicMeta {
linkIndices: Set<number>
nodeIndices: Set<number>
}
+ cardTops: (nodes: PathNodeData[], canvasHeight: number) => Map<number, number>
resolvedNodeCards: (
nodes: PathNodeData[],
activeIndices: {
linkIndices: Set<number>
nodeIndices: Set<number>
},
- canvasHeight: number
+ cardTops: Map<number, number>
) => PathNodeData[]
}
}
@@ -108,7 +115,8 @@ export const pathsInteractionLogic = kea<pathsInteractionLogicType>([
}),
requestClearHover: true,
clearHover: true,
- setCardHovered: (hovered: boolean) => ({ hovered }),
+ hoverCard: (nodeIndex: number) => ({ nodeIndex }),
+ unhoverCard: true,
}),
reducers({
@@ -128,6 +136,7 @@ export const pathsInteractionLogic = kea<pathsInteractionLogicType>([
null as HoverTarget | null,
{
hoverNode: (_, { nodeIndex }) => ({ type: 'node', nodeIndex }),
+ hoverCard: (_, { nodeIndex }) => ({ type: 'node', nodeIndex }),
hoverLink: (_, { sourceIndex, targetIndex, linkIndex }) => ({
type: 'link',
sourceIndex,
@@ -141,29 +150,60 @@ export const pathsInteractionLogic = kea<pathsInteractionLogicType>([
cardHovered: [
false,
{
- setCardHovered: (_, { hovered }) => hovered,
+ hoverCard: () => true,
+ // Leaving a card re-enables the SVG handlers right away. If the pointer lands on
+ // an SVG element within the debounce window, its handler starts a new hover and
+ // cancels the pending clear, so the card to SVG transition stays seamless.
+ unhoverCard: () => false,
clearHover: () => false,
+ setNodes: () => false,
+ },
+ ],
+ // The card whose popover is open. Debounced through clearHover, so that crossing the
+ // seam between a card and its own popover does not close the popover.
+ cardPopoverIndex: [
+ null as number | null,
+ {
+ hoverCard: (_, { nodeIndex }) => nodeIndex,
+ hoverNode: () => null,
+ hoverLink: () => null,
+ clearHover: () => null,
+ setNodes: () => null,
},
],
}),
listeners(({ actions, cache }) => ({
requestClearHover: () => {
- clearTimeout(cache.clearHoverTimeout)
- cache.clearHoverTimeout = setTimeout(() => {
- actions.clearHover()
- }, CLEAR_HOVER_DEBOUNCE_MS)
+ // Same key, so a new request replaces the pending one. This is a one-shot, so it
+ // deregisters itself once it fires: the plugin re-runs the setup of every entry that
+ // is still registered when the tab becomes visible, which would otherwise re-arm this
+ // timer on every return to the tab for the rest of the logic's life.
+ // `pauseOnPageHidden` is off because a 30 ms pointer debounce is not background work,
+ // and pausing it holds a pending clear until the tab is visible again.
+ cache.disposables.add(
+ () => {
+ const timer = setTimeout(() => {
+ cache.disposables.dispose(CLEAR_HOVER_TIMER)
+ actions.clearHover()
+ }, CLEAR_HOVER_DEBOUNCE_MS)
+ return () => clearTimeout(timer)
+ },
+ CLEAR_HOVER_TIMER,
+ { pauseOnPageHidden: false }
+ )
},
hoverNode: () => {
- clearTimeout(cache.clearHoverTimeout)
+ cache.disposables.dispose(CLEAR_HOVER_TIMER)
},
hoverLink: () => {
- clearTimeout(cache.clearHoverTimeout)
+ cache.disposables.dispose(CLEAR_HOVER_TIMER)
+ },
+ hoverCard: () => {
+ cache.disposables.dispose(CLEAR_HOVER_TIMER)
},
- setCardHovered: ({ hovered }) => {
- if (hovered) {
- clearTimeout(cache.clearHoverTimeout)
- }
+ unhoverCard: () => {
+ actions.requestClearHover()
},
})),
@@ -203,18 +243,25 @@ export const pathsInteractionLogic = kea<pathsInteractionLogicType>([
return forward
},
],
+ cardTops: [
+ (s) => [s.nodes, s.canvasHeight],
+ (nodes: PathNodeData[], canvasHeight: number): Map<number, number> =>
+ resolveCardOverlaps(nodes, canvasHeight),
+ ],
resolvedNodeCards: [
- (s) => [s.nodes, s.activeIndices, s.canvasHeight],
+ (s) => [s.nodes, s.activeIndices, s.cardTops],
(
nodes: PathNodeData[],
activeIndices: { nodeIndices: Set<number>; linkIndices: Set<number> },
- canvasHeight: number
+ cardTops: Map<number, number>
): PathNodeData[] => {
const withState =
activeIndices.nodeIndices.size > 0
? activateNodes(nodes, activeIndices.nodeIndices)
: deactivateNodes(nodes)
- return resolveCardOverlaps(withState, canvasHeight)
+ return withState.map((node) =>
+ cardTops.has(node.index) ? { ...node, resolvedTop: cardTops.get(node.index) } : node
+ )
},
],
}),