January 31 2025
Keyboard Navigation and Focus
How to design keyboard navigation and focus flow that stay clear and reliable across the interface.
Andrews Ribeiro
Founder & Engineer
5 min Intermediate Frontend
The problem
A lot of interfaces look finished and still fail the first honest test:
- navigate only with the keyboard
- clearly see where focus is
- open and close components without getting lost
When that breaks, the experience is not just “less accessible.”
It becomes confusing.
The user presses Tab and focus disappears. Opens a modal and keeps navigating the buttons behind it. Closes the overlay and lands in some random place on the screen. All of that makes the product feel disorganized even when the visual design is polished.
Mental model
Think about it like this:
Focus is the interface’s navigation cursor for people who are not using a mouse.
If that cursor is invisible, disordered, or trapped in the wrong place, the person loses orientation.
Keyboard and focus are not a side feature of navigation. In many flows, they are the navigation.
So the better question is not:
- “does this screen work with a keyboard?”
The better question is:
- “does this screen stay predictable when the keyboard becomes the main way to move through it?”
Breaking it down
Focus order needs to follow the logic of the screen
Focus should move in a way that feels natural.
In practice, that usually means:
- following the visual and structural order of the interface
- not jumping into unrelated areas
- not forcing the person to cross irrelevant elements just to reach what matters
When DOM order and experience order fight each other, keyboard navigation reveals it quickly.
Visible focus is not a cosmetic detail
Some teams remove outline because they think it looks ugly.
That is almost always a mistake.
If you do not want the browser’s default style, that is fine. But you still need to replace it with another strong, visible, and consistent indicator.
Without that, the user is navigating in the dark.
Overlay components need to govern focus
Modals, drawers, menus, and popovers all change the interaction context.
So they also need to manage focus:
- when they open, focus should enter the new context
- while they are open, navigation should stay inside that context when that makes sense
- when they close, focus should return to the element that started the action
If you ignore that cycle, the interface starts feeling broken underneath.
A native element solves half the problem before JavaScript
This connects directly to semantics.
A real button already accepts focus, responds to keyboard input, and joins the navigation flow the way users expect.
A styled div does not.
When the team starts from the wrong element, it usually ends up rebuilding basic behavior with patches.
Simple example
Imagine a “Delete” button that opens a confirmation modal.
Broken flow:
- the user presses
Enteron the button - the modal opens visually
- focus stays on the background button or disappears
Tabstarts walking through elements outside the modal- when the modal closes, focus returns to an unpredictable place
Healthy flow:
- the user presses
Enteron the button - the modal opens
- focus moves to the first useful element inside the modal
Tabnavigates inside the modal contextEscor the close button ends the interaction- focus returns to the “Delete” button
That second flow is not polish.
It is what keeps the interface understandable during a context switch.
Common mistakes
- removing
outlineand putting nothing in its place - testing interactive components only with the mouse
- opening a modal without moving focus inside it
- closing a modal without returning focus to the origin
- building a button with
divand trying to fix it later withonKeyDown - using
tabIndexas a shortcut for bad structure instead of fixing the base
How a senior thinks
More experienced engineers treat keyboard and focus as part of the interface contract.
Not as late polishing.
The question changes from:
- “we will think about accessibility later”
to:
- “what is the navigation path of this screen when the user is not using a mouse?”
That changes how you build components, review PRs, and test flows.
A stronger engineer also knows that bad focus behavior can be hard to notice from the developer’s own context and immediately obvious for someone who depends on keyboard navigation. That is why they validate early, prefer native elements whenever possible, and treat context changes more carefully.
What the interviewer wants to see
When this topic shows up in interviews, the interviewer usually does not want a memorized list of attributes.
They want to see whether you understand:
- that keyboard navigation is a main flow
- that focus must be visible and predictable
- that overlay components need to control focus entry and exit
- that correct semantics removes a large part of the problem before any patching
A strong answer often sounds like this:
I think about focus as the current position of navigation. If it becomes invisible or moves to places without logic, the interface already lost predictability. In modals, menus, and drawers, I manage exactly where focus enters, how it moves, and where it returns when the context closes.
A good interface does not work only on click. It keeps making sense when the keyboard takes over.
When focus behaves well, the interface feels solid. When it behaves badly, it feels broken even without a visual bug.
Quick summary
What to keep in your head
- Keyboard and focus are not optional refinement. They are part of the main navigation model of the interface.
- If focus order does not follow the logic of the screen, the user loses context even when the layout looks good.
- Modal, menu, and interactive components need to control where focus enters, how it stays, and where it returns.
- In interviews, strong answers show that you think about navigation predictability, not just isolated attributes.
Practice checklist
Use this when you answer
- Can I validate a whole screen using only `Tab`, `Shift+Tab`, `Enter`, `Space`, and `Esc`?
- Can I explain when focus should move into a modal and where it should return when the modal closes?
- Can I point out why removing `outline` without replacing it breaks the interface?
- Can I tell the difference between a semantics problem, a focus problem, and a navigation-order problem?
You finished this article
Share this page
Copy the link manually from the field below.