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(dashboards): require drag distance before entering edit mode (#103017)

Thomas Obermüllerfecae2395c1d
diff --git a/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.test.tsx b/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.test.tsx
index 2419d96474df..e12e4a12ba75 100644
--- a/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.test.tsx
+++ b/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.test.tsx
@@ -86,7 +86,7 @@ describe('EditModeEdgeOverlay', () => {
it('renders eight edge and corner hit areas with data attr', () => {
const { getAllByTitle } = render(<EditModeEdgeOverlay onEnterEditMode={() => {}} />)
- const zones = getAllByTitle('Click to edit layout')
+ const zones = getAllByTitle('Drag to resize')
expect(zones).toHaveLength(8)
zones.forEach((zone) => {
expect(zone).toHaveAttribute('data-attr', 'dashboard-edit-mode-from-card-edge')
@@ -106,7 +106,7 @@ describe('EditModeEdgeOverlay', () => {
const onEnterEditMode = jest.fn()
const { getAllByTitle } = render(<EditModeEdgeOverlay onEnterEditMode={onEnterEditMode} />)
- const zones = getAllByTitle('Click to edit layout')
+ const zones = getAllByTitle('Drag to resize')
fireEvent.mouseDown(zones[zoneIndex])
expect(onEnterEditMode).toHaveBeenCalledTimes(1)
@@ -115,7 +115,7 @@ describe('EditModeEdgeOverlay', () => {
it('reveals all resize handles while any zone is hovered', () => {
const { container, getAllByTitle } = render(<EditModeEdgeOverlay onEnterEditMode={() => {}} />)
- const zones = getAllByTitle('Click to edit layout')
+ const zones = getAllByTitle('Drag to resize')
expect(container.querySelectorAll('.handle')).toHaveLength(0)
@@ -128,7 +128,7 @@ describe('EditModeEdgeOverlay', () => {
it('keeps handles shown while moving between overlapping zones', () => {
const { container, getAllByTitle } = render(<EditModeEdgeOverlay onEnterEditMode={() => {}} />)
- const zones = getAllByTitle('Click to edit layout')
+ const zones = getAllByTitle('Drag to resize')
// Enter a corner, then the adjacent edge, before leaving the corner — count must not hit zero.
fireEvent.mouseEnter(zones[0])
@@ -147,7 +147,7 @@ describe('EditModeEdgeOverlay', () => {
} {
const onEnterEditMode = jest.fn()
const { container, getAllByTitle } = render(<EditModeEdgeOverlay onEnterEditMode={onEnterEditMode} />)
- const zones = getAllByTitle('Click to edit layout')
+ const zones = getAllByTitle('Drag to resize')
const scrollable = fakeScrollableElement(SCROLLABLE_BASE)
container.appendChild(scrollable)
jest.spyOn(document, 'elementsFromPoint').mockImplementation((x: number, y: number) =>
diff --git a/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.tsx b/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.tsx
index 09bf4d85c3d0..7f33c250256a 100644
--- a/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.tsx
+++ b/frontend/src/lib/components/Cards/InsightCard/EditModeEdgeOverlay.tsx
@@ -303,7 +303,7 @@ export const EditModeEdgeOverlay: React.FC<EditModeEdgeOverlayProps> = ({ onEnte
if (container && pointIsOverScrollbarInside(container, event.clientX, event.clientY)) {
return
}
- // Treat any press (click or drag attempt) as intent to edit
+ // Claim the press so it does not start a text selection; pointer travel decides whether it becomes a resize.
event.preventDefault()
event.stopPropagation()
onEnterEditMode(event, edge)
@@ -327,7 +327,7 @@ export const EditModeEdgeOverlay: React.FC<EditModeEdgeOverlayProps> = ({ onEnte
}}
onMouseLeave={() => releaseHover()}
aria-hidden="true"
- title="Click to edit layout"
+ title="Drag to resize"
data-attr={EDGE_ZONE_DATA_ATTR}
// eslint-disable-next-line react/forbid-dom-props
style={{
diff --git a/frontend/src/lib/components/Cards/TextCard/TextCard.test.tsx b/frontend/src/lib/components/Cards/TextCard/TextCard.test.tsx
index 85a551fcaaa1..1a322952f1b5 100644
--- a/frontend/src/lib/components/Cards/TextCard/TextCard.test.tsx
+++ b/frontend/src/lib/components/Cards/TextCard/TextCard.test.tsx
@@ -48,7 +48,7 @@ describe('TextCard', () => {
)
// Wiring guard only: the exact zone count is pinned by EditModeEdgeOverlay's own test.
- const edges = getAllByTitle('Click to edit layout')
+ const edges = getAllByTitle('Drag to resize')
expect(edges.length).toBeGreaterThan(0)
fireEvent.mouseDown(edges[0])
diff --git a/frontend/src/scenes/dashboard/DashboardItems.tsx b/frontend/src/scenes/dashboard/DashboardItems.tsx
index ebb17932108c..35791a2a0edf 100644
--- a/frontend/src/scenes/dashboard/DashboardItems.tsx
+++ b/frontend/src/scenes/dashboard/DashboardItems.tsx
@@ -24,7 +24,11 @@ import {
getInsightQueryError,
isWidgetTileVisibleOnPlacement,
} from 'scenes/dashboard/dashboardUtils'
-import { continueDragGestureInEditMode, continueResizeGestureInEditMode } from 'scenes/dashboard/editLayoutGesture'
+import {
+ continueDragGestureInEditMode,
+ continueResizeGestureInEditMode,
+ whenPressBecomesDrag,
+} from 'scenes/dashboard/editLayoutGesture'
import { useDashboardLayoutInteraction } from 'scenes/dashboard/useDashboardLayoutInteraction'
import { useSurveyLinkedInsights } from 'scenes/surveys/hooks/useSurveyLinkedInsights'
import { getBestSurveyOpportunityFunnel } from 'scenes/surveys/utils/opportunityDetection'
@@ -287,9 +291,11 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP
() =>
canEnterEditModeFromEdge
? (e: React.MouseEvent<HTMLDivElement>, edge: EditModeEdge) => {
- setDashboardEditing({ filters: true, layout: true }, DashboardEventSource.CardEdgeHover)
- // continue the press into a live resize so the user doesn't have to release and grab again
- continueResizeGestureInEditMode(e, edge)
+ whenPressBecomesDrag(e, (moveEvent) => {
+ setDashboardEditing({ filters: true, layout: true }, DashboardEventSource.CardEdgeHover)
+ // continue the press into a live resize so the user doesn't have to release and grab again
+ continueResizeGestureInEditMode(e, edge, moveEvent)
+ })
}
: undefined,
[canEnterEditModeFromEdge, setDashboardEditing]
@@ -319,9 +325,11 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP
}
e.preventDefault()
e.stopPropagation()
- setDashboardEditing({ filters: true, layout: true }, DashboardEventSource.CardDragHandle)
- // continue the press into a live drag so the user doesn't have to release and grab again
- continueDragGestureInEditMode(e)
+ whenPressBecomesDrag(e, (moveEvent) => {
+ setDashboardEditing({ filters: true, layout: true }, DashboardEventSource.CardDragHandle)
+ // continue the press into a live drag so the user doesn't have to release and grab again
+ continueDragGestureInEditMode(e, moveEvent)
+ })
}
: undefined,
[canEnterEditModeFromEdge, setDashboardEditing]
diff --git a/frontend/src/scenes/dashboard/editLayoutGesture.test.ts b/frontend/src/scenes/dashboard/editLayoutGesture.test.ts
index b34ae5ce4263..d2ed580975ce 100644
--- a/frontend/src/scenes/dashboard/editLayoutGesture.test.ts
+++ b/frontend/src/scenes/dashboard/editLayoutGesture.test.ts
@@ -2,9 +2,11 @@ import type { MouseEvent as ReactMouseEvent } from 'react'
import { EditModeEdge } from 'lib/components/Cards/InsightCard/EditModeEdgeOverlay'
import {
+ EDIT_MODE_GESTURE_THRESHOLD_PX,
continueDragGestureInEditMode,
continueResizeGestureInEditMode,
resolveResizeHandleDirection,
+ whenPressBecomesDrag,
} from 'scenes/dashboard/editLayoutGesture'
describe('editLayoutGesture', () => {
@@ -34,6 +36,68 @@ describe('editLayoutGesture', () => {
})
})
+ describe('whenPressBecomesDrag', () => {
+ const press = { clientX: 100, clientY: 100 } as ReactMouseEvent
+
+ const drag = (toX: number, toY: number, buttons: number = 1): void => {
+ window.dispatchEvent(new MouseEvent('mousemove', { clientX: toX, clientY: toY, buttons }))
+ }
+
+ it.each([
+ ['released without moving', 100, 100],
+ ['a one pixel twitch', 101, 100],
+ ['just short of the threshold', 100 + EDIT_MODE_GESTURE_THRESHOLD_PX - 1, 100],
+ ['just short of the threshold on the diagonal', 105, 105],
+ ])('leaves %s as a click', (_case, toX, toY) => {
+ const onDragStarted = jest.fn()
+
+ whenPressBecomesDrag(press, onDragStarted)
+ drag(toX, toY)
+ window.dispatchEvent(new MouseEvent('mouseup'))
+
+ expect(onDragStarted).not.toHaveBeenCalled()
+ })
+
+ it.each([
+ ['horizontally', 100 + EDIT_MODE_GESTURE_THRESHOLD_PX, 100],
+ ['vertically', 100, 100 + EDIT_MODE_GESTURE_THRESHOLD_PX],
+ ['on the diagonal', 110, 110],
+ ])('starts the drag once the pointer travels %s', (_case, toX, toY) => {
+ const onDragStarted = jest.fn()
+
+ whenPressBecomesDrag(press, onDragStarted)
+ drag(toX, toY)
+
+ expect(onDragStarted).toHaveBeenCalledTimes(1)
+ const crossing = onDragStarted.mock.calls[0][0] as MouseEvent
+ expect(crossing.clientX).toBe(toX)
+ expect(crossing.clientY).toBe(toY)
+ })
+
+ it('starts the drag only once however far the pointer keeps moving', () => {
+ const onDragStarted = jest.fn()
+
+ whenPressBecomesDrag(press, onDragStarted)
+ drag(150, 150)
+ drag(200, 200)
+
+ expect(onDragStarted).toHaveBeenCalledTimes(1)
+ })
+
+ it.each([
+ ['the button is released', () => window.dispatchEvent(new MouseEvent('mouseup'))],
+ ['the release happened outside the window, so no mouseup arrived', () => drag(300, 300, 0)],
+ ])('stops watching the pointer once %s', (_case, endPress) => {
+ const onDragStarted = jest.fn()
+
+ whenPressBecomesDrag(press, onDragStarted)
+ endPress()
+ drag(300, 300)
+
+ expect(onDragStarted).not.toHaveBeenCalled()
+ })
+ })
+
describe('gesture continuation', () => {
let rafQueue: FrameRequestCallback[]
let grid: HTMLDivElement
@@ -50,6 +114,9 @@ describe('editLayoutGesture', () => {
const pressEvent = (target: Element, clientX: number = 150, clientY: number = 100): ReactMouseEvent =>
({ target, clientX, clientY, button: 0 }) as unknown as ReactMouseEvent
+ const moveEvent = (clientX: number, clientY: number): MouseEvent =>
+ new MouseEvent('mousemove', { clientX, clientY })
+
beforeEach(() => {
rafQueue = []
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback) => {
@@ -84,7 +151,7 @@ describe('editLayoutGesture', () => {
it('replays the mousedown on the matching resize handle once it renders', () => {
const onHandleMouseDown = jest.fn()
- continueResizeGestureInEditMode(pressEvent(gridItem, 150, 0), 'n')
+ continueResizeGestureInEditMode(pressEvent(gridItem, 150, 0), 'n', moveEvent(150, 12))
flushFrames() // handle not rendered yet — keeps waiting
const handle = mountResizeHandle('n')
@@ -101,7 +168,7 @@ describe('editLayoutGesture', () => {
it('does not replay if the mouse button is released before the handle renders', () => {
const onHandleMouseDown = jest.fn()
- continueResizeGestureInEditMode(pressEvent(gridItem, 150, 0), 'n')
+ continueResizeGestureInEditMode(pressEvent(gridItem, 150, 0), 'n', moveEvent(150, 12))
window.dispatchEvent(new MouseEvent('mouseup'))
const handle = mountResizeHandle('n')
@@ -114,7 +181,7 @@ describe('editLayoutGesture', () => {
it('gives up after the frame budget if edit mode never renders', () => {
const onHandleMouseDown = jest.fn()
- continueResizeGestureInEditMode(pressEvent(gridItem, 150, 0), 'n')
+ continueResizeGestureInEditMode(pressEvent(gridItem, 150, 0), 'n', moveEvent(150, 12))
flushFrames(15)
const handle = mountResizeHandle('n')
@@ -128,7 +195,7 @@ describe('editLayoutGesture', () => {
const onDragHandleMouseDown = jest.fn()
cardMeta.addEventListener('mousedown', onDragHandleMouseDown)
- continueDragGestureInEditMode(pressEvent(cardMeta, 50, 20))
+ continueDragGestureInEditMode(pressEvent(cardMeta, 50, 20), moveEvent(62, 20))
flushFrames() // grid still in view mode — keeps waiting
expect(onDragHandleMouseDown).not.toHaveBeenCalled()
@@ -140,5 +207,23 @@ describe('editLayoutGesture', () => {
expect(replayed.clientX).toBe(50)
expect(replayed.clientY).toBe(20)
})
+
+ it('replays the travel that already happened so the drag starts from the press origin', () => {
+ const onDragHandleMouseDown = jest.fn()
+ const onDocumentMouseMove = jest.fn()
+ cardMeta.addEventListener('mousedown', onDragHandleMouseDown)
+ document.addEventListener('mousemove', onDocumentMouseMove)
+
+ continueDragGestureInEditMode(pressEvent(cardMeta, 50, 20), moveEvent(80, 45))
+ grid.className = 'react-grid-layout dashboard-edit-mode'
+ flushFrames()
+ document.removeEventListener('mousemove', onDocumentMouseMove)
+
+ const replayedPress = onDragHandleMouseDown.mock.calls[0][0] as MouseEvent
+ expect([replayedPress.clientX, replayedPress.clientY]).toEqual([50, 20])
+ expect(onDocumentMouseMove).toHaveBeenCalledTimes(1)
+ const replayedMove = onDocumentMouseMove.mock.calls[0][0] as MouseEvent
+ expect([replayedMove.clientX, replayedMove.clientY]).toEqual([80, 45])
+ })
})
})
diff --git a/frontend/src/scenes/dashboard/editLayoutGesture.ts b/frontend/src/scenes/dashboard/editLayoutGesture.ts
index b77d56105629..58c0ba29b9d9 100644
--- a/frontend/src/scenes/dashboard/editLayoutGesture.ts
+++ b/frontend/src/scenes/dashboard/editLayoutGesture.ts
@@ -6,6 +6,36 @@ import { EditModeEdge } from 'lib/components/Cards/InsightCard/EditModeEdgeOverl
const CORNER_THRESHOLD_PX = 32
/** How many animation frames to wait for edit mode to render before giving up on continuing the gesture. */
const MAX_FRAMES_TO_WAIT = 10
+/** Above react-grid-layout's own 3px drag threshold, because entering edit mode costs more to undo than a stray drag. */
+export const EDIT_MODE_GESTURE_THRESHOLD_PX = 8
+
+/** `onDragStarted` receives the move that crossed the threshold, which react-grid-layout needs to resume the gesture. */
+export function whenPressBecomesDrag(event: ReactMouseEvent, onDragStarted: (moveEvent: MouseEvent) => void): void {
+ const { clientX, clientY } = event
+
+ function stopWatching(): void {
+ window.removeEventListener('mousemove', handleMove, { capture: true })
+ window.removeEventListener('mouseup', stopWatching, { capture: true })
+ }
+
+ function handleMove(moveEvent: MouseEvent): void {
+ // A release outside the browser window never delivers mouseup here, so trust what the move
+ // reports is still held rather than the release we may never see.
+ if (!(moveEvent.buttons & 1)) {
+ stopWatching()
+ return
+ }
+ const travelled = Math.hypot(moveEvent.clientX - clientX, moveEvent.clientY - clientY)
+ if (travelled < EDIT_MODE_GESTURE_THRESHOLD_PX) {
+ return
+ }
+ stopWatching()
+ onDragStarted(moveEvent)
+ }
+
+ window.addEventListener('mousemove', handleMove, { capture: true })
+ window.addEventListener('mouseup', stopWatching, { capture: true })
+}
/** Pick the resize handle direction for a press on a tile edge, upgrading presses near a corner to the corner handle. */
export function resolveResizeHandleDirection(
@@ -41,7 +71,7 @@ export function resolveResizeHandleDirection(
* before edit mode rendered continues without the user having to release and press again. react-grid-layout
* only listens for mousedown while drag/resize is enabled, so the original press never reaches it.
*/
-function replayMouseDown(event: ReactMouseEvent, findTarget: () => Element | null): void {
+function replayMouseDown(event: ReactMouseEvent, findTarget: () => Element | null, pointerNow: MouseEvent): void {
const { clientX, clientY, button } = event
let cancelled = false
const cancel = (): void => {
@@ -69,30 +99,49 @@ function replayMouseDown(event: ReactMouseEvent, findTarget: () => Element | nul
target.dispatchEvent(
new MouseEvent('mousedown', { bubbles: true, cancelable: true, view: window, clientX, clientY, button })
)
+ // Replaying the press at its origin keeps the tile from jumping, but leaves react-grid-layout
+ // believing the pointer never moved. This hands it the travel it missed.
+ target.ownerDocument.dispatchEvent(
+ new MouseEvent('mousemove', {
+ bubbles: true,
+ cancelable: true,
+ view: window,
+ clientX: pointerNow.clientX,
+ clientY: pointerNow.clientY,
+ button,
+ })
+ )
}
requestAnimationFrame(tryReplay)
}
/** Continue a press on a tile edge into a live resize once edit mode's resize handles have rendered. */
-export function continueResizeGestureInEditMode(event: ReactMouseEvent, edge: EditModeEdge): void {
+export function continueResizeGestureInEditMode(
+ event: ReactMouseEvent,
+ edge: EditModeEdge,
+ pointerNow: MouseEvent
+): void {
const gridItem = (event.target as Element | null)?.closest('.react-grid-item')
if (!gridItem) {
return
}
const rect = gridItem.getBoundingClientRect()
const direction = resolveResizeHandleDirection(edge, rect, event.clientX, event.clientY)
- replayMouseDown(event, () => gridItem.querySelector(`.react-resizable-handle-${direction}`))
+ replayMouseDown(event, () => gridItem.querySelector(`.react-resizable-handle-${direction}`), pointerNow)
}
/** Continue a press on a tile's drag handle into a live drag once edit mode has enabled dragging. */
-export function continueDragGestureInEditMode(event: ReactMouseEvent): void {
+export function continueDragGestureInEditMode(event: ReactMouseEvent, pointerNow: MouseEvent): void {
const target = event.target as Element | null
const gridItem = target?.closest('.react-grid-item')
if (!target || !gridItem) {
return
}
- replayMouseDown(event, () =>
- // the grid signals that dragging is live by swapping to the edit-mode class
- target.isConnected && gridItem.closest('.react-grid-layout.dashboard-edit-mode') ? target : null
+ replayMouseDown(
+ event,
+ () =>
+ // the grid signals that dragging is live by swapping to the edit-mode class
+ target.isConnected && gridItem.closest('.react-grid-layout.dashboard-edit-mode') ? target : null,
+ pointerNow
)
}