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(replay): stop category dropdown collapsing the filter popover (#62954)

Xander7b66a9bb57a3
diff --git a/frontend/src/lib/lemon-ui/Popover/Popover.test.tsx b/frontend/src/lib/lemon-ui/Popover/Popover.test.tsx
new file mode 100644
index 000000000000..1582615b8f12
--- /dev/null
+++ b/frontend/src/lib/lemon-ui/Popover/Popover.test.tsx
@@ -0,0 +1,51 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, render, screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+
+import { CLICK_OUTSIDE_BLOCK_CLASS } from 'lib/hooks/useOutsideClickHandler'
+
+import { Popover } from './Popover'
+
+describe('Popover', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ function renderPopover(extra?: React.ReactNode): { onClickOutside: jest.Mock } {
+ const onClickOutside = jest.fn()
+ render(
+ <div>
+ <Popover visible overlay={<div>overlay content</div>} onClickOutside={onClickOutside}>
+ <button type="button">reference</button>
+ </Popover>
+ {extra}
+ </div>
+ )
+ return { onClickOutside }
+ }
+
+ it('dismisses when clicking a plain outside element', async () => {
+ const { onClickOutside } = renderPopover(<button type="button">outside</button>)
+
+ await userEvent.click(screen.getByText('outside'))
+
+ expect(onClickOutside).toHaveBeenCalled()
+ })
+
+ // Regression: a nested menu portaled out of a parent popover's *reference* subtree
+ // (e.g. the TaxonomicFilter category pill in the search input suffix) inherits the
+ // wrong overlay level, so the parent can't recognize it as nested. The element opts
+ // out via CLICK_OUTSIDE_BLOCK_CLASS — clicking it must not dismiss the parent.
+ it('does not dismiss when clicking an element marked with CLICK_OUTSIDE_BLOCK_CLASS', async () => {
+ const { onClickOutside } = renderPopover(
+ <button type="button" className={CLICK_OUTSIDE_BLOCK_CLASS}>
+ nested menu item
+ </button>
+ )
+
+ await userEvent.click(screen.getByText('nested menu item'))
+
+ expect(onClickOutside).not.toHaveBeenCalled()
+ })
+})
diff --git a/frontend/src/lib/lemon-ui/Popover/Popover.tsx b/frontend/src/lib/lemon-ui/Popover/Popover.tsx
index dc424d329b70..0c77b31210ee 100644
--- a/frontend/src/lib/lemon-ui/Popover/Popover.tsx
+++ b/frontend/src/lib/lemon-ui/Popover/Popover.tsx
@@ -91,6 +91,13 @@ export const PopoverReferenceContext = React.createContext<[boolean, Placement]
// Used so a parent popover doesn't treat a click on a deeper-nested popover as an outside click.
const openPopoverFloatings: Array<{ level: number; element: HTMLElement }> = []
+// Whether a click target opted out of outside-dismiss via CLICK_OUTSIDE_BLOCK_CLASS. Mirrors the
+// global exemption the legacy useOutsideClickHandler already applies, so both outside-click paths
+// agree. `Element` (not `HTMLElement`) so a click landing on an SVG icon inside an opted-out
+// element still matches.
+const optedOutOfOutsideDismiss = (target: EventTarget | Node | null): boolean =>
+ target instanceof Element && !!target.closest(`.${CLICK_OUTSIDE_BLOCK_CLASS}`)
+
/** This is a custom popover control that uses `floating-ui` to position DOM nodes.
*
* Often used with buttons for various menu. If this is your intention, use `LemonButtonWithDropdown`.
@@ -275,14 +282,22 @@ export const Popover = React.forwardRef<HTMLDivElement, PopoverProps>(function P
const dismiss = useDismiss(context, {
enabled: visible,
- // useDismiss only treats the floating + reference elements as "inside". Two things
- // need explicit exemption: additionalRefs (consumer-registered companion elements)
- // and deeper-nested popovers (portaled, so DOM-siblings rather than descendants).
+ // useDismiss only treats the floating + reference elements as "inside". Three things
+ // need explicit exemption: elements opting out via CLICK_OUTSIDE_BLOCK_CLASS,
+ // additionalRefs (consumer-registered companion elements), and deeper-nested popovers
+ // (portaled, so DOM-siblings rather than descendants).
outsidePress: (event) => {
const target = event.target as Node | null
if (!target) {
return true
}
+ // Honor the block class on the floating-ui dismiss path too, not just onClickInside —
+ // a nested menu in a parent popover's *reference* subtree (e.g. the TaxonomicFilter
+ // category pill in the search input's suffix) inherits the wrong overlay level, so the
+ // level check below can't recognize it as nested.
+ if (optedOutOfOutsideDismiss(target)) {
+ return false
+ }
if (additionalRefsRef.current.some((r) => r.current?.contains(target))) {
return false
}
@@ -325,7 +340,7 @@ export const Popover = React.forwardRef<HTMLDivElement, PopoverProps>(function P
}, []) // oxlint-disable-line react-hooks/exhaustive-deps
const _onClickInside: MouseEventHandler<HTMLDivElement> = (e): void => {
- if (e.target instanceof HTMLElement && e.target.closest(`.${CLICK_OUTSIDE_BLOCK_CLASS}`)) {
+ if (optedOutOfOutsideDismiss(e.target)) {
return
}
onClickInside?.(e)
diff --git a/frontend/src/scenes/session-recordings/filters/RecordingsUniversalFilterAddFilterPopover.test.tsx b/frontend/src/scenes/session-recordings/filters/RecordingsUniversalFilterAddFilterPopover.test.tsx
new file mode 100644
index 000000000000..38c67371a109
--- /dev/null
+++ b/frontend/src/scenes/session-recordings/filters/RecordingsUniversalFilterAddFilterPopover.test.tsx
@@ -0,0 +1,109 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, render, screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { Provider } from 'kea'
+
+import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
+import UniversalFilters from 'lib/components/UniversalFilters/UniversalFilters'
+
+import { useMocks } from '~/mocks/jest'
+import { actionsModel } from '~/models/actionsModel'
+import { groupsModel } from '~/models/groupsModel'
+import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
+import { initKeaTests } from '~/test/init'
+import { mockGetEventDefinitions, mockGetPropertyDefinitions } from '~/test/mocks'
+import { FilterLogicalOperator, UniversalFiltersGroup } from '~/types'
+
+import { RecordingsUniversalFilterAddFilterPopover } from './RecordingsUniversalFiltersEmbed'
+
+const DEFAULT_GROUP: UniversalFiltersGroup = {
+ type: FilterLogicalOperator.And,
+ values: [{ type: FilterLogicalOperator.And, values: [] }],
+}
+
+jest.mock('lib/components/AutoSizer', () => ({
+ AutoSizer: ({ renderProp }: { renderProp: (size: { height: number; width: number }) => React.ReactNode }) =>
+ renderProp({ height: 400, width: 400 }),
+}))
+
+describe('RecordingsUniversalFilterAddFilterPopover (pill category dropdown)', () => {
+ beforeEach(() => {
+ useMocks({
+ get: {
+ '/api/projects/:team/event_definitions': mockGetEventDefinitions,
+ '/api/projects/:team/property_definitions': mockGetPropertyDefinitions,
+ '/api/environments/:team/persons/properties': [],
+ '/api/environments/:team/events/values': [],
+ '/api/environments/:team/persons/values': [],
+ },
+ post: {
+ '/api/environments/:team/query': { results: [] },
+ },
+ })
+ initKeaTests()
+ actionsModel.mount()
+ groupsModel.mount()
+ propertyDefinitionsModel.mount()
+ })
+
+ afterEach(() => {
+ cleanup()
+ })
+
+ function renderPopover(): void {
+ const taxonomicGroupTypes = [
+ TaxonomicFilterGroupType.Events,
+ TaxonomicFilterGroupType.EventProperties,
+ TaxonomicFilterGroupType.PersonProperties,
+ ]
+ render(
+ <Provider>
+ <UniversalFilters
+ rootKey="replay-add-filter-pill-test"
+ group={DEFAULT_GROUP}
+ onChange={jest.fn()}
+ taxonomicGroupTypes={taxonomicGroupTypes}
+ >
+ <RecordingsUniversalFilterAddFilterPopover
+ categoryDropdownVariant="pill"
+ taxonomicGroupTypes={taxonomicGroupTypes}
+ />
+ </UniversalFilters>
+ </Provider>
+ )
+ }
+
+ it('applies the picked category from the pill menu while keeping the filter open', async () => {
+ renderPopover()
+
+ // Open the replay filter — focusing the search input reveals the category pill.
+ const input = screen.getByTestId('replay-filters-add-filter-input')
+ await userEvent.click(input)
+
+ const pillTrigger = await screen.findByTestId('taxonomic-category-dropdown-trigger-pill')
+ // Defaults to the first group.
+ expect(pillTrigger).toHaveTextContent('Events')
+
+ // Type a query so we can prove the surrounding popover is not dismissed by the pick:
+ // the bug routed the menu click through the parent popover's outside-press handler,
+ // which cleared the query and collapsed the filter.
+ await userEvent.type(input, 'email')
+ expect(input).toHaveValue('email')
+
+ // Open the pill menu and select a visible option. Re-query the trigger — typing
+ // re-renders the input suffix, detaching the node captured above.
+ await userEvent.click(screen.getByTestId('taxonomic-category-dropdown-trigger-pill'))
+ const personPropertiesItem = await screen.findByTestId('taxonomic-category-dropdown-item-person_properties')
+ await userEvent.click(personPropertiesItem)
+
+ // The picked category is applied (findByRole waits for the re-render)...
+ expect(
+ await screen.findByRole('button', { name: 'Current category: Person properties. Click to change.' })
+ ).toBeInTheDocument()
+
+ // ...and the main filter stays open with its search query intact.
+ expect(screen.getByTestId('replay-filters-add-filter-input')).toBeInTheDocument()
+ expect(screen.getByTestId('replay-filters-add-filter-input')).toHaveValue('email')
+ })
+})
diff --git a/frontend/src/scenes/session-recordings/filters/RecordingsUniversalFiltersEmbed.tsx b/frontend/src/scenes/session-recordings/filters/RecordingsUniversalFiltersEmbed.tsx
index 760d308b2ff9..37d9d0634ab6 100644
--- a/frontend/src/scenes/session-recordings/filters/RecordingsUniversalFiltersEmbed.tsx
+++ b/frontend/src/scenes/session-recordings/filters/RecordingsUniversalFiltersEmbed.tsx
@@ -493,7 +493,7 @@ function SavedFilterNameEditor({
)
}
-function RecordingsUniversalFilterAddFilterPopover({
+export function RecordingsUniversalFilterAddFilterPopover({
categoryDropdownVariant,
taxonomicGroupTypes,
}: {