Focus management principles
Learn about principles of accessible focus management, potential challenges and examples.
Learn about principles of accessible focus management, potential challenges and examples.
Accessible focus management is the practice of using programmatic focus changes to enhance comprehension and usability of a website. Cloudscape uses focus management to streamline tasks and direct attention to important areas of a document. Focus management benefits a variety of users including those using assistive technology (AT) such as a screen reader, those using a keyboard to navigate, and those who may have increased the zoom of their browser to make reading more comfortable.
Focus management is used to redirect the user’s attention and to make keyboard navigation more efficient.
Screen readers can only announce one item at a time. When an event requires a user’s attention outside of the focused element, we can redirect their attention by programmatically setting focus onto the desired component.
Some user actions or events require moving their focus to navigate efficiently. This often means using javascript to provide alternate models of navigation, such as enabling arrow keys to navigate between tabs in a tab list. Sometimes a user interaction indicates an intention to interact with specific content. For example, clicking a button that opens a modal requires setting focus into the newly open modal. The occurrence of a post-submit error requires moving focus to the first element in error to aid remediation.
In order to design for accessible focus management it is important to understand some basics of how users might read and navigate a page. There are two types of focus:
Browser focus refers to the browsers’ sense of the current active page element, for example a form field or link. This is the element that is referenced by document.activeElement and which the :focus CSS selector will apply to.
Screen reader “focus” (also known as the “Virtual Cursor”) refers to the element that is currently being read by a screen reader. This focus is actually a queue of announcements managed by the screen reader. This may be the same as browser focus (in the case of interactive elements), or it may be different (for example: the browser focus is on an element, but paragraph below it is being read from the screen reader focus). In the case of users navigating non-visually with a screen reader, it is important to note that the users’ attention will only be on the element that has screen reader focus and changes that occur outside of this element will not be noticed.
Browser focus can be moved to interactive items such as inputs and links using the tab key and shortcuts provided by assistive technology and/or the browser. Note: Mac browsers sometimes need to have tab-to-focus enabled. Screen reader focus can change as a user utilizes reading commands such as using arrow keys to move through text, or using a keyboard shortcut to jump to the next heading.
When browser focus is moved programmatically by calling focus() on an element, screen reader “focus” will be placed on that element as well.
When a modal opens, focus is set into the element with role=“dialog”. By default, focus is set onto the close button because its the first interactive item.
When focus is first placed into the dialog, the user hears the accessible name of the dialog. This is automatically derived from the contents of the component’s “header” prop, which is placed into an H2 that has been associated to the dialog element with aria-labelledby. All other elements in the document outside of the modal are hidden from assistive technology and focus is trapped within the modal so that a user does not accidentally end up “behind” the open modal. The modal in Cloudscape automatically handles all of the focus requirements for opening a modal.
When the dialog is closed, reorient the user by returning their focus back to the button that launched the dialog. The next focusable element in the DOM order after the button is usually best if that button no longer exists.
What Cloudscape handles:
The Modal component automatically sets focus back on the element that opened it.
Implementation considerations:
If the original button no longer exists, the focus must be set programmatically as a call-back. It is recommended to set it on the element that followed the original button in the tab order.
It is important to make the user aware of a message in an alert or flashbar so that they are able to easily act on it. Alerts and flashbars in Cloudscape persist to ensure the user has ample time to interact with them, and move focus to make sure the user is aware of the alert. It is recommended to not rely solely on aria-live for important announcements as users who may be highly-zoomed do not have access to live announcements.
Consider the balance between interrupting the user and ensuring they have the information they need. Follow the recommendations below when an alert or flashbar appears:
Success, error, and warning
Set focus on the element that contains the icon and the header and/or content.
Info, in Progress, progress bar
Do not move focus, use a live region component to announce the message.
What Cloudscape handles:
The flashbar component automatically handles all of the focus requirements for flash messages if you provide a value for ariaRole. For alerts, use the focus ref function to set focus on the alert content.
Implementation considerations:
Ensure focus lands on an element that is logical and helpful for the user when an alert or flashbar is dismissed.
If there are more flashbars or alerts appearing, focus on the next one in the list (or the last one if there are no more).
If there are no more flashbars or alerts, then set focus to the top of the page or on the page’s H1.
The attribute editor in Cloudscape is an illustrative example of using focus to show outcome, and what to do when a button disappears.
When “Add new item” is activated
Set focus into the first newly created field.
When “Remove” is activated
Set focus on the next attribute input in the list, or the last one if there is only one remaining. If the user has just removed the last item, set focus onto the “Add new item” button.
Implementation considerations:
The attribute editor does not handle any focus automatically. Implement the focus management described above.