dotUI
dotUI
beta
  1. Components
  2. Colors
  3. Color Swatch

Color Swatch

A ColorSwatch displays a preview of a selected color.

<ColorSwatch color="#f00" />

Installation

Install the following dependencies:

npm install react-aria-components

Copy and paste the following code into your project.

"use client";

import {
  ColorSwatch as AriaColorSwatch,
  type ColorSwatchProps as AriaColorSwatchProps,
} from "react-aria-components";
import { tv } from "tailwind-variants";

const colorSwatchStyles = tv({
  base: "size-5 rounded-sm border",
});

interface ColorSwatchProps extends Omit<AriaColorSwatchProps, "className"> {
  className?: string;
}
const ColorSwatch = ({ className, ...props }: ColorSwatchProps) => {
  return <AriaColorSwatch className={colorSwatchStyles({ className })} {...props} />;
};

export type { ColorSwatchProps };
export { ColorSwatch };

Update the import paths to match your project setup.

Usage

Use a ColorSwatch to display a preview of a selected color.

API Reference

PropTypeDefaultDescription
color*
string | Color | null
-
The color value to display in the swatch.
children
ReactNode | (values: ColorSwatchRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode
-
The children of the component. A function may be provided to alter the children based on component state.
className
string
-
The CSS className for the element.
style
CSSProperties | (values: ColorSwatchRenderProps & {defaultStyle: CSSProperties}) => CSSProperties
-
The inline style for the element. A function may be provided to compute the style based on component state.

Built by mehdibha. The source code is available on GitHub.