Link Button
A link that looks like a button: an anchor styled with stock buttonVariants, never a button, so navigation keeps its link semantics. Takes Button's variant and size; size lg sets a 16px label. Pass render to use your framework's link, such as Next.js Link.
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { buttonVariants } from "@/components/ui/button"
/**
* The classes of a link that looks like a button: stock `buttonVariants`,
* plus LinkButton's own sizes (`lg` sets a 16px label). For an element that
* can't be a LinkButton.
*/
function linkButtonVariants({
variant,
size,
className,
}: VariantProps<typeof buttonVariants> & { className?: string } = {}) {
return cn(
buttonVariants({ variant, size }),
// Every size is stock's except lg, which takes a 16px label. Stock stays
// untouched in ui/button.tsx, so a shadcn re-download never undoes it.
size === "lg" && "text-base",
className
)
}
/**
* A link that looks like a button: an `<a>` with the button's look, never a
* button. Base UI's `Button` gives any element `role="button"`, so navigation
* uses this instead.
*
* `variant` and `size` are stock Button's; `lg` sets a 16px label. Pass
* `render` to use your framework's link, such as
* `render={<Link href="/pricing" />}` with Next.js, or change the default
* here once and every block follows.
*/
function LinkButton({
className,
variant = "default",
size = "default",
render,
...props
}: useRender.ComponentProps<"a"> & VariantProps<typeof buttonVariants>) {
return useRender({
defaultTagName: "a",
render,
// Written as data-slot="button" on the link, so styles that dress a
// button's slot dress this too.
state: { slot: "button", variant, size },
props: mergeProps<"a">(
{ className: linkButtonVariants({ variant, size, className }) },
props
),
})
}
export { LinkButton, linkButtonVariants }
import { LinkButton } from "@/components/link-button/link-button"
import { DemoLabel } from "@/components/text/demo-label"
const COPY = { label: "Get started" } as const
const VARIANTS = [
"default",
"outline",
"secondary",
"ghost",
"destructive",
"link",
] as const
const SIZES = ["xs", "sm", "default", "lg"] as const
// Every variant at the default size, then every size in the default variant.
const EXAMPLES = [
...VARIANTS.map((variant) => ({
caption: `Variant ${variant}`,
props: { variant },
})),
...SIZES.map((size) => ({ caption: `Size ${size}`, props: { size } })),
]
/** Link Button in every variant and every size. */
export default function LinkButtonDemo() {
return (
<div className="flex flex-wrap items-start gap-6 p-6">
{EXAMPLES.map(({ caption, props }) => (
<div key={caption} className="grid justify-items-start gap-4">
<LinkButton href="#" {...props}>
{COPY.label}
</LinkButton>
<DemoLabel>{caption}</DemoLabel>
</div>
))}
</div>
)
}
import type { ComponentProps } from "react"
import { cn } from "cn"
/**
* A caption under a demo or gallery piece, in medium-weight mono at
* 12px/20px. Long captions wrap with balanced last lines. The caption
* inherits its ground's text color; native paragraph props pass through.
*/
function DemoLabel({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="demo-label"
className={cn("font-mono text-xs/5 font-medium text-pretty", className)}
{...props}
/>
)
}
export { DemoLabel }
Installation
npx shadcn@latest add @01orbital/link-buttonUsage
import { LinkButton } from "@/components/link-button/link-button"
import { DemoLabel } from "@/components/text/demo-label"
const COPY = { label: "Get started" } as const
const VARIANTS = [
"default",
"outline",
"secondary",
"ghost",
"destructive",
"link",
] as const
const SIZES = ["xs", "sm", "default", "lg"] as const
// Every variant at the default size, then every size in the default variant.
const EXAMPLES = [
...VARIANTS.map((variant) => ({
caption: `Variant ${variant}`,
props: { variant },
})),
...SIZES.map((size) => ({ caption: `Size ${size}`, props: { size } })),
]
/** Link Button in every variant and every size. */
export default function LinkButtonDemo() {
return (
<div className="flex flex-wrap items-start gap-6 p-6">
{EXAMPLES.map(({ caption, props }) => (
<div key={caption} className="grid justify-items-start gap-4">
<LinkButton href="#" {...props}>
{COPY.label}
</LinkButton>
<DemoLabel>{caption}</DemoLabel>
</div>
))}
</div>
)
}This example installs with everything it uses:
npx shadcn@latest add @01orbital/link-button-demoAPI reference
A link that looks like a button: an <a> with the button's look, never a button. Base UI's Button gives any element role="button", so navigation uses this instead.
variant and size are stock Button's; lg sets a 16px label. Pass render to use your framework's link, such as render={<Link href="/pricing" />} with Next.js, or change the default here once and every block follows.
It accepts the props of the Base UI part it wraps, and the HTML attributes of the element it renders, and the props of the stock Button it builds on.
Dependencies
npm: @base-ui/react, class-variance-authority, cn. The CLI installs them for you.
Requires these pieces. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Used in blocks
90 blocks are built with Link Button; the first 12:
About Community Avatar Wall
About Team Card Grid
Added to Cart Dialog
Alternating Case Study Split Cards
Alternating Screenshot Feature Rows
Amber Halo Studio Hero
Audience Tabs Pricing
Aurora Arc Contact CTA
Base Plan With Add-ons
Bento Grid Not Found
Bevel Card
Bezel Glass Card
Last updated on