<!-- generated by scripts/gen-docs.mjs from docs/system/back-link.md. Edit the source and rerun. -->
# BackLink

## Description
Takes you one level up to the list you came from, the same way on every page.

`import { BackLink } from "@/components/cobalt/ui/back-link"`, source `registry/cobalt/ui/back-link.tsx`, status `ready-with-gaps`.
Foundation: `hand-rolled`. Traps checked: `trap/link-as-button` (Description and Usage, `rule/back-link-is-a-link-to-the-list`: the one deliberate exception, a link drawn as the grey secondary button because it is a control on every page and must read as one before it is hovered, as the source says; it stays an `<a href>`), `trap/link-cue` (States, default: the resting cue is the grey fill, measured 17.03:1 for its text, not an underline), `trap/weight-shift` (States, hover: the fill deepens one step and the weight stays 500, measured), `trap/current-unmarked` (n/a, a back link does not point at the current page), `trap/tabs-routes` (n/a, one link, no set), `trap/narrow-hidden-nav` (Limits, `rule/back-link-label-length`: the label does not wrap, so it leaves the column past 42 characters), `trap/safe-area` (n/a, it sits in the page flow, away from the screen edges), `trap/external-glyph-internal` (Usage, `rule/back-link-arrow-is-the-only-glyph`: the arrow points into the app, no external glyph), `trap/hover-unguarded` (States, hover: Tailwind's `hover:` sits under `(hover: hover)`, and nothing is revealed on hover), `trap/motion-origin`, `trap/motion-frequent`, `trap/motion-layout-property`, `trap/reduced-motion-ignored` (Motion: the colour change on `--t-press` and the `pressable` tint are the only motion; `base.css` keeps the colour under reduce and the tint is instant).

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Not applicable: hand-rolled | An `<a href>` through `renderLink` from `registry/cobalt/lib/link.tsx` (D5, D19), drawn with `buttonVariants({ variant: "secondary" })` from the Button, so it is `--control` tall, `rounded-lg`, grey and `font-medium` with the `pressable` tint and no shadow (D11, D20), the same box as a secondary button; fully round is kept for pills that are on or off; `w-fit`, `max-w-full`, `gap-1.5` and `ps-2.5` for the 16px `ArrowLeftIcon`, `aria-hidden`, before the label, turned by `rtl:rotate-180` so it points the way back on a right-to-left page; the label in a span cut with an ellipsis, given `dir="auto"`, and whole in the system tooltip while it is cut (`TruncatedText`); `children` typed `string`, so the label is always text |

## Examples
A record's page with the way back to its list above the title, drawn by your router's link so going back does not reload the app.

Real uses, 0 call sites (`rg -n "<BackLink\b" app registry --glob '!registry/cobalt/ui/*'`): the showcase has no record pages under a list yet, and the examples folder does not count.

Uses inside the registry: none. It borrows `buttonVariants` from the Button, `registry/cobalt/ui/back-link.tsx:28`.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/back-link/default.tsx` | default | A record's page with the way back to its list above the title, drawn by your router's link so going back does not reload the app |
| Not applicable: hover is reached with the pointer on any example | state:hover | The grey fill one step deeper, `--secondary-hover` |
| Not applicable: focus-visible is reached with the keyboard on any example | state:focus-visible | The 2px ring around the button |
| Not applicable: pressed is the `--hover` tint while the pointer is down on any example | state:pressed | The press tint over the fill |
| `docs/system/examples/back-link/in-card.tsx` | composition:Card | Above a record's card in a side panel; a way back too long for the column ends in an ellipsis and shows whole on hover |

Example: A record's page with the way back to its list above the title, drawn by your router's link so going back does not reload the app (`docs/system/examples/back-link/default.tsx`)

```tsx
import { useState } from "react";

import { BackLink } from "@/components/cobalt/ui/back-link";
import { TextLink } from "@/components/cobalt/ui/text-link";
import type { RenderLink } from "@/lib/cobalt/link";

const events = [
  { href: "/events/autumn-reading-night", title: "Autumn reading night", when: "Thu, Oct 23, 7:00 PM" },
  { href: "/events/motion-study-group", title: "Motion study group", when: "Wed, Oct 29, 6:30 PM" },
];

export default function Example() {
  // Stands in for your router: it holds the path, and its Link changes it.
  const [path, setPath] = useState(events[0]!.href);
  const routerLink: RenderLink = ({ href, children, ...props }) => (
    <a
      {...props}
      href={href}
      onClick={(event) => {
        event.preventDefault();
        setPath(href);
      }}
    >
      {children}
    </a>
  );
  const open = events.find((event) => event.href === path);
  if (!open)
    return (
      <ul className="flex max-w-sm flex-col gap-2 text-sm">
        {events.map((event) => (
          <li key={event.href}>
            <TextLink href={event.href} renderLink={routerLink}>
              {event.title}
            </TextLink>
          </li>
        ))}
      </ul>
    );
  return (
    <div className="flex max-w-sm flex-col items-start gap-4">
      <BackLink href="/events" renderLink={routerLink}>
        All events
      </BackLink>
      <div className="flex flex-col gap-1">
        <h3 className="text-xl font-semibold tracking-tight">{open.title}</h3>
        <p className="text-sm text-muted-foreground">{open.when}</p>
      </div>
    </div>
  );
}
```

Example: Above a record's card in a side panel; a way back too long for the column ends in an ellipsis and shows whole on hover (`docs/system/examples/back-link/in-card.tsx`)

```tsx
import { BackLink } from "@/components/cobalt/ui/back-link";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/cobalt/ui/card";

export default function Example() {
  return (
    <div className="flex w-full max-w-60 flex-col gap-4">
      <BackLink href="#events">All events at the Northgate library</BackLink>
      <Card>
        <CardHeader>
          <CardTitle>Autumn reading night</CardTitle>
          <CardDescription>Thu, Oct 23, 7:00 PM</CardDescription>
        </CardHeader>
        <CardContent>
          <p className="text-sm text-muted-foreground">18 going, 4 on the waitlist.</p>
        </CardContent>
      </Card>
    </div>
  );
}
```

## Variants
Not applicable: one look, the grey button with the arrow. `BackLink` has no variant axis, and `className` is for placement.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| default | render | follow it, focus it | the grey button, 36px tall with a 10px corner, the arrow before the label; an `<a href>` in the tree | test |
| hover | pointer over, under `(hover: hover)` | follow it | pointer only, the fill one step deeper on `--t-press`; no shape or weight change | by hand |
| focus-visible | keyboard focus | follow it with Enter | the 2px ring outside the corner | by hand |
| pressed | pointer down | release to follow | the `--hover` tint over the fill while the pointer is down | by hand |

### State precedence
- Hover and focus-visible: both show. The fill is a background and the ring is an outline.
- Pressed and hover: both show. The tint sits over the deeper fill.
- Pressed and focus-visible: both show. The ring stays as it is and the tint sits inside it.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| pointer-down to pointer-up | input | `pressable`, `--t-press` (140ms) on `--curve` | box-shadow, color | `box-shadow` is not in the reduced-motion list, so the press tint is instant; the colour change stays |
| pointer over and out | announce | `--t-press` on `--curve` | background-color, color | the same colour change |

## Props

<!-- Props generated from registry/cobalt/ui/back-link.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `children` | `string` |  |  |
| `href` | `string` |  |  |
| `className?` | `string` |  |  |
| `renderLink?` | `RenderLink` | `defaultLink` |  |

`BackLink` takes `href` (the list's address), `children` (the label, a `string`), `renderLink` (`RenderLink` from `registry/cobalt/lib/link.tsx`, a plain anchor by default) and `className` for placement. It forwards no native attributes; the host's link receives `href`, `className` and `children` and may add its own.

## Usage

### When to use
- A page sits one level under a list, such as an event under "All events", and you need the way back above the title.
- The way back must look the same on every page of the app.

### When not to use
- The path is deeper than one level and every ancestor must show. Use Nav for the sections and a plain row of text links for the ancestors, the Breadcrumb row of coverage-gaps, instead.
- The link moves between sections of the app. Use Nav instead.
- The link sits in running text. Use TextLink instead.
- The control runs an action and goes nowhere, such as closing a sheet. Use Button instead.

### Rules
- `rule/back-link-is-a-link-to-the-list`: When a page offers the way back, give `BackLink` the list's `href` and the host's `renderLink`, instead of a `Button` that calls `history.back()`, because a deep-linked page has no history inside the app, so back leaves the site, and a real link opens in a new tab and prefetches. Evidence: person D5; principle platform: `history.back()` on a page opened from a link outside the app goes to the previous site. Check: test `back-link.test.tsx` renders an `<a href>` and no `<button`; review of every "Back" control for a `navigate(-1)`.
  - Don't: `<Button variant="ghost" onClick={() => navigate(-1)}><ArrowLeftIcon /> Back</Button>`
  - Do: `<BackLink href="/events" renderLink={renderLink}>All events</BackLink>`
- `rule/back-link-one-per-page-above-the-title`: When a record page renders a back link, render 1, as the first element above the page title, instead of a second one lower on the page or one in the footer, because the way back is found by its place, and two of them promise two different destinations. Evidence: principle heuristic: consistency and standards, one control in one place; principle wcag: 3.2.3 Consistent navigation, the same mechanism in the same relative order. Check: review of every page that renders `BackLink`.
  - Don't: `<h1>Autumn reading night</h1> ... <BackLink href="/events">All events</BackLink> ... <BackLink href="/">Home</BackLink>`
  - Do: `<BackLink href="/events">All events</BackLink> <h1>Autumn reading night</h1>`
- `rule/back-link-router-link`: When the host has a router, pass its `Link` through `renderLink` and spread the given `className`, because the default is a plain anchor that reloads the app, and the button classes are what make it the same control on every page. Evidence: person D19; single use docs/system/examples/back-link/default.tsx:16-18, the example shows the host's link keeping the given `className`. Check: test `back-link.test.tsx` renders through a custom link and keeps the secondary button classes.
  - Don't: `<BackLink href="/people">People</BackLink>` in an app with a router
  - Do: `<BackLink href="/people" renderLink={({ href, className, children }) => <Link to={href} className={className}>{children}</Link>}>People</BackLink>`

### Content
- Follows `rule/writing-link-object`.
- `rule/back-link-names-the-list`: When the label is written, name the list the link goes to as an object in sentence case, such as "All events" or "People", at most 24 characters, instead of "Back", "Go back" or "Back to the previous page", because the label is the only thing that says where you land, and the arrow already says back. Evidence: person D11; principle wcag: 2.4.4 Link purpose in context, the text alone identifies the target. Check: lint copy/template and copy/length on the link slot (scripts/copy-check.mjs), which lists BackLink.
  - Don't: `<BackLink href="/events">Back</BackLink>`
  - Do: `<BackLink href="/events">All events</BackLink>`

### Anti-slop
- `rule/back-link-arrow-is-the-only-glyph`: When an agent writes the way back as "← Back" or "< Back" in text, or adds a chevron of its own, render `BackLink`, whose `ArrowLeftIcon` is `aria-hidden` and whose label is the list's name, because a text arrow is read aloud as "leftwards arrow" or "less than" and the glyph is already drawn once. Evidence: person D12; principle wcag: 1.3.1 Info and relationships, a decorative glyph is hidden from the reader. Check: review of link labels starting with a punctuation glyph.
  - Don't: `<a href="/events" className="text-sm">← Back</a>`
  - Do: `<BackLink href="/events">All events</BackLink>`
- `rule/back-link-no-ghost-or-underline`: When an agent restyles the way back as a ghost `Button`, an underlined text link or a pill with a shadow through `className`, keep `className` for placement and let the grey button stand, because the resting fill is what makes it read as a control before it is hovered, and the owner banned shadows on flat controls. Evidence: person D11; measured it at 36px with no shadow and its text at 17.03:1, `.design-system/evidence/back-link/measure-390.json`. Check: lint rule/component-override (scripts/check-system.mjs).
  - Don't: `<BackLink href="/events" className="bg-transparent underline shadow-sm">All events</BackLink>`
  - Do: `<BackLink href="/events" className="mb-4">All events</BackLink>`

### Limits
- `rule/back-link-label-length`: When a label would run past 42 characters at 390px, shorten it to the list's name instead, because the label never wraps (`white-space: nowrap`), so at 43 characters the button reaches the 310px column and the label is cut with an ellipsis, read whole only in the tooltip. Evidence: measured overflow of the column at 43 characters (318.7 > 310px) and of the viewport at 48, `.design-system/evidence/back-link/measure-390.json`. The cut, with the whole label in the tooltip, measured at 320px, docs/research/2026-10-16-break-ui-display.md. Check: probe `.design-system/scripts/measure-menus-nav.mjs` at 390; lint copy/length on the link slot at 24 characters (scripts/copy-check.mjs).
  - Don't: `<BackLink href="/events">All events in the whole workspace this year and before</BackLink>`
  - Do: `<BackLink href="/events">All events</BackLink>`

## Accessibility
Rests on a plain `<a href>`, or the host's link through `renderLink`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the link | focuses it and shows the ring | the link | by hand |
| Enter | the link | follows it to the list | where the router puts it, the list page | by hand |
| Space | the link | nothing, since a link is not a button | the link | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| root | link | its text | `href` | "link, All events" | test |
| arrow | none | none, `aria-hidden="true"` | none | nothing | test |

## Tokens
| Part | State | Token |
|---|---|---|
| root | all | `--control` through `min-h-(--control)`, `--r-lg`, `--t-press`, `--curve`, `--ring` |
| root | rest | `--secondary`, `--secondary-foreground` |
| root | hover | `--muted-hover` |
| arrow | all | `size-4`, `currentColor` |

## Related
- Nav: the way between sections.
- Button: the control this link borrows its look from, the secondary variant.
- TextLink: a link written as text, inside a sentence or beside a heading.
- Pill: a choice that is on or off.
- IconButton: names a button that is only an icon once, so the one string is read aloud by a screen reader and shown on hover as the tooltip.
