How do I trap focus in a modal?
Question
How do I trap focus in a modal?
Answer
It is always recommended to use native elements when possible, such as the native dialog element. Then, the browser will handle focus and trap it within the modal. However, if you are using a custom modal element and want to trap keyboard focus, ensure that all modal elements are within a dialog container and that the dialog container has role="dialog" and aria-modal="true”.
To learn more about managing focus, check out Accessible Web's "Keyboard Navigation & Focus Management" course.