Components

Dialog

A modal with a scrim, a centered popup, and a built-in close button

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@nyte-ai/ui";
import { Dialog as DialogBase } from "@nyte-ai/ui/dialog";

A modal that opens on top of the page and takes focus. Reach for it when a task needs the user's full attention and can be cancelled.

The root exports give you the styled surface. There is no styled Trigger or Close, so those come from the @nyte-ai/ui/dialog namespace with render={<Button … />}. Both paths export the name Dialog, so a file using both aliases one. The namespace also carries Portal, Backdrop, and Viewport.

Usage

  • Every dialog needs a DialogTitle. It becomes the accessible name. Hide it visually rather than omitting it.
  • Use DialogDescription for the one sentence that explains the consequence. Longer content goes in the body.
  • Confirmations that destroy something use Alert dialog, which does not dismiss on outside press.
  • showCloseButton is on by default. Turn it off when the footer already has a Cancel.
  • DialogContent renders the portal, backdrop, and popup as one unit. Use the namespace parts when you need to place them yourself or need a scrollable Viewport.
  • Nest dialogs normally. Style the parent with the [data-nested-dialog-open] selector and the --nested-dialogs CSS variable. Backdrops of child dialogs are not rendered.

Anatomy

<Dialog>
  <DialogBase.Trigger />
  <DialogContent>
    {/* Portal › Backdrop › Popup, plus Close */}
    <DialogHeader>
      <DialogTitle />
      <DialogDescription />
    </DialogHeader>

    <DialogFooter />
  </DialogContent>
</Dialog>

The same tree with the unstyled namespace, where nothing from the root is in play:

<Dialog.Root>
  <Dialog.Trigger />
  <Dialog.Portal>
    <Dialog.Backdrop />
    <Dialog.Viewport>
      <Dialog.Popup>
        <Dialog.Title />
        <Dialog.Description />
        <Dialog.Close />
      </Dialog.Popup>
    </Dialog.Viewport>
  </Dialog.Portal>
</Dialog.Root>

Examples

<Dialog>
  <DialogBase.Trigger render={<Button variant="outline" />}>Rename session</DialogBase.Trigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Rename session</DialogTitle>
      <DialogDescription>The name shows in the sidebar and in the window title.</DialogDescription>
    </DialogHeader>
    <DialogFooter>
      <DialogBase.Close render={<Button variant="ghost" />}>Cancel</DialogBase.Close>
      <DialogBase.Close render={<Button />}>Save</DialogBase.Close>
    </DialogFooter>
  </DialogContent>
</Dialog>

Pass open and onOpenChange to drive the dialog from your own state, which is how you open it from a menu item or close it after a submit.

const [open, setOpen] = useState(false);

<Dialog open={open} onOpenChange={setOpen}>
  <DialogContent showCloseButton={false}>
    <DialogHeader>
      <DialogTitle>Rename session</DialogTitle>
    </DialogHeader>
    <form
      onSubmit={async () => {
        await save();
        setOpen(false);
      }}
    >

    </form>
  </DialogContent>
</Dialog>;

Props

ExportRendersStyling
DialogRootNone, it renders no element
DialogContentPortal, Backdrop, Popup, and CloseStyles land on the popup
DialogHeaderA <div> that stacks title and descriptionLayout only
DialogTitleTitleTitle type scale
DialogDescriptionDescriptionMuted body type
DialogFooterA <div> that right-aligns its buttonsLayout only

DialogContent adds one prop on top of the popup props.

PropTypeDefaultDescription
showCloseButtonbooleantrueRender the ✕ close button in the top-right corner.

Every styled export accepts xstyle alongside className and style. The backdrop is not exposed, change it with the scrim token, see Tokens.

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.

Portal

A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div> element.

Portal Props:

PropTypeDefaultDescription
containerHTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null-A parent element to render the portal element into.
classNamestring | ((state: Dialog.Portal.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.Portal.State) => React.CSSProperties | undefined)-Style applied to the element, or a function that returns a style object based on the component's state.
keepMountedbooleanfalseWhether to keep the portal mounted in the DOM while the popup is hidden.
renderReactElement | ((props: HTMLProps, state: Dialog.Portal.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.

Backdrop

An overlay displayed beneath the popup. Renders a <div> element.

Backdrop Props:

PropTypeDefaultDescription
forceRenderbooleanfalseWhether the backdrop is forced to render even when nested.
classNamestring | ((state: Dialog.Backdrop.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.Backdrop.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.Backdrop.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.

Backdrop Data Attributes:

AttributeTypeDescription
data-open-Present when the dialog is open.
data-closed-Present when the dialog is closed.
data-starting-style-Present when the dialog begins animating in.
data-ending-style-Present when the dialog is animating out.

Viewport

A positioning container for the dialog popup that can be made scrollable. Renders a <div> element.

Viewport Props:

PropTypeDefaultDescription
classNamestring | ((state: Dialog.Viewport.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.Viewport.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.Viewport.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.

Viewport 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.

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 DialogTitle and described by DialogDescription.
  • 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.
  • Content outside the dialog is inert while it is open, and background scroll is locked. Set modal={false} on the root to allow interaction with the rest of the page.
  • initialFocus and finalFocus on the popup override where focus lands.