NAVIGATION

Sliding Tabs.

Tabs with an indicator that slides to the selected tab and a lighter highlight that follows the pointer. Comes in pill and underline styles.

Same component, different example content.

acme-web

Production is healthy. The last deploy finished 12 minutes ago.

99.98% uptime184 ms median response3 open alerts
REACT + TYPESCRIPT + CSSMIT LICENSE

Installation

In a project with React and shadcn configured, run:

npx shadcn@latest add https://aduneer.github.io/opalframe/r/sliding-tabs.json

Copies the component and its stylesheet into components/ui. No additional runtime dependencies. Tailwind classes work through className.

For manual installation, copy both files from Source below. Keep the CSS import alongside the component.

Usage

import { SlidingTabs } from "@/components/ui/sliding-tabs";

export default function Example() {
  return (
    <SlidingTabs
      label="Project sections"
      tabs={[
        { id: "overview", label: "Overview", content: <p>Everything is running.</p> },
        { id: "activity", label: "Activity", content: <p>3 deploys today.</p> },
        { id: "settings", label: "Settings", content: <p>Region: Frankfurt</p> },
      ]}
    />
  );
}

Customize

Colors and sizes are scoped CSS variables. Override them on your own class.

<SlidingTabs tabs={tabs} variant="underline" className="your-tabs" />

.your-tabs {
  --tabs-accent: #a6cfff;
  --tabs-selected: #293449;
  --tabs-surface: #1e2530;
  --tabs-line: #3f4f64;
}

Props

PropTypeWhat it does
tabsSlidingTab[]Unique id, label, and panel content, plus an optional icon and disabled flag.
value / defaultValuestringControlled or initial selected tab ID. Defaults to the first enabled tab.
onValueChange(id: string) => voidCalled when a tab is selected by click or keyboard.
variant"pill" | "underline"Indicator style. Defaults to pill.
labelstringAccessible name for the tab list. Defaults to Sections.
className / reducedMotionstring / booleanRoot customization, and turning off the sliding in addition to the OS preference.

Accessibility & motion

Follows the ARIA tabs pattern with automatic activation: Arrow Left and Right move between enabled tabs and select them, Home and End jump to the first and last, and only the selected tab is in the Tab order. Each panel is labelled by its tab and can take focus. Both indicators are decorative, and the hover highlight is skipped for touch. Long tab lists scroll sideways. Reduced motion removes the sliding and the panel fade.

Dependencies

React only. Styles live in a plain CSS file next to the component, and Tailwind utilities work through className.

Source

sliding-tabs.tsx
"use client";

import {
  useEffect,
  useId,
  useRef,
  useState,
  type KeyboardEvent,
  type ReactNode,
} from "react";
import "./sliding-tabs.css";

export interface SlidingTab {
  id: string;
  label: string;
  icon?: ReactNode;
  content: ReactNode;
  disabled?: boolean;
}

export interface SlidingTabsProps {
  tabs: SlidingTab[];
  value?: string;
  defaultValue?: string;
  onValueChange?: (id: string) => void;
  /** "pill" fills behind the selected tab; "underline" draws a line under it. */
  variant?: "pill" | "underline";
  label?: string;
  className?: string;
  reducedMotion?: boolean;
}

type Box = { left: number; width: number };

/** Tabs with a measured indicator for the selected tab and a softer one for hover. */
export function SlidingTabs({
  tabs,
  value,
  defaultValue,
  onValueChange,
  variant = "pill",
  label = "Sections",
  className = "",
  reducedMotion = false,
}: SlidingTabsProps) {
  const uid = useId();
  const [internal, setInternal] = useState(defaultValue);
  const selected =
    tabs.find((tab) => tab.id === (value ?? internal) && !tab.disabled) ??
    tabs.find((tab) => !tab.disabled);
  const selectedId = selected?.id;
  const list = useRef<HTMLDivElement>(null);
  const buttons = useRef(new Map<string, HTMLButtonElement>());
  const [indicator, setIndicator] = useState<Box>();
  const [hover, setHover] = useState<Box & { visible: boolean }>();

  useEffect(() => {
    const element = list.current;
    const current = selectedId && buttons.current.get(selectedId);
    if (!element || !current) return;
    const measure = () => {
      const next = { left: current.offsetLeft, width: current.offsetWidth };
      setIndicator((previous) =>
        previous?.left === next.left && previous.width === next.width
          ? previous
          : next,
      );
    };
    const observer = new ResizeObserver(measure);
    observer.observe(element);
    for (const button of buttons.current.values()) observer.observe(button);
    const frame = requestAnimationFrame(measure);
    return () => {
      observer.disconnect();
      cancelAnimationFrame(frame);
    };
  }, [selectedId, tabs]);

  function select(tab: SlidingTab) {
    if (value === undefined) setInternal(tab.id);
    onValueChange?.(tab.id);
  }

  function navigate(event: KeyboardEvent<HTMLButtonElement>, id: string) {
    const available = tabs.filter((tab) => !tab.disabled);
    const index = available.findIndex((tab) => tab.id === id);
    let next: number;
    if (event.key === "ArrowRight") next = (index + 1) % available.length;
    else if (event.key === "ArrowLeft")
      next = (index - 1 + available.length) % available.length;
    else if (event.key === "Home") next = 0;
    else if (event.key === "End") next = available.length - 1;
    else return;
    event.preventDefault();
    select(available[next]);
    buttons.current.get(available[next].id)?.focus();
  }

  if (!selected) return null;
  return (
    <div
      className={`is-tabs ${className}`}
      data-variant={variant}
      data-reduced-motion={reducedMotion || undefined}
    >
      <div
        className="is-tabs-list"
        role="tablist"
        aria-label={label}
        ref={list}
        onPointerLeave={() =>
          setHover((previous) =>
            previous ? { ...previous, visible: false } : previous,
          )
        }
      >
        {hover && (
          <span
            className="is-tabs-hover"
            aria-hidden="true"
            data-visible={hover.visible}
            style={{ left: hover.left, width: hover.width }}
          />
        )}
        {indicator && (
          <span
            className="is-tabs-indicator"
            aria-hidden="true"
            style={{ left: indicator.left, width: indicator.width }}
          />
        )}
        {tabs.map((tab) => {
          const active = tab.id === selected.id;
          return (
            <button
              key={tab.id}
              id={`${uid}-tab-${tab.id}`}
              className="is-tabs-tab"
              role="tab"
              type="button"
              aria-selected={active}
              aria-controls={`${uid}-panel-${tab.id}`}
              tabIndex={active ? 0 : -1}
              disabled={tab.disabled}
              ref={(node) => {
                if (node) buttons.current.set(tab.id, node);
                else buttons.current.delete(tab.id);
              }}
              onClick={() => select(tab)}
              onKeyDown={(event) => navigate(event, tab.id)}
              onPointerEnter={(event) => {
                if (event.pointerType === "touch" || tab.disabled) return;
                const target = event.currentTarget;
                setHover({
                  left: target.offsetLeft,
                  width: target.offsetWidth,
                  visible: true,
                });
              }}
            >
              {tab.icon && (
                <span className="is-tabs-icon" aria-hidden="true">
                  {tab.icon}
                </span>
              )}
              {tab.label}
            </button>
          );
        })}
      </div>
      {tabs.map((tab) => (
        <div
          key={tab.id}
          id={`${uid}-panel-${tab.id}`}
          className="is-tabs-panel"
          role="tabpanel"
          aria-labelledby={`${uid}-tab-${tab.id}`}
          tabIndex={0}
          hidden={tab.id !== selected.id}
        >
          {tab.content}
        </div>
      ))}
    </div>
  );
}
sliding-tabs.css
.is-tabs {
  --tabs-surface: var(--surface, #181d25);
  --tabs-text: var(--text, #f0f3f8);
  --tabs-muted: var(--muted, #adb7c6);
  --tabs-line: var(--line, #343d4a);
  --tabs-selected: var(--raised, #242a33);
  --tabs-hover: color-mix(in srgb, var(--tabs-text) 6%, transparent);
  --tabs-accent: var(--accent, #b2cfff);
  width: 100%;
  min-width: 0;
  color: var(--tabs-text);
}
.is-tabs *,
.is-tabs *::before,
.is-tabs *::after {
  box-sizing: border-box;
}
.is-tabs-list {
  position: relative;
  display: flex;
  gap: 2px;
  width: fit-content;
  max-width: 100%;
  padding: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  border: 1px solid var(--tabs-line);
  border-radius: 12px;
  background: var(--tabs-surface);
}
.is-tabs-list::-webkit-scrollbar {
  display: none;
}
.is-tabs-tab {
  position: relative;
  z-index: 1;
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--tabs-muted);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color 160ms;
}
.is-tabs-tab[aria-selected="true"] {
  color: var(--tabs-text);
}
.is-tabs-tab:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.is-tabs-tab:focus-visible {
  outline: 2px solid var(--tabs-accent);
  outline-offset: -2px;
}
.is-tabs-icon {
  display: grid;
  place-items: center;
  width: 16px;
  height: 16px;
}
.is-tabs-icon > svg {
  width: 16px;
  height: 16px;
}
.is-tabs-indicator,
.is-tabs-hover {
  position: absolute;
  top: 4px;
  bottom: 4px;
  border-radius: 8px;
  pointer-events: none;
  transition:
    left 260ms cubic-bezier(0.22, 1, 0.36, 1),
    width 260ms cubic-bezier(0.22, 1, 0.36, 1),
    opacity 160ms;
}
.is-tabs-indicator {
  background: var(--tabs-selected);
  box-shadow:
    inset 0 1px 0 #ffffff0d,
    0 1px 3px #00000024;
}
.is-tabs-hover {
  background: var(--tabs-hover);
  opacity: 0;
}
.is-tabs-hover[data-visible="true"] {
  opacity: 1;
}
.is-tabs[data-variant="underline"] .is-tabs-list {
  width: 100%;
  padding: 0;
  gap: 4px;
  border-width: 0 0 1px;
  border-radius: 0;
  background: transparent;
}
.is-tabs[data-variant="underline"] .is-tabs-tab {
  min-height: 44px;
  padding: 0 12px;
}
.is-tabs[data-variant="underline"] .is-tabs-indicator {
  top: auto;
  bottom: 0;
  height: 2px;
  border-radius: 2px;
  background: var(--tabs-accent);
  box-shadow: none;
}
.is-tabs[data-variant="underline"] .is-tabs-hover {
  top: 6px;
  bottom: 6px;
}
.is-tabs-panel {
  padding-top: 20px;
  border-radius: 4px;
  animation: is-tabs-panel 240ms ease-out;
}
.is-tabs-panel:focus-visible {
  outline: 2px solid var(--tabs-accent);
  outline-offset: 4px;
}
@keyframes is-tabs-panel {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
}
.is-tabs[data-reduced-motion] .is-tabs-tab,
.is-tabs[data-reduced-motion] .is-tabs-indicator,
.is-tabs[data-reduced-motion] .is-tabs-hover,
.is-tabs[data-reduced-motion] .is-tabs-panel {
  transition: none;
  animation: none;
}
@media (prefers-reduced-motion: reduce) {
  .is-tabs .is-tabs-tab,
  .is-tabs .is-tabs-indicator,
  .is-tabs .is-tabs-hover,
  .is-tabs .is-tabs-panel {
    transition: none;
    animation: none;
  }
}