# Anchor (/docs/anchor)





`Anchor` renders a themed `<a>`. It takes the size and weight of the text around it, renders through your router's `Link` when the app provides one, and gives a link that opens a new tab a safe `rel` and a warning.

```tsx
import { Anchor } from "@42-central/ui-react/anchor";

<Anchor href="/docs/colors">the colours page</Anchor>
<Anchor href="https://42.fr" external>42.fr</Anchor>
```

## Playground [#playground]

<story.Playground />

## Variants and colours [#variants-and-colours]

`variant` picks how the link stands out, and `color` picks the palette:

* `default` paints the link in its palette. With no `color` it uses the colours of Figma's link, the same as `<Button variant="link">`: primary text that dims on hover. With a `color` it uses that palette's text colour, which gets darker on hover.
* `subtle` keeps the surrounding text colour, and shows the palette only on hover.

<Preview align="block" code="&#x22;<Anchor href=\&#x22;#\&#x22;>default</Anchor>\n<Anchor href=\&#x22;#\&#x22; variant=\&#x22;subtle\&#x22;>subtle</Anchor>\n{COLORS.map((c) => <Anchor key={c} href=\&#x22;#\&#x22; color={c}>{c}</Anchor>)}&#x22;">
  <div className="flex flex-col items-center gap-3">
    <div className="flex flex-wrap gap-4">
      <Anchor href="#variants-and-colours">
        default
      </Anchor>

      <Anchor href="#variants-and-colours" variant="subtle">
        subtle
      </Anchor>
    </div>

    <div className="flex flex-wrap justify-center gap-3">
      {COLORS.map((c) => (
              <Anchor key={c} href="#variants-and-colours" color={c}>
                {c}
              </Anchor>
            ))}
    </div>
  </div>
</Preview>

## Underline [#underline]

`underline` sets when the underline shows: `always` (the default), `hover` or `never`. In running text, keep it `always`: a link without a colour is the same colour as the text around it, so the underline is the only thing that shows it is a link. `hover` and `never` are for a link whose place already shows it is one, like a menu, a table of contents or a footer.

<Preview align="block" code="&#x22;<Anchor href=\&#x22;#\&#x22; underline=\&#x22;always\&#x22;>always</Anchor>\n<Anchor href=\&#x22;#\&#x22; underline=\&#x22;hover\&#x22;>hover</Anchor>\n<Anchor href=\&#x22;#\&#x22; underline=\&#x22;never\&#x22;>never</Anchor>&#x22;">
  <div className="flex flex-wrap gap-4">
    <Anchor href="#underline" underline="always">
      always
    </Anchor>

    <Anchor href="#underline" underline="hover">
      hover
    </Anchor>

    <Anchor href="#underline" underline="never">
      never
    </Anchor>
  </div>
</Preview>

## Size [#size]

An `Anchor` has no font size of its own. It takes the size and weight of the text around it, so a link in a caption is caption-sized. Set `size` (the [`Text`](/docs/text) scale, `xs` to `4xl`) only for a standalone link.

<Preview align="block" code="&#x22;<Text size=\&#x22;lg\&#x22;>Read <Anchor href=\&#x22;#\&#x22;>the colours page</Anchor> before you pick a palette.</Text>\n<Text size=\&#x22;md\&#x22;>Read <Anchor href=\&#x22;#\&#x22;>the colours page</Anchor> before you pick a palette.</Text>\n<Text size=\&#x22;xs\&#x22; c=\&#x22;muted\&#x22;>Read <Anchor href=\&#x22;#\&#x22;>the colours page</Anchor> before you pick a palette.</Text>&#x22;">
  <AnchorInheritDemo />
</Preview>

## Subtle links [#subtle-links]

`variant="subtle"` with `underline="hover"` is the recipe for a list of links: they take the list's colour and only light up under the pointer.

<Preview code="&#x22;<nav aria-label=\&#x22;On this page\&#x22; className=\&#x22;text-sm text-black/50 dark:text-white/50\&#x22;>\n  <ul>\n    <li><Anchor href=\&#x22;#installation\&#x22; variant=\&#x22;subtle\&#x22; underline=\&#x22;hover\&#x22;>Installation</Anchor></li>\n    <li><Anchor href=\&#x22;#usage\&#x22; variant=\&#x22;subtle\&#x22; underline=\&#x22;hover\&#x22;>Usage</Anchor></li>\n  </ul>\n</nav>&#x22;">
  <AnchorSubtleDemo />
</Preview>

## External links [#external-links]

`external` is for a link that leaves the app. It:

* opens in a new tab (`target="_blank"`, unless you set another `target`);
* adds `rel="noopener noreferrer"` to the `rel` you pass, so the new page can't reach back into yours through `window.opener` and isn't sent your URL as the referrer;
* renders a plain `<a>`, not your router's `Link`: a router can't navigate off-site, and some reject an absolute URL;
* ends with an arrow, and a visually hidden "(opens in a new tab)" for screen readers.

<Preview code="&#x22;<Text>\n  Questions? Ask on <Anchor href=\&#x22;https://42.fr\&#x22; external>42.fr</Anchor>.\n</Text>&#x22;">
  <Text>
    Questions? Ask on 

    <Anchor href="https://42.fr">42.fr</Anchor>

    .
  </Text>
</Preview>

Any `target="_blank"` gets the same safe `rel`, without the arrow or the note. Use that for a file that opens in its own tab inside your app.

The arrow is the `external` key of `icons`. Pass another icon, or `null` to draw none; the hidden note stays either way. The kit's arrow flips in right-to-left text. An icon you pass is drawn as given, so add `rtl:-scale-x-100` if it points along the line, and `className={iconStroke}` (from `@42/ui-react`) for the kit's 1.5px line. The note comes from `translations.external`.

<Preview code="&#x22;<Anchor\n  href=\&#x22;https://42.fr\&#x22;\n  external\n  icons={{ external: <ExternalLink className={iconStroke} /> }}\n  translations={{ external: \&#x22;(nouvel onglet)\&#x22; }}\n>\n  42.fr\n</Anchor>&#x22;">
  <Anchor href="https://42.fr" icons="{ external: <ExternalLink className={iconStroke} /> }" translations="{ external: &#x22;(nouvel onglet)&#x22; }">
    42.fr
  </Anchor>
</Preview>

## Router links [#router-links]

Wrap the app once in `LinkComponentProvider` with your router's `Link`, and every `Anchor` renders through it, so navigation stays client-side. [`NavLink`](/docs/nav-link) and [`Spotlight`](/docs/spotlight) use the same provider. Pass `linkComponent` to override it for one link. See [`useLinkComponent`](/docs/hooks/use-link-component).

```tsx
import { LinkComponentProvider } from "@42-central/ui-react";
import Link from "next/link";

<LinkComponentProvider component={Link}>
  <App />
</LinkComponentProvider>;
```

The router's `Link` gets every prop `Anchor` would put on its `<a>`, `href` included. A router whose `Link` takes `to` instead (React Router) needs a two-line wrapper that renames the prop:

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

const Link = ({ href, ...props }: { href?: string }) => <RouterLink to={href ?? "#"} {...props} />;
```

In this demo, `RouterLink` stands in for a router's `Link`: it records where it would navigate instead of loading a page. The external link stays a plain `<a>` either way.

<Preview code="&#x22;<LinkComponentProvider component={RouterLink}>\n  <Text>\n    Open <Anchor href=\&#x22;#settings\&#x22;>Settings</Anchor>, or read about it on{\&#x22; \&#x22;}\n    <Anchor href=\&#x22;https://42.fr\&#x22; external>42.fr</Anchor>.\n  </Text>\n</LinkComponentProvider>&#x22;">
  <AnchorRouterDemo />
</Preview>

For one link, `asChild` renders your own element with the anchor's styles, and wins over both the provider and `linkComponent`. With `external`, the arrow and the note go inside your element.

```tsx
<Anchor asChild color="blue">
  <Link to="/settings" params={{ tab: "profile" }}>
    Profile settings
  </Link>
</Anchor>
```

## Server components [#server-components]

`Anchor` is a client component (`"use client"`). It has to read `LinkComponentProvider`, which is a React context, and only a client component can read one. You can still render it from a server component: pass `children`, `href` and the other props as usual. [`Breadcrumb`](/docs/breadcrumb) stays a server component because it takes only a per-instance `linkComponent` and ignores the provider.

## Anatomy [#anatomy]

`Anchor` is a single part. It renders one element: the `<a>`, your router's `Link`, or your `asChild` element.

```tsx
<a data-color="blue" data-external data-testid="docs-link" target="_blank" rel="noopener noreferrer">
  42.fr
  <span aria-hidden="true"><svg /></span>  {/* external only: icons.external */}
  <span className="sr-only"> (opens in a new tab)</span>  {/* external only: translations.external */}
</a>
```

`testId` renders as `data-testid` on that element, whichever it is. See [Testing](/docs/testing). `data-external` is present on an external link, and `data-color` only when `color` is set.

## API [#api]

<TypeTable
  type="{
  href: {
    type: &#x22;string&#x22;,
    description: &#x22;Where the link goes. Passed to your router's Link as is.&#x22;,
  },
  variant: {
    type: '&#x22;default&#x22; | &#x22;subtle&#x22;',
    default: '&#x22;default&#x22;',
    description: &#x22;default paints the link in its palette; subtle keeps the surrounding colour until hover.&#x22;,
  },
  color: {
    type: &#x22;'brand' | 'neutral' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'cyan' | 'blue' | 'purple' | 'pink' | (string & {})&#x22;,
    description: &#x22;The palette. Without it, the link is primary text, like Figma's link and Button's link variant.&#x22;,
  },
  underline: {
    type: '&#x22;always&#x22; | &#x22;hover&#x22; | &#x22;never&#x22;',
    default: '&#x22;always&#x22;',
    description: &#x22;When the underline shows. Keep always in running text.&#x22;,
  },
  size: {
    type: '&#x22;xs&#x22; | &#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22; | &#x22;2xl&#x22; | &#x22;3xl&#x22; | &#x22;4xl&#x22;',
    description: &#x22;Font size from the Text scale. Inherited from the surrounding text when unset.&#x22;,
  },
  external: {
    type: &#x22;boolean&#x22;,
    default: &#x22;false&#x22;,
    description:
      'A link that leaves the app: target=&#x22;_blank&#x22;, rel=&#x22;noopener noreferrer&#x22; added to yours, a plain <a> instead of the router\'s Link, an arrow and a hidden &#x22;(opens in a new tab)&#x22;.',
  },
  linkComponent: {
    type: &#x22;ElementType&#x22;,
    description: &#x22;Overrides LinkComponentProvider for this link. Must accept href.&#x22;,
  },
  icons: {
    type: &#x22;{ external?: ReactNode }&#x22;,
    description: &#x22;Replaces the arrow after an external link. null draws none; the hidden note stays.&#x22;,
  },
  translations: {
    type: &#x22;{ external?: string }&#x22;,
    default: '{ external: &#x22;(opens in a new tab)&#x22; }',
    description: &#x22;The hidden note read after an external link's text.&#x22;,
  },
  asChild: {
    type: &#x22;boolean&#x22;,
    default: &#x22;false&#x22;,
    description: &#x22;Renders your single child element with the anchor's styles and props. Wins over the provider and linkComponent.&#x22;,
  },
  ref: {
    type: &#x22;Ref<HTMLAnchorElement>&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the link element. See [Testing](/docs/testing).&#x22;,
  },
}"
/>

Every other prop is forwarded to the link element. A `rel` you pass is kept; on a link that opens a new tab, `noopener` and `noreferrer` are added to it.

## Accessibility [#accessibility]

* It's a real link: an `<a href>` (or your router's, or your `asChild` element), so it is focusable, follows on Enter, and shows the kit's focus ring.
* Keep `underline="always"` in running text. Colour alone doesn't mark a link for everyone ([WCAG 1.4.1](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)), and a link without a `color` is the colour of the text around it.
* An `external` link says it opens a new tab before it does: the arrow for sighted users, and "(opens in a new tab)" in its accessible name for screen readers ("42.fr (opens in a new tab)"). The arrow itself is `aria-hidden`.
* A link's text should say where it goes. "Read the colours page" works out of context; "click here" doesn't.
