Like before, the first thing we want to do is audit our component with a screen reader.
The modal button
When a screen reader lands on the modal button, it says âClick meâ.
âClick me!â is the accessible name for the button. But âClick meâ is not helpful for screen readers because they donât know what te expect after clicking the button.
We want to give screen reader users some context. In this case, since the button opens a login modal, letâs say âOpen login formâ.
When the user opens the modal, we bring their focus to the first input field. If you use Voiceover, you should hear âEmail, edit text, [email protected]â.
âEmailâ comes from the label.
âEdit textâ because the user focused on an <input> element.
The SVG is the âone more itemâ Voiceover mentioned. Unfortunately, setting aria-hidden="true" on the SVG doesnât remove âand one more itemâ from Voiceover.
I had to change the HTML and CSS to fix the password field.
The CSS is given to you in the starter file for this lesson. Hereâs the HTML:
What I did was to use a label the <input> with a for attribute instead of wrapping the <input> with a <label>.
Note: Please change the HTML for the email field too.
With this change, Voiceover reads â8 characters, Password, secure edit text, bullet bullet bullet.â (No more of that "and one more item nonsense).
Note: NVDA doesnât have a problem with display: flex on <span>. This section here is purely a Voiceover fix.
The close button
Continue tabbing in the modal. When you reach the close button, youâll notice it doesnât have an accessible name. Screen readers simply say âbuttonâ.
We need to give the button an accessible name. In this case, letâs use âClose login formâ.
Weâll set the accessible name with aria-label since the words âClose login formâ is not on the page.
Users should not be able interact with things outside of a modal when the modal is opened.
We have already prevent normal keyboard users from moving out by trapping focus in the modal. Screen reader users, however, can still move out of the modal with their shortcuts.
For example, if you use Voiceover, you can:
use VO + Shift + â exit the modal
then VO + â to go <main>
then VO + Shift + â to go to the Open login form" button.
To fix this issue, we can add aria-modal="true" to the modal.
Now Voiceover users canât move out of the modal.
Hear the tng sound? Thatâs Voiceoverâs way of say âno can doâ.
Note: I noticed NVDA users canât move out of the modal when we added the dialogue role. This seems to be a bug because there are non-modal dialogues out there too.
Fallback for aria-modal
We need to add a fallback for screen readers that donât support aria-modal.
We can do this by adding aria-hidden to other elements when the modal is opened. In this case, <main> is the only other element.