# Link
A styled anchor that can render a router link.
Source page: https://dinachi.dev/docs/components/link

## Installation
```bash
npx @dinachi/cli@latest add link
```
Adds to your project:
- `components/ui/link/link.tsx`
- `components/ui/link/index.ts`
Installs:
- `@base-ui/react` ^1.9.0
- `class-variance-authority` ^0.7.1

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

export function DocsLink() {
  return <Link href="/docs">Read the docs</Link>
}
```

## Examples

### Default
A link in the default style.
```tsx
import { Link } from '@/components/ui/link';

export function Example() {
  return <Link href="#">DinachiUI Documentation</Link>;
}
```

### Variants
Default, muted, plain and unstyled.
```tsx
import { Link } from '@/components/ui/link';

export function Example() {
  return (
    <div className="flex flex-col gap-4">
      <Link href="#" variant="default">Default link with underline</Link>
      <Link href="#" variant="muted">Muted link for secondary actions</Link>
      <Link href="#" variant="plain">Plain link without underline</Link>
      <Link href="#" variant="unstyled">Unstyled link for custom styling</Link>
    </div>
  );
}
```

### External
Opens in a new tab and shows an icon.
```tsx
import { Link } from '@/components/ui/link';

export function Example() {
  return (
    <div className="flex flex-col gap-4">
      <Link href="https://github.com" external>GitHub</Link>
      <Link href="https://react.dev" external variant="muted">React Documentation</Link>
    </div>
  );
}
```

### Render element
Swaps the anchor for another element, such as a router link.
```tsx
import { Link } from '@/components/ui/link';
// import NextLink from 'next/link';

export function Example() {
  // <Link render={<NextLink href="/about" />}>About</Link>
  return (
    <Link render={<button type="button" />}>
      Renders as a button
    </Link>
  );
}
```

### Render function
Spreads the props onto your own element.
```tsx
import { Link } from '@/components/ui/link';
// import NextLink from 'next/link';

export function Example() {
  // <Link render={(props) => <NextLink {...props} href="/about" />}>About</Link>
  return (
    <Link render={(props) => <button type="button" {...props} />} variant="muted">
      Render function
    </Link>
  );
}
```

## Notes

### Router links
`render` takes an element or a function, as in [Base UI](https://base-ui.com/react/utils/use-render). Either renders one element and one tab stop.

```tsx
import NextLink from "next/link"

<Link render={<NextLink href="/about" />}>About</Link>
<Link render={(props) => <NextLink {...props} href="/about" />}>About</Link>
```

React Router: `render={<RouterLink to="/about" />}`.

### Styling other elements
```tsx
import { linkVariants } from "@/components/ui/link"

<a className={linkVariants({ variant: "muted" })}>Styled anchor</a>
```

### Agents
Link does not register with the [agent bridge](/docs/components/agent). The bridge lists any `a[href]` as `link`, with the href as `value`, taking `press`. An `a` without `href` is not listed.

```ts
await agent.act({ steps: [{ ref: "Pricing", action: "press" }] })
```

## API reference

### Link
An `a` element styled by `linkVariants`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `href` **agent** | `string` | - | The destination. The agent reads it as value. |
| `variant` | `"default" \| "muted" \| "plain" \| "unstyled"` | `"default"` | The visual style. `unstyled` adds only the focus ring. |
| `external` | `boolean` | `false` | Opens in a new tab; adds an icon and screen reader note. |
| `target` | `string` | `"_blank" when external` | Overrides the external default. |
| `rel` | `string` | `"noopener noreferrer" when external` | Overrides the external default. |
| `render` | `ReactElement \| (props, state) => ReactElement` | - | Swap the element, such as a router link, keep the styles. |

### linkVariants
Link's class function, for other elements.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"default" \| "muted" \| "plain" \| "unstyled"` | `"default"` | The same styles Link takes. |

## Accessibility
- A native anchor, announced as a link and reached by Tab.
- External links add hidden "(opens in a new tab)" text; the icon is aria-hidden.
- The focus ring shows on keyboard focus only.
### Keyboard
| Keys | Does |
| --- | --- |
| `Enter` | Follows the link |

## Source
`components/ui/link/link.tsx`
```tsx
"use client"

import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { useRender } from "@base-ui/react/use-render"
import { cn } from "@/lib/utils"

const linkVariants = cva(
  "transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
  {
    variants: {
      variant: {
        default:
          "text-primary underline underline-offset-4 hover:text-primary/80",
        muted:
          "text-muted-foreground underline underline-offset-4 hover:text-foreground",
        plain: "text-foreground hover:text-primary",
        unstyled: "",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  }
)

export interface LinkProps
  extends React.AnchorHTMLAttributes<HTMLAnchorElement>,
    VariantProps<typeof linkVariants> {
  /** Render as a different element (e.g., Next.js Link, React Router Link).
   *  Accepts a ReactElement or a render function `(props, state) => ReactElement`. */
  render?: useRender.RenderProp
  /** Show external link indicator and add target="_blank" */
  external?: boolean
}

const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
  (
    { className, variant, render, external, children, rel, target, ...props },
    ref
  ) => {
    const externalProps = external
      ? {
          target: target ?? ("_blank" as const),
          rel: rel ?? "noopener noreferrer",
        }
      : { target, rel }

    const content = (
      <>
        {children}
        {external && (
          <>
            <svg
              className="inline-block h-3 w-3 shrink-0"
              fill="none"
              stroke="currentColor"
              strokeWidth={2}
              strokeLinecap="round"
              strokeLinejoin="round"
              viewBox="0 0 24 24"
              aria-hidden="true"
            >
              <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" />
              <polyline points="15 3 21 3 21 9" />
              <line x1="10" y1="14" x2="21" y2="3" />
            </svg>
            <span className="sr-only">(opens in a new tab)</span>
          </>
        )}
      </>
    )

    return useRender({
      defaultTagName: 'a',
      render,
      ref,
      props: {
        className: cn(
          linkVariants({ variant }),
          external && "inline-flex items-center gap-1",
          className
        ),
        ...externalProps,
        "data-slot": "link",
        ...props,
        children: content,
      },
    })
  }
)

Link.displayName = "Link"

export { Link, linkVariants }
```
