Skip to content

Footer

A brand line, link columns, the legal links and a light and dark switch, over a wordmark that fades out.

168 lines · React · Tailwind 4

Install

$ bunx shadcn@latest add @blocco/footer

Code

blocks/free/footer.tsx168 lines
import type { ReactNode } from "react";
import { Moon, Sun } from "@/registry/blocco/ui/icons";
import { cn } from "@/registry/blocco/ui/cn";

const linkClass = "hover:text-accent";

function Column({ title, children, wide }: { title: string; children: ReactNode; wide?: boolean }) {
  return (
    <div className={wide ? "col-span-2 sm:col-span-1" : undefined}>
      <p className="text-sm font-semibold text-text">{title}</p>
      <ul
        className={cn(
          "mt-3 flex flex-col gap-1 *:py-1 sm:gap-0.5",
          // On a phone a wide column wraps its links like a line of text, so
          // an odd count leaves a ragged line end, not an empty grid cell.
          wide && "max-sm:flex-row max-sm:flex-wrap max-sm:gap-x-5 max-sm:gap-y-0",
        )}
      >
        {children}
      </ul>
    </div>
  );
}

function Links({ items }: { items: string[] }) {
  return items.map((label) => (
    <li key={label}>
      <a href="#" className={linkClass}>
        {label}
      </a>
    </li>
  ));
}

/** Light and dark side by side, the chosen one raised. Either button sets
 * the theme; which one is raised comes from the root's `data-theme` through
 * CSS, not from state, so a server-rendered page and a hydrated one agree. */
function ThemeSwitch() {
  const set = (mode: "light" | "dark") => {
    document.documentElement.dataset.theme = mode;
    try {
      localStorage.setItem("theme", mode);
    } catch {
      // Private mode: the choice lasts until the page closes.
    }
  };
  const button =
    "grid cursor-pointer place-items-center rounded-full p-1.5 text-muted [@media(hover:hover)]:hover:text-text [:root:not([data-theme=dark])_&[data-mode=light]]:bg-surface-2 [:root:not([data-theme=dark])_&[data-mode=light]]:text-text [:root[data-theme=dark]_&[data-mode=dark]]:bg-surface-2 [:root[data-theme=dark]_&[data-mode=dark]]:text-text";
  return (
    <div
      role="group"
      aria-label="Theme"
      className="flex w-fit rounded-full border border-border p-0.5"
    >
      <button
        type="button"
        aria-label="Light theme"
        data-mode="light"
        className={button}
        onClick={() => set("light")}
      >
        <Sun size={14} />
      </button>
      <button
        type="button"
        aria-label="Dark theme"
        data-mode="dark"
        className={button}
        onClick={() => set("dark")}
      >
        <Moon size={14} />
      </button>
    </div>
  );
}

/** The name, set as large as the page is wide and fading into it, cut off by
 * the bottom edge. Decoration: the logo at the top of the footer is the one a
 * screen reader meets. */
function Wordmark({ name }: { name: string }) {
  return (
    <svg
      aria-hidden="true"
      viewBox="0 0 1000 250"
      className="-mb-[6%] mt-8 block w-full select-none sm:mt-12"
    >
      <defs>
        <linearGradient id="footer-wordmark" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0" stopColor="var(--border)" />
          <stop offset="0.85" stopColor="var(--border)" stopOpacity="0" />
        </linearGradient>
      </defs>
      <text
        x="500"
        y="232"
        textAnchor="middle"
        textLength="980"
        lengthAdjust="spacingAndGlyphs"
        fill="url(#footer-wordmark)"
        className="font-sans font-bold"
        fontSize="300"
      >
        {name}
      </text>
    </svg>
  );
}

/** A brand blurb, three link columns, a bottom bar with the legal links and
 * the theme switch, and the name as a wordmark fading out below. */
export function Footer({ name = "Acme" }: { name?: string }) {
  return (
    <footer className="overflow-hidden text-sm text-muted">
      <div className="mx-auto max-w-5xl border-t border-border px-6 pt-10">
        <div className="grid grid-cols-2 gap-x-6 gap-y-8 sm:grid-cols-[1.6fr_1fr_1fr_1fr] sm:gap-y-10">
          <div className="col-span-2 flex flex-col gap-3 sm:col-span-1">
            <a href="#" className="w-fit text-lg font-bold tracking-tight text-text">
              {name}
            </a>
            <p className="max-w-xs">Short links and QR codes for teams, on your own domain.</p>
          </div>
          <Column title="Product">
            <Links items={["QR codes", "Branded links", "Analytics", "Pricing", "Docs"]} />
          </Column>
          <Column title="Company">
            <Links items={["Blog", "Changelog", "Support", "Careers"]} />
          </Column>
          <Column title="Compare" wide>
            <Links items={["Bitly", "Rebrandly", "Short.io", "All comparisons"]} />
          </Column>
        </div>

        {/* On a phone: the legal links on one line, then the copyright and
            the theme switch sharing the next. `contents` hands nav and switch
            to this grid there; from sm up they are one flex group again. */}
        <div className="mt-8 grid grid-cols-[1fr_auto] items-center gap-y-4 border-t border-border pt-6 text-xs sm:mt-10 sm:flex sm:justify-between sm:gap-3">
          <span>
            © {new Date().getFullYear()} {name}, Inc.
          </span>
          <div className="contents sm:flex sm:items-center sm:gap-6">
            <nav className="flex flex-wrap gap-x-5 gap-y-1 *:py-2 max-sm:order-first max-sm:col-span-2 sm:items-center sm:gap-4 sm:*:py-0">
              <a href="#" className={linkClass}>
                Privacy
              </a>
              <a href="#" className={linkClass}>
                Terms
              </a>
              <a href="#" className={linkClass}>
                Cookie settings
              </a>
            </nav>
            <ThemeSwitch />
          </div>
        </div>
        <Wordmark name={name} />
      </div>
    </footer>
  );
}

export default function FooterBlock() {
  return (
    <div className="pt-16">
      <Footer />
    </div>
  );
}