DEV Community


Posted on

Crafting a Seamless UX: Custom Confirmation Dialogs with Turbo πŸš€βœ¨

Introduction πŸ‘‹

In the intricate landscape of user experience (UX) design, confirmation dialogs stand as guardians against inadvertent data loss.
This article explores the significance of confirmation dialogs and their indispensable role in refining user interface (UI) design.

The UX Power of Confirmation Dialogs πŸ’‘

Confirmation dialogs, ubiquitous in digital interfaces, act as a crucial defense mechanism, preventing unintended actions that could jeopardize critical user data. Their primary purpose is to add an extra layer of security, compelling users to confirm their intent before embarking on potentially irreversible actions.

Guarding Against Accidental Deletion

Consider a scenario where a user, navigating an application, accidentally clicks the "Delete" button. Without a confirmation dialog,
this innocent mistake could result in the loss of valuable data. Confirmation dialogs offer users a moment of pause, minimizing the risk of unintended consequences.

Empowering User Control

Confirmation dialogs not only shield against data loss but also empower users, providing greater control over their actions within a platform. Well-designed confirmation dialogs align with user-centric design principles, prioritizing user safety and satisfaction.

Best Practices for Confirmation Dialogs 🌟

To maximize the effectiveness of confirmation dialogs, adherence to best practices is crucial. Clear, concise, and easily understandable language is paramount. Ambiguous wording can lead to user frustration, defeating the purpose of the dialog.

Strategic placement and design ensure that dialogs are noticeable without being intrusive. Intuitive icons and consistent visual cues help users quickly grasp the significance of the dialog, fostering a seamless and efficient user experience.

When to use

  • Important system events requiring prompt user intervention.
  • Actions with destructive consequences, like deletion or impactful transactions.
  • Especially for actions that are irreversible.

When not to use

Avoid implementing confirmation dialogs for actions that lack serious consequences, such as changing user preferences. Utilize them exclusively for actions with substantial implications.

Implementing Custom Dialogs in Rails 7 with Turbo πŸ› οΈ

Assuming you're using Rails 7 with Turbo, integrating a custom dialog box is a breeze. Design your dialog box, create a partial, and render it from your application layout. My personal touch? Crafted the dialog using Tailwind CSS for a sleek look and feel. Any questions? Feel free to ask!

tailwindcss dialog


            svg.w-6.h-6.text-red-600[aria-hidden="true" xmlns="" fill="none" viewbox="0 0 20 20"]
              path[stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 9h2v5m-2 0h4M9.408 5.5h.01M19 10a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"]

              = t('dialog.title')

                = t('dialog.default_body')
            = t('dialog.cancel')

            = t('dialog.apply')

Enter fullscreen mode Exit fullscreen mode

In case you're not using slim templates you can easily convert this piece of template to erb using a tool such a

// app/javascript/application.js

Turbo.setConfirmMethod((message, element) => {
  let dialog = document.getElementById("turbo-confirm")
  const body = document.body;

  // dynamic body, we could do similar thing with title or buttons and even with colors!
  // By adding more data attributes to our element and handle here after that
  dialog.querySelector("[id='dialog-body']").innerHTML = window.DOMPurify.sanitize(message);

  // prevent background from scrolling

  return new Promise((resolve, reject) => {
    dialog.addEventListener("close", () => {
        dialog.returnValue == "confirm" ? resolve(true) : body.classList.remove('overflow-hidden');
    }, { once: true })
Enter fullscreen mode Exit fullscreen mode

Here's how you can easily use this feature in your template:

- confirm_body = 'Are you sure you want to deactivate your account? All of your data will be permanently removed. This action cannot be undone.'
= link_to(deactivate_account_path, data: { turbo_method: :patch, turbo_confirm: confirm_body })
Enter fullscreen mode Exit fullscreen mode

So, as you embark on your coding endeavors, remember that the is not just about lines of code β€” it's about crafting experiences that resonate with users, ensuring they navigate your Rails 7 application with confidence and satisfaction. Happy coding, and may your user experience always be Turbo-charged! πŸš€βœ¨

Top comments (0)