Components
Link
A link to a page, inline with text, that navigates on the client and handles links to other sites.
Introduction
Link is the text link: a word or a phrase that goes to another page. It is the one to use in a paragraph, a footer, a breadcrumb or a list of references. It goes through next/link, so moving around your site is fast, and it looks after the details of links that leave it.
- Inline: it wraps with the text around it and takes its size, so it can sit in a sentence.
- A link to another site (
https://…) opens in a new tab withrel="noopener noreferrer", gets an arrow, and says "(opens in a new tab)" to screen readers. - Three looks: underlined, underlined on hover, and muted.
isCurrentmarks the page the user is on, andisDisabledturns the link off.- Takes every prop
next/linkdoes.
For a link that looks like a button, use Button with an href.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Link needs Next.js for next/link.
# bun
bun add tailwind-variants tailwind-merge clsx
# pnpm
pnpm add tailwind-variants tailwind-merge clsx
# npm
npm install tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/link-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Link. It is `inline`, not `inline-flex`, so a long link wraps over
// several lines like the text around it. The size is inherited unless set.
export const linkStyles = tv({
slots: {
root: [
"rounded-sm outline-none transition-colors",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-bg",
"aria-[current=page]:font-medium",
"aria-disabled:pointer-events-none aria-disabled:cursor-not-allowed aria-disabled:opacity-50",
],
icon: "ml-0.5 inline size-[0.85em] align-[-0.1em]",
},
variants: {
variant: {
// Underlined all the time: the clearest inside a paragraph.
underline: {
root: "text-fg underline decoration-fg/40 underline-offset-4 hover:decoration-fg",
},
// Looks like text until you point at it.
hover: { root: "text-fg underline-offset-4 hover:underline" },
// Quiet: for footers and secondary navigation.
muted: {
root: "text-muted-fg underline-offset-4 hover:text-fg hover:underline",
},
},
size: {
inherit: {},
sm: { root: "text-sm" },
md: { root: "text-base" },
lg: { root: "text-lg" },
},
},
defaultVariants: { variant: "underline", size: "inherit" },
});2. Add the Link file
src/components/ui/link/link.tsxShow code
import NextLink from "next/link";
import type { ComponentProps, ReactNode } from "react";
import { ArrowUpRightIcon } from "@/components/icons";
import { cn } from "@/lib/cn";
import { linkStyles } from "@/types/link-styles";
type Href = ComponentProps<typeof NextLink>["href"];
export interface LinkProps
extends Omit<ComponentProps<typeof NextLink>, "className" | "children"> {
href: Href;
children?: ReactNode;
/** Underlined all the time, underlined on hover, or muted until hover. */
variant?: "underline" | "hover" | "muted";
/** Takes the size of the text around it unless set. */
size?: "sm" | "md" | "lg";
/**
* Open in a new tab, and mark it for screen readers. Defaults to `true` for
* a link to another site (`https://…`) and `false` for the rest.
*/
isExternal?: boolean;
/** An arrow after an external link. Defaults to `true`. */
showIcon?: boolean;
/** The link to the page the user is on: marks it with `aria-current="page"`. */
isCurrent?: boolean;
/** Not clickable, and not in the tab order. Rendered without a destination. */
isDisabled?: boolean;
className?: string;
}
const isOtherSite = (href: Href) =>
typeof href === "string"
? /^https?:\/\//i.test(href)
: Boolean(href.protocol || href.host || href.hostname);
/**
* A link to a page. It goes through `next/link`, so internal navigation
* happens on the client with prefetching, and a link to another site opens in
* a new tab with an arrow and a hidden note for screen readers.
*
* It renders inline and takes the size of the text around it, so it can sit in
* a sentence. For a link that looks like a button, use `Button` with `href`.
*
* @example
* <p>Read the <Link href="/docs">docs</Link> or the
* <Link href="https://github.com/">source</Link>.</p>
*/
export function Link({
variant,
size,
isExternal,
showIcon = true,
isCurrent = false,
isDisabled = false,
className,
children,
href,
...props
}: LinkProps) {
const styles = linkStyles({ variant, size });
const external = isExternal ?? isOtherSite(href);
const classes = cn(styles.root(), className);
// A disabled link keeps its look and its role, and loses the destination.
if (isDisabled) {
return (
// biome-ignore lint/a11y/useFocusableInteractive: a disabled link is deliberately left out of the tab order
// biome-ignore lint/a11y/useSemanticElements: an anchor without href is not a link to assistive tech; role="link" with aria-disabled is how a disabled link is announced
<span role="link" aria-disabled="true" className={classes}>
{children}
</span>
);
}
if (external) {
return (
<NextLink
{...props}
href={href}
target="_blank"
rel="noopener noreferrer"
aria-current={isCurrent ? "page" : undefined}
className={classes}
>
{children}
{showIcon ? <ArrowUpRightIcon className={styles.icon()} /> : null}
<span className="sr-only"> (opens in a new tab)</span>
</NextLink>
);
}
return (
<NextLink
{...props}
href={href}
aria-current={isCurrent ? "page" : undefined}
className={classes}
>
{children}
</NextLink>
);
}It imports ArrowUpRightIcon from @/components/icons. Add the icon, or swap it for your own. It has no client code, so it works in server components.
3. Use it
import { Link } from "@/components/ui/link/link";Anatomy
An anchor, and for a link to another site an arrow and a hidden note.
<Link href="/docs">Read the docs</Link>
<Link href="https://github.com/">Source on GitHub</Link>| Part | Rendered as | Notes |
|---|---|---|
| Link | a (next/link) | Inline, so it wraps with the text around it and takes its size. Internal links navigate on the client. |
| Arrow | svg | After a link to another site, unless showIcon is false. |
| Hidden note | span, screen readers only | "(opens in a new tab)" after the text of an external link. |
Examples
Links here go to real pages of this site, and to a few other sites.
Usage
In a paragraph
The link takes the font size of its paragraph, and wraps with it.
centerUI is a set of components you copy into your project. Start with the introduction, then browse the components, or read the source on GitHub (opens in a new tab).
Variants
Underline is the clearest in a paragraph, because color alone is not enough to tell a link from text. Hover and muted suit menus, footers and lists.
Sizes
External
A link to another site opens in a new tab with an arrow. Turn the arrow off with showIcon, or decide for yourself with isExternal, for example to open a PDF of your own site in a new tab.
Current page
isCurrent marks the link to the page the user is on, with aria-current="page", and makes it bolder.
Disabled
A disabled link is announced as disabled, cannot be clicked, and is left out of the tab order. It has no destination.
next/link props
Everything next/link accepts works here, such as an object for href, replace, scroll, prefetch or onClick.
Component API
Link takes the props below, and the rest go to next/link.
| Prop | Type | Default |
|---|---|---|
| href | string | UrlObject | - (required) |
| variant | "underline" | "hover" | "muted" | "underline" |
| size | "sm" | "md" | "lg" | inherited |
| isExternal | boolean | true for https://… |
| showIcon | boolean | true |
| isCurrent | boolean | false |
| isDisabled | boolean | false |
| className | string | - |
| …next/link props | prefetch, replace, scroll, onClick, … | - |