DEV Community

Cover image for A11y tips: how to manage focus inside modals
Carlos Espada
Carlos Espada

Posted on

2

A11y tips: how to manage focus inside modals

To handle the focus in a modal in an accessible way, the same steps must always be followed, no matter how complex the modal that we are showing is:

  1. We show the modal when the user activates the button that opens it
  2. We immediately bring the focus to the button to close the modal, or in case to its first <input> if it had them. Never to an element other than these two.
  3. From there, the user has to be able to handle the focus in a circular way through the elements of the modal without leaving them. Once you reach the last element, you have to start over with the first one, the focus cannot go to elements that are behind the modal since the user would lose the reference of where they are.
  4. To exit the modal, the user can click the close button or the Esc key, which we will make it work by hiding the modal.
  5. Once closed, the focus must return to the place where the user left the navigation before displaying the modal, that is, to the button that opened it.

On the W3C website there are examples of how to do this, and Scott O'Hara has written a very good article that explains in detail how to make modals accessible.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay