: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(frontend): add delayed hover dropdown opening (#78799)
Hugues Pouillot844f96e6d3b0
diff --git a/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.test.tsx b/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.test.tsxindex b0d5783a62ee..b39cdc9ce43f 100644--- a/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.test.tsx+++ b/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.test.tsx@@ -1,6 +1,6 @@import '@testing-library/jest-dom'-import { cleanup, fireEvent, render, screen } from '@testing-library/react'+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'import { LemonDropdown } from './LemonDropdown'@@ -8,6 +8,32 @@ describe('LemonDropdown', () => {// jest.setupAfterEnv does not enable RTL auto-cleanup; unmount between tests so the portal stays isolated.afterEach(() => {cleanup()+ jest.useRealTimers()+ })++ it('delays opening a hover dropdown when configured', () => {+ jest.useFakeTimers()+ const onVisibilityChange = jest.fn()++ render(+ <LemonDropdown+ trigger="hover"+ hoverOpenDelayMs={500}+ onVisibilityChange={onVisibilityChange}+ overlay={<div>Menu</div>}+ >+ <button>Open</button>+ </LemonDropdown>+ )++ fireEvent.mouseEnter(screen.getByText('Open'))+ expect(onVisibilityChange).not.toHaveBeenCalled()++ act(() => jest.advanceTimersByTime(499))+ expect(onVisibilityChange).not.toHaveBeenCalled()++ act(() => jest.advanceTimersByTime(1))+ expect(onVisibilityChange).toHaveBeenCalledWith(true)})// `e.relatedTarget` on a mouseleave is null when the cursor leaves the document and can be adiff --git a/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.tsx b/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.tsxindex 7d98e36e4208..77707fe57d93 100644--- a/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.tsx+++ b/frontend/src/lib/lemon-ui/LemonDropdown/LemonDropdown.tsx@@ -1,4 +1,4 @@-import React, { MouseEventHandler, useContext, useRef, useState } from 'react'+import React, { MouseEventHandler, useContext, useEffect, useRef, useState } from 'react'import { Popover, PopoverOverlayContext, PopoverProps } from '../Popover'@@ -18,6 +18,7 @@ export interface LemonDropdownProps extends Omit<PopoverProps, 'children' | 'viscloseOnClickInside?: boolean/** @default 'click' */trigger?: 'click' | 'hover'+ hoverOpenDelayMs?: numberchildren: React.ReactElement<Record<string, any> & {onClick: MouseEventHandler@@ -38,6 +39,7 @@ export const LemonDropdown = React.forwardRef<HTMLDivElement, LemonDropdownPropsonMouseLeaveInside,closeOnClickInside = true,trigger = 'click',+ hoverOpenDelayMs = 0,children,startVisible,...popoverProps@@ -51,6 +53,23 @@ export const LemonDropdown = React.forwardRef<HTMLDivElement, LemonDropdownPropsconst floatingRef = useRef<HTMLDivElement>(null)const referenceRef = useRef<HTMLSpanElement>(null)+ const hoverOpenTimeoutRef = useRef<number | null>(null)++ const clearHoverOpenTimeout = (): void => {+ if (hoverOpenTimeoutRef.current !== null) {+ window.clearTimeout(hoverOpenTimeoutRef.current)+ hoverOpenTimeoutRef.current = null+ }+ }++ useEffect(+ () => () => {+ if (hoverOpenTimeoutRef.current !== null) {+ window.clearTimeout(hoverOpenTimeoutRef.current)+ }+ },+ []+ )const effectiveVisible = visible ?? localVisible@@ -94,6 +113,7 @@ export const LemonDropdown = React.forwardRef<HTMLDivElement, LemonDropdownProps>{React.cloneElement(children, {onClick: (e: React.MouseEvent): void => {+ clearHoverOpenTimeout()setVisible(!effectiveVisible)children.props.onClick?.(e)if (parentPopoverLevel > -1) {@@ -104,10 +124,19 @@ export const LemonDropdown = React.forwardRef<HTMLDivElement, LemonDropdownProps},onMouseEnter: (): void => {if (trigger === 'hover') {- setVisible(true)+ clearHoverOpenTimeout()+ if (hoverOpenDelayMs > 0) {+ hoverOpenTimeoutRef.current = window.setTimeout(() => {+ hoverOpenTimeoutRef.current = null+ setVisible(true)+ }, hoverOpenDelayMs)+ } else {+ setVisible(true)+ }}},onMouseLeave: (e: React.MouseEvent): void => {+ clearHoverOpenTimeout()const relatedTarget = e.relatedTargetif (trigger === 'hover' &&