← ui

focus-source

ui

the focus ring for the keyboard only, never on what a click focused

npx shadcn add @ag/focus-source
Already installed? Preview an update
npx shadcn add @ag/focus-source --dry-run

Your installed source stays unchanged until you update it. How updates work

Latest changes · 2026-10-30
  • Fixed: the button you last clicked was ringed after you came back to the window or the tab, when a key had been your last input there (an arrow that scrolled the page). The browser focuses the same element again on a return and calls that focus visible; focus-source now keeps the mark it had instead of reading the return as new keyboard focus. Keys held with ⌘, ⌃ or ⌥ (⌃Tab, ⌘Tab, ⌘←) no longer count as moving focus.
Updated 2026-10-30 · release notes

also called focus-visible

shelf foundations

used by tree

the demo above, verbatim

focus-source.demo.tsx
"use client"

import { Sample } from "@/app/samples"
import { Button } from "@/registry/base-nova/ui/button"

export default function FocusSourceDemo() {
  return (
    <Sample
      name="source"
      glance
      title="the ring"
      description="answers the keyboard only"
    >
      <p className="mb-4 text-muted-foreground text-xs leading-5">
        Click a button, then press any keys: no ring. Press Tab, or arrow
        through them: the ring shows where you are. FocusSource is mounted once
        in this site's root layout.
      </p>
      <div className="flex flex-wrap gap-2">
        <Button variant="secondary">one</Button>
        <Button variant="secondary">two</Button>
        <Button variant="secondary">three</Button>
      </div>
    </Sample>
  )
}

the source, every file an install writes

focus-source.tsx
"use client"

// Where focus came from, so the focus ring (tokens) answers the keyboard and nothing
// else. Mount it once, in the root layout.
//
// The browser alone cannot keep that promise: a click gives a button focus (Chrome,
// Firefox), and the next key pressed anywhere (space, a player's J, an arrow on a
// page that listens to keys) is its cue that "a keyboard is in use", so the ring
// lands on whatever was last clicked. This says, on the document element, whether
// the focus the page holds now came from a press (`data-focus-pointer`), and the
// ring (the base rule and the `focus-visible:` variant, both in tokens) skips
// focus held under that mark. Focus that arrives by the keyboard's own navigation
// (Tab, an arrow moving through a group, Home, End, the page keys) clears it, so it
// is always ringed: the keyboard keeps every ring it should have. Without this
// mounted, the ring is the browser's `:focus-visible`, as before.
//
// The mark lives on `<html>`, never on the focused element, which React owns: a mark
// written onto an element of a Suspense boundary not hydrated yet was read by React
// as an attribute the server never rendered (a hydration mismatch). And it listens
// from the first moment of the page: `listen` is self-contained and runs inline while
// the page is parsed, so a press before the page hydrates is known too (a press then,
// a key after, rang the pressed button); an effect runs it where the component mounts
// on the client alone. It listens once per document, for as long as the document
// lives, whatever mounts it.

import * as React from "react"

/** The document's listeners, once. Self-contained: serialized into the page. */
function listen() {
  const root = document.documentElement
  const own = root as HTMLElement & { agFocusSource?: true }
  if (own.agFocusSource) return
  own.agFocusSource = true
  // Keys that move focus: after one of them, focus is the keyboard's.
  const navigation = [
    "Tab",
    "ArrowUp",
    "ArrowDown",
    "ArrowLeft",
    "ArrowRight",
    "Home",
    "End",
    "PageUp",
    "PageDown",
  ]
  let pointer = false
  // What held focus when the window or the tab lost it: coming back, the browser
  // focuses it again (focus, focusin) and calls that focus visible if a key was the
  // last input, an arrow that scrolled the page say. That is no new focus, so it keeps
  // the mark it had; marking it from the last input rang the button clicked before.
  let left: EventTarget | null = null
  const mark = (on: boolean) => root.toggleAttribute("data-focus-pointer", on)
  window.addEventListener("blur", () => {
    left = document.activeElement
  })
  document.addEventListener(
    "pointerdown",
    (e) => {
      pointer = true
      // A press on what already holds focus (reached by Tab, then clicked) moves no
      // focus, so no focusin marks it: mark it here.
      const held = document.activeElement
      if (held && e.target instanceof Node && held.contains(e.target))
        mark(true)
    },
    true,
  )
  document.addEventListener(
    "keydown",
    (e) => {
      // With ⌘, ⌃ or ⌥ held these are no focus navigation: ⌃Tab and ⌘Tab leave the
      // tab or the app, ⌘← goes back, ⌥↓ scrolls.
      if (e.metaKey || e.ctrlKey || e.altKey) return
      if (navigation.includes(e.key)) pointer = false
    },
    true,
  )
  document.addEventListener(
    "focusin",
    (e) => {
      const back = e.target === left
      left = null
      if (!back) mark(pointer)
    },
    true,
  )
}

const SCRIPT = `(${listen})()`

export function FocusSource() {
  React.useEffect(listen, [])
  // biome-ignore lint/security/noDangerouslySetInnerHtml: the module's own function, serialized, so it runs while the page is parsed
  return <script dangerouslySetInnerHTML={{ __html: SCRIPT }} />
}