As users navigate around using only the keyboard, focus rings provide a necessary clue as to the currently active item.
Keyboard shortcuts are another curb cut example. Sites like Twitter and Facebook offer keyboard shortcuts for almost any action which are great for both keyboard only users and and power users!
Skip links help users skip over large headers and navigation and jump right into the "main" content of your site. When a user hits tab for the first time, a button will appear and offer users to jump right to the main section.
You can use the tab key to navigate to the next tabbable item and shift + tab to navigate to the previous item.
<a>
For a list of all focusable elements check out Focusable.
You can add the tabindex attribute to any element like this:
<div tabindex="0">I'm focusable</div>
I would like to hear more about the maximum value for tabindex in HTML.https://t.co/WtKxdMWgiw pic.twitter.com/Z7L9dRctTI
— Jon Kuperman (@jkup) April 25, 2021
Sometimes, especially on single page applications, it's helpful to store the currently focused element before a page transition so you can return to it later.
// A modal is about to be opened
// Store the current news item
const currentItem = document.activeElement;
// Open the modal
// On modal close, refocus on the news item they had open
currentItem.focus()
Another useful concept is tab trapping. Consider opening a modal on a page which contains a form. A keyboard only user will want to tab around the form but unless we help, tabbing while focused on the last form element will send us all the way back to the main document.
for an example modal.
Let's practice making skip links together in exercise 4.