Skip to content

Icon Properties

Customizable icon component with multiple styling options, sizes, and color variants. Enhances UI with visual cues and supports both light and dark modes.

Installation

Add Icon 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 icon
Adds
Icon.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
@radix-ui/react-slotif 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

Create or update your Icon component

Copy this code to create your Icon component

/components/ui/Icon.tsx
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cn } from "@/lib/utils";

// Server-compatible: no hooks or context, so Icon can be rendered from server components.
// The icon inside the container is sized with CSS (`[&>*]:size-*`), so children are never cloned.

/** Decorative palette (Icon is an explicit palette API, see CONVENTIONS.md). */
export type IconColor = "neutral" | "gray" | "blue" | "orange" | "red" | "green" | "yellow" | "purple" | "pink" | "teal" | "cyan";
export type IconVariant = "stroke" | "lighter";
export type IconSize = "sm" | "md" | "lg" | "xl" | "2xl";

/** @deprecated Combined form, e.g. `variant="blue-lighter"` — use `variant="lighter" color="blue"`. */
export type LegacyIconVariant = `${IconColor}-${IconVariant}`;
/** @deprecated Use `sm` / `md` / `lg` / `xl` / `2xl`. */
type LegacyIconSize = "small" | "medium" | "large" | "x-large" | "2x-large";

const LEGACY_SIZES: Record<LegacyIconSize, IconSize> = { small: "sm", medium: "md", large: "lg", "x-large": "xl", "2x-large": "2xl" };

// Container size and the size of its direct children (the icon).
const SIZE_STYLES: Record<IconSize, { container: string; icon: string }> = {
    sm: { container: "h-8 w-8 p-1.5 [&>*]:size-4", icon: "size-4" }, // 16px icon
    md: { container: "h-10 w-10 p-2.5 [&>*]:size-5", icon: "size-5" }, // 20px icon
    lg: { container: "h-12 w-12 p-3 [&>*]:size-6", icon: "size-6" }, // 24px icon
    xl: { container: "h-14 w-14 p-3.5 [&>*]:size-7", icon: "size-7" }, // 28px icon
    "2xl": { container: "h-16 w-16 p-4 [&>*]:size-8", icon: "size-8" }, // 32px icon
};

const VARIANT_STYLES: Record<LegacyIconVariant, string> = {
    "neutral-lighter": "bg-neutral-50 text-muted-foreground dark:bg-neutral-800",
    "neutral-stroke": "border border-border text-muted-foreground",

    "gray-lighter": "bg-gray-50 text-gray-500 dark:bg-gray-800 dark:text-gray-400",
    "gray-stroke": "border border-gray-200 text-gray-500 dark:border-gray-700 dark:text-gray-400",

    "blue-lighter": "bg-blue-50 text-blue-500 dark:bg-blue-900/20 dark:text-blue-400",
    "blue-stroke": "border border-blue-200 text-blue-500 dark:border-blue-700 dark:text-blue-400",

    "orange-lighter": "bg-orange-50 text-orange-500 dark:bg-orange-900/20 dark:text-orange-400",
    "orange-stroke": "border border-orange-200 text-orange-500 dark:border-orange-700 dark:text-orange-400",

    "red-lighter": "bg-red-50 text-red-500 dark:bg-red-900/20 dark:text-red-400",
    "red-stroke": "border border-red-200 text-red-500 dark:border-red-700 dark:text-red-400",

    "green-lighter": "bg-green-50 text-green-500 dark:bg-green-900/20 dark:text-green-400",
    "green-stroke": "border border-green-200 text-green-500 dark:border-green-700 dark:text-green-400",

    "yellow-lighter": "bg-yellow-50 text-yellow-500 dark:bg-yellow-900/20 dark:text-yellow-400",
    "yellow-stroke": "border border-yellow-200 text-yellow-500 dark:border-yellow-700 dark:text-yellow-400",

    "purple-lighter": "bg-purple-50 text-purple-500 dark:bg-purple-900/20 dark:text-purple-400",
    "purple-stroke": "border border-purple-200 text-purple-500 dark:border-purple-700 dark:text-purple-400",

    "pink-lighter": "bg-pink-50 text-pink-500 dark:bg-pink-900/20 dark:text-pink-400",
    "pink-stroke": "border border-pink-200 text-pink-500 dark:border-pink-700 dark:text-pink-400",

    "teal-lighter": "bg-teal-50 text-teal-500 dark:bg-teal-900/20 dark:text-teal-400",
    "teal-stroke": "border border-teal-200 text-teal-500 dark:border-teal-700 dark:text-teal-400",

    "cyan-lighter": "bg-cyan-50 text-cyan-500 dark:bg-cyan-900/20 dark:text-cyan-400",
    "cyan-stroke": "border border-cyan-200 text-cyan-500 dark:border-cyan-700 dark:text-cyan-400",
};

const hasKey = <T extends object>(record: T, key: string): key is Extract<keyof T, string> => Object.prototype.hasOwnProperty.call(record, key);

export function resolveIconSize(size?: IconSize | LegacyIconSize | null): IconSize {
    if (!size) return "md";
    if (hasKey(LEGACY_SIZES, size)) return LEGACY_SIZES[size];
    return hasKey(SIZE_STYLES, size) ? size : "md";
}

/** A legacy combined variant (`"blue-lighter"`) sets both parts; an explicit `color` still wins over its colour. */
export function resolveIconVariant(variant?: IconVariant | LegacyIconVariant | null, color?: IconColor | null): { variant: IconVariant; color: IconColor } {
    if (variant && hasKey(VARIANT_STYLES, variant)) {
        const separator = variant.lastIndexOf("-");
        return { color: color ?? (variant.slice(0, separator) as IconColor), variant: variant.slice(separator + 1) as IconVariant };
    }
    return { variant: variant === "lighter" ? "lighter" : "stroke", color: color ?? "neutral" };
}

function hasTextContent(children: React.ReactNode) {
    return React.Children.toArray(children).some((child) => typeof child === "string" || typeof child === "number");
}

export interface IconRootProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "color"> {
    /** Render the single child element instead of a <div>, merging Icon's props onto it. */
    asChild?: boolean;
    /** `stroke` (bordered) or `lighter` (tinted background). The legacy combined form `"blue-lighter"` is still accepted. */
    variant?: IconVariant | LegacyIconVariant;
    color?: IconColor;
    size?: IconSize | LegacyIconSize;
}

/**
 * Round container that sizes and colours the icon inside it.
 * Decorative by default (`aria-hidden`); pass `aria-label` to expose it as an image (`role="img"`).
 * It stays exposed when it contains text, and with `asChild` the child element decides.
 */
const IconRoot = React.forwardRef<HTMLDivElement, IconRootProps>(({ asChild = false, variant, color, size, className, children, ...props }, ref) => {
    const resolvedSize = resolveIconSize(size);
    const resolved = resolveIconVariant(variant, color);
    const labelled = props["aria-label"] !== undefined || props["aria-labelledby"] !== undefined;
    const decorative = !asChild && !labelled && !hasTextContent(children);
    const Component = asChild ? Slot : "div";

    return (
        <Component
            ref={ref}
            role={labelled ? "img" : undefined}
            aria-hidden={decorative || undefined}
            data-size={resolvedSize}
            // Rounding comes before className so a consumer `rounded-*` class wins.
            className={cn("flex items-center justify-center rounded-full", SIZE_STYLES[resolvedSize].container, VARIANT_STYLES[`${resolved.color}-${resolved.variant}`], className)}
            {...props}
        >
            {children}
        </Component>
    );
});
IconRoot.displayName = "Icon.Root";

export interface IconChildProps extends React.HTMLAttributes<HTMLDivElement> {
    size?: IconSize | LegacyIconSize;
}

/** Sized wrapper for icon content outside an Icon.Root. Inside a Root, the Root's size applies. */
const IconChild = React.forwardRef<HTMLDivElement, IconChildProps>(({ size, className, ...props }, ref) => {
    return <div ref={ref} className={cn(SIZE_STYLES[resolveIconSize(size)].icon, className)} {...props} />;
});
IconChild.displayName = "Icon.Child";

/** `Icon` is the same component as `Icon.Root` (kept for `import { Icon } from "@/components/ui/Icon"`). */
export { IconRoot as Root, IconRoot as Icon, IconChild as Child };
export default IconRoot;

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

Variant Icon

Icons with different style variants. Combine a variant with a color, e.g. variant="lighter" color="blue".

Stroke Variants

Color options for icon strokes: neutral, gray, blue, orange, red, green, yellow, purple, pink, and teal.

Lighter Variants

Icons with subtle background colors in the same palette as stroke variants.

Size Options

Five size variants available:

  • 2xl (2x-large): For high-impact display
  • xl (x-large): For featured content
  • lg (large): For navigation and primary actions
  • md (medium): For general interface elements
  • sm (small): For compact spaces

Examples

Icon usage examples with different colors:

  • Cyan: Water-related functions
  • Red: Alerts and warnings
  • Yellow: Ideas and innovation
  • Pink: Care and support
  • Blue: Connectivity status
  • Orange: Mobile functionality
  • Green: Home and navigation
  • Purple: Documents and files

API Reference

The Icon component provides a consistent way to display and style icons throughout your application.

Icon

Wrapper (Icon.Root, also exported as Icon) that sizes and colors the icon placed inside it, from any icon library. Forwards its ref and DOM props.

PropTypeDefaultDescription
variant
"stroke""lighter"
"stroke"

stroke has a colored border; lighter has a tinted background. The combined form (e.g. "blue-lighter") is a deprecated alias for variant + color.

color
"neutral""gray""blue""orange""red""green""yellow""purple""pink""teal""cyan"
"neutral"

Color of the icon

size
"sm""md""lg""xl""2xl"
"md"

Container 32–64px with a 16–32px icon. small, medium, large, x-large and 2x-large are deprecated aliases.

asChild"boolean""false"

Render the single child element instead of a div, merging the icon props onto it

aria-label"string""undefined"

Accessible name; exposes the icon as role="img". Without it the icon is decorative (aria-hidden).

className"string""undefined"

Additional CSS classes, merged last (e.g. rounded-lg replaces rounded-full)

Used in these blocks

Block families with at least one block built on this component. Each page previews its blocks and lists the components and packages they use.