Aster UI / Guides
Manage focus and the focus ring
Aster draws a 2px outline in --aster-focus when the browser matches
:focus-visible.
Pressing Tab usually shows the ring, and moving focus from script can show it
too. A field's frame shows the ring whenever focus is inside it, including
after a click.
Choose where focus starts
When your application opens new content, move focus to the element the person
needs next. A heading can give screen reader users context before they reach
the controls: give it tabindex="-1" to keep it out of the Tab order, then call
.focus() once it has rendered. For a modal dialog, follow the
ARIA dialog pattern
and choose the first target from its content and the action it asks for.
Browsers can show the ring when script moves focus, including when a window first opens and the person has not clicked yet. A software update window, for example, focuses its title so the person reads it first. When the ring makes a title look interactive, remove it from that title only:
<h1 id="update-title" tabindex="-1">Update available</h1>
document.getElementById("update-title")?.focus();
@layer app {
#update-title[tabindex="-1"]:focus-visible { outline: none; }
}
Keep the ring on buttons, links, fields and scrollable regions that Tab reaches.
Focus moved by the browser
A ring can appear on a page whose script never calls .focus(), because the
browser moved focus:
- Pressing Tab after the page loads, including from the address bar, focuses the first focusable element, often a logo or home link. The person pressed a key, so the ring belongs there.
- Back and Forward can restore a page from the browser's cache with focus on the element the person last used.
- Browser extensions, and moving between developer tools and the page with the keyboard, can also focus an element.
Keep the ring in these cases and make the first focusable element a good first stop:
Give a link that wraps an image and text its own shape, so the ring follows the link:
@layer app { .brand { border-radius: 8px; padding: 4px 6px; margin: -4px -6px; } }Put a "Skip to content" link first in
<body>, visible only while it has focus, so the first Tab lands on something useful.
Find which element has focus
Run document.activeElement in the browser console, then think back to the
last input: a Tab, a key that reloaded the page, or Back. A text field,
select, picker or date field draws the ring on its frame whenever focus is
inside it, so document.activeElement names the focused input, combobox or
button within the frame; see
field focus rings.
Test a screen as it opens with both mouse and keyboard, then check where Tab and Shift+Tab move.
Where to go next
Compose fields and layouts covers the field controls
that own their ring. Themes describes --aster-focus and the
other colour tokens.