Tagged “modal”
Use the dialog element (reasonably), by Scott O’Hara
Here’s an important update on native modal dialogues. TL;DR – it’s now OK to use dialog
.
Last year I posted that Safari now supported the HTML dialog
element meaning that we were within touching distance of being able to adopt it with confidence. My caveat was:
However first I think we’d need to make an informed decision regarding our satisfaction with support, based on the updated advice in Scott O’Hara’s article Having an Open Dialog.
(Accessibility expert Scott O’Hara has been diligently testing the dialog
element for years.)
However the happy day has arrived. The other day Scott posted Use the dialog element (reasonably). It includes this advice:
I personally think it’s time to move away from using custom dialogs, and to use the
dialog
element instead.
That’s an important green-light!
And this of course means that we can stop DIYing modal dialogues from div
s plus super-complicated scripting and custom ARIA, and instead let a native HTML element do most of the heavy lifting for us.
From a Design System perspective, I’d previously suggested to my team that when we revisit our Modal component we should err toward a good custom dialogue library first, however I’m now likely to recommend we go for native dialog
instead. Which is great!
We use too many damn modals (modalz modalz modalz dot com)
At our fortnightly Accessibility Forum at work, we just had a great discussion about modal dialogues. We started by discussing whether focus should be completely trapped within the modal or if the user should at least have access to the browser toolbar (we decided on the former). We then moved onto a general discussion on the pros and cons of modals, which led me to share MODALZ MODALZ MODALZ with the team.
Adrian Egger’s website presents some interesting suggestions, which I’ll summarise below:
Have you considered using the following alternatives to modals?
- Non-modal dialogs (e.g. toasts): for non-critical interactions that don’t block the user, use these rather than modals
- New page: lead the user to a different page to isolate the interaction without losing access to functionality such as navigation.
- Go inline: present your content inline to be less disruptive.
- Expanding elements: Use expanding elements such as accordions, toolbars, tooltips, or sliding sidebars (or other modeless elements).
- “Undo” patterns: instead of confirmation modals, consider using inline “Undo” option to speed up the user’s interactions.
When all else fails here’s how to get modals right:
Do:
- Make it easy to close: make it simple to get rid of ‐ by escape key, clicking outside the modal window, and a clearly labelled close button.
- Single purpose: limit the interaction to one, straightforward task.
- Keep it short: be brief and concise in your content
- Accessibility: this is fairly self-explanatory — if you can’t make it accessible, don’t use a modal.
Do not:
- Modal inception: avoid opening a modal from a modal.
- Fullscreen modals: you might as well navigate to a new page.
- Multi-step modals: never create a multi-step modal. That way madness lies.
- Self-spawning modals: never present modals unless prompted by the user.
- Marketing modals: seriously, nobody wants that sh*t — especially not your newsletter.
Modals are so hard to do well and present so many possible issues that I think before diving into designing/building one, it’s worth questioning if a modal is really appropriate for your use case.
Refactoring a modal dialogue in 2022
My team will soon be refactoring our modal dialogue component. Ours has a few deficiencies, needs better developer experience and documentation, is not built to our Design System component standards, and could use a resilience boost from some progressive enhancement.
For a long time the best – meaning accessible, framework-agnostic, feature-packed – modal implementations were custom. Specifically:
However with recent browser advances (especially from Safari), there’s an argument that the time has now come that we no longer need custom solutions and can go native. So we might reach for the native <dialog>
HTML element.
However first I think we’d need to make an informed decision regarding our satisfaction with support, based on the updated advice in Scott O’Hara’s article Having an Open Dialog.
Additionally we should definitely be keeping one eye on proposals around the exciting new togglepopup
and popup
attributes which promise the holy grail of entirely HTML-powered modal dialogues with no JavaScript dependency.
Accessible interactions (on Adactio)
Jeremy Keith takes us through his thought process regarding the choice of link or button
when planning accessible interactive disclosure elements.
A button
is generally a solid choice as it’s built for general interactivity and carries the expectation that when activated, something somewhere happens. However in some cases a link might be appropriate, for example when the trigger and target content are relatively far apart in the DOM and we feel the need move the user to the target / give it focus.
For a typical disclosure pattern where some content is shown/hidden by an adjacent trigger, a button
suits perfectly. The DOM elements are right next to each other and flow into each other so there’s no need to move or focus anything.
However in the case of a log-in link in a navigation menu which—when enhanced by JavaScript—opens a log-in form inside a modal dialogue, a link might be better. In this case you might use an anchor with a fragment identifier (<a href="#login-modal">Log in</a>
) pointing to a login-form far away at the bottom of the page. This simple baseline will work if JavaScript is unavailable or fails, however when JavaScript is available we can intercept the link’s default behaviour and enhance things. Furthermore because the expectation with links is that you’ll go somewhere and modal dialogues are kinda like faux pages, the link feels appropriate.
While not explicit in the article, another thing I take from this is that by structuring your no-JavaScript experience well, this will help you make appropriate decisions when considering the with-JavaScript experience. There’s a kind of virtuous circle there.
Accessible modal dialogues in 2019
I previously noted Keith J Grant’s article on the HTML dialog
element which promised a native means of handling popups and modal dialogues. I’ve not yet used dialog
in production, partly because of spotty browser support (although there is a polyfill) but also partly because—for reasons I couldn’t quite put my finger on after reading the spec—it just didn’t feel like the finished article.
Fast forward to March 2019 and Scott O’Hara has written an excellent piece describing why it’s still not ready. There are a combination of factors: lack of browser support, reliance on JavaScript; and multiple accessibility issues.
The good news is: having dug deeper into Scott’s work I see that among the many accessible components he has shared with the world is a custom, accessible modal dialogue. So next time the need arises, I think I’ll start there.
Thanks, Scott!
Meet the New Dialog Element
Introducing dialog
: a new, easier, standards-based means of rendering a popup or modal dialogue.
The new element can be styled via CSS and comes with Javascript methods to show and close a dialog. We can also listen for and react to the show and close events.
Although currently only supported in Chrome, the Google Chrome dev team have provided a polyfill which patches support in all modern browsers and back to IE9.
See all tags.