Alert Dialog

An alert dialog is a modal dialog that interrupts the user's workflow to communicate an important message and acquire a response.

Are you sure you want to delete this user ?

This action cannot be undone. This will permanently delete your account and remove your data from our servers.

<div>
  <button data-dialog-trigger="dialog3" class="btn sz-md variant-destructive">
      <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-4">
          <path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
      </svg>
      <span class="btn-label">Delete</span>
  </button>
  <dialog class="shadow-xl size-fit m-auto max-w-lg card variant-outlined origin-bottom" role="dialog" id="dialog3" aria-labelledby="dialog3_label">
      <h2 id="dialog3_label" class="text-title font-medium">Are you sure you want to delete this user ?</h2>
      <p class="text-body text-sm mt-3">This action cannot be undone. This will permanently delete your account and remove your data from our servers.</p>
      
      <div class="mt-8 flex justify-end gap-3">
          <button data-dialog-close class="btn sz-sm variant-outlined">
              <span class="btn-label">Cancel</span>
          </button>
          <button class="btn sz-sm variant-neutral" type="submit" form="userAddress">
              <span class="btn-label">Yes, Continue</span>
          </button>
      </div>
  </dialog>
</div>

Installation

1. Add Component TS

Copy paste the following in a .ts file

document.addEventListener('DOMContentLoad', () => {
  const dialogTriggers = document.querySelectorAll('[data-dialog-trigger]') as NodeListOf<HTMLButtonElement>;

  dialogTriggers.forEach((dialogTrigger) => {
      const dialogId = dialogTrigger.getAttribute('data-dialog-trigger') as string;

      if (!dialogId) {
          console.error('Dialog trigger is missing a valid "data-dialog-trigger" attribute.');
          return; 
      }

      const dialog = document.getElementById(dialogId) as HTMLDialogElement | null;

      if (!dialog) {
          console.error(`Dialog with ID "${dialogId}" not found.`);
          return; 
      }

      const dialogClose = dialog.querySelector('[data-dialog-close]') as HTMLButtonElement | null;

      if (!dialogClose) {
          console.error(`Close button not found in dialog with ID "${dialogId}".`);
          return; 
      }

      dialogTrigger.addEventListener('click', () => dialog.showModal());
      dialogClose.addEventListener('click', () => dialog.close());
  });
});

2. Add Css for animation

Copy and paste the following css in your main css file

dialog[open]{
  opacity: 1;
  transform: scale(1);
}

dialog{
  opacity: 0;
  transform: scale(0.9);
  transition:
      opacity 0.1s ease-out,
      transform 0.1s ease-out allow-discrete,
      overlay 0.1s ease-out allow-discrete,
      display 0.1s ease-out allow-discrete;
}

@starting-style {
  dialog[open] {
      opacity: 0;
      transform: scale(0.9);
  }
}

dialog::backdrop{
  background-color: rgb(0 0 0 / 0%);
  transition:
      display 0.1s allow-discrete,
      overlay 0.1s allow-discrete,
      background-color 0.1s;
}

dialog[open]::backdrop {
  background-color: var(--overlay-bg);
}

@starting-style {
  dialog[open]::backdrop {
      background-color: rgb(0 0 0 / 0%);
  }
}