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.

1.5 KB
Raw Blame
import { useContext, useLayoutEffect, useRef } from 'react'
import { useDisplayRegions } from '@/ui/displayRegionContext'

import { overlayPane } from '@/ui/displayRegions'
import { OverlayFrameContext } from '@/ui/overlayFrame'
import { useSceneDimensions } from '@/ui/useSceneDimensions'
import { useScreenInsets } from '@/ui/useScreenInsets'

import { placeAnchoredMenu, type RowAnchor } from './contextMenuPlacement'

export function useContextMenuLayout(
    anchor: RowAnchor,
    visible: boolean,
    onClose: () => void,
    width: number,
    height: number
): ReturnType<typeof placeAnchoredMenu> {
    const scene = useSceneDimensions()
    const host = useContext(OverlayFrameContext)
    const frame = host ?? { x: 0, y: 0, width: scene.width, height: scene.height }
    const regions = useDisplayRegions()
    const insets = useScreenInsets()
    const close = useRef(onClose)
    close.current = onClose
    const layoutKey = JSON.stringify([frame.width, frame.height, regions, insets])
    const previous = useRef(layoutKey)
    useLayoutEffect(() => {
        if (previous.current !== layoutKey && visible) close.current()
        previous.current = layoutKey
    }, [layoutKey, visible])
    const bounds = {
        x: 0,
        y: insets.top,
        width: frame.width,
        height: Math.max(0, frame.height - insets.top - insets.bottom),
    }
    const localAnchor = { ...anchor, x: anchor.x - frame.x, y: anchor.y - frame.y }
    const pane = overlayPane(bounds, regions, localAnchor)
    return placeAnchoredMenu({ anchor, frame, pane, width, height })
}