aster.UI / Docs
Aster products
Showcase ↗

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:

Keep the ring in these cases and make the first focusable element a good first stop:

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.