import { Machine, Service } from '@zag-js/core'; import { DismissableElementHandlers, PersistentElementOptions } from '@zag-js/dismissable'; import { RequiredBy, DirectionProperty, CommonProperties, MaybeElement, PropTypes } from '@zag-js/types'; interface OpenChangeDetails { open: boolean; } interface TriggerValueChangeDetails { /** * The value of the trigger that activated the dialog */ value: string | null; /** * The trigger element */ triggerElement: HTMLElement | null; } interface ElementIds { trigger?: (string | ((value?: string) => string)) | undefined; positioner?: string | undefined; backdrop?: string | undefined; content?: string | undefined; closeTrigger?: string | undefined; title?: string | undefined; description?: string | undefined; } interface DialogProps extends DirectionProperty, CommonProperties, DismissableElementHandlers, PersistentElementOptions { /** * The ids of the elements in the dialog. Useful for composition. */ ids?: ElementIds | undefined; /** * Whether to trap focus inside the dialog when it's opened * @default true */ trapFocus?: boolean | undefined; /** * Whether to prevent scrolling behind the dialog when it's opened * @default true */ preventScroll?: boolean | undefined; /** * Whether to prevent pointer interaction outside the element and hide all content below it * @default true */ modal?: boolean | undefined; /** * Element to receive focus when the dialog is opened */ initialFocusEl?: (() => MaybeElement) | undefined; /** * Element to receive focus when the dialog is closed */ finalFocusEl?: (() => MaybeElement) | undefined; /** * Whether to restore focus to the element that had focus before the dialog was opened */ restoreFocus?: boolean | undefined; /** * Whether to close the dialog when the outside is clicked * @default true */ closeOnInteractOutside?: boolean | undefined; /** * Whether to close the dialog when the escape key is pressed * @default true */ closeOnEscape?: boolean | undefined; /** * Human readable label for the dialog, in event the dialog title is not rendered */ "aria-label"?: string | undefined; /** * The dialog's role * @default "dialog" */ role?: "dialog" | "alertdialog" | undefined; /** * The controlled open state of the dialog */ open?: boolean | undefined; /** * The initial open state of the dialog when rendered. * Use when you don't need to control the open state of the dialog. * @default false */ defaultOpen?: boolean | undefined; /** * Function to call when the dialog's open state changes */ onOpenChange?: ((details: OpenChangeDetails) => void) | undefined; /** * The controlled active trigger value */ triggerValue?: string | null | undefined; /** * The initial active trigger value when rendered. * Use when you don't need to control the active trigger value. */ defaultTriggerValue?: string | null | undefined; /** * Function to call when the active trigger changes */ onTriggerValueChange?: ((details: TriggerValueChangeDetails) => void) | undefined; } type PropsWithDefault = "closeOnInteractOutside" | "closeOnEscape" | "role" | "modal" | "trapFocus" | "restoreFocus" | "preventScroll" | "initialFocusEl"; interface DialogSchema { props: RequiredBy; state: "open" | "closed"; context: { rendered: { title: boolean; description: boolean; }; triggerValue: string | null; }; guard: "isOpenControlled"; effect: "trackDismissableElement" | "preventScroll" | "trapFocus" | "hideContentBelow"; action: "checkRenderedElements" | "setInitialFocus" | "invokeOnClose" | "invokeOnOpen" | "toggleVisibility" | "setTriggerValue"; event: { type: "CONTROLLED.OPEN" | "CONTROLLED.CLOSE" | "OPEN" | "CLOSE" | "TOGGLE" | "TRIGGER_VALUE.SET"; value?: string | null | undefined; }; } type DialogService = Service; type DialogMachine = Machine; interface TriggerProps { /** * The value that identifies this specific trigger */ value?: string; } interface DialogApi { /** * Whether the dialog is open */ open: boolean; /** * Function to open or close the dialog */ setOpen: (open: boolean) => void; /** * The active trigger value */ triggerValue: string | null; /** * Function to set the active trigger value */ setTriggerValue: (value: string | null) => void; getTriggerProps: (props?: TriggerProps) => T["button"]; getBackdropProps: () => T["element"]; getPositionerProps: () => T["element"]; getContentProps: () => T["element"]; getTitleProps: () => T["element"]; getDescriptionProps: () => T["element"]; getCloseTriggerProps: () => T["button"]; } export type { DialogApi, DialogMachine, DialogProps, DialogSchema, DialogService, ElementIds, OpenChangeDetails, TriggerProps, TriggerValueChangeDetails };