ColorPicker

PreviousNext
Docs
react-ariaui

Preview

Loading preview…
components/ui/ColorPicker.tsx
'use client';
import {
  Button,
  ColorPicker as AriaColorPicker,
  ColorPickerProps as AriaColorPickerProps
} from 'react-aria-components';
import {DialogTrigger} from '@/registry/react-aria/ui/Dialog';
import {ColorSwatch} from '@/registry/react-aria/ui/ColorSwatch';
import {ColorSlider} from '@/registry/react-aria/ui/ColorSlider';
import {ColorArea} from '@/registry/react-aria/ui/ColorArea';
import {ColorField} from '@/registry/react-aria/ui/ColorField';
import {Popover} from '@/registry/react-aria/ui/Popover';

import './ColorPicker.css';

export interface ColorPickerProps extends Omit<AriaColorPickerProps, 'children'> {
  label?: string;
  children?: React.ReactNode;
}

export function ColorPicker({ label, children, ...props }: ColorPickerProps) {
  return (
    (
      <AriaColorPicker {...props}>
        <DialogTrigger>
          <Button className="color-picker">
            <ColorSwatch />
            <span>{label}</span>
          </Button>
          <Popover hideArrow placement="bottom start" className="color-picker-dialog">
            {children || (
              <>
                <ColorArea
                  colorSpace="hsb"
                  xChannel="saturation"
                  yChannel="brightness"
                />
                <ColorSlider colorSpace="hsb" channel="hue" />
                <ColorField label="Hex" />
              </>
            )}
          </Popover>
        </DialogTrigger>
      </AriaColorPicker>
    )
  );
}

Installation

npx shadcn@latest add @react-aria/css-colorpicker

Usage

import { CssColorpicker } from "@/components/ui/css-colorpicker"
<CssColorpicker />