Skip to content

Link Button

A lightweight, versatile component designed for text links with button-like behavior. Built on Radix UI's Slot primitive, it supports various styles, sizes, and states with minimal visual footprint.

Installation

Add Link Button to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.

Terminal
npx ck-ui-cli@latest add link-button
Adds
LinkButton.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
@radix-ui/react-slotclass-variance-authorityif your project does not have them yet

First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.

Manual installationInstall the packages and copy the files yourself.

Install the following dependencies

Run these commands in your terminal to install the required packages

Terminal
npm install @radix-ui/react-slot class-variance-authority

Create or update your LinkButton component

Copy this code to create your LinkButton component

/components/ui/LinkButton.tsx
import * as React from "react";
import Link from "next/link";
import { Slot } from "@radix-ui/react-slot";
import { cva } from "class-variance-authority";
import { cn } from "@/lib/utils";

// Server-compatible: no hooks or context, so LinkButton can be rendered from server components.

export type LinkButtonColor = "primary" | "neutral" | "destructive" | "success";
export type LinkButtonSize = "sm" | "md";

/** @deprecated Use `destructive`. */
type LegacyLinkButtonColor = "danger";
/** @deprecated Use `sm` / `md`. */
type LegacyLinkButtonSize = "small" | "medium";

const LEGACY_COLORS: Record<LegacyLinkButtonColor, LinkButtonColor> = { danger: "destructive" };
const LEGACY_SIZES: Record<LegacyLinkButtonSize, LinkButtonSize> = { small: "sm", medium: "md" };

export function resolveLinkButtonColor(color?: LinkButtonColor | LegacyLinkButtonColor | null): LinkButtonColor {
    if (!color) return "primary";
    return color in LEGACY_COLORS ? LEGACY_COLORS[color as LegacyLinkButtonColor] : (color as LinkButtonColor);
}

export function resolveLinkButtonSize(size?: LinkButtonSize | LegacyLinkButtonSize | null): LinkButtonSize {
    if (!size) return "md";
    return size in LEGACY_SIZES ? LEGACY_SIZES[size as LegacyLinkButtonSize] : (size as LinkButtonSize);
}

const linkButtonRootVariants = cva(
    [
        "group/link-button inline-flex items-center justify-center whitespace-nowrap rounded-sm",
        "transition-colors duration-base ease-standard",
        "underline decoration-transparent underline-offset-[3px]",
        "hover:decoration-current focus-visible:decoration-current",
        "focus-ring",
        "disabled:pointer-events-none disabled:text-muted-foreground disabled:no-underline dark:disabled:text-muted-foreground",
        "aria-disabled:pointer-events-none aria-disabled:text-muted-foreground aria-disabled:no-underline dark:aria-disabled:text-muted-foreground",
    ],
    {
        variants: {
            // Link text uses the -strong shades: at least 4.5:1 on the page background in both themes.
            color: {
                primary: "text-primary-strong hover:text-primary-hover focus-visible:text-primary-hover dark:hover:text-blue-400 dark:focus-visible:text-blue-400",
                neutral:
                    "text-neutral-700 hover:text-neutral-900 focus-visible:text-neutral-900 dark:text-neutral-300 dark:hover:text-white dark:focus-visible:text-white",
                destructive: "text-destructive-strong hover:text-red-800 focus-visible:text-red-800 dark:hover:text-red-400 dark:focus-visible:text-red-400",
                success: "text-success-strong hover:text-success-hover focus-visible:text-success-hover dark:hover:text-green-400 dark:focus-visible:text-green-400",
            },
            size: {
                md: "h-5 gap-1 size-label-sm",
                sm: "h-4 gap-1 size-label-xs",
            },
            underline: {
                true: "decoration-current",
                false: "",
            },
        },
        defaultVariants: {
            color: "primary",
            size: "md",
            underline: false,
        },
    }
);

// Without an explicit size, LinkButton.Icon follows the parent LinkButton's data-size (pure CSS, works with asChild).
const linkButtonIconVariants = cva("flex shrink-0 items-center justify-center", {
    variants: {
        size: {
            inherit: "size-5 group-data-[size=sm]/link-button:size-4",
            md: "size-5",
            sm: "size-4",
        },
    },
    defaultVariants: {
        size: "inherit",
    },
});

export interface LinkButtonRootProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "color"> {
    /** Render the single child element (e.g. a router link) instead of a <button>, merging LinkButton's props onto it. */
    asChild?: boolean;
    /** Render a Next.js link. Without `href` (and without `asChild`) a <button type="button"> is rendered. */
    href?: string;
    color?: LinkButtonColor | LegacyLinkButtonColor;
    /** @deprecated Use `color`. Kept for existing call sites; `color` wins when both are set. */
    variant?: LinkButtonColor | LegacyLinkButtonColor;
    size?: LinkButtonSize | LegacyLinkButtonSize;
    /** Show the underline permanently instead of only on hover and keyboard focus. */
    underline?: boolean;
}

const LinkButtonRoot = React.forwardRef<HTMLButtonElement, LinkButtonRootProps>(
    ({ className, color, variant, size, underline = false, asChild = false, href, disabled, type, children, ...props }, ref) => {
        const resolvedSize = resolveLinkButtonSize(size);
        const classes = cn(linkButtonRootVariants({ color: resolveLinkButtonColor(color ?? variant), size: resolvedSize, underline }), className);

        if (asChild) {
            return (
                <Slot ref={ref} className={classes} data-size={resolvedSize} aria-disabled={disabled || undefined} {...props}>
                    {children}
                </Slot>
            );
        }

        if (href !== undefined) {
            const anchorProps = props as React.AnchorHTMLAttributes<HTMLAnchorElement>;
            const anchorRef = ref as unknown as React.Ref<HTMLAnchorElement>;

            // A disabled link has no href, so it cannot be followed or focused.
            if (disabled) {
                return (
                    <a ref={anchorRef} className={classes} data-size={resolvedSize} aria-disabled="true" {...anchorProps}>
                        {children}
                    </a>
                );
            }

            return (
                <Link ref={anchorRef} href={href} className={classes} data-size={resolvedSize} {...anchorProps}>
                    {children}
                </Link>
            );
        }

        return (
            <button ref={ref} type={type ?? "button"} className={classes} data-size={resolvedSize} disabled={disabled} {...props}>
                {children}
            </button>
        );
    }
);
LinkButtonRoot.displayName = "LinkButton.Root";

export interface LinkButtonIconProps extends React.HTMLAttributes<HTMLElement> {
    /** Overrides the size inherited from LinkButton.Root. */
    size?: LinkButtonSize | LegacyLinkButtonSize;
    /** Element or icon component to render, e.g. `as={RiArrowRightSLine}`. Defaults to a <span>. */
    as?: React.ElementType;
}

const LinkButtonIcon = React.forwardRef<HTMLElement, LinkButtonIconProps>(({ as: Component = "span", className, size, ...props }, ref) => {
    return (
        <Component
            ref={ref}
            aria-hidden="true"
            className={cn(linkButtonIconVariants({ size: size ? resolveLinkButtonSize(size) : "inherit" }), className)}
            {...props}
        />
    );
});
LinkButtonIcon.displayName = "LinkButton.Icon";

const LinkButton = {
    Root: LinkButtonRoot,
    Icon: LinkButtonIcon,
};

export { LinkButton, LinkButtonRoot as Root, LinkButtonIcon as Icon, linkButtonRootVariants, linkButtonIconVariants };
export default LinkButton;

Test Your Component

  • 1. Add the component to a test page
  • 2. Check all props and variants work as expected
  • 3. Verify responsive behavior
  • 4. Test with different content/data
  • 5. Ensure accessibility features work correctly

Colors

Set the intent with the color prop. Use primary for main actions, neutral for secondary actions, destructive for destructive operations, and success for confirmation actions. The older variant prop (and danger) is still accepted as a deprecated alias.

Underline

Add permanent underlines to link buttons for increased visibility and to match traditional hyperlink styling expectations. By default, underlines only appear on hover and keyboard focus, but using the underline prop makes them permanently visible.

Sizes

LinkButton provides size options to fit various UI contexts. Choose md (default) for standard text links and sm for more compact interfaces or when space is limited. The legacy names medium and small are still accepted.

With Icons

Enhance your link buttons with icons for improved visual communication. Place LinkButton.Icon before or after the text, or on both sides; its position is simply its place among the children. Icons are decorative (aria-hidden) and follow the link button's size unless you pass their own size.

API Reference

LinkButton renders a <button>, a Next.js link (with href), or your own element (with asChild, via the Radix UI Slot primitive). It shows the shared keyboard focus ring. See the Radix UI Slot documentation for more details on the base component.

Main link button component with support for various colors and sizes. It also accepts the native attributes of the element it renders.

See Radix UI docs for base props.

PropTypeDefaultDescription
color
"primary""neutral""destructive""success"
"primary"

Intent colour of the link button

variant
"primary""neutral""destructive""success""danger"
"undefined"

Deprecated alias for color (danger = destructive). color wins when both are set.

size
"md""sm"
"md"

Size of the link button. The legacy names medium and small are still accepted.

underline"boolean""false"

Show the underline permanently instead of only on hover and keyboard focus

href"string""undefined"

Renders a Next.js link. Without href (and without asChild) a <button type="button"> is rendered.

disabled"boolean""false"

Disables the button. A disabled link is rendered without href and with aria-disabled.

asChild"boolean""false"

Render the single child element (e.g. your own link) with the link button styles and props merged onto it

Adds a decorative icon to the link button, sized to match it.

PropTypeDefaultDescription
as"React.ElementType"""span""

Icon component or element type to render, e.g. as={RiArrowRightSLine}

size
"md""sm"
"inherited"

Overrides the size inherited from LinkButton.Root