Box Modal

A typical "boxed" modal using the native <dialog> element.

Notes:

A few things to note when using the <dialog> element.

  • window.#id.showModal()should be used to open the <dialog>. window.#id.open() can be used, however this comes with reduced accessibility and functionality. It's good to be aware of what each of these techniques to open the <dialog> does and to factor in any additional development for loss of functionality.
  • Using the <dialog> element, means there is no 'click outside' (or the backdrop) to close the modal. However a user can still use the Esc key to close the <dialog>. Updated! Using dialog.js module to invoke the dialog now allows us to add "light dismiss" to dialog elements.
  • The <dialog> element has an autofocus feature, which focuses the first selectable element when opening the modal. If content is scrollable and the first selectable element is at the bottom of the content it will focus and auto scroll to the bottom. This won't be desirable behaviour for this context, so have a plan to manage this. A good way to do it, is to autofocus a close button at the top so that when the modal opens, it does not automatically scroll to the bottom.
  • Close the <dialog> element by submitting a form within the dialog (for pages that are not surrounded by a master form element), with <button formmethod="dialog">Cancel</button>. Use onclick="window.#id.close()" to close the element when not using a form inside the <dialog>.
  • Ensure that developers know what happens when a required input is used and how it affects closing the <dialog> element.
  • Whilst the <dialog> renders all other page elements inert, it does not stop page scrolling. Updated! This is circumvented by using CSS on the HTML when a <dialog> element is open.

Dialog Element

Using the HTML <dialog> element for modals.

Title

I'm a dialog.

Box Sizes

A typical "boxed" modal, sizes can be adjusted with class modifiers.

Title

I'm a default sized dialog box.

Body Elements

Layout options for the box modal include a header and footer useful for occasions when content within the modal is scrollable.

Modal Header

Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda deleniti aspernatur eveniet illum aut, cumque delectus nobis, perspiciatis aliquam quibusdam est nam, cum vitae ut. Libero suscipit deserunt in reiciendis!

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus quaerat exercitationem numquam saepe nulla debitis aspernatur, repellat aliquam. Harum dolorum distinctio quo dolore aperiam perspiciatis non aliquid adipisci impedit accusamus.

Managing Overflow

When space is limited i.e when on a mobile device, ensure the content is scroll-able within the modal.

Title

Lorem ipsum dolor sit, amet consectetur adipisicing elit. Consequuntur, ullam. Natus assumenda sint dicta! Quos eveniet voluptatem vel, consectetur hic ullam error accusantium, quod necessitatibus fugit ducimus quae nemo quisquam!

Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis quos harum excepturi iste, dolore adipisci aliquid soluta? Quisquam, quidem commodi? Molestias, ab. Iure, autem quae eligendi veniam saepe assumenda cupiditate?

Close

Allow the dialog element to be closed with a "X" button.

Title

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis perferendis, id tenetur ipsum mollitia harum fuga eveniet labore natus neque accusamus sapiente rem quod fugit rerum aliquam quisquam? Inventore, itaque.

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quidem, laborum consequatur minima quasi recusandae quos porro, expedita veritatis odio nisi aut! Delectus doloribus, pariatur doloremque porro quo sint quas omnis.


Support:

Support for the dialog element.

Data on support for the dialog feature across the major browsers from caniuse.com