Components

Dialog

A modal panel centered over a scrim

Import
import { Dialog } from "@nyte-ai/ui/dialog";

A cancellable modal that takes focus. For a required response, use Alert dialog.

Usage

  • Every dialog needs a Dialog.Title as its accessible name. To hide it, use srOnly from Focus.

Anatomy

<Dialog.Root>
  <Dialog.Trigger />
  <Dialog.Popup>
    <Dialog.Header>
      <Dialog.Title />
      <Dialog.Description />
    </Dialog.Header>
    …
    <Dialog.Footer>
      <Dialog.Close />
    </Dialog.Footer>
  </Dialog.Popup>
</Dialog.Root>

Examples

<Dialog.Root>
  <Dialog.Trigger render={<Button variant="outline">Rename Session</Button>} />
  <Dialog.Popup>
    <Dialog.Header>
      <Dialog.Title>Rename Session</Dialog.Title>
      <Dialog.Description>
        The name shows in the sidebar and in the window title.
      </Dialog.Description>
    </Dialog.Header>
    <Dialog.Footer>
      <Dialog.Close render={<Button variant="outline">Cancel</Button>} />
      <Dialog.Close
        render={
          <Button variant="solid" tone="primary">
            Save
          </Button>
        }
      />
    </Dialog.Footer>
  </Dialog.Popup>
</Dialog.Root>
const [open, setOpen] = useState(false);

<Dialog.Root open={open} onOpenChange={setOpen}>
  <Dialog.Popup>
    <Dialog.Title>Rename Session</Dialog.Title>
    <form
      onSubmit={async () => {
        await save();
        setOpen(false);
      }}
    >
      …
    </form>
  </Dialog.Popup>
</Dialog.Root>;
import { Dialog } from "@nyte-ai/ui/dialog";
import { srOnly } from "@nyte-ai/ui/styles/a11y.stylex";
import { create } from "@stylexjs/stylex";

const styles = create({
  popup: { gap: 0, width: "min(610px, calc(100vw - 48px))", padding: 0 },
});

<Dialog.Popup xstyle={styles.popup}>
  <Dialog.Title xstyle={srOnly}>Search</Dialog.Title>…
</Dialog.Popup>;

Props

PartRendersStyling
Dialog.RootNothingNone
Dialog.Trigger<button>None
Dialog.PopupA portal, the scrim, a <div>Bordered translucent panel
Dialog.Header<div>Stacks title and description
Dialog.Title<h2>Large, semibold
Dialog.Description<p>Body size, secondary text
Dialog.Footer<div>Right-aligns its buttons
Dialog.Close<button>None

Except Dialog.Root, each part takes xstyle, className, and style. className and style take plain values, not functions of state. See Theming.

The part tables below come from Base UI, MIT, © Material-UI SAS.

Root

Groups all parts of the dialog. Doesn't render its own HTML element.

Root Props:

PropTypeDefaultDescription
defaultOpenbooleanfalseWhether the dialog is initially open. To render a controlled dialog, use the open prop instead.
openboolean-Whether the dialog is currently open.
onOpenChange((open: boolean, eventDetails: Dialog.Root.ChangeEventDetails) => void)-Event handler called when the dialog is opened or closed.
actionsRefReact.RefObject<Dialog.Root.Actions | null>-A ref to imperative actions. unmount: Manually unmounts the dialog. Call this after any externally controlled closing animation finishes.close: Closes the dialog imperatively when called.
defaultTriggerIdstring | null-ID of the trigger that the dialog is associated with. This is useful in conjunction with the defaultOpen prop to create an initially open dialog.
disablePointerDismissalbooleanfalseWhether to prevent the dialog from closing on outside presses. For non-modal dialogs, this also prevents the dialog from closing when focus moves outside of it.
handleDialog.Handle<Payload>-A handle to associate the dialog with a trigger. If specified, allows external triggers to control the dialog's open state. Can be created with the Dialog.createHandle() method.
modalboolean | 'trap-focus'trueDetermines if the dialog enters a modal state when open. true: user interaction is limited to just the dialog: focus is trapped, document page scroll is locked, and pointer interactions on outside elements are disabled.false: user interaction with the rest of the document is allowed.'trap-focus': focus is trapped inside the dialog, but document page scroll is not locked and pointer interactions outside of it remain enabled. When modal is true or 'trap-focus', render <Dialog.Close> inside <Dialog.Popup> so touch screen readers can escape the popup.
onOpenChangeComplete((open: boolean) => void)-Event handler called after any animations complete when the dialog is opened or closed.
triggerIdstring | null-ID of the trigger that the dialog is associated with. This is useful in conjunction with the open prop to create a controlled dialog. There's no need to specify this prop when the dialog is uncontrolled (that is, when the open prop is not set).
childrenReact.ReactNode | PayloadChildRenderFunction<Payload>-The content of the dialog. This can be a regular React node or a render function that receives the payload of the active trigger.

Trigger

A button that opens the dialog. Renders a <button> element.

Trigger Props:

PropTypeDefaultDescription
handleDialog.Handle<Payload>-A handle to associate the trigger with a dialog. Can be created with the Dialog.createHandle() method.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
payloadPayload-A payload to pass to the dialog when it is opened.
idstring-ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the dialog in controlled mode (with the Dialog.Root triggerId prop).
classNamestring | ((state: Dialog.Trigger.State) => string | undefined)-CSS class applied to the element, or a function that returns a class based on the component's state.
styleReact.CSSProperties | ((state: Dialog.Trigger.State) => React.CSSProperties | undefined)-Style applied to the element, or a function that returns a style object based on the component's state.
renderReactElement | ((props: HTMLProps, state: Dialog.Trigger.State) => ReactElement)-Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Trigger Data Attributes:

AttributeTypeDescription
data-popup-open-Present when the corresponding dialog is open.
data-disabled-Present when the trigger is disabled.

Renders the portal, scrim, and popup. It takes the Base UI popup props, including ref, and scrolls when its content is taller than the window.

PropTypeDefaultDescription
tintTintScopes the panel to a hue. The scrim stays neutral.

A container for the dialog contents. Renders a <div> element.

Popup Props:

PropTypeDefaultDescription
initialFocusboolean | React.RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | void | HTMLElement | null)-Determines the element to focus when the dialog is opened. By default, focus moves to the first tabbable element inside the popup, except when the dialog is opened by touch — then the popup itself is focused to avoid opening the virtual keyboard. false: Do not move focus.true: Move focus based on the default behavior (first tabbable element or popup).RefObject: Move focus to the ref element.function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing.
finalFocusboolean | React.RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null)-Determines the element to focus when the dialog is closed. false: Do not move focus.true: Move focus based on the default behavior (trigger or previously focused element).RefObject: Move focus to the ref element.function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing.
classNamestring | ((state: Dialog.Popup.State) => string | undefined)-CSS class applied to the element, or a function that returns a class based on the component's state.
styleReact.CSSProperties | ((state: Dialog.Popup.State) => React.CSSProperties | undefined)-Style applied to the element, or a function that returns a style object based on the component's state.
renderReactElement | ((props: HTMLProps, state: Dialog.Popup.State) => ReactElement)-Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Popup Data Attributes:

AttributeTypeDescription
data-open-Present when the dialog is open.
data-closed-Present when the dialog is closed.
data-nested-Present when the dialog is nested within another dialog.
data-nested-dialog-open-Present when the dialog has other open dialogs nested within it.
data-starting-style-Present when the dialog begins animating in.
data-ending-style-Present when the dialog is animating out.

Popup CSS Variables:

VariableTypeDescription
--nested-dialogsnumberIndicates how many dialogs are nested within.

Title

A heading that labels the dialog. Renders an <h2> element.

Title Props:

PropTypeDefaultDescription
classNamestring | ((state: Dialog.Title.State) => string | undefined)-CSS class applied to the element, or a function that returns a class based on the component's state.
styleReact.CSSProperties | ((state: Dialog.Title.State) => React.CSSProperties | undefined)-Style applied to the element, or a function that returns a style object based on the component's state.
renderReactElement | ((props: HTMLProps, state: Dialog.Title.State) => ReactElement)-Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Description

A paragraph with additional information about the dialog. Renders a <p> element.

Description Props:

PropTypeDefaultDescription
classNamestring | ((state: Dialog.Description.State) => string | undefined)-CSS class applied to the element, or a function that returns a class based on the component's state.
styleReact.CSSProperties | ((state: Dialog.Description.State) => React.CSSProperties | undefined)-Style applied to the element, or a function that returns a style object based on the component's state.
renderReactElement | ((props: HTMLProps, state: Dialog.Description.State) => ReactElement)-Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Close

A button that closes the dialog. Renders a <button> element.

Close Props:

PropTypeDefaultDescription
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classNamestring | ((state: Dialog.Close.State) => string | undefined)-CSS class applied to the element, or a function that returns a class based on the component's state.
styleReact.CSSProperties | ((state: Dialog.Close.State) => React.CSSProperties | undefined)-Style applied to the element, or a function that returns a style object based on the component's state.
renderReactElement | ((props: HTMLProps, state: Dialog.Close.State) => ReactElement)-Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Close Data Attributes:

AttributeTypeDescription
data-disabled-Present when the button is disabled.

Accessibility

  • The popup has role="dialog", labelled by Dialog.Title and described by Dialog.Description.
  • Focus moves into the popup on open and returns to the trigger on close. Tab cycles inside the popup.
  • Escape closes. Outside press closes unless disablePointerDismissal is set on the root.
  • While open, outside content is inert and page scroll is locked.
  • initialFocus and finalFocus on the popup override where focus lands.