Skip to content

Bar lists

Top pages, sources and countries with the label inside a shared-scale bar.

104 lines · React · Tailwind 4

Install

$ bunx shadcn@latest add @blocco/bar-lists

Code

blocks/free/bar-lists.tsx104 lines
import { PanelTabs } from "@/registry/blocco/ui/tabs";

type Row = { label: string; count: number; mono?: boolean };

const lists: { value: string; label: string; heading: string; rows: Row[] }[] = [
  {
    value: "pages",
    label: "Top pages",
    heading: "Page",
    rows: [
      { label: "/pricing", count: 4820, mono: true },
      { label: "/blog/launch-week", count: 3105, mono: true },
      { label: "/docs/custom-domains", count: 2260, mono: true },
      { label: "/", count: 1944, mono: true },
      { label: "/changelog", count: 871, mono: true },
      { label: "/blog/qr-codes-that-last", count: 402, mono: true },
    ],
  },
  {
    value: "sources",
    label: "Sources",
    heading: "Source",
    rows: [
      { label: "Direct", count: 5210 },
      { label: "Newsletter", count: 3340 },
      { label: "x.com", count: 2105 },
      { label: "news.ycombinator.com", count: 1480, mono: true },
      { label: "Google", count: 960 },
      { label: "linkedin.com", count: 355, mono: true },
    ],
  },
  {
    value: "countries",
    label: "Countries",
    heading: "Country",
    rows: [
      { label: "United States", count: 4690 },
      { label: "Germany", count: 2310 },
      { label: "United Kingdom", count: 2105 },
      { label: "Italy", count: 1422 },
      { label: "France", count: 1186 },
      { label: "Brazil", count: 702 },
    ],
  },
];

const fmt = (n: number) => n.toLocaleString("en");

function BarList({ heading, rows }: { heading: string; rows: Row[] }) {
  const max = Math.max(...rows.map((r) => r.count));
  const total = rows.reduce((sum, r) => sum + r.count, 0);
  return (
    <div className="grid grid-cols-[minmax(0,1fr)_4rem_3.25rem] items-center gap-x-3 gap-y-1 sm:grid-cols-[minmax(0,1fr)_5rem_4rem]">
      <span className="text-xs text-muted">{heading}</span>
      <span className="text-right text-xs text-muted">Visitors</span>
      <span className="text-right text-xs text-muted">Share</span>
      {rows.map((row) => (
        <div key={row.label} className="contents">
          <div className="relative flex h-[30px] items-center overflow-hidden rounded-md">
            <div
              aria-hidden
              className="absolute inset-y-0 left-0 rounded-md bg-accent/12"
              style={{ width: `${(row.count / max) * 100}%` }}
            />
            <span
              className={
                row.mono
                  ? "relative truncate px-2.5 font-mono text-sm"
                  : "relative truncate px-2.5 text-sm"
              }
              title={row.label}
            >
              {row.label}
            </span>
          </div>
          <span className="tnum text-right text-sm">{fmt(row.count)}</span>
          <span className="tnum text-right text-sm text-muted">
            {Math.round((row.count / total) * 100)}%
          </span>
        </div>
      ))}
    </div>
  );
}

/**
 * Top pages, sources and countries in one card behind tabs. The label sits
 * inside a tinted bar, and each list shares one scale from zero.
 */
export default function BarLists() {
  return (
    <section className="mx-auto max-w-2xl px-6 py-12 sm:py-16">
      <PanelTabs
        end="Last 30 days"
        tabs={lists.map((list) => ({
          value: list.value,
          label: list.label,
          panel: <BarList heading={list.heading} rows={list.rows} />,
        }))}
      />
    </section>
  );
}