# Link

> An inline link inside a sentence, in accent with a soft underline that fills in on hover, and a small arrow when it leaves the app.

Docs: https://plumbago.ochotzas.com/components/link

## Installation

```sh
npx plumbago add link
```

This copies `components/ui/link.tsx` into your project, along with `cn`, and installs `@base-ui/react`.

## Usage

```tsx
import { Link } from "@/components/ui/link"

<p>
  Read the <Link href="/allergens">allergen guide</Link> before you publish.
</p>
```

## Examples

### Preview

```tsx
import { Link } from "@/components/ui/link"

export default function LinkDemo() {
  return (
    <p className="max-w-read text-body text-ink">
      Dishes marked as vegan are checked against the <Link href="/allergens">allergen guide</Link> before the menu goes live.
    </p>
  )
}
```

### External

```tsx
import { Link } from "@/components/ui/link"

export default function LinkExternal() {
  return (
    <p className="max-w-read text-body text-ink">
      Fish arrives every morning before 9.{" "}
      <Link href="https://example.com" target="_blank">
        Order from the supplier
      </Link>{" "}
      by 18:00 the day before.
    </p>
  )
}
```

### In a footnote

```tsx
import { Island, IslandFooter, ListRow, ListRowText, ListRowTitle, ListRowValue } from "@/components/ui/island"
import { Link } from "@/components/ui/link"

export default function LinkInAFootnote() {
  return (
    <div className="w-full max-w-md">
      <Island variant="list">
        <ListRow>
          <ListRowText>
            <ListRowTitle>Hold tables for</ListRowTitle>
          </ListRowText>
          <ListRowValue>15 min</ListRowValue>
        </ListRow>
        <ListRow>
          <ListRowText>
            <ListRowTitle>Cancellation fee</ListRowTitle>
          </ListRowText>
          <ListRowValue>€10 a guest</ListRowValue>
        </ListRow>
      </Island>
      <IslandFooter>
        Guests agree to these when they book. <Link href="/policy">Read the booking policy</Link>.
      </IslandFooter>
    </div>
  )
}
```

### Inside an island

```tsx
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle } from "@/components/ui/island"
import { Link } from "@/components/ui/link"

export default function LinkInsideAnIsland() {
  return (
    <Island variant="list" className="w-full max-w-md">
      <ListRow>
        <ListRowText>
          <ListRowTitle>Seasonal tasting menu</ListRowTitle>
          <ListRowSubtitle>
            Prices follow the <Link href="/suppliers">supplier list</Link>.
          </ListRowSubtitle>
        </ListRowText>
      </ListRow>
    </Island>
  )
}
```

### Link vs plain button

```tsx
import { Button } from "@/components/ui/button"
import { Link } from "@/components/ui/link"

export default function LinkVsPlainButton() {
  return (
    <div className="flex w-full max-w-md flex-col items-start gap-4">
      <p className="text-body text-ink">
        Tables are held for 15 minutes. See the <Link href="/policy">booking policy</Link> for details.
      </p>
      <Button variant="plain" size="sm" className="-ml-4">
        Show all reservations
      </Button>
    </div>
  )
}
```

## Built on Base UI

Link wraps these Base UI parts and keeps their props:

- [Merge props](https://base-ui.com/react/utils/merge-props)
- [Use render](https://base-ui.com/react/utils/use-render)

## Source

```tsx
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cn } from "@/lib/cn"

export type LinkProps = Omit<useRender.ComponentProps<"a">, "className"> & {
  external?: boolean
  className?: string
}

export function Link({ render, external, className, target, rel, children, ...props }: LinkProps) {
  const opensElsewhere = external ?? target === "_blank"
  const dataAttributes = { "data-slot": "link", "data-external": opensElsewhere ? "" : undefined }
  return useRender({
    defaultTagName: "a",
    render,
    props: mergeProps<"a">(
      {
        ...dataAttributes,
        target,
        rel: rel ?? (target === "_blank" ? "noreferrer" : undefined),
        className: cn(
          "cursor-pointer rounded-xs text-accent underline decoration-accent/40 decoration-1 underline-offset-[3px]",
          "transition-[text-decoration-color] duration-fast ease-out hover:decoration-accent active:decoration-accent",
          "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent [-webkit-tap-highlight-color:transparent]",
          className,
        ),
        children: opensElsewhere ? (
          <>
            {children}
            <svg
              viewBox="0 0 12 12"
              aria-hidden="true"
              className="ml-0.5 inline-block size-[0.7em] fill-none stroke-current align-[0.05em]"
              strokeWidth="1.6"
              strokeLinecap="round"
              strokeLinejoin="round"
            >
              <path d="M4 2.5h5.5V8M9.25 2.75 2.5 9.5" />
            </svg>
            {target === "_blank" && <span className="sr-only"> (opens in a new tab)</span>}
          </>
        ) : (
          children
        ),
      },
      props,
    ),
  })
}
```
