Building accessible tabs and segmented controls in React

November 14, 2026 · 8 min read

Tabs look simple, which is why so many are broken. A row of buttons that changes some content works with a mouse, but without the right structure a keyboard user has to press Tab through every item, and a screen reader user hears a list of unrelated buttons. The fix is a small, well-defined pattern. Once you know it, adding a sliding highlight on top is the easy part.

First, decide what you are building

The words "tabs" and "segmented control" get mixed up, but they behave differently for assistive technology:

  • Tabs switch between panels of content on the same page. They use the tablist, tab and tabpanel roles.
  • A segmented control chooses one option from a small set, like list or grid view. It is closer to a radio group and uses role="radiogroup" with role="radio" items.

If the selection swaps a region of the page, build tabs. If it changes a setting or a view mode, build a radio group. This guide covers tabs, and the same keyboard behaviour applies to both.

The roles and attributes

The container gets role="tablist". Each button gets role="tab" and aria-selected set to true for the active one. Each panel gets role="tabpanel", and aria-controls on the tab points to the panel's id. That is enough for a screen reader to announce "Projects, tab, 1 of 3, selected".

Roving tabindex

Only the active tab should be reachable with the Tab key. The others get tabIndex of -1. This is called a roving tabindex. It means a keyboard user passes the whole tab list with one Tab press, lands on the active tab, and then uses the arrow keys to move within it. After the tab list, the next Tab press goes straight to the panel content.

Keyboard behaviour

  • ArrowRight moves to the next tab and wraps from the last to the first.
  • ArrowLeft moves to the previous tab and wraps from the first to the last.
  • Home jumps to the first tab and End jumps to the last.
  • Focus moves with the selection when panels are cheap to render. If switching is slow, move focus first and activate on Enter or Space instead.
tabs.tsx
"use client"

import { useRef, useState } from "react"

export function Tabs({ tabs }: { tabs: string[] }) {
  const [active, setActive] = useState(0)
  const refs = useRef<(HTMLButtonElement | null)[]>([])

  function onKeyDown(e: React.KeyboardEvent, i: number) {
    const last = tabs.length - 1
    const next =
      e.key === "ArrowRight" ? (i === last ? 0 : i + 1)
      : e.key === "ArrowLeft" ? (i === 0 ? last : i - 1)
      : e.key === "Home" ? 0
      : e.key === "End" ? last
      : null
    if (next === null) return
    e.preventDefault()
    setActive(next)
    refs.current[next]?.focus()
  }

  return (
    <div role="tablist">
      {tabs.map((t, i) => (
        <button
          key={t}
          ref={(el) => { refs.current[i] = el }}
          role="tab"
          aria-selected={i === active}
          tabIndex={i === active ? 0 : -1}
          onClick={() => setActive(i)}
          onKeyDown={(e) => onKeyDown(e, i)}
        >
          {t}
        </button>
      ))}
    </div>
  )
}

Adding the sliding pill

The moving highlight behind the active tab is a shared layout animation. Render the pill only inside the active tab and give it a layoutId. When the active tab changes, Motion sees that an element with the same layoutId disappeared in one place and appeared in another, and animates between the two positions with a spring.

pill.tsx
import { useId } from "react"
import { motion } from "motion/react"

// inside each tab button:
{active && (
  <motion.span
    layoutId={"pill-" + useId()}
    className="absolute inset-0 rounded-full bg-white shadow"
    transition={{ type: "spring", stiffness: 500, damping: 36 }}
  />
)}

Call useId once at the top of the component, not inside the map, and reuse the value. The unique id matters: if you render two tab groups on the same page with the same layoutId, the pill will fly between them.

Test it without a mouse

Put the mouse away and use only the keyboard. Tab should land on the active tab and skip the rest. Arrow keys should move and wrap. Then turn on a screen reader such as VoiceOver or NVDA and listen to how it announces the control. Finally, check the focus ring: it should be clearly visible on the tab, not hidden by the pill.

The Segmented Tabs component on makeui follows this pattern exactly, so you can copy it and read the source as a worked example.

More guides