Tabs

Tabs in two looks on one project card: a raised pill that slides inside a soft track, and an accent line that slides under the chosen tab. The arrow keys move between them.

Website redesignexample.com

1,284visits this week+12%

  • HomeLive
  • PricingIn review
  • JournalDraft

Options

Screen
Colour

Code

import { useEffect, useId, useRef, useState } from "react";
import "./tabs.css";

/* Tabs with a mark that slides to the chosen one. Anything passed as children
   sits beside the tabs, in a header row.

   <Tabs kind="pill" label="Period" items={[{ label: "Week", content: <p>1,284 visits</p> }]} /> */
export function Tabs({ kind = "line", label, items, initial = 0, children }) {
  const id = useId();
  const list = useRef(null);
  const [chosen, setChosen] = useState(initial);
  const [ready, setReady] = useState(false);

  /* Sets the chosen tab's left edge and width on the list, and again when the list changes size. */
  useEffect(() => {
    const place = () => {
      const tab = list.current.querySelector('[aria-selected="true"]');
      list.current.style.setProperty("--x", tab.offsetLeft);
      list.current.style.setProperty("--w", tab.offsetWidth);
      list.current.style.setProperty("--on", 1);
    };
    place();
    const watch = new ResizeObserver(place);
    watch.observe(list.current);
    return () => watch.disconnect();
  }, [chosen]);

  useEffect(() => {
    const frame = requestAnimationFrame(() => setReady(true));
    return () => cancelAnimationFrame(frame);
  }, []);

  const move = (event, index) => {
    const last = items.length - 1;
    const to = { ArrowRight: index === last ? 0 : index + 1, ArrowLeft: index === 0 ? last : index - 1, Home: 0, End: last }[event.key];
    if (to === undefined) return;
    event.preventDefault();
    list.current.querySelectorAll('[role="tab"]')[to].focus();
    setChosen(to);
  };

  const tabs = (
    <div className="dh-tabs-list" role="tablist" aria-label={label} ref={list} data-ready={ready ? "" : undefined}>
      {items.map((item, index) => (
        <button
          type="button"
          role="tab"
          key={item.label}
          id={id + "tab" + index}
          aria-controls={id + "panel" + index}
          aria-selected={index === chosen}
          tabIndex={index === chosen ? 0 : -1}
          onClick={() => setChosen(index)}
          onKeyDown={(event) => move(event, index)}
        >
          {item.label}
        </button>
      ))}
      <i className="dh-tabs-mark" aria-hidden="true" />
    </div>
  );

  return (
    <div className="dh-tabs" data-kind={kind}>
      {children ? (
        <header>
          {children}
          {tabs}
        </header>
      ) : (
        tabs
      )}
      {items.map((item, index) => (
        <div
          className="dh-tabs-panel"
          role="tabpanel"
          key={item.label}
          id={id + "panel" + index}
          aria-labelledby={id + "tab" + index}
          tabIndex={0}
          hidden={index !== chosen}
        >
          {item.content}
        </div>
      ))}
    </div>
  );
}

function Figure({ value, label, change }) {
  return (
    <p className="dh-tabs-figure">
      <b>{value}</b>
      <span>{label}</span>
      <em>{change}</em>
    </p>
  );
}

function Rows({ rows }) {
  return (
    <ul className="dh-tabs-rows">
      {rows.map(([name, value, dot]) => (
        <li key={name}>
          <span>{name}</span>
          <em data-dot={dot}>{value}</em>
        </li>
      ))}
    </ul>
  );
}

export function Example() {
  return (
    <div className="dh-tabs-card">
      <Tabs
        kind="pill"
        label="Period"
        items={[
          { label: "Week", content: <Figure value="1,284" label="visits this week" change="+12%" /> },
          { label: "Month", content: <Figure value="5,902" label="visits this month" change="+8%" /> },
          { label: "Year", content: <Figure value="61,340" label="visits this year" change="+31%" /> },
        ]}
      >
        <p>
          <b>Website redesign</b>
          <span>example.com</span>
        </p>
      </Tabs>
      <Tabs
        label="Project"
        items={[
          { label: "Pages", content: <Rows rows={[["Home", "Live", "good"], ["Pricing", "In review", "warn"], ["Journal", "Draft", "none"]]} /> },
          { label: "Activity", content: <Rows rows={[["Maya Okafor changed Home", "2h"], ["Sam Jensen left a note on Pricing", "5h"], ["Maya Okafor added Journal", "1d"]]} /> },
          { label: "Members", content: <Rows rows={[["Maya Okafor", "Owner"], ["Sam Jensen", "Editor"], ["Ella Marsh", "Viewer"]]} /> },
          { label: "Settings", content: <Rows rows={[["Address", "example.com"], ["Language", "English"], ["Search engines", "Allowed"]]} /> },
        ]}
      />
    </div>
  );
}

Website redesignexample.com

1,284visits this week+12%

  • HomeLive
  • PricingIn review
  • JournalDraft