# Back button

> A plain button with a chevron and the title of the previous screen, aligned to the content edge.

Docs: https://plumbago.ochotzas.com/components/back-button

## Installation

```sh
npx plumbago add back-button
```

This copies `components/ui/back-button.tsx` into your project, along with `cn`, `button`.

## Usage

```tsx
import { BackButton } from "@/components/ui/back-button"

<BackButton onClick={() => history.back()}>Menu</BackButton>
```

## Examples

### Preview

```tsx
import { BackButton } from "@/components/ui/back-button"

export default function BackButtonDemo() {
  return (
    <div className="w-full max-w-md">
      <BackButton>Menu</BackButton>
      <h2 className="mt-2 text-title1 font-bold text-ink">Mains</h2>
      <p className="mt-1 text-footnote text-ink-2">8 dishes, 2 sold out</p>
    </div>
  )
}
```

### As a link

```tsx
import { BackButton } from "@/components/ui/back-button"

export default function BackButtonAsALink() {
  return (
    <div className="w-full max-w-md">
      <BackButton render={<a href="#reservations" />}>Reservations</BackButton>
      <h2 className="mt-2 text-title1 font-bold text-ink">Table for four</h2>
      <p className="mt-1 text-footnote text-ink-2">Friday, 16 October at 19:30</p>
    </div>
  )
}
```

### Default title

```tsx
import { BackButton } from "@/components/ui/back-button"

export default function BackButtonDefaultTitle() {
  return <BackButton />
}
```

### Above a list

```tsx
import { BackButton } from "@/components/ui/back-button"
import { Island, ListRow, ListRowText, ListRowTitle, ListRowValue } from "@/components/ui/island"

const hours = [
  { days: "Monday", time: "Closed" },
  { days: "Tuesday to Friday", time: "12:00–23:00" },
  { days: "Saturday and Sunday", time: "11:00–23:30" },
]

export default function BackButtonAboveAList() {
  return (
    <div className="w-full max-w-md">
      <BackButton>Settings</BackButton>
      <h2 className="mt-2 mb-4 text-title1 font-bold text-ink">Opening hours</h2>
      <Island variant="list">
        {hours.map((row) => (
          <ListRow key={row.days}>
            <ListRowText>
              <ListRowTitle>{row.days}</ListRowTitle>
            </ListRowText>
            <ListRowValue className="tabular-nums">{row.time}</ListRowValue>
          </ListRow>
        ))}
      </Island>
    </div>
  )
}
```

## Source

```tsx
import { cn } from "@/lib/cn"
import { Button, type ButtonProps } from "@/components/ui/button"

export type BackButtonProps = Omit<ButtonProps, "variant" | "size" | "loading">

export function BackButton({ render, nativeButton, className, children = "Back", ...props }: BackButtonProps) {
  return (
    <Button
      data-slot="back-button"
      variant="plain"
      render={render}
      nativeButton={nativeButton ?? (render ? false : undefined)}
      className={cn("-ml-4 gap-0.5 px-2.5 text-[17px]", className)}
      {...props}
    >
      <svg viewBox="0 0 20 20" aria-hidden="true" className="fill-none stroke-current" strokeWidth="2.25" strokeLinecap="round" strokeLinejoin="round">
        <path d="m12.5 4.5-5.5 5.5 5.5 5.5" />
      </svg>
      {children}
    </Button>
  )
}
```
