Components

Dialog

A popup that opens on top of the entire page.

Usage guidelines#

  • Dialog doesn't support gestures: Use Drawer when you need gesture support or snap points. A panel that slides in from the edge of the screen and doesn't need gesture support is a positioned Dialog.

Anatomy#

Import the component and assemble its parts:

Anatomytsx
import { Dialog } from '@egoist/solid-base/dialog';

<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#

State#

By default, Dialog is an uncontrolled component that manages its own state.

Uncontrolled dialogtsx
<Dialog.Root>
  <Dialog.Trigger>Open</Dialog.Trigger>
  <Dialog.Portal>
    <Dialog.Popup>
      <Dialog.Title>Example dialog</Dialog.Title>
      <Dialog.Close>Close</Dialog.Close>
    </Dialog.Popup>
  </Dialog.Portal>
</Dialog.Root>

Use open and onOpenChange props if you need to access or control the state of the dialog. For example, you can control the dialog state in order to open it imperatively from another place in your app.

Controlled dialogtsx
const [open, setOpen] = createSignal(false);

<Dialog.Root open={open()} onOpenChange={setOpen}>
  <Dialog.Trigger>Open</Dialog.Trigger>
  <Dialog.Portal>
    <Dialog.Popup>
      <form
        // Close the dialog once the form data is submitted
        onSubmit={async (event) => {
          event.preventDefault();
          await submitData();
          setOpen(false);
        }}
      >
        ...
      </form>
    </Dialog.Popup>
  </Dialog.Portal>
</Dialog.Root>;

It's also common to use onOpenChange if your app needs to do something when the dialog is closed or opened. This is recommended over an effect that watches the open signal.

Running code when dialog state changestsx
<Dialog.Root
  open={open()}
  onOpenChange={(open) => {
    // Do stuff when the dialog is closed
    if (!open) {
      doStuff();
    }
    // Set the new state
    setOpen(open);
  }}
>

Nested dialogs#

You can nest dialogs within one another normally.

Use the [data-nested-dialog-open] selector and the var(--nested-dialogs) CSS variable to customize the styling of the parent dialog. Backdrops of the child dialogs won't be rendered so that you can present the parent dialog in a clean way behind the one on top of it.

Custom focus management#

You can control where the focus goes when the dialog opens and closes using the initialFocus and finalFocus props on the <Dialog.Popup> component.

You can also set these props to false to prevent focus from moving when the dialog opens or closes, or to a function that returns the element to focus based on the interaction type.

Outside scroll dialog#

The dialog can be made scrollable by using <Dialog.Viewport> as an outer scrollable container for <Dialog.Popup> while the popup can extend past the bottom edge.

Inside scroll dialog#

The dialog can be made scrollable by making an inner container scrollable while the popup stays fully on screen. <Dialog.Viewport> is used as a positioning container for <Dialog.Popup>.

Placing elements outside the popup#

When adding elements that should appear "outside" the colored popup area, continue to place them inside <Dialog.Popup>, but create a child element that has the popup styles. This ensures they are kept in the tab order and announced correctly by screen readers.

<Dialog.Popup> has pointer-events: none, while inner content (the colored popup and close button) has pointer-events: auto so clicks on the backdrop continue to be registered.

Detached triggers#

A dialog can be controlled by a trigger located either inside or outside the <Dialog.Root> component. For simple, one-off interactions, place the <Dialog.Trigger> inside <Dialog.Root>, as shown in the example at the top of this page.

However, if defining the dialog's content next to its trigger is not practical, you can use a detached trigger. This involves placing the <Dialog.Trigger> outside of <Dialog.Root> and linking them with a handle created by the Dialog.createHandle() function.

The imperative methods on the handle, such as open() and openWithPayload(), require a <Dialog.Root> using the same handle to be mounted. Calls made while no root is attached to the handle — before one mounts, or after it unmounts — are ignored. Each time a root mounts, it starts from fresh state: a call made while no root was attached is not replayed, and no open state carries over from a previous mount.

Detached triggerstsx
const demoDialog = Dialog.createHandle();

<Dialog.Trigger handle={demoDialog}>Open</Dialog.Trigger>

<Dialog.Root handle={demoDialog}>
  ...
</Dialog.Root>

Multiple triggers#

A single dialog can be opened by multiple trigger elements. You can achieve this by using the same handle for several detached triggers, or by placing multiple <Dialog.Trigger> components inside a single <Dialog.Root>.

Multiple triggers within the Root parttsx
<Dialog.Root>
  <Dialog.Trigger>Trigger 1</Dialog.Trigger>
  <Dialog.Trigger>Trigger 2</Dialog.Trigger>
  ...
</Dialog.Root>
Multiple detached triggerstsx
const demoDialog = Dialog.createHandle();

<Dialog.Trigger handle={demoDialog}>Trigger 1</Dialog.Trigger>
<Dialog.Trigger handle={demoDialog}>Trigger 2</Dialog.Trigger>
<Dialog.Root handle={demoDialog}>
  ...
</Dialog.Root>

The dialog can render different content depending on which trigger opened it. This is achieved by passing a payload to the <Dialog.Trigger> and using the function-as-a-child pattern in <Dialog.Root>.

The payload can be strongly typed by providing a type argument to the createHandle() function:

Detached triggers with payloadtsx
const demoDialog = Dialog.createHandle<{ text: string }>();

<Dialog.Trigger handle={demoDialog} payload={{ text: 'Trigger 1' }}>
  Trigger 1
</Dialog.Trigger>

<Dialog.Trigger handle={demoDialog} payload={{ text: 'Trigger 2' }}>
  Trigger 2
</Dialog.Trigger>

<Dialog.Root handle={demoDialog}>
  {(state) => (
    <Dialog.Portal>
      <Dialog.Popup>
        <Dialog.Title>Dialog</Dialog.Title>
        <Show when={state.payload}>
          {(payload) => (
            <Dialog.Description>This has been opened by {payload().text}</Dialog.Description>
          )}
        </Show>
      </Dialog.Popup>
    </Dialog.Portal>
  )}
</Dialog.Root>

Controlled mode with multiple triggers#

You can control the dialog's open state externally using the open and onOpenChange props on <Dialog.Root>. This allows you to manage the dialog's visibility based on your application's state. When using multiple triggers, you have to manage which trigger is active with the triggerId prop on <Dialog.Root> and the id prop on each <Dialog.Trigger>.

Note that there is no separate onTriggerIdChange prop. Instead, the onOpenChange callback receives an additional argument, eventDetails, which contains the trigger element that initiated the state change.

Open from a menu#

In order to open a dialog using a menu, control the dialog state and open it imperatively using the onClick handler on the menu item.

API reference#

<Dialog.Root>

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

PropTypeDefault
openboolean—

Whether the dialog is currently open.

defaultOpenbooleanfalse

Whether the dialog is initially open.

To render a controlled dialog, use the open prop instead.

modalboolean | 'trap-focus'true

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

onOpenChange((open: boolean, eventDetails: DialogRoot.ChangeEventDetails) => void)—

Event handler called when the dialog is opened or closed.

onOpenChangeComplete((open: boolean) => void)—

Event handler called after any animations complete when the dialog is opened or closed.

disablePointerDismissalbooleanfalse

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

actionsRefActionsRef<DialogRoot.Actions>—

A ref to imperative actions: a ref object ({ current: null }) or a callback receiving them (and null on unmount). - unmount: Ends the closing phase of the dialog after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onOpenChange first, otherwise the dialog completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the dialog imperatively when called.

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

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

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.

<Dialog.Trigger>

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

PropTypeDefault
disabledbooleanfalse

Whether the component ignores user interaction.

handleDialogHandle<Payload>—

A handle to associate the trigger with a dialog. Can be created with the Dialog.createHandle() method.

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 DialogRoot triggerId prop).

nativeButtonbooleantrue

Whether 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>).

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the trigger is disabled.

<Dialog.Backdrop>

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

PropTypeDefault
forceRenderbooleanfalse

Whether the backdrop is forced to render even when nested.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<Dialog.Close>

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

PropTypeDefault
disabledbooleanfalse

Whether the component ignores user interaction.

nativeButtonbooleantrue

Whether 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>).

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the button is disabled.

<Dialog.Description>

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

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<Dialog.Popup>

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

PropTypeDefault
initialFocusFocusTargetProp—

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). - An element or a ref object ({ current }): Move focus to that 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.

finalFocusFocusTargetProp—

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). - An element or a ref object ({ current }): Move focus to that 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.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
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.

CSS variableDescription
--nested-dialogs

Indicates how many dialogs are nested within.

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

PropTypeDefault
keepMountedbooleanfalse

Whether to keep the portal mounted in the DOM while the popup is hidden.

containerPortalContainer—

A parent element to render the portal element into.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<Dialog.Viewport>

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

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
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.

<Dialog.Title>

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

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

createHandle#

Creates a Dialog.Handle that connects a <Dialog.Root> with detached <Dialog.Trigger> components. Pass a type argument to type the payload: Dialog.createHandle<Payload>().

Handle#

Method / property Description
open(triggerId) Opens the dialog and associates it with the trigger that has this id (a Dialog.Trigger using this handle). Pass null to open without a trigger.
openWithPayload(payload) Opens the dialog with the given payload, without associating it with any trigger.
close() Closes the dialog.
isOpen Whether the dialog is currently open (false while no root is attached). Tracked when read in a reactive scope.

Call these methods from event handlers or effects, not while rendering.