Ensuring your Users are on the Right Path: the Future of Modals and Focus Management

Rate this content
Bookmark

With *dialog* and the inert attribute landing in all major browsers in 2022, we as web developers now have simple yet powerful primitives to help build complex app-like flows on the web, rather than the over-engineered or leaky solutions we've relied on for years. Let's demystify these primitives and talk through how they make your code simpler: from plain HTML, Web Components, to React/similar.

This talk has been presented at JSNation 2022, check out the latest edition of this JavaScript Conference.

FAQ

Focus management involves controlling the focus flow within a user interface, ensuring that users can interact with elements in a logical and intuitive order. It's important because it enhances accessibility, prevents users from interacting with hidden elements behind modals, and improves overall user experience.

The dialogue element is an HTML component that allows creation of modal dialogues. It improves focus management by trapping focus within the dialogue while it's visible, preventing interaction with elements in the background and solving common issues with UI layering.

Key features include trapping focus within the dialogue, rendering in the top layer regardless of other CSS contexts, and built-in handling of the escape key to close the dialogue. It also supports custom styling and can be integrated seamlessly into web applications.

The 'inert' attribute is a new browser feature that makes any subtree of the DOM non-interactive, meaning users cannot click, focus, or interact with any element marked as inert. This is useful for disabling parts of the UI dynamically, such as during form submission or loading states.

The 'field set' element groups related form elements and can manage their interaction state collectively. By setting the 'disabled' attribute on a field set, all form controls within it become non-interactive, which is useful for preventing user input during processes like form validation or submission.

As of 2022, browsers have enhanced support for built-in primitives like the dialogue element and the inert attribute, allowing developers to manage focus more effectively and create more accessible modal experiences without relying on third-party libraries or complex hacks.

Modern HTML primitives for focus management, such as the dialogue and inert attributes, allow developers to discard old, hacky code and improve application performance and user experience. They provide native browser support for common UI challenges, making web applications more robust and accessible.

No, once the inert attribute is applied to a DOM subtree, it cannot be selectively removed from parts of that subtree. The entire subtree remains non-interactive unless the inert attribute is removed from the root of that subtree.

Sam Thorogood
Sam Thorogood
17 min
20 Jun, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk discusses the new features of Modals and focus management in web development. The dialogue element allows for the creation of modal dialogs that appear on top of other elements and prevent interaction with elements below. The note element can mark a subtree of the DOM as inert, making it unusable. The field set component can group input elements together and disable them. Using these new primitives can improve focus control and user experience in web applications.

1. Focus Management and the Dialogue Element

Short description:

Hey there, my name is Sam, and I'm here today to talk about ensuring your users are on the right path, the futures of Modals, and focus management. The web has come a long way, and for the first time this year, all the evergreens are supporting these amazing new features that we can use as either primitives or as part of some greater library. So firstly, what is focus management, and why do you care? Now, I feel like this is the quintessential reason of why it's important. The things I want to talk about today are dialog and a note. These are the two new concepts that are actually available on browsers this year, and they're both quite cool. Before we go any further, who is this guy, why am I talking to you? I'm a former Googler, I was there for over 10 years, it was obviously really fun, I was on the developer relations team, and so part of my job was to do this kind of outreach. Our technology is still on the web, and so we want to use these features even in our SaaS product to make our user experiences better. And my general vibe of why I'm coming here and talking about these concepts in general, and why I want this in my browser, I want this in my web app, is that to me, these new features are letting us throw away tons of old code that did weird hacks and horrible things to browsers that really they weren't designed to do. So having new primitives in 2022 is really lovely, I can get rid of all this code and get on with the more interesting parts of my job. The first component I'm gonna talk about today is called the dialogue element. Now, dialogue can do a whole bunch of stuff, the spec is pretty wide. But the reason why we most care about it as web developers is I can create this element on my page, wherever I like, and then call showModal in JavaScript. What this does is then creates that dialogue in what's called a top layer. Now the top layer is special, it does two things for us. One is that I can't tap behind it, this focus problem has been solved, while it's visible, I can't interact with anything below it including other dialogues that I previously opened. The second thing that's really cool here is that it actually exists outside the browser's kind of normal rendering layer. It's in a thing called the top layer, and what that means is even if you're inside some weird stacking context or your Z indexes are all bizarre, then actually the dialogue will always be on top. I actually like to demonstrate this by, if you can see here, I've got this weird element that's transformed in bizarre ways. If I click this button, the dialogue is actually inside this transformed element, but the browser's just gonna ignore all of that and hoist it to the top. So in that way, it's quite cool. It's scoped and encapsulated in a way that you can use it in a component or some library and it will still appear right at the top, ready for the user to interact with it.

♪♪ ♪♪ ♪♪ ♪♪ Hey there, my name is Sam, and I'm here today to talk about ensuring your users are on the right path, the futures of Modals, and focus management. Now, that's a handful, and I submitted this talk to JSNation a while ago, and since then, I think what it should be called is basically focus management in 2022.

Now, the web has come a long way, and actually, for the first time this year, all the evergreens are supporting these amazing new features that we can use as either primitives or as part of some greater library.

So firstly, what is focus management, and why do you care? Now, I feel like this is the quintessential reason of why it's important. Now, we've got dialog here. I can click around, I can hit submit, but what I can actually do while this is out operating is press Tab a bunch of times and get behind this UI. I can still click some buttons that are on the page here. Now, this is a demo, but lots of sites have this problem if you look around, and the challenge is that it's just really hard, and for the longest time, we've got libraries and polyfills that try to really maintain this, to try to solve this problem for you, but either they're not used correctly, they just can't do what you want them to do, or people just forget, and they just use a really basic mechanism and hope that users will never get there, and this example is obviously pretty tricky, not everyone knows how to tab behind UI elements, but this kind of problem will leak out in other ways, causing users some weird behavior, and some examples here.

To be very precise, the things I want to talk about today are dialog and a note. Now, these are the two new concepts that are actually available on browsers this year, and they're both quite cool. They're actually quite old in terms of specs, they were written a long time ago, but as I've mentioned, it's the first time that you can use them in pretty much all evergreens with no hassle.

So, before we go any further, who is this guy, why am I talking to you? I'm a former Googler, I was there for over 10 years, it was obviously really fun, I was on the developer relations team, and so part of my job was to do this kind of outreach. I've actually recently left Google and gone to a small early stage startup in the green energy space called Gridcog, it's a lot of fun, I would highly recommend working in climate, it's an important thing, and I'm always happy to talk to people about that, orthogonally to the content of this talk.

Having said that, our technology is still on the web, and so we want to use these features even in our SaaS product to make our user experiences better. And my general vibe of why I'm coming here and talking about these concepts in general, and why I want this in my browser, I want this in my web app, is that to me, these new features are letting us throw away tons of old code that did weird hacks and horrible things to browsers that really they weren't designed to do. So having new primitives in 2022 is really lovely, I can get rid of all this code and get on with the more interesting parts of my job. Not dealing with focus management, but actually writing applications that do interesting things.

The first component I'm gonna talk about today is called the dialogue element. Now, dialogue can do a whole bunch of stuff, the spec is pretty wide. But the reason why we most care about it as web developers is I can create this element on my page, wherever I like, and then call showModal in JavaScript. What this does is then creates that dialogue in what's called a top layer. Now the top layer is special, it does two things for us. One is that I can't tap behind it, this focus problem has been solved, while it's visible, I can't interact with anything below it including other dialogues that I previously opened. The second thing that's really cool here is that it actually exists outside the browser's kind of normal rendering layer. It's in a thing called the top layer, and what that means is even if you're inside some weird stacking context or your Z indexes are all bizarre, then actually the dialogue will always be on top. I actually like to demonstrate this by, if you can see here, I've got this weird element that's transformed in bizarre ways. If I click this button, the dialogue is actually inside this transformed element, but the browser's just gonna ignore all of that and hoist it to the top. So in that way, it's quite cool. It's scoped and encapsulated in a way that you can use it in a component or some library and it will still appear right at the top, ready for the user to interact with it.

Now, that's the most interesting part of the dialogue for me, but there's a few other things I wanna cover before we go on.

2. Backdrop Element and Escape Key Behavior

Short description:

One of the interesting aspects of the dialogue element is the backdrop element, which provides a full-screen element for every dialogue. It also includes a built-in handler for the escape key. While this behavior may seem strange, it dates back to when it was created in 2014. If you want to prevent the dialogue from closing when the escape key is pressed, you can add an event handler for the Cancel event.

One of them is this backdrop element, and so every dialogue for free will have like a full screen element. You can change its color or background or whatever. It also has a built-in handler for the escape key. Now this is kinda bizarre but kinda reflects when it was created back in 2014, but if I can hit escape on my keyboard now, then this dialogue will close, and that's fine, but a bit weird because there's no analogy on mobile or other platforms. You know, you can't press your back button on your mobile phone to close it. That will still, unfortunately, go to the previous page. There is something coming on the pipeline called Close Watcher as a bit of an aside, which is totally in draft state at this point, which does help deal with things like closing dialogues, and in fact, it's one of the examples called out in its description, but who knows if that'll come about. My advice, honestly, is if you don't want this behavior, you can actually add an event handler for the Cancel event, and then the user hitting Escape will do nothing, right? And you can control that keyboard interaction, or maybe you close the dialog with buttons. You can do that entirely yourself.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Less Cruft, More Power: Leverage the Power of the Web Platform
C3 Dev Festival 2024C3 Dev Festival 2024
30 min
Less Cruft, More Power: Leverage the Power of the Web Platform
This talk focuses on the powerful features of CSS and HTML that can level up developer skills and enhance web UI. Scroll-driven animations, popover API, and anchor positioning are explored as ways to create dynamic effects, improve performance, and increase accessibility. The talk also emphasizes the benefits of building presentations with CSS and HTML, separating logic from styling, and leveraging core platform features. Wishlist features for the web platform and the challenges of pushing updates across browsers are discussed.
Asynchronous UX
React Advanced Conference 2021React Advanced Conference 2021
21 min
Asynchronous UX
Top Content
Today's Talk covers the importance of building Asynchronous UX with React and single-page applications, providing code and UX examples. It explores data fetching, adding progress indicators, handling errors, and user-initiated actions. The Talk also discusses handling component unmounts, multiple actions, idempotency, and context loss. Finally, it touches on considerations for optimistic updates and the use of CRDT or other technologies for collaborative applications.
React Native Animations Should Be Fun
React Advanced Conference 2022React Advanced Conference 2022
28 min
React Native Animations Should Be Fun
This talk is about animations in React Native, specifically focusing on React Native Reanimated. It covers the use of interpolations and extrapolations in animations, animating items in a carousel or list, sticky elements and interpolation, and fluidity and layout animations. The talk provides valuable tips and tricks for creating performant animations and explores the use of math formulas for natural movements.
Game Development with ReactJS, CSS, and React Three Fiber
JS GameDev Summit 2023JS GameDev Summit 2023
22 min
Game Development with ReactJS, CSS, and React Three Fiber
Jorge Rubiano, a Software Engineer from Colombia, shares puzzle, isometric, and board games developed using React.js and CSS. He demonstrates the use of WebGL and 3.js for creating 3D games on the web. ReactiveFiber, a renderer that combines React and 3.js, is showcased in movement-based and color-changing games. The Talk concludes with the development of a bowling game using ReactiveFiber and complex components.
Raygui: An Immediate-Mode C UI for Wasm Tools Development
JS GameDev Summit 2023JS GameDev Summit 2023
19 min
Raygui: An Immediate-Mode C UI for Wasm Tools Development
Welcome to the presentation of RightGui, an immediate mode CUI library for tools development. RightGui is a high-performance library that is stateless and uses small self-contained functions to process inputs and draw controls. It provides a variety of controls, customizable styles, and support tools for live style configuration. Additionally, there are tools like the icons editor and layout creator that allow for customizations and code generation. Reiki offers a variety of tools, including a sound editor, image manipulation canvas, and resource packer.
PrimeVue | The Next-Gen UI Component Library
Vue.js Live 2024Vue.js Live 2024
24 min
PrimeVue | The Next-Gen UI Component Library
Prime Vue is a comprehensive UI component suite with over 90 components, including date pickers, buttons, tables, and grids. It offers flexibility through styled and unstyled modes, allowing for customization using design tokens or Tailwind. Prime Vue is WCAG compliant and supports Material design. The upcoming version 4 introduces a new theming API using CSS variables, and it includes features like dark mode switching and integration with Figma. The team has plans to release a UI Designer, advanced components, and a drag-and-drop UI Builder in the future.

Workshops on related topic

Should we have business logic in the UI?
JSNation 2022JSNation 2022
148 min
Should we have business logic in the UI?
WorkshopFree
Samuel Pinto
Samuel Pinto
How many times did you say or hear “this is business logic, it should not be here”?In this workshop, we will create a modern frontend application using old patterns and you will learn how to build apps that have decoupled UI and services.We will start with a React application that has its whole logic in the UI. Then step by step we will extract the rules and operations to hit that sweet spot of independence.