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.

2.0 KB
Raw Blame
import { CLICK_OUTSIDE_BLOCK_CLASS } from 'lib/hooks/useOutsideClickHandler'

// Shared monaco-editor portal div for popups (tooltips, suggestion list,
// etc). Monaco's global services (HoverService, ContextView) register
// DomListeners against whatever node we hand them as overflowWidgetsDomNode
// and never release those listeners across editor disposals because the
// services themselves are module-level singletons (lazy-init via
// GlobalIdleValue). If we create a fresh div per editor and remove it on
// unmount, the singletons retain the detached div forever.
//
// Solution: one shared, never-removed div for every editor instance. The
// DomListeners stay bound to a div that's always attached, so nothing
// leaks. The `data-attr="monaco-overflow-root"` lets memlab/playwright
// assert there is exactly one of these on body no matter how many editors
// mount or unmount.

let sharedMonacoOverflowRootEl: HTMLDivElement | null = null

export function sharedMonacoOverflowRoot(): HTMLDivElement | undefined {
    if (typeof document === 'undefined') {
        return undefined
    }
    if (sharedMonacoOverflowRootEl && document.body.contains(sharedMonacoOverflowRootEl)) {
        return sharedMonacoOverflowRootEl
    }
    sharedMonacoOverflowRootEl = document.createElement('div')
    // Block class: this div is at body level, so clicking a suggestion widget here would otherwise
    // dismiss the popover hosting the editor (e.g. a SQL expression inside the TaxonomicFilter).
    sharedMonacoOverflowRootEl.classList.add('monaco-editor', CLICK_OUTSIDE_BLOCK_CLASS)
    sharedMonacoOverflowRootEl.style.zIndex = 'var(--z-tooltip)'
    sharedMonacoOverflowRootEl.setAttribute('data-attr', 'monaco-overflow-root')
    document.body.appendChild(sharedMonacoOverflowRootEl)
    return sharedMonacoOverflowRootEl
}

export function _resetSharedMonacoOverflowRootForTests(): void {
    if (sharedMonacoOverflowRootEl && document.body.contains(sharedMonacoOverflowRootEl)) {
        sharedMonacoOverflowRootEl.remove()
    }
    sharedMonacoOverflowRootEl = null
}