# Docs Page

Docs page with header, content and contents list

Docs Page lays out a single documentation page: a header with the title, a lead and optional tabs; a reading column capped at a comfortable line length; and a table of contents that sticks beside it on large screens. Every page on this site uses it.

The page's actions, such as Source, go to the end of the [Docs Shell](/docs/blocks/docs-shell) toolbar, not the header.

## Installation

Install the dependencies.

```bash
npm install @kushagradhawan/kookie-ui @hugeicons/react @hugeicons/core-free-icons
```

This block imports [Page Header](/docs/blocks/page-header). Copy that into `components/blocks` first.

Copy these files into `components/blocks/docs-page`. Keep the folder names, since blocks import each other by relative path.

`components/blocks/docs-page/docs-page.tsx`

```tsx
'use client';

import * as React from 'react';
import { Box, Flex, Link, Tabs, Text } from '@kushagradhawan/kookie-ui';
import { DocsPageHeader } from './docs-page-header';

export interface DocsPageMeta {
  title: string;
  description?: string;
  /** Shown above the title, e.g. "Components". */
  category?: string;
  /** Link to the page's source, shown as a "Source" button. */
  source?: string;
}

/** One view of a page that has several, such as Documentation and Examples. */
export interface DocsPageView {
  value: string;
  label: string;
  /** Shown before the label in the tab. */
  icon?: React.ReactNode;
  content: React.ReactNode;
}

export interface DocsPageProps {
  /** The page's content. Leave it out when you pass `views`. */
  children?: React.ReactNode;
  /**
   * Several views of one page, shown as tabs under the header. Each tab controls the panel that
   * holds its view, so a screen reader can move from a tab to what it shows. The first view shows
   * first.
   */
  views?: DocsPageView[];
  /** Builds the default header. Ignored when `header` is set. */
  meta?: DocsPageMeta;
  /** Replaces the meta-based header entirely. */
  header?: React.ReactNode;
  /** Extra buttons in the header's action row. */
  headerActions?: React.ReactNode;
  /**
   * Your own tabs under the header. Prefer `views`: tabs passed here have no panel to control
   * unless you render one yourself.
   */
  headerTabs?: React.ReactNode;
  /** @deprecated Copying moved to the docs toolbar, shown on every page with a Markdown twin. Ignored. */
  showCopyButton?: boolean;
  /** Rendered in a sticky column from the `lg` breakpoint. Without it, the column is centred. */
  tableOfContents?: React.ReactNode;
  /** Maximum width of the reading column. @default "44rem" */
  maxWidth?: string;
  /** Copyright line under the content, or your own footer element to render there instead. */
  footer?: { name: string; url?: string; githubUrl?: string; links?: { label: string; href: string }[] } | React.ReactElement;
}

/**
 * A documentation page: header, a reading column and an optional table of contents. The column
 * carries `data-content-area`, which the table of contents reads from.
 */
export function DocsPage({ children, views, meta, header, headerActions, headerTabs, showCopyButton, tableOfContents, maxWidth = '44rem', footer }: DocsPageProps) {
  const [view, setView] = React.useState(views?.[0]?.value);
  const active = views?.find((item) => item.value === view) ?? views?.[0];
  const tabs = views ? (
    <Tabs.List>
      {views.map((item) => (
        <Tabs.Trigger key={item.value} value={item.value}>
          {item.icon}
          {item.label}
        </Tabs.Trigger>
      ))}
    </Tabs.List>
  ) : (
    headerTabs
  );

  const column = (
    <Flex direction="column" gap={{ initial: '7', sm: '8' }} minWidth="0">
      {header ?? (meta && <DocsPageHeader meta={meta} actions={headerActions} tabs={tabs} showCopyButton={showCopyButton} />)}

      {active ? (
        // The panel the active tab controls. Its links and controls take focus, not the panel.
        <Tabs.Content value={active.value} tabIndex={-1}>
          <Box minWidth="0" data-content-area>
            {active.content}
          </Box>
        </Tabs.Content>
      ) : (
        <Box minWidth="0" data-content-area>
          {children}
        </Box>
      )}

      {React.isValidElement(footer) ? (
        <Box mt="10">{footer}</Box>
      ) : (
        footer && (
          <Box mt="10">
            <Text as="p" size="2" emphasis="medium">
              © {new Date().getFullYear()}{' '}
              {footer.url ? (
                <Link href={footer.url} target="_blank" rel="noreferrer" color="gray">
                  {footer.name}
                </Link>
              ) : (
                footer.name
              )}
              . Licensed under MIT.
              {footer.githubUrl && (
                <>
                  {' '}
                  <Link href={footer.githubUrl} target="_blank" rel="noreferrer" color="gray">
                    GitHub
                  </Link>
                  .
                </>
              )}
              {footer.links?.map((link) => (
                <React.Fragment key={link.href}>
                  {' '}
                  <Link href={link.href} color="gray">
                    {link.label}
                  </Link>
                  .
                </React.Fragment>
              ))}
            </Text>
          </Box>
        )
      )}
    </Flex>
  );

  return (
    <Flex justify="center" align="start" gap="10" px={{ initial: '6', sm: '8', md: '10' }} pt={{ initial: '7', sm: '8' }} pb="10">
      <Box flexGrow="1" minWidth="0" maxWidth={maxWidth}>
        {active ? (
          <Tabs.Root value={active.value} onValueChange={setView}>
            {column}
          </Tabs.Root>
        ) : (
          column
        )}
      </Box>

      {/* Chapters always pass one, so their column sits in the same place whether or not the
          page has headings. A page without one, like the homepage, centres its column instead. */}
      {tableOfContents && (
        <Box
          display={{ initial: 'none', lg: 'block' }}
          width="13rem"
          flexShrink="0"
          position="sticky"
          // Clears a floating toolbar over the page, as in Docs Shell.
          top="var(--kui-pane-inset-start, var(--space-7))"
        >
          {tableOfContents}
        </Box>
      )}
    </Flex>
  );
}
```

`components/blocks/docs-page/docs-page-header.tsx`

```tsx
'use client';

import * as React from 'react';
import { Box, Separator, ToolbarButton, ToolbarFill } from '@kushagradhawan/kookie-ui';
import { HugeiconsIcon } from '@hugeicons/react';
import { SourceCodeIcon } from '@hugeicons/core-free-icons';
import { PageHeader } from '../page-header/page-header';
import { DOCS_PAGE_ACTIONS } from '../docs-shell/docs-shell';
import type { DocsPageMeta } from './docs-page';

export interface DocsPageHeaderProps {
  meta: DocsPageMeta;
  /** Extra actions at the end of the shell's top bar, before Source. Use `ToolbarButton`s so they join its arrow keys. */
  actions?: React.ReactNode;
  /**
   * @deprecated Copying moved to the docs toolbar (`components/page-toolbar`), which shows it on
   * every page that has a Markdown twin. Ignored.
   */
  showCopyButton?: boolean;
  /** Tabs under the header, e.g. Documentation and Examples. */
  tabs?: React.ReactNode;
  /** Rule under the header. Ignored when there are tabs, which draw their own. @default false */
  separator?: boolean;
}

/**
 * The title, the description and optional tabs. The page's actions go to the shell's top bar. There
 * is no category line: the sidebar already shows which section the page is in.
 */
export function DocsPageHeader({ meta, actions, tabs, separator = false }: DocsPageHeaderProps) {
  return (
    <PageHeader.Root gap="0">
      {/* Rendered inside the shell's top bar through its ToolbarSlot, so these join its
          arrow-key row and take its size. Outside a shell they render nothing. */}
      <ToolbarFill name={DOCS_PAGE_ACTIONS}>
        {actions}
        {meta.source && (
          <ToolbarButton asChild iconOnly aria-label="View source on GitHub" tooltip="Source" tooltipSide="bottom">
            <a href={meta.source} target="_blank" rel="noreferrer">
              <HugeiconsIcon icon={SourceCodeIcon} strokeWidth={1.75} />
            </a>
          </ToolbarButton>
        )}
      </ToolbarFill>

      <PageHeader.Content gap="6">
        <PageHeader.Title size={{ initial: '8', sm: '9' }}>{meta.title}</PageHeader.Title>
        {meta.description && (
          <PageHeader.Description size={{ initial: '3', sm: '4' }} emphasis="loud">{meta.description}</PageHeader.Description>
        )}
      </PageHeader.Content>

      {tabs ? (
        <PageHeader.Tabs mt="7">{tabs}</PageHeader.Tabs>
      ) : (
        separator && (
          <Box mt="7">
            <Separator />
          </Box>
        )
      )}
    </PageHeader.Root>
  );
}
```

## Usage

```tsx
'use client';

import { DocsPage } from '@/components/blocks/docs-page/docs-page';
import { TableOfContents } from '@/components/blocks/table-of-contents/table-of-contents';
import Content from './content.mdx';

export default function ButtonPage() {
  return (
    <DocsPage
      meta={{
        title: 'Button',
        description: 'Trigger an action or event.',
      }}
      tableOfContents={<TableOfContents />}
    >
      <Content />
    </DocsPage>
  );
}
```

The content column is marked with `data-content-area`. [Table of Contents](/docs/blocks/table-of-contents) reads headings from it.

## Anatomy

| File | Contains |
| ---- | -------- |
| `docs-page.tsx` | `DocsPage` and the `DocsPageMeta` type |
| `docs-page-header.tsx` | `DocsPageHeader`, built on [Page Header](/docs/blocks/page-header). Sends its actions to the Docs Shell toolbar through `ToolbarFill`. Usable on its own |

## Props

| Prop | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| `meta` | `DocsPageMeta` | - | Title, description and source link. |
| `header` | `ReactNode` | - | Replaces the generated header. |
| `headerActions` | `ReactNode` | - | Extra controls at the end of the Docs Shell toolbar, before Source. Use `ToolbarButton`s so the arrow keys reach them. |
| `headerTabs` | `ReactNode` | - | Tabs under the header, e.g. Documentation and Examples. They replace the header's rule. |
| `tableOfContents` | `ReactNode` | - | Shown in a sticky column from the `lg` breakpoint. |
| `maxWidth` | `string` | `'44rem'` | Width cap for the reading column, around 75 characters of body text. |
| `footer` | `{ name: string; url?: string; githubUrl?: string }` | - | Copyright line under the content. |

### DocsPageMeta

| Field | Type | Description |
| ----- | ---- | ----------- |
| `title` | `string` | Page title, rendered as the `h1`. |
| `description` | `string` | The lead under the title. |
| `source` | `string` | URL for a Source button at the end of the Docs Shell toolbar. |

## Examples

### Site Defaults

Wrap `DocsPage` once with your footer and use the wrapper everywhere. This site does this.

```tsx
import { DocsPage, type DocsPageProps } from '@/components/blocks/docs-page/docs-page';

const FOOTER = { name: 'Acme', url: 'https://acme.com', githubUrl: 'https://github.com/acme/docs' };

export function SiteDocsPage(props: DocsPageProps) {
  return <DocsPage {...props} footer={FOOTER} />;
}
```

### With Markdown

The page doesn't style its content. Render MDX inside [Markdown](/docs/blocks/markdown)'s `MarkdownContent` for type sizes and spacing that match the header.

## Accessibility

The title renders as the page's only `h1`. Source opens in a new tab, and its accessible name is "View source on GitHub".
