Skip to content
Inspireca Design

Interactive Example · Tabs

Tabs with a sliding indicator

A marker that slides to the selected tab shows where you came from and where you are, with full arrow-key support.

Written by Inspireca Design (drafted with AI assistance)Updated Not yet reviewed by an editor

Try it

Use it with a mouse, by touch or with the keyboard. The controls below show its other states.

A summary of the project: goal, owner and current status.
Preview controls

How it works

When the marker jumps from one tab to another, you have to find it again. When it slides, your eye follows it, and the movement itself tells you which direction you went.

What moves

There is one marker, exactly one third of the bar wide. Selecting a tab moves it by whole widths with a transform, over 200 milliseconds. Use the Indicator control to switch between a pill behind the label and a line under it; the mechanism is the same. The panel below fades in.

Why a single moving marker

The alternative, fading one highlight out and another in, needs no positioning, but it loses the sense of direction. Moving one element with a transform is also cheap for the browser, because nothing else has to be laid out again.

Limits

This version assumes tabs of equal width. If your labels differ a lot in length, the marker has to be measured and resized, which is more work and worth it only if equal widths look wrong. More than about five tabs on a phone is a sign to use a different pattern.

Accessibility notes

  • Roles: tablist, tab and tabpanel, with aria-selected and aria-controls, following the Tabs pattern in the WAI-ARIA Authoring Practices.
  • Keyboard: Tab moves into the tab bar and then on to the panel. Left and Right arrows move between tabs and select them; Home and End jump to the first and last.
  • One stop in the Tab order. Only the selected tab can be reached with Tab; the arrows reach the others.
  • Selected is not shown by colour alone: the marker's position and shape show it too.
  • Reduced motion: the marker moves at once and the panel does not fade.

The code

This is the source of the preview above: React with Tailwind CSS classes (sliding-tabs.tsx). Colour names such as bg-primary and the ex- animation classes come from this site’s own stylesheet; replace them with your own.

"use client";

import { useId, useRef, useState } from "react";
import type { DemoProps } from "@/lib/examples/keys";

const TABS = [
  { name: "Overview", text: "A summary of the project: goal, owner and current status." },
  { name: "Activity", text: "The latest changes, newest first." },
  { name: "Files", text: "Everything attached to the project." },
];

/**
 * Tabs whose marker slides to the selected tab. The marker is one element moved with a
 * transform (cheap to animate). Left and Right arrows move between tabs, Home and End jump.
 */
export default function SlidingTabs({ values }: DemoProps) {
  const id = useId();
  const [active, setActive] = useState(0);
  const refs = useRef<(HTMLButtonElement | null)[]>([]);
  const line = values.indicator === "line";

  const go = (index: number) => {
    const next = (index + TABS.length) % TABS.length;
    setActive(next);
    refs.current[next]?.focus();
  };
  const onKeyDown = (e: React.KeyboardEvent) => {
    if (e.key === "ArrowRight") { e.preventDefault(); go(active + 1); }
    else if (e.key === "ArrowLeft") { e.preventDefault(); go(active - 1); }
    else if (e.key === "Home") { e.preventDefault(); go(0); }
    else if (e.key === "End") { e.preventDefault(); go(TABS.length - 1); }
  };

  return (
    <div className="w-full max-w-sm">
      <div role="tablist" aria-label="Project sections" onKeyDown={onKeyDown}
        className={`relative grid grid-cols-3 ${line ? "border-b border-border" : "rounded-xl bg-surface p-1"}`}>
        {/* The marker: one third wide, moved by whole widths. */}
        <span aria-hidden
          style={{ transform: `translateX(${active * 100}%)` }}
          className={`pointer-events-none absolute left-0 w-1/3 transition-transform duration-200 ease-out motion-reduce:transition-none ${
            line ? "bottom-0 h-0.5 bg-foreground" : "inset-y-0 p-1"}`}>
          {!line && <span className="block h-full rounded-lg bg-background shadow-[0_1px_3px_rgb(0_0_0/0.12)]" />}
        </span>
        {TABS.map((tab, i) => (
          <button key={tab.name} ref={(el) => { refs.current[i] = el; }} type="button" role="tab"
            id={`${id}-tab-${i}`} aria-selected={i === active} aria-controls={`${id}-panel-${i}`}
            // Only the selected tab is in the Tab order; arrows reach the others.
            tabIndex={i === active ? 0 : -1}
            onClick={() => setActive(i)}
            className={`relative z-10 h-11 rounded-lg text-sm font-medium outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-foreground motion-reduce:transition-none ${i === active ? "text-foreground" : "text-muted-foreground hover:text-foreground"}`}>
            {tab.name}
          </button>
        ))}
      </div>
      {TABS.map((tab, i) => (
        <div key={tab.name} role="tabpanel" id={`${id}-panel-${i}`} aria-labelledby={`${id}-tab-${i}`} hidden={i !== active} tabIndex={0}
          className="ex-fade-in mt-4 min-h-12 rounded-lg text-left text-sm text-muted-foreground outline-none focus-visible:ring-2 focus-visible:ring-foreground">
          {tab.text}
        </div>
      ))}
    </div>
  );
}

Prompt to recreate it

Replace everything in [SQUARE BRACKETS]. Treat the result as a first draft and test it with a keyboard.

Build an accessible tabs component in [FRAMEWORK] with [NUMBER] tabs of equal width: [TAB NAMES].

- Use role="tablist", role="tab" and role="tabpanel" with aria-selected, aria-controls and aria-labelledby.
- Keyboard: Left/Right arrows move and select, Home/End jump, only the selected tab has tabindex 0.
- Show the selected tab with ONE marker element that slides to it using transform: translateX, 200 ms ease-out. Marker style: [PILL BEHIND THE LABEL / LINE UNDER THE LABEL].
- Do not animate width, left or any layout property.
- With prefers-reduced-motion, move the marker without a transition.
- Each tab at least 44 px tall.

Use these tokens: [COLOURS, RADIUS]. Tell me what you would change if the tabs had different widths.

See it in real apps

Still screenshots from the Inspireca Design library of the kinds of screen where this interaction is used. A screenshot cannot show the motion itself.

  • Interactive Examples

    Card that expands in place

    A summary card that opens to show its details without leaving the page, built on a real button and a height animation that needs no measuring.

  • Interactive Examples

    Button press, loading and success feedback

    A button that dips when pressed, shows a spinner while it works and confirms when it is done, with words that carry the meaning at every step.

  • Interactive Examples

    Link underline that appears on hover and focus

    A hover effect for navigation links that works just as well for keyboard users, and moves nothing else on the page.

  • Interactive Examples

    Skeleton loading placeholder

    Grey shapes hold the place of content while it loads, in the same layout, so nothing jumps when the real content arrives.