Homepage » Software Development » The Wait is Over: DIALOG element reaches full support
Jun. 09, 2022
5 minutes read
Share this article
First, it would be Firefox 98, which added support in March of this year, and Safari would do the same soon after with its most recent version (15.4), thus passing the HTML5
Category to which it belongs: flow content, sectioning root.
Allowed content: flow content.
DOM interface: HTMLDialogElement.
It is used to represent a dialog box or modal window, either to inform the user of something important or to involve the user in some action or decision-making. Both are inspired by the ones we commonly see in all operating systems.
Besides the positive impact of accessibility, this feature brings on the user side, it also improves the development experience. Let’s see how:
Pressing the Esc key closes the dialog, an interaction that any user -disabled or not- will always appreciate.
By default, the focus is automatically placed on the first focusable element inside unless another is explicitly specified using the autofocus attribute.
When the dialog closes, the focus returns to where it was before (typically the button or element that triggered its opening).
On the other hand, for assistive technologies -such as screen readers- to communicate the purpose of the dialog to the user, it is enough to use aria-labelledby on the element, pointing to the id of its title or supplying the text directly through aria-label. If this is not descriptive enough, you can use aria-describedby.
In this way, when the dialog is opened, its title will be announced, followed by the word “dialog” (if not provided, only “dialog” is announced). Furthermore, of course, we can now forget about the use of role=”dialog“ since, being a semantic label, its role is implicit, and it can only be changed to the more specific role=”alertdialog“.
Invoking the element’s show() method displays the dialog without preventing navigation through the rest of the document. On the other hand, showModal() confines the user to perform focus trapping until the close() method is executed, for example, when we press a button or Esc.
In addition, showModal() adds an overlay layer to hide the underlying content, which can be accessed by CSS for customization via the :backdrop pseudo-element.
If you want to know the state -open or closed- of the dialog, you can consult the (implicit) open attribute of the element, which returns a Boolean. A warning: you should never modify or remove this attribute’s value. Firstly, the browser would completely lose track of the state, producing various undesirable effects. Secondly, why would you want to do it if it is ultimately controlled “automagically”?
At this point, and if you have some knowledge of accessibility, the series of advantages that this element brings will be more than evident to you. Until now, we had to have several considerations that, if neglected, could easily turn our manners into an accessibility nightmare.
Another option was to look for a library that provides accessible dialogs in exchange for giving up control over our markup or spending the time it was originally intended to save on customization.
In any case, it was necessary to make sure that the following is complied with:
We have probably found ourselves performing these tasks repetitively in the past. You could say that we won’t have to worry about them anymore.
However, as with any new feature, we should use them wisely. It is necessary to provide some kind of fallback for older user agents: live browser versions or supporting technologies whose update cycle is slower.
A good option, quite robust, can be a11y-dialog. There is also a polyfill, published by the Chrome team. Let’s not forget that backward compatibility is also part of accessibility.
From my role as a front-end developer and a11y analyst at Coderio, I have embraced the adoption of new technologies to improve our service and daily work. These updates allow us to create inclusive content that can be consumed by any human being, regardless of their physical or mental conditions.
So much so that accessibility is part of the company’s policies, which range from compliance with international standards when developing software to creating various job vacancies for hiring new staff.
Today, we find ourselves advancing with full conviction in that direction to continue generating changes at a structural level, professionally and business-wise.
Accelerate your software development with our on-demand nearshore engineering teams.