Link
Takes people to another page, or to another part of the same page.
Bring one valid ID. Check which IDs we accept.
Installation
npm install @base-ui/react class-variance-authority clsx tailwind-mergeAdd the cn helper if you haven't already:
import { clsx, type ClassValue } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";
const twMerge = extendTailwindMerge({
extend: {
theme: {
text: [
"display",
"heading-xl",
"heading-lg",
"heading-md",
"heading-sm",
"heading-xs",
"body-lg",
"body-md",
"body-sm",
"label",
"helper",
"button",
],
"font-weight": ["regular"],
},
},
});
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}Add the component:
"use client";
import { useRender } from "@base-ui/react/use-render";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
const linkVariants = cva(
[
"text-link-primary visited:text-link-visited hover:text-link-primary-hover",
"decoration-from-font [text-underline-position:from-font]",
"rounded-[2px] focus-visible:focus-ring",
"[&_svg]:shrink-0",
],
{
variants: {
size: {
medium: "text-body-md [&_svg]:size-5",
small: "text-body-sm [&_svg]:size-4",
},
standalone: {
true: "inline-flex items-center gap-1 hover:underline",
false: "underline",
},
},
defaultVariants: {
standalone: false,
},
},
);
type LinkProps = useRender.ComponentProps<"a"> &
VariantProps<typeof linkVariants>;
function Link({
className,
size,
standalone,
render,
ref,
...props
}: LinkProps) {
return useRender({
defaultTagName: "a",
render,
ref,
props: {
"data-slot": "link",
...props,
className: cn(linkVariants({ size, standalone }), className),
},
});
}
export { Link, linkVariants, type LinkProps };Usage
import { Link } from "@/components/ui/link";
<Link href="/requirements">Check what to bring</Link>;Examples
Standalone
Use a standalone link with a chevron for a "go to" link on its own line, one that takes people to the next step. Standalone links are underlined on hover only, because their position already shows they're links. Never put a chevron link inside a sentence.
With next/link
Pass your framework's link component to render. Habi's styles and props are merged onto it, and you keep client-side navigation and prefetching.
Read the Button guidance before you start.
External links
Open external links in the same tab. Only open a new tab when people would lose their place otherwise, like in the middle of a form, and say so in the link text: "Read the guidance (opens in new tab)".
Habi's icons come from Heroicons.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
standalone | boolean | false | For a link on its own line. Underlined on hover only, and lines up an icon after the text. |
size | "medium" | "small" | – | Sets the text size. Leave it out for links inside text, so they match the text around them. |
render | ReactElement | function | – | Renders a different element, like next/link, with Habi's styles merged on. |
Link also accepts every prop of the HTML <a> element, like href, target and rel.
Accessibility
- Underlines: links in text are always underlined, so people who can't see the color difference can still find them (WCAG 1.4.1).
- Link text: say where the link goes: "Check what to bring", not "Click here". Screen reader users often move from link to link, so the text has to make sense on its own.
- Keyboard: Tab moves to the link and Enter follows it. The focus ring shows where you are.
- Visited links turn purple, so people can see which pages they've already read.
- Links or buttons: use a link to go somewhere and a button to do something. If a link must look like a button, use
buttonVariants. - Icons are hidden from screen readers. The link text already says where it goes.
Figma
- Underlines: links in text are always underlined, so people who can't see the color difference can still find them (WCAG 1.4.1).
- Link text: say where the link goes: "Check what to bring", not "Click here". Screen reader users often move from link to link, so the text has to make sense on its own.
- Keyboard: Tab moves to the link and Enter follows it. The focus ring shows where you are.
- Visited links turn purple, so people can see which pages they've already read.
- Links or buttons: use a link to go somewhere and a button to do something. If a link must look like a button, use
buttonVariants. - Icons are hidden from screen readers. The link text already says where it goes.