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.

3.3 KB
Raw Blame
import { Dialog } from '@base-ui/react/dialog'
import { Suspense } from 'react'

import { IconX } from '@posthog/icons'

import { cn } from 'lib/utils/css-classes'
import { lazyWithRetry } from 'lib/utils/retryImport'

import { WrappingLoadingSkeleton } from '../WrappingLoadingSkeleton/WrappingLoadingSkeleton'

const ButtonPrimitive = lazyWithRetry(() =>
    import('../Button/ButtonPrimitives').then((m) => ({ default: m.ButtonPrimitive }))
)

function DialogPrimitive({
    children,
    open,
    onOpenChange,
    className,
    disablePointerDismissal = false,
    initialFocus,
}: {
    children: React.ReactNode
    open: boolean
    onOpenChange: (open: boolean, eventDetails?: Dialog.Root.ChangeEventDetails) => void
    className?: string
    disablePointerDismissal?: boolean
    initialFocus?: React.ComponentProps<typeof Dialog.Popup>['initialFocus']
}): JSX.Element {
    return (
        <Dialog.Root
            open={open}
            onOpenChange={(open, event) => onOpenChange(open, event)}
            disablePointerDismissal={disablePointerDismissal}
        >
            {open && (
                <Dialog.Portal>
                    <Dialog.Backdrop className="fixed inset-0 min-h-dvh min-w-dvw bg-black opacity-20 transition-all duration-150 data-[ending-style]:opacity-0 data-[starting-style]:opacity-0 dark:opacity-70 z-[var(--z-modal)]" />
                    <Dialog.Popup
                        initialFocus={initialFocus}
                        // dvh override is support-gated so browsers without dvh keep the vh cap.
                        className={cn(
                            '@container fixed top-4 left-1/2 w-[400px] max-w-[calc(100vw-3rem)] max-h-[60vh] supports-[max-height:1dvh]:max-h-[60dvh] -translate-x-1/2 rounded-lg bg-surface-secondary shadow-xl border border-primary transition-all duration-150 data-[ending-style]:scale-95 data-[ending-style]:opacity-0 data-[starting-style]:scale-95 data-[starting-style]:opacity-0 flex flex-col overflow-hidden z-[var(--z-force-modal-above-popovers)]',
                            className
                        )}
                    >
                        {children}
                    </Dialog.Popup>
                </Dialog.Portal>
            )}
        </Dialog.Root>
    )
}

function DialogPrimitiveTitle({
    children,
    className = 'sr-only',
}: {
    children: React.ReactNode
    className?: string
}): JSX.Element {
    return <Dialog.Title className={className}>{children}</Dialog.Title>
}

function DialogClose({ className = '' }: { className?: string }): JSX.Element {
    return (
        <Suspense
            fallback={
                <WrappingLoadingSkeleton className={cn('size-[30px]', className)}>
                    <IconX />
                </WrappingLoadingSkeleton>
            }
        >
            <Dialog.Close
                className={className}
                render={
                    <ButtonPrimitive iconOnly>
                        <IconX />
                    </ButtonPrimitive>
                }
            />
        </Suspense>
    )
}

function DialogTrigger({ children, ...rest }: React.ComponentProps<typeof Dialog.Trigger>): JSX.Element {
    return <Dialog.Trigger {...rest}>{children}</Dialog.Trigger>
}

export { DialogPrimitive, DialogPrimitiveTitle, DialogClose, DialogTrigger }