Link
A link to another page, in a sentence, in a navigation list or on a line of its own. It inherits the size of the text around it. An external link opens a new tab and says so. For an action that changes something use the button; for a link that looks like a button use the button classes.
Install
npx shadcn@latest add https://thrumui.dev/r/link.jsonAdds components/ui/link.tsx. Nothing else in your project changes.
import { linkClass } from "@/components/ui/link";Automatically installs icons arrow-right and arrow-up-right from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "inline" | "subtle" | "standalone" | Default "inline" | inline is underlined, for a link inside a sentence. subtle is muted and underlined on hover, for a list of links. standalone is medium weight with a trailing arrow, for a link on a line of its own. |
external | boolean | Default false | Adds target="_blank" and rel="noreferrer", an arrow pointing out, and the hidden text “(opens in a new tab)”. It replaces the trailing arrow of standalone. |
href | string | – | The address. Without it the anchor is not a link and takes no focus. |
className | string | – | Extra classes, merged last. |
...props | ComponentProps<"a"> | – | Every other anchor attribute is passed through. A target or rel of your own replaces the ones external sets. |
linkClass() | ({ variant }) => string | – | A second export: the same classes for a routing component that renders its own anchor. |
Motion and sound
Motion
- On hover the underline of an inline link goes from 1 to 2 pixels thick. A subtle link gains an underline, and its colour changes from muted to the text colour over 150 ms.
- On hover the arrow of a standalone link moves 2 pixels in the reading direction, over 150 ms.
- Reduced motion: the changes land at once.
Sound
Silent. This component makes no sound.
Accessibility
- It is a native anchor: the link role, its name and Enter come from the browser. Name it with text that says where it goes.
- An inline link is underlined at rest, so it is told from the text around it without relying on colour. Keep subtle for lists of links, where position already says what they are.
- An external link ends with the hidden words “(opens in a new tab)”. The arrow is hidden from screen readers.
- The focus ring is 2 pixels of the theme's ring colour, 2 pixels away from the text.
- A link in a sentence is as tall as its line of text. A link on its own should sit in a row at least 24 pixels tall.
| Keys | Action |
|---|---|
| Tab | Focus the link. |
| Enter | Follow the link. |
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { ArrowRightIcon } from "@/components/icons/arrow-right";
import { ArrowUpRightIcon } from "@/components/icons/arrow-up-right";
export type LinkVariant = "inline" | "subtle" | "standalone";
export type LinkProps = ComponentProps<"a"> & {
/** `inline` is underlined, for a link inside a sentence. `subtle` is underlined on hover, for navigation lists. `standalone` ends in an arrow, for a link on a line of its own. */
variant?: LinkVariant;
/** The link leaves the site: it opens a new tab, shows an arrow pointing out, and tells screen readers so. */
external?: boolean;
};
const BASE =
"rounded-sm text-foreground decoration-1 underline-offset-4 " +
"transition-[color,text-decoration-color] duration-150 ease-out motion-reduce:transition-none " +
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring";
const VARIANT: Record<LinkVariant, string> = {
inline: "underline hover:decoration-2",
subtle: "text-muted-foreground no-underline hover:text-foreground hover:underline",
standalone: "group/link font-medium no-underline hover:underline",
};
/** The classes of a link, for a routing component that renders its own anchor. */
export function linkClass({ variant = "inline" }: { variant?: LinkVariant } = {}): string {
return cn(BASE, VARIANT[variant]);
}
// Both arrows are cut to their view box, so no blank band separates them from the last word.
const GLYPH = "ms-[0.25em] inline-block size-[0.75em] align-[-0.05em]";
/**
* A native `<a>`. It sets no font size or weight of its own (`standalone` is medium), so it follows the text it sits in.
* Use it for navigation. For an action that changes something, use a button.
*/
export function Link({ variant = "inline", external = false, className, children, ...props }: LinkProps) {
const arrow = external || variant === "standalone";
return (
<a data-slot="link" className={cn(linkClass({ variant }), className)} {...(external ? { target: "_blank", rel: "noreferrer" } : {})} {...props}>
{children}
{arrow && (
// Glued to the last word by a word joiner, so the arrow never starts a line on its own. Hidden, so the joiner stays out of the link's name.
<span aria-hidden="true" className="whitespace-nowrap">
⁠
{external ? (
<ArrowUpRightIcon size="0.75em" viewBox="4.5 4.5 15 15" className={GLYPH} />
) : (
<ArrowRightIcon
size="0.75em"
viewBox="3 3 18 18"
className={cn(GLYPH, "transition-[translate] duration-150 ease-out group-hover/link:translate-x-0.5 motion-reduce:transition-none rtl:-scale-x-100 rtl:group-hover/link:-translate-x-0.5")}
/>
)}
</span>
)}
{external && <span className="sr-only"> (opens in a new tab)</span>}
</a>
);
}Files and dependencies
- Adds
components/ui/link.tsx- Packages
- None
- Brings along
- Nothing
- Icons
arrow-rightarrow-up-right
Changelog
- 1.0.0 · 2026-12-03First release.