All files / src/components/page/TabGroups TabGroups.tsx

100% Statements 3/3
100% Branches 6/6
100% Functions 3/3
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                                        92x     286x                       456x                                              
import React from "react";
import { Link } from "react-router-dom";
 
export interface TabLink<K extends string> {
  key: K;
  label: string;
  icon: React.ElementType;
  /** A route, written against `APP_ROUTES.USER` like every link. */
  to: string;
}
 
export interface TabLinkGroup<K extends string> {
  label: string;
  tabs: TabLink<K>[];
}
 
/**
 * A settings page's tab bar: labelled groups of tabs, each tab its own
 * route. Personal's account settings and an organisation's or project's
 * settings both draw it, so the two read as one kind of page.
 */
export function TabGroups<K extends string>({
  groups,
  active,
}: {
  groups: TabLinkGroup<K>[];
  active: K;
}) {
  return (
    <div className="flex flex-wrap items-end gap-3">
      {groups.map((group, i) => (
        <React.Fragment key={group.label}>
          {i > 0 && (
            <div className="hidden sm:block self-stretch w-px bg-gray-200 dark:bg-gray-700" />
          )}
          <div className="flex flex-col gap-1 max-w-full">
            <span className="px-1 text-[10px] font-semibold uppercase tracking-wider text-gray-400 dark:text-gray-500">
              {group.label}
            </span>
            {/* Wraps rather than running off a phone's screen: the tab bar is
                the only way between settings pages. */}
            <div className="flex flex-wrap gap-1 p-1 rounded-lg bg-gray-100 dark:bg-gray-700">
              {group.tabs.map(({ key, label, icon: Icon, to }) => (
                <Link
                  key={key}
                  to={to}
                  aria-current={active === key ? "page" : undefined}
                  className={`flex items-center gap-1.5 px-3 py-1.5 rounded-md text-sm font-medium transition-all ${
                    active === key
                      ? "bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 shadow-sm"
                      : "text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"
                  }`}
                >
                  <Icon className="size-3.5 shrink-0" />
                  {label}
                </Link>
              ))}
            </div>
          </div>
        </React.Fragment>
      ))}
    </div>
  );
}
 
export default TabGroups;