# Link

## Introduction

The Link component is a navigational element that allows users to move between pages or access external resources. Built on top of Joy UI's Link, it provides consistent styling, multiple visual variants, and comprehensive accessibility features out of the box.

Links are fundamental to web navigation. Use them to connect related content, reference external documentation, or enable movement between sections of your application. For actions that do not navigate (such as submitting a form or opening a modal), use a Button instead.

```tsx
<Link
  children="Link"
  variant="plain"
/>
```

| Field     | Description | Default |
| --------- | ----------- | ------- |
| variant   | —           | "plain" |
| color     | —           | —       |
| level     | —           | —       |
| disabled  | —           | —       |
| underline | —           | —       |

## Usage

```tsx
import { Link } from '@ceed/cds';

function MyComponent() {
  return (
    <div>
      <Link href="/about">About Us</Link>

      <Link href="https://example.com" target="_blank" rel="noopener noreferrer">
        External Site
      </Link>

      <Link href="/contact" color="primary" underline="hover">
        Contact
      </Link>
    </div>
  );
}
```

## Features

### Variants

Links support four visual styles through the `variant` prop: `plain`, `outlined`, `soft`, and `solid`.

```
<Canvas of={Link.Variants} />
```

### Colors

Apply semantic colors to communicate different purposes -- `primary`, `neutral`, `success`, `warning`, and `danger`.

```
<Canvas of={Link.Colors} />
```

### Underline Styles

Control the underline behavior with three options: `none` (never shown), `hover` (shown on hover), and `always` (permanently visible).

```
<Canvas of={Link.Underline} />
```

### Typography Levels

Links can adopt any typography level from the design system, enabling consistent text hierarchies when links appear alongside headings or body text.

```
<Canvas of={Link.Levels} />
```

### Disabled State

Links can be disabled to prevent interaction while remaining visible in the layout.

```
<Canvas of={Link.Disabled} />
```

### External Links

For links that navigate away from your application, include `target="_blank"` and `rel="noopener noreferrer"` for security.

```
<Canvas of={Link.ExternalLink} />
```

### Inline with Text

Links integrate seamlessly within paragraph text, inheriting the surrounding font styles.

```
<Canvas of={Link.InlineWithText} />
```

## Common Use Cases

### Inline Text Links

```tsx
function ArticleContent() {
  return (
    <Typography level="body-md">
      For more information, visit our <Link href="/services">services page</Link> or read the{' '}
      <Link href="/faq">frequently asked questions</Link>.
    </Typography>
  );
}
```

### Footer Navigation

```tsx
function FooterLinks() {
  return (
    <Stack direction="row" spacing={2} divider={<Divider orientation="vertical" />}>
      <Link href="/privacy" level="body-sm" color="neutral">
        Privacy Policy
      </Link>
      <Link href="/terms" level="body-sm" color="neutral">
        Terms of Service
      </Link>
      <Link href="/cookies" level="body-sm" color="neutral">
        Cookie Policy
      </Link>
    </Stack>
  );
}
```

### Router Integration

```tsx
import { Link as RouterLink } from 'react-router-dom';

function AppNavigation() {
  return (
    <Link component={RouterLink} to="/dashboard">
      Go to Dashboard
    </Link>
  );
}
```

## Props and Customization

### Key Props

| Prop             | Type                                                           | Default     | Description                            |
| ---------------- | -------------------------------------------------------------- | ----------- | -------------------------------------- |
| `children`       | `ReactNode`                                                    | -           | Link content                           |
| `href`           | `string`                                                       | -           | Link URL                               |
| `level`          | Typography level                                               | `'body-md'` | Text size (inherits Typography levels) |
| `color`          | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'primary'` | Link color                             |
| `variant`        | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                   | -           | Visual style                           |
| `underline`      | `'none' \| 'hover' \| 'always'`                                | `'hover'`   | Underline behavior                     |
| `disabled`       | `boolean`                                                      | `false`     | Disables the link                      |
| `startDecorator` | `ReactNode`                                                    | -           | Content before link text               |
| `endDecorator`   | `ReactNode`                                                    | -           | Content after link text                |
| `component`      | `ElementType`                                                  | `'a'`       | Root element type (polymorphic)        |
| `sx`             | `SxProps`                                                      | -           | Custom styles                          |

> **Note**: Link also accepts all Joy UI Link props.

## Best Practices

- **Use descriptive link text.** The text should clearly communicate the destination. Avoid generic labels such as "click here" or "read more".

```tsx
// Good
<Link href="/pricing">View pricing plans</Link>

// Bad
<Link href="/pricing">Click here</Link>
```

- **Indicate external links.** When a link opens in a new tab, add a visual cue (such as an icon) and the appropriate security attributes.

```tsx
// Good
<Link href="https://docs.example.com" target="_blank" rel="noopener noreferrer">
  Documentation
</Link>

// Bad -- opens in new tab with no indication
<Link href="https://docs.example.com" target="_blank">
  Documentation
</Link>
```

- **Do not use links for actions.** If clicking triggers a side effect (form submission, modal opening, deletion), use a Button component instead.

```tsx
// Good
<Button onClick={handleSubmit}>Submit Form</Button>

// Bad
<Link onClick={handleSubmit}>Submit Form</Link>
```

- **Maintain visual consistency.** Use the same variant, color, and underline settings for links that serve the same purpose across different pages.

- **Match typography level to context.** When a link sits inside a heading or body text block, set its `level` prop to match the surrounding text for visual harmony.

## Accessibility

- **Semantic HTML**: Renders a native `<a>` element by default, ensuring proper semantics and browser behavior. The `component` prop allows substitution with router-specific elements.
- **Descriptive text**: Screen readers announce the link text as the accessible name. Always write link text that makes sense out of context since screen reader users often navigate by listing all links on a page.
- **Keyboard navigation**: Links are focusable with `Tab` and activated with `Enter`. Visible focus indicators are provided for keyboard users.
- **External link attributes**: Always add `target="_blank"` together with `rel="noopener noreferrer"` on external links to prevent security vulnerabilities and provide an `aria-label` that mentions the new tab behavior when appropriate.
