: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(insights): restore new-insight keyboard shortcut by forwarding LemonMenu ref (#62316)
posthog[bot]60234f25466e
diff --git a/frontend/src/lib/lemon-ui/LemonMenu/LemonMenu.test.tsx b/frontend/src/lib/lemon-ui/LemonMenu/LemonMenu.test.tsxnew file mode 100644index 000000000000..b18eea461902--- /dev/null+++ b/frontend/src/lib/lemon-ui/LemonMenu/LemonMenu.test.tsx@@ -0,0 +1,57 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import userEvent from '@testing-library/user-event'+import { useRef } from 'react'++import { LemonButton } from '../LemonButton'+import { LemonMenu } from './LemonMenu'++describe('LemonMenu', () => {+ // jest.setupAfterEnv does not enable RTL auto-cleanup; unmount between tests so `screen` stays isolated.+ afterEach(() => {+ cleanup()+ })++ it('forwards its ref to the trigger DOM node', () => {+ let resolvedNode: HTMLElement | null = null++ render(+ <LemonMenu+ items={[{ label: 'First', to: '/first' }]}+ ref={(node) => {+ resolvedNode = node+ }}+ >+ <LemonButton>Open</LemonButton>+ </LemonMenu>+ )++ // The ref must land on the real trigger button, not on the (DOM-less) LemonMenu component.+ expect(resolvedNode).toBeInstanceOf(HTMLButtonElement)+ expect(resolvedNode).toBe(screen.getByRole('button', { name: 'Open' }))+ })++ it('clicking the ref-driven trigger opens the menu', async () => {+ function Wrapper(): JSX.Element {+ const ref = useRef<HTMLElement>(null)+ return (+ <>+ <button onClick={() => ref.current?.click()}>Trigger via ref</button>+ <LemonMenu items={[{ label: 'First', to: '/first' }]} ref={ref}>+ <LemonButton>Open</LemonButton>+ </LemonMenu>+ </>+ )+ }++ render(<Wrapper />)++ expect(screen.queryByText('First')).not.toBeInTheDocument()++ // Triggering a click through the forwarded ref (as AppShortcut does) must open the menu.+ await userEvent.click(screen.getByRole('button', { name: 'Trigger via ref' }))++ expect(screen.getByText('First')).toBeInTheDocument()+ })+})diff --git a/frontend/src/lib/lemon-ui/LemonMenu/LemonMenu.tsx b/frontend/src/lib/lemon-ui/LemonMenu/LemonMenu.tsxindex 9965072dd3c5..c153de66a87d 100644--- a/frontend/src/lib/lemon-ui/LemonMenu/LemonMenu.tsx+++ b/frontend/src/lib/lemon-ui/LemonMenu/LemonMenu.tsx@@ -1,4 +1,5 @@-import React, { FunctionComponent, ReactNode, useCallback, useMemo } from 'react'+import { useMergeRefs } from '@floating-ui/react'+import React, { FunctionComponent, ReactNode, cloneElement, useCallback, useMemo } from 'react'import { KeyboardShortcut, KeyboardShortcutProps } from '~/layout/navigation-3000/components/KeyboardShortcut'@@ -112,14 +113,18 @@ export interface LemonMenuPropsfocusBasedKeyboardNavigation?: boolean}-export function LemonMenu({- items,- activeItemIndex,- tooltipPlacement,- onVisibilityChange,- focusBasedKeyboardNavigation = true,- ...dropdownProps-}: LemonMenuProps): JSX.Element {+export const LemonMenu = React.forwardRef<HTMLElement, LemonMenuProps>(function LemonMenu(+ {+ items,+ activeItemIndex,+ tooltipPlacement,+ onVisibilityChange,+ focusBasedKeyboardNavigation = true,+ children,+ ...dropdownProps+ },+ ref+): JSX.Element {const { referenceRef, itemsRef } = useKeyboardNavigation<HTMLElement, HTMLButtonElement>(items.flatMap((item) => (item && isLemonMenuSection(item) ? item.items : item)).length,activeItemIndex,@@ -139,6 +144,11 @@ export function LemonMenu({[onVisibilityChange, activeItemIndex])+ // LemonMenu renders no DOM itself — only the trigger child does. Forward an externally-provided+ // ref (e.g. from AppShortcut) onto that child so it lands on a real DOM node, otherwise consumers+ // attaching a ref to LemonMenu silently get nothing.+ const triggerRef = useMergeRefs([ref, (children as { ref?: React.Ref<HTMLElement> }).ref])+return (<LemonDropdownoverlay={@@ -153,9 +163,12 @@ export function LemonMenu({referenceRef={referenceRef}onVisibilityChange={_onVisibilityChange}{...dropdownProps}- />+ >+ {cloneElement(children, { ref: triggerRef })}+ </LemonDropdown>)-}+})+LemonMenu.displayName = 'LemonMenu'export interface LemonMenuOverlayProps {items: LemonMenuItems