// // Copyright 2026 DXOS.org // // @import-as-namespace import { Dialog as DialogPrimitive, useDialogContext } from '@ark-ui/react/dialog'; import { Portal } from '@ark-ui/react/portal'; import React, { type ComponentPropsWithoutRef, type ReactNode, type RefObject, createContext, forwardRef, useContext, useEffect, useState, } from 'react'; import { mx } from '@dxos/ui-theme'; import { type ThemedClassName } from '@dxos/ui-types'; import { recipes } from '../../recipes.ts'; import { type Size } from '../../sizes.ts'; import { Button } from '../Button/Button.tsx'; import { Container, type ContainerProps, DefaultGutterProvider } from '../Container/Container.tsx'; import { Group } from '../Group/Group.tsx'; import { usePopupSize } from '../ScrollArea/PopupScroll.tsx'; import * as ScrollArea from '../ScrollArea/ScrollArea.tsx'; /** * Marks the control a dialog focuses when it opens (the current constant's role); zag's own initial-focus lookup reads * it, ahead of the first tabbable control. */ export const DIALOG_AUTOFOCUS_ATTRIBUTE = 'data-autofocus'; // // Root // type DialogPlacement = 'start' | 'center' | 'end'; type DialogRootProps = DialogPrimitive.RootProps & { /** * Where the Content sits unless it says otherwise: a host that opens dialogs it does not render (a layout showing * a surface's Content) places them here. */ placement?: DialogPlacement; }; /** Lets the Content keep the dialog open on an outside click, since a surface often renders the Content alone. */ const OutsideDismissContext = createContext<((dismissable: boolean) => void) | undefined>(undefined); const PlacementContext = createContext(undefined); /** Ark dialog; content mounts on first open and unmounts on close unless the caller opts out. */ const DialogRoot = ({ lazyMount = true, unmountOnExit = true, closeOnInteractOutside = true, placement, ...props }: DialogRootProps) => { const [contentDismissable, setContentDismissable] = useState(true); return ( ); }; DialogRoot.displayName = 'Dialog.Root'; // // Trigger // type DialogTriggerProps = DialogPrimitive.TriggerProps; /** Use `asChild` to open the dialog from a `Button`. */ const DialogTrigger = forwardRef((props, forwardedRef) => ( )); DialogTrigger.displayName = 'Dialog.Trigger'; // // Content // type DialogContentProps = ThemedClassName & { /** Overrides the size inherited from the trigger's nearest sized ancestor (Phase 4 decision 2); `md` without one. */ size?: Size; /** Portals into this element instead of the body (e.g. a sized scope, AUDIT 2.2). */ container?: RefObject; /** * `end` docks the dialog at the viewport's block end (e.g. a chat panel) and `start` hangs it from the top (e.g. a * picker whose list grows downward) instead of centring it; the Root's `placement` otherwise. */ placement?: DialogPlacement; /** * `false` drops the scrim and lets pointer events through around the dialog, for a non-modal (`modal={false}`) * dialog that leaves the page usable. */ scrim?: boolean; /** `false` keeps the dialog open on a click outside, e.g. while it holds unsaved input; Escape still closes it. */ closeOnInteractOutside?: boolean; }; /** Portalled surface at `level='raised'` over a scrim, centred in the viewport: a column of Header, Body and Footer. */ const DialogContent = forwardRef( ( { classNames, size, container, placement: placementProp, scrim = true, closeOnInteractOutside, children, ...props }, forwardedRef, ) => { const dialog = useDialogContext(); const rootPlacement = useContext(PlacementContext); const placement = placementProp ?? rootPlacement ?? 'center'; const setDismissable = useContext(OutsideDismissContext); useEffect(() => { if (closeOnInteractOutside !== false || !setDismissable) { return; } setDismissable(false); return () => setDismissable(true); }, [closeOnInteractOutside, setDismissable]); const dialogSize = usePopupSize(size, dialog.open, [dialog.getTriggerProps().id], 'md'); return ( {scrim && } {children} ); }, ); DialogContent.displayName = 'Dialog.Content'; // // Header // type DialogHeaderProps = ThemedClassName>; /** A block row holding the Title and an optional trailing CloseTrigger. */ const DialogHeader = forwardRef(({ classNames, ...props }, forwardedRef) => (
)); DialogHeader.displayName = 'Dialog.Header'; // // Title // type DialogTitleProps = ThemedClassName & { /** Names the dialog for assistive tech without showing a heading. */ srOnly?: boolean; }; const DialogTitle = forwardRef( ({ classNames, srOnly, ...props }, forwardedRef) => ( ), ); DialogTitle.displayName = 'Dialog.Title'; // // Description // type DialogDescriptionProps = ThemedClassName & { /** Describes the dialog for assistive tech without showing the text. */ srOnly?: boolean; }; const DialogDescription = forwardRef( ({ classNames, srOnly, ...props }, forwardedRef) => ( ), ); DialogDescription.displayName = 'Dialog.Description'; // // CloseTrigger // type DialogCloseTriggerProps = Omit & { /** With `asChild`, the child (e.g. a Cancel `Button`) closes the dialog instead of the default icon button. */ children?: ReactNode; icon?: string; label?: string; }; const DialogCloseTrigger = forwardRef( ({ asChild, children, icon = 'ph--x--regular', label = 'Close', ...props }, forwardedRef) => asChild ? ( {children} ) : (