: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
fix(annotations): keep chart popovers open and scrollable (#107026)
posthog[bot]9c70eb24f397
diff --git a/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.scss b/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.scssindex 288f4a2a2c8b..4017f3cf43d4 100644--- a/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.scss+++ b/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.scss@@ -50,10 +50,13 @@opacity 100ms ease,transform 100ms ease;+ // Shrink to the popover height, so that only .LemonModal__content scrolls.LemonModal {+ flex: 1 1 auto;min-width: var(--annotations-popover-width);max-width: var(--annotations-popover-width);- min-height: 100%;+ min-height: 0;+ max-height: 100%;margin: 0;border: none;box-shadow: none;@@ -61,6 +64,13 @@}.AnnotationCard {+ // TextContent scrolls by default. In the popover, only .LemonModal__content scrolls.+ .RichMarkdownEditor,+ .LemonMarkdown {+ overflow: visible;+ overflow-wrap: anywhere;+ }+.profile-package {vertical-align: bottom;}diff --git a/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.tsx b/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.tsxindex 5f5d05e2c163..960ac32811b8 100644--- a/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.tsx+++ b/frontend/src/lib/components/AnnotationsOverlay/AnnotationsOverlay.tsx@@ -25,6 +25,7 @@ import { AnnotationModal } from 'products/annotations/frontend/components/Annotaimport { annotationModalLogic, annotationScopeToName } from 'products/annotations/frontend/logics/annotationModalLogic'import { AnnotationsOverlayLogicProps, annotationsOverlayLogic } from './annotationsOverlayLogic'+import { useAnnotationPopoverHover } from './useAnnotationPopoverHover'import { AnnotationsChartGeometry, useAnnotationsPositioning } from './useAnnotationsPositioning'const MIN_BADGE_SPACING_PX = 24@@ -116,8 +117,9 @@ export const AnnotationsOverlay = React.memo(function AnnotationsOverlay({}const logic = annotationsOverlayLogic(annotationsOverlayLogicProps)const { activeDate, tickDates, annotationBadgeDataIndices, groupedAnnotations, readOnly } = useValues(logic)- const { closePopover } = useActions(logic)+ const { closePopover, deactivateDate } = useActions(logic)const { closeModal } = useActions(annotationModalLogic)+ const hover = useAnnotationPopoverHover(deactivateDate)useEffect(() => {return () => {@@ -210,6 +212,8 @@ export const AnnotationsOverlay = React.memo(function AnnotationsOverlay({widthPx={tickIntervalPx}annotations={EMPTY_ANNOTATIONS}badgeRefs={badgeRefs}+ onMouseEnter={hover.cancel}+ onMouseLeave={hover.start}/>)})}@@ -221,6 +225,8 @@ export const AnnotationsOverlay = React.memo(function AnnotationsOverlay({widthPx={MIN_BADGE_SPACING_PX}annotations={cluster.annotations}badgeRefs={badgeRefs}+ onMouseEnter={hover.cancel}+ onMouseLeave={hover.start}/>))}{activeBadgeElement && (@@ -228,6 +234,7 @@ export const AnnotationsOverlay = React.memo(function AnnotationsOverlay({overlayRefs={[overlayRef, modalContentRef, modalOverlayRef]}badgeElement={activeBadgeElement}cluster={activeCluster}+ hover={hover}/>)}<AnnotationModal@@ -247,6 +254,8 @@ interface AnnotationsBadgeProps {widthPx: numberannotations: DatedAnnotationType[]badgeRefs: React.MutableRefObject<Map<string, HTMLButtonElement>>+ onMouseEnter: () => void+ onMouseLeave: (point: { x: number; y: number }) => void}interface AnnotationsBadgeCSSProperties extends React.CSSProperties {@@ -261,9 +270,11 @@ const AnnotationsBadge = React.memo(function AnnotationsBadgeRaw({widthPx,annotations,badgeRefs,+ onMouseEnter,+ onMouseLeave,}: AnnotationsBadgeProps): JSX.Element {const { isDateLocked, activeDate, isPopoverShown } = useValues(annotationsOverlayLogic)- const { activateDate, deactivateDate, lockDate, unlockDate } = useActions(annotationsOverlayLogic)+ const { activateDate, lockDate, unlockDate } = useActions(annotationsOverlayLogic)const [hovered, setHovered] = useState(false)const buttonRef = useRef<HTMLButtonElement>(null)@@ -304,14 +315,15 @@ const AnnotationsBadge = React.memo(function AnnotationsBadgeRaw({}onMouseEnter={() => {setHovered(true)+ onMouseEnter()if (!isDateLocked) {activateDate(date)}}}- onMouseLeave={() => {+ onMouseLeave={(event) => {setHovered(false)if (!isDateLocked) {- deactivateDate()+ onMouseLeave({ x: event.clientX, y: event.clientY })}}}onClick={!isDateLocked ? lockDate : active ? unlockDate : () => activateDate(date)}@@ -344,15 +356,21 @@ function AnnotationsPopover({overlayRefs,badgeElement,cluster,+ hover,}: {overlayRefs: React.MutableRefObject<HTMLDivElement | null>[]badgeElement: HTMLButtonElement | nullcluster: AnnotationBadgeCluster | undefined+ hover: ReturnType<typeof useAnnotationPopoverHover>}): JSX.Element {const { activeDate, groupingUnit, isDateLocked, insightId, isPopoverShown, annotationsOverlayProps, readOnly } =useValues(annotationsOverlayLogic)const { closePopover } = useActions(annotationsOverlayLogic)const { openModalToCreateAnnotation } = useActions(annotationModalLogic)+ const handleClose = (): void => {+ hover.cancel()+ closePopover()+ }const popoverAnnotations = cluster?.annotations ?? []@@ -388,10 +406,14 @@ function AnnotationsPopover({placement="top"fallbackPlacements={['top-end', 'top-start']}referenceElement={badgeElement}+ floatingRef={hover.popupRef}visible={isPopoverShown}- onClickOutside={closePopover}+ onClickOutside={handleClose}+ onMouseEnterInside={hover.cancel}+ onMouseLeaveInside={isDateLocked ? undefined : hover.leavePopup}showArrowpadded={false}+ overflowHiddenoverlay={<LemonModalinline@@ -414,11 +436,11 @@ function AnnotationsPopover({)}closable={isDateLocked}- onClose={closePopover}+ onClose={handleClose}width="var(--annotations-popover-width)">{popoverAnnotations.length > 0 ? (- <ul className="flex flex-col gap-2 w-full overflow-y-auto">+ <ul className="flex flex-col gap-2 w-full">{popoverAnnotations.map((annotation) => (<AnnotationCard key={annotation.id} annotation={annotation} />))}diff --git a/frontend/src/lib/components/AnnotationsOverlay/useAnnotationPopoverHover.test.ts b/frontend/src/lib/components/AnnotationsOverlay/useAnnotationPopoverHover.test.tsnew file mode 100644index 000000000000..f50f4120d42a--- /dev/null+++ b/frontend/src/lib/components/AnnotationsOverlay/useAnnotationPopoverHover.test.ts@@ -0,0 +1,42 @@+import { act, fireEvent, renderHook } from '@testing-library/react'++import { useAnnotationPopoverHover } from './useAnnotationPopoverHover'++describe('useAnnotationPopoverHover', () => {+ beforeEach(() => jest.useFakeTimers())+ afterEach(() => jest.useRealTimers())++ it('keeps the popup open on the path from the badge and closes after leaving it', () => {+ const onLeave = jest.fn()+ const { result } = renderHook(() => useAnnotationPopoverHover(onLeave))+ const popup = document.createElement('div')+ jest.spyOn(popup, 'getBoundingClientRect').mockReturnValue({+ left: 100,+ right: 300,+ top: 100,+ bottom: 200,+ } as DOMRect)+ result.current.popupRef.current = popup++ act(() => result.current.start({ x: 150, y: 250 }))+ act(() => jest.advanceTimersByTime(300))+ fireEvent.mouseMove(document, { clientX: 160, clientY: 225 })+ act(() => jest.advanceTimersByTime(300))+ expect(onLeave).not.toHaveBeenCalled()++ act(() => result.current.cancel())+ act(() => jest.runOnlyPendingTimers())+ expect(onLeave).not.toHaveBeenCalled()++ act(() => result.current.leavePopup())+ expect(onLeave).toHaveBeenCalledTimes(1)++ act(() => result.current.start({ x: 150, y: 250 }))+ fireEvent.mouseMove(document, { clientX: 400, clientY: 225 })+ expect(onLeave).toHaveBeenCalledTimes(2)++ act(() => result.current.start({ x: 150, y: 250 }))+ act(() => jest.runOnlyPendingTimers())+ expect(onLeave).toHaveBeenCalledTimes(3)+ })+})diff --git a/frontend/src/lib/components/AnnotationsOverlay/useAnnotationPopoverHover.ts b/frontend/src/lib/components/AnnotationsOverlay/useAnnotationPopoverHover.tsnew file mode 100644index 000000000000..c1512b93f735--- /dev/null+++ b/frontend/src/lib/components/AnnotationsOverlay/useAnnotationPopoverHover.ts@@ -0,0 +1,79 @@+import { useCallback, useEffect, useRef } from 'react'+import type { MutableRefObject } from 'react'++import { isPointInSafeTriangle } from 'lib/ui/Menus/safeTriangle'+import type { Point } from 'lib/ui/Menus/safeTriangle'++const HOVER_STALL_MS = 400++export function useAnnotationPopoverHover(onLeave: () => void): {+ popupRef: MutableRefObject<HTMLDivElement | null>+ start: (point: Point) => void+ cancel: () => void+ leavePopup: () => void+} {+ const popupRef = useRef<HTMLDivElement | null>(null)+ const anchorRef = useRef<Point | null>(null)+ const timeoutRef = useRef<number | null>(null)+ const mouseMoveRef = useRef<((event: MouseEvent) => void) | null>(null)++ const cancel = useCallback((): void => {+ if (timeoutRef.current !== null) {+ window.clearTimeout(timeoutRef.current)+ timeoutRef.current = null+ }+ if (mouseMoveRef.current) {+ document.removeEventListener('mousemove', mouseMoveRef.current)+ mouseMoveRef.current = null+ }+ anchorRef.current = null+ }, [])++ const leavePopup = useCallback((): void => {+ cancel()+ onLeave()+ }, [cancel, onLeave])++ const start = useCallback(+ (point: Point): void => {+ cancel()+ anchorRef.current = point++ const scheduleClose = (): void => {+ if (timeoutRef.current !== null) {+ window.clearTimeout(timeoutRef.current)+ }+ timeoutRef.current = window.setTimeout(leavePopup, HOVER_STALL_MS)+ }++ const onMouseMove = (event: MouseEvent): void => {+ const popup = popupRef.current+ const anchor = anchorRef.current+ if (popup?.contains(event.target as Node)) {+ cancel()+ } else if (+ !popup ||+ (anchor &&+ isPointInSafeTriangle(+ { x: event.clientX, y: event.clientY },+ anchor,+ popup.getBoundingClientRect()+ ))+ ) {+ scheduleClose()+ } else {+ leavePopup()+ }+ }++ mouseMoveRef.current = onMouseMove+ document.addEventListener('mousemove', onMouseMove)+ scheduleClose()+ },+ [cancel, leavePopup]+ )++ useEffect(() => cancel, [cancel])++ return { popupRef, start, cancel, leavePopup }+}