Skip to content

armgit / mapcn

Public Imported from GitHub
0

Beautiful map components. 100% Free, Zero config, one command setup.

TypeScript
8.3 KB
Raw Blame
import Link from "next/link";
import { Children, Fragment, isValidElement } from "react";
import { Button } from "@/components/ui/button";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { cn } from "@/lib/utils";
import { DocsToc } from "./docs-toc";
import { DocsSponsors } from "@/components/sponsor-logos";
import { ChevronLeft, ChevronRight } from "lucide-react";

export function slugify(text: string): string {
  return text
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/(^-|-$)/g, "");
}

interface TocItem {
  title: string;
  slug: string;
}

// DocsHeader - Page title and description
interface DocsTitleProps {
  title: string;
  description: string;
  action?: React.ReactNode;
}

function DocsTitle({ title, description, action }: DocsTitleProps) {
  return (
    <div className="space-y-2">
      <div className="flex items-baseline justify-between gap-4">
        <h1 className="text-foreground text-3xl font-semibold tracking-tight">
          {title}
        </h1>
        {action}
      </div>
      <p className="text-muted-foreground text-base leading-relaxed">
        {description}
      </p>
    </div>
  );
}

function collectToc(children: React.ReactNode): TocItem[] {
  const items: TocItem[] = [];

  for (const child of Children.toArray(children)) {
    if (!isValidElement(child)) continue;

    if (child.type === Fragment) {
      const { children: nested } = child.props as {
        children?: React.ReactNode;
      };
      items.push(...collectToc(nested));
      continue;
    }

    if (child.type === DocsSection) {
      const { title } = child.props as DocsSectionProps;
      if (title) items.push({ title, slug: slugify(title) });
    }
  }

  return items;
}

// DocsLayout - Full page wrapper with nav
interface DocsLayoutProps {
  title: string;
  description: string;
  children: React.ReactNode;
  prev?: { title: string; href: string };
  next?: { title: string; href: string };
  toc?: TocItem[];
  /** Shown at the end of the title row. */
  action?: React.ReactNode;
}

export function DocsLayout({
  title,
  description,
  children,
  prev,
  next,
  toc,
  action,
}: DocsLayoutProps) {
  const tocItems = toc ?? collectToc(children);

  return (
    <div className="flex min-w-0 flex-1">
      <div className="mx-auto flex max-w-[52rem] min-w-0 flex-1 flex-col pt-10 pb-20 lg:px-12 lg:pt-12">
        <DocsTitle title={title} description={description} action={action} />
        <div className="mt-12 mb-12 space-y-14 [&>*:has(+[data-slot=docs-note])]:mb-6">
          {children}
        </div>
        {(prev || next) && (
          <div className="mt-auto flex items-center justify-between gap-4">
            {prev ? (
              <Button
                variant="ghost"
                size="sm"
                asChild
                className="-ml-2 h-auto py-2"
              >
                <Link href={prev.href}>
                  <ChevronLeft /> {prev.title}
                </Link>
              </Button>
            ) : (
              <div />
            )}
            {next && (
              <Button
                variant="ghost"
                size="sm"
                asChild
                className="-mr-2 h-auto py-2"
              >
                <Link href={next.href}>
                  {next.title} <ChevronRight />
                </Link>
              </Button>
            )}
          </div>
        )}
        <DocsSponsors className="mt-12 xl:hidden" />
      </div>

      <aside className="hidden w-42 shrink-0 xl:block">
        <nav className="sticky top-14 -mx-3 max-h-[calc(100svh-3.5rem)] overflow-y-auto px-3 pt-12 pb-10 [scrollbar-gutter:stable]">
          {tocItems.length > 0 && <DocsToc items={tocItems} />}
          <DocsSponsors className={tocItems.length > 0 ? "mt-8" : undefined} />
        </nav>
      </aside>
    </div>
  );
}

// DocsSection - Content section with optional title
interface DocsSectionProps {
  title?: string;
  children: React.ReactNode;
}

export function DocsSection({ title, children }: DocsSectionProps) {
  const id = title ? slugify(title) : undefined;
  return (
    <section className="scroll-m-24 space-y-4" id={id}>
      {title && (
        <div className="group flex w-fit items-baseline gap-2">
          <h2 className="text-foreground text-xl font-semibold tracking-tight">
            {title}
          </h2>
          <a
            href={`#${id}`}
            aria-label={`Link to ${title}`}
            className="text-muted-foreground/60 hover:text-foreground text-xl font-semibold opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100"
          >
            #
          </a>
        </div>
      )}
      <div className="[&_em]:text-muted-foreground space-y-4 text-[15px] leading-[1.75] [&_strong]:font-medium [&>ol]:list-decimal [&>ol]:space-y-2 [&>ol]:pl-5 [&>ol>li]:leading-[1.75] [&>p]:leading-[1.75] [&>ul]:list-disc [&>ul]:space-y-2 [&>ul]:pl-5 [&>ul>li]:leading-[1.75]">
        {children}
      </div>
    </section>
  );
}

// DocsNote - Highlighted note/callout
interface DocsNoteProps {
  children: React.ReactNode;
}

export function DocsNote({ children }: DocsNoteProps) {
  return (
    <div
      data-slot="docs-note"
      className="bg-surface text-foreground/80 [&_strong]:text-foreground rounded-lg px-5 py-4 text-[15px] leading-[1.75] [&_strong]:font-medium"
    >
      {children}
    </div>
  );
}

// DocsLink - Styled link
interface DocsLinkProps {
  href: string;
  children: React.ReactNode;
  external?: boolean;
}

export function DocsLink({ href, children, external }: DocsLinkProps) {
  return (
    <Link
      href={href}
      target={external ? "_blank" : undefined}
      rel={external ? "noopener noreferrer" : undefined}
      className="decoration-foreground/25 hover:text-foreground hover:decoration-foreground/60 underline decoration-1 underline-offset-[5px] transition-colors"
    >
      {children}
    </Link>
  );
}

// DocsCode - Inline code
export function DocsCode({
  children,
  className,
}: {
  children: React.ReactNode;
  className?: string;
}) {
  return (
    <code
      className={cn(
        "bg-muted relative rounded-md px-1.5 py-0.5 font-mono text-sm",
        className,
      )}
    >
      {children}
    </code>
  );
}

// DocsPropTable - API reference table for component props
interface DocsPropTableProps {
  props: {
    name: string;
    type: string;
    default?: string;
    description: string;
  }[];
  title?: string;
}

export function DocsPropTable({ props, title }: DocsPropTableProps) {
  return (
    <div className="prop-table space-y-3 [.prop-table+&]:mt-8">
      {title && <h3 className="text-foreground font-semibold">{title}</h3>}
      <div className="overflow-hidden">
        <Table className="[&_tbody_tr:last-child]:border-b [&_td:first-child]:pl-0 [&_td:last-child]:pr-0 [&_th:first-child]:pl-0 [&_th:last-child]:pr-0">
          <TableHeader>
            <TableRow>
              <TableHead className="h-10 text-xs font-medium">Prop</TableHead>
              <TableHead className="h-10 text-xs font-medium">Type</TableHead>
              <TableHead className="h-10 text-xs font-medium">
                Default
              </TableHead>
              <TableHead className="h-10 text-xs font-medium">
                Description
              </TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {props.map((prop) => (
              <TableRow key={prop.name}>
                <TableCell className="py-3 align-top">
                  <DocsCode className="text-[13px]">{prop.name}</DocsCode>
                </TableCell>
                <TableCell className="py-3 align-top whitespace-normal">
                  <DocsCode className="text-foreground/70 text-xs">
                    {prop.type}
                  </DocsCode>
                </TableCell>
                <TableCell className="py-3 align-top">
                  <DocsCode className="text-foreground/70 text-xs whitespace-normal">
                    {prop.default ?? "—"}
                  </DocsCode>
                </TableCell>
                <TableCell className="text-foreground/70 min-w-[180px] py-3 text-sm leading-relaxed whitespace-normal">
                  {prop.description}
                </TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </div>
    </div>
  );
}