Dev Helpers
← Back

Grouped guide directory

Place this file at components/GroupedGuideDirectory.tsx in an App Router project.

Open this example in the interactive library

Source code

// components/GroupedGuideDirectory.tsx

import Link from "next/link";

export default function GroupedGuideDirectory({
  items = [
    { slug: "getting-started", title: "Getting started" },
    { slug: "building", title: "Building" },
  ],
}: { items?: { slug: string; title: string }[] } = {}) {
  const letters = [...new Set(items.map((i) => i.title[0]?.toUpperCase()))];
  return (
    <>
      {letters.map((l) => (
        <section key={l}>
          <h2>{l}</h2>
          {items
            .filter((i) => i.title[0]?.toUpperCase() === l)
            .map((i) => (
              <p key={i.slug}>
                <Link href={"/guide/" + encodeURIComponent(i.slug)}>{i.title}</Link>
              </p>
            ))}
        </section>
      ))}
    </>
  );
}