Anchor
PolymorphicA themed link for running text and standalone links, rendered through your router's Link and safe when it opens a new tab.
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.
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
Component
Dir
Presets
Props
Code
<Component underline="always" external={false}>
Read the guide
</Component>Variants and colours
variant picks how the link stands out, and color picks the palette:
defaultpaints the link in its palette. With nocolorit uses the colours of Figma's link, the same as<Button variant="link">: primary text that dims on hover. With acolorit uses that palette's text colour, which gets darker on hover.subtlekeeps the surrounding text colour, and shows the palette only on hover.
Code
<Anchor href="#">default</Anchor>
<Anchor href="#" variant="subtle">subtle</Anchor>
{COLORS.map((c) => <Anchor key={c} href="#" color={c}>{c}</Anchor>)}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.
Code
<Anchor href="#" underline="always">always</Anchor>
<Anchor href="#" underline="hover">hover</Anchor>
<Anchor href="#" underline="never">never</Anchor>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 scale, xs to 4xl) only for a standalone link.
Read the colours page before you pick a palette.
Read the colours page before you pick a palette.
Read the colours page before you pick a palette.
Code
<Text size="lg">Read <Anchor href="#">the colours page</Anchor> before you pick a palette.</Text>
<Text size="md">Read <Anchor href="#">the colours page</Anchor> before you pick a palette.</Text>
<Text size="xs" c="muted">Read <Anchor href="#">the colours page</Anchor> before you pick a palette.</Text>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.
Code
<nav aria-label="On this page" className="text-sm text-black/50 dark:text-white/50">
<ul>
<li><Anchor href="#installation" variant="subtle" underline="hover">Installation</Anchor></li>
<li><Anchor href="#usage" variant="subtle" underline="hover">Usage</Anchor></li>
</ul>
</nav>External links
external is for a link that leaves the app. It:
- opens in a new tab (
target="_blank", unless you set anothertarget); - adds
rel="noopener noreferrer"to therelyou pass, so the new page can't reach back into yours throughwindow.openerand isn't sent your URL as the referrer; - renders a plain
<a>, not your router'sLink: 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.
Questions? Ask on 42.fr (opens in a new tab).
Code
<Text>
Questions? Ask on <Anchor href="https://42.fr" external>42.fr</Anchor>.
</Text>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.
Code
<Anchor
href="https://42.fr"
external
icons={{ external: <ExternalLink className={iconStroke} /> }}
translations={{ external: "(nouvel onglet)" }}
>
42.fr
</Anchor>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 and Spotlight use the same provider. Pass linkComponent to override it for one link. See useLinkComponent.
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:
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.
Open Settings, or read about it on 42.fr (opens in a new tab).
Settings renders through RouterLink; 42.fr stays a plain <a>. Click one.
Code
<LinkComponentProvider component={RouterLink}>
<Text>
Open <Anchor href="#settings">Settings</Anchor>, or read about it on{" "}
<Anchor href="https://42.fr" external>42.fr</Anchor>.
</Text>
</LinkComponentProvider>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.
<Anchor asChild color="blue">
<Link to="/settings" params={{ tab: "profile" }}>
Profile settings
</Link>
</Anchor>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 stays a server component because it takes only a per-instance linkComponent and ignores the provider.
Anatomy
Anchor is a single part. It renders one element: the <a>, your router's Link, or your asChild element.
<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. data-external is present on an external link, and data-color only when color is set.
API
Prop
Type
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
- It's a real link: an
<a href>(or your router's, or yourasChildelement), 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), and a link without acoloris the colour of the text around it. - An
externallink 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 isaria-hidden. - A link's text should say where it goes. "Read the colours page" works out of context; "click here" doesn't.