# ColorSwatch **Category**: react **URL**: https://www.heroui.com/docs/react/components/color-swatch **Source**: https://raw.githubusercontent.com/heroui-inc/heroui/refs/heads/v3/apps/docs/content/docs/react/components/(colors)/color-swatch.mdx > A visual preview of a color value with accessibility support *** ## Import ```tsx import { ColorSwatch } from '@heroui/react'; ``` ### Usage ```tsx import {ColorSwatch} from "@heroui/react"; export function ColorSwatchBasic() { return (
); } ``` ### Sizes ```tsx import {ColorSwatch} from "@heroui/react"; export function ColorSwatchSizes() { return (
); } ``` ### Shapes ```tsx import {ColorSwatch} from "@heroui/react"; export function ColorSwatchShapes() { return (
); } ``` ### Transparency ```tsx import {ColorSwatch} from "@heroui/react"; export function ColorSwatchTransparency() { return (
); } ``` ### Custom Styles with Render Props You can use the `style` render props to access the color value and create custom visual effects. ```tsx "use client"; import {ColorSwatch} from "@heroui/react"; export function ColorSwatchCustomStyles() { const colors = ["#0485F7", "#EF4444", "#F59E0B", "#10B981", "#D946EF"]; return (
{/* Glow effect */}
Glow Effect
{colors.map((color) => ( ({ boxShadow: `0 0 20px 2px ${color}`, })} /> ))}
{/* Gradient swatch */}
Gradient
{colors.map((color) => ( ({ background: `linear-gradient(135deg, ${c.toString("css")}, white)`, })} /> ))}
); } ``` ### Accessibility Use `colorName` to provide a custom accessible name for the color, and `aria-label` to add context about how the color is used. ```tsx import {ColorSwatch} from "@heroui/react"; export function ColorSwatchAccessibility() { return (
); } ``` ### Custom Render Function ```tsx "use client"; import {ColorSwatch} from "@heroui/react"; export function CustomRenderFunction() { return (
} />
} />
} />
} />
} />
); } ``` ## Related Components - **ColorSwatchPicker**: Color swatch selection from a list of colors - **ColorField**: Input for entering color values with hex format - **ColorArea**: 2D color picker for selecting colors from a gradient area ## Styling ### Passing Tailwind CSS classes ```tsx import {ColorSwatch} from '@heroui/react'; function CustomColorSwatch() { return ( ); } ``` ### Customizing the component classes To customize the ColorSwatch component classes, you can use the `@layer components` directive.
[Learn more](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes). ```css @layer components { .color-swatch { @apply border-2 border-white; } } ``` HeroUI follows the [BEM](https://getbem.com/) methodology to ensure component variants and states are reusable and easy to customize. ### CSS Classes The ColorSwatch component uses these CSS classes ([View source styles](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/color-swatch.css)): #### Base Classes - `.color-swatch` - Base swatch styles with checkered background for transparency #### Shape Classes - `.color-swatch--circle` - Circular shape (default) - `.color-swatch--square` - Square shape with rounded corners #### Size Classes - `.color-swatch--xs` - Extra small (16px) - `.color-swatch--sm` - Small (24px) - `.color-swatch--md` - Medium (32px, default) - `.color-swatch--lg` - Large (36px) - `.color-swatch--xl` - Extra large (40px) ## API Reference ### ColorSwatch Props | Prop | Type | Default | Description | |------|------|---------|-------------| | `color` | `string \| Color` | - | The color value to display (hex, rgb, hsl, etc.) | | `colorName` | `string` | - | Accessible name for the color (overrides auto-generated description) | | `className` | `string` | - | Additional CSS classes | | `shape` | `"circle" \| "square"` | `"circle"` | Shape of the swatch | | `size` | `"xs" \| "sm" \| "md" \| "lg" \| "xl"` | `"md"` | Size of the swatch | | `style` | `CSSProperties \| ((renderProps) => CSSProperties)` | - | Inline styles or render props function with access to color | | `aria-label` | `string` | - | Accessible label for the swatch | | `render` | `DOMRenderFunction` | - | Overrides the default DOM element with a custom render function.| ### Style Render Props When using the `style` prop as a function, you receive render props with access to the color: ```tsx ({ boxShadow: `0 4px 14px ${color.toString("css")}80`, })} /> ``` The `color` object provides methods like: - `color.toString("css")` - Returns CSS color string - `color.toString("hex")` - Returns hex color string - `color.getChannelValue("alpha")` - Returns alpha channel value