Documentation
Design
Components
Dialog

EccUtilsDesignDialog Component

2.0.0-alpha.52
<ecc-utils-design-dialog/>
EccUtilsDesignDialog

Overview

The ecc-utils-design-dialog component is a modal window over the page, like shadcn/ui's Dialog (opens in a new tab). It is a generic building block: confirmations, forms, notices, preferences and the like are built from it, together with the other components.

  • It opens on a click on its trigger slot or when open is set.
  • It closes on Escape (unless content inside handled it and called preventDefault()), on a click on the overlay, on its close button, on a click on an element with the data-dialog-close attribute, or when open is unset.
  • While it is open, the rest of the page is inert and doesn't scroll. Focus moves into the dialog (to the first element with autofocus, else to the first focusable element), Tab and Shift+Tab cycle through the dialog only, and on close focus returns to the trigger that opened it (also in Safari and Firefox on macOS, where a mouse click doesn't focus buttons), or else to the element that had it before.
  • It is a dialog with aria-modal="true", named by its title (or label) and described by its description, so screen readers announce both.
  • User actions fire the cancelable ecc-dialog-open-changed event.

The dialog uses the browser's native <dialog> element (showModal()), which all current browsers support.

Usage

Basic Dialog

A dialog with a trigger, a title, a description, a body and actions in the footer. "Cancel" closes the dialog through data-dialog-close.

Edit profileEdit profileMake changes to your profile here. Click save when you're done.
CancelSave changes

Controlled Dialog

Without a trigger, the app opens and closes the dialog through open and follows the user's actions through ecc-dialog-open-changed, like open and onOpenChange in shadcn/ui. The event doesn't bubble, so a controlled dialog only gets its own events, also when other dialogs are nested in it. Here the dialog can only be closed through its buttons: the overlay click is turned off and the close button hidden.

dialog-controlled.tsx
import React, { useState } from 'react';
import { EccUtilsDesignDialog } from '@elixir-cloud/design/react/dialog';
import { EccUtilsDesignButton } from '@elixir-cloud/design/react/button';
 
export default function ConfirmDelete({ onDelete }: { onDelete: () => void }) {
  const [open, setOpen] = useState(false);
  return (
    <>
      <EccUtilsDesignButton variant="destructive" onClick={() => setOpen(true)}>
        Delete
      </EccUtilsDesignButton>
      <EccUtilsDesignDialog
        open={open}
        no-overlay-close
        hide-close-button
        onEccDialogOpenChanged={(event) => setOpen(event.detail.open)}
      >
        <span slot="title">Delete this service?</span>
        <span slot="description">This can't be undone.</span>
        <div slot="footer" className="flex gap-2">
          <EccUtilsDesignButton variant="outline" onClick={() => setOpen(false)}>
            Cancel
          </EccUtilsDesignButton>
          <EccUtilsDesignButton
            variant="destructive"
            onClick={() => {
              onDelete();
              setOpen(false);
            }}
          >
            Delete
          </EccUtilsDesignButton>
        </div>
      </EccUtilsDesignDialog>
    </>
  );
}

Slots

SlotDescription
(default)The body of the dialog.
triggerAn element that opens the dialog on click, usually a button. It gets aria-haspopup="dialog" and aria-expanded; a custom element such as ecc-utils-design-button gets them on the control in its shadow root, too.
titleThe title. It names the dialog for assistive technology; without it, set label.
descriptionA description below the title. It describes the dialog for assistive technology.
footerActions at the bottom, e.g. buttons.

The header (title and description) and the footer are hidden when their slots are empty. Any element inside the dialog with the data-dialog-close attribute closes it on click, like DialogClose in shadcn/ui.

Properties

PropertyAttributeTypeDefaultDescription
openopenbooleanfalseWhether the dialog is open. Reflected to the attribute.
labellabelstring""Accessible name of the dialog, used if the title slot is empty.
closeLabelclose-labelstring"Close"Accessible name of the close button, e.g. for translations.
hideCloseButtonhide-close-buttonbooleanfalseHides the close button in the top right corner.
noOverlayCloseno-overlay-closebooleanfalseKeeps the dialog open on a click on the overlay.

Methods

MethodDescription
show()Opens the dialog, like open = true.
close()Closes the dialog, like open = false.

Events

Event NameReact Event NameDetail TypeDescription
ecc-dialog-open-changedonEccDialogOpenChanged{ open: boolean; reason: EccDialogOpenChangeReason }Fired when the user opens or closes the dialog.

ecc-dialog-open-changed

Fired before the dialog opens or closes because of a user action, not when open is changed by code. Unlike the events of the other components, it doesn't bubble: listen on the dialog itself (in React, onEccDialogOpenChanged), not on an ancestor. So the listener of an outer dialog doesn't get the events of dialogs nested in it, and setting open from detail.open is safe. detail.open is the new state, and detail.reason says what caused it (type EccDialogOpenChangeReason, exported with EccDialogOpenChangedEvent from @elixir-cloud/design/events):

ReasonCause
triggerA click on the trigger slot.
escapeEscape, or another close request of the browser (e.g. the back gesture on Android).
overlayA click on the overlay.
close-buttonA click on the close button.
closeA click on an element with data-dialog-close.

The event is cancelable: event.preventDefault() keeps the dialog as it is, e.g. to ask before discarding changes. If the browser closes the dialog by itself (some browsers do on repeated Escape presses without other user input), the dialog fires the event with the reason escape after closing, and it can't be canceled.

<EccUtilsDesignDialog
  onEccDialogOpenChanged={(event) => {
    if (!event.detail.open && hasUnsavedChanges) {
      event.preventDefault();
    }
  }}
>
  ...
</EccUtilsDesignDialog>

Styling

The dialog's parts can be styled with ::part():

PartElement
dialogThe native dialog element.
contentThe panel inside the dialog.
headerThe container of the title and the description.
titleThe container of the title.
descriptionThe container of the description.
bodyThe container of the body.
footerThe container of the footer.
close-buttonThe close button.
ecc-utils-design-dialog::part(dialog) {
  max-width: 48rem;
}

Accessibility

  • The dialog is modal: the rest of the page is inert while it is open, and focus stays inside. On close, focus returns to the trigger that opened it, also after a mouse click in browsers that don't focus buttons on click (Safari, Firefox on macOS).
  • The page behind the dialog doesn't scroll while a dialog is open, as in shadcn/ui; the space of the page's scrollbar is kept, so the page doesn't shift. Nested dialogs share the lock, and the page scrolls again once the last one closes.
  • Give every dialog a title, or a label if it has none, so that screen readers announce what it is about.
  • Nested dialogs work: Escape, overlay clicks and data-dialog-close close the innermost open dialog only.
  • Content that is taller than the window scrolls inside the dialog.
  • The trigger slot reacts to clicks only. Put a button (or another control that fires a click on Enter and Space) into it, so it works with the keyboard too.

Released under Apache 2.0 License.

Copyright © 2023-2026 ELIXIR