Skip to Content
Cristal

Overlays

Dialog

A window that interrupts the page for a decision or a short input.

<cr-dialog>CrDialog

Example

The content goes in the default slot, the action buttons in slot="footer". Control opening with open, and set your state back to false in onCrClose.

Delete product

This action cannot be undone.

CancelDelete
'use client';

import { useState } from 'react';
import { CrButton, CrDialog } from '@cristal-ui/react';

export default function DialogConfirm() {
  const [open, setOpen] = useState(false);
  const [status, setStatus] = useState('');

  return (
    <>
      <CrButton onClick={() => setOpen(true)}>Delete product</CrButton>
      <span role="status">{status}</span>

      <CrDialog label="Delete this product?" open={open} onCrClose={() => setOpen(false)}>
        <p>This action cannot be undone.</p>
        <CrButton slot="footer" variant="secondary" onClick={() => setOpen(false)}>
          Cancel
        </CrButton>
        <CrButton
          slot="footer"
          onClick={() => {
            setStatus('Product deleted.');
            setOpen(false);
          }}
        >
          Delete
        </CrButton>
      </CrDialog>
    </>
  );
}

Accessibility

Cristal uses the native <dialog> element opened in modal mode:

  • focus moves into the window when it opens, stays trapped inside, and returns to the opening button when it closes;
  • Escape closes the window; a click on the dimmed background next to it closes it too; closeOnBackdrop={false} prevents it, for example for a long form that a stray click would lose;
  • the rest of the page becomes inert (neither clickable nor readable by screen readers) and no longer scrolls;
  • the title (label) is used as the window's name; the close button is labelled "Close", or "Fermer" on a French page (customizable with closeLabel).

API

Properties

PropertyTypeDefaultDescription
closeLabelstring—Libellé du bouton de fermeture (« Close », ou « Fermer » en français, par défaut).
closeOnBackdropbooleantrueUn clic sur le fond assombri, à côté, ferme la fenêtre ; false pour l'empêcher (formulaire long, confirmation).
label (obligatoire)string—Titre affiché, et nom de la fenêtre pour les lecteurs d'écran.
openbooleanfalseOuvre ou ferme la fenêtre.

Events

Reactevent.detailDescription
onCrClosevoidÉmis à chaque fermeture : Échap, bouton Fermer, clic à côté (avec closeOnBackdrop) ou open à false.

Slots

SlotDescription
defaultContenu de la fenêtre.
slot="footer"Boutons d'action.

CSS parts

To style it from outside: cr-dialog::part(name) { … }

PartDescription
bodyLa zone de contenu.
closeLe bouton Fermer.
dialogL'élément dialog.
footerLa zone des actions.
titleLe titre.

On this page