color-picker
kibo-ui-registry/color-pickerFreeComponent
Allows users to select a color. Modeled after the color picker in Figma.
Live preview
live · rendered by the registry
Rendered by Kibo UI Registry on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.kibo-ui.com/r/color-picker.jsonSource
1"use client";23import Color from "color";4import { PipetteIcon } from "lucide-react";5import { Slider } from "radix-ui";6import {7 type ComponentProps,8 createContext,9 type HTMLAttributes,10 memo,11 useCallback,12 useContext,13 useEffect,14 useMemo,15 useRef,16 useState,17} from "react";18import { Button } from "@/components/ui/button";19import { Input } from "@/components/ui/input";20import {21 Select,22 SelectContent,23 SelectItem,24 SelectTrigger,25 SelectValue,26} from "@/components/ui/select";27import { cn } from "@/lib/utils";2829type ColorPickerContextValue = {30 hue: number;31 saturation: number;32 lightness: number;33 alpha: number;34 mode: string;35 setHue: (hue: number) => void;36 setSaturation: (saturation: number) => void;37 setLightness: (lightness: number) => void;38 setAlpha: (alpha: number) => void;39 setMode: (mode: string) => void;40};4142const ColorPickerContext = createContext<ColorPickerContextValue | undefined>(43 undefined44);4546export const useColorPicker = () => {47 const context = useContext(ColorPickerContext);4849 if (!context) {50 throw new Error("useColorPicker must be used within a ColorPickerProvider");51 }5253 return context;54};5556export type ColorPickerProps = HTMLAttributes<HTMLDivElement> & {57 value?: Parameters<typeof Color>[0];58 defaultValue?: Parameters<typeof Color>[0];59 onChange?: (value: Parameters<typeof Color.rgb>[0]) => void;60};6162export const ColorPicker = ({63 value,64 defaultValue = "#000000",65 onChange,66 className,67 ...props68}: ColorPickerProps) => {69 const selectedColor = Color(value);70 const defaultColor = Color(defaultValue);7172 const [hue, setHue] = useState(73 selectedColor.hue() || defaultColor.hue() || 074 );75 const [saturation, setSaturation] = useState(76 selectedColor.saturationl() || defaultColor.saturationl() || 10077 );78 const [lightness, setLightness] = useState(79 selectedColor.lightness() || defaultColor.lightness() || 5080 );81 const [alpha, setAlpha] = useState(82 selectedColor.alpha() * 100 || defaultColor.alpha() * 10083 );84 const [mode, setMode] = useState("hex");8586 // Update color when controlled value changes87 useEffect(() => {88 if (value) {89 const color = Color.rgb(value).rgb().object();9091 setHue(color.r);92 setSaturation(color.g);93 setLightness(color.b);94 setAlpha(color.a);95 }96 }, [value]);9798 // Notify parent of changes99 useEffect(() => {100 if (onChange) {101 const color = Color.hsl(hue, saturation, lightness).alpha(alpha / 100);102// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from Kibo UI Registry
All 41 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| announcementannouncement | Kibo UI Registry | Components | Free | no deps | |
| avatar-stackavatar-stack | Kibo UI Registry | Components | Free | no deps | |
| bannerbanner | Kibo UI Registry | Components | Free | 2 deps | |
| calendarcalendar | Kibo UI Registry | Components | Free | 3 deps | |
| choiceboxchoicebox | Kibo UI Registry | Components | Free | 2 deps | |
| code-blockcode-block | Kibo UI Registry | Components | Free | 5 deps · 2 files | |
| comboboxcombobox | Kibo UI Registry | Components | Free | 2 deps | |
| comparisoncomparison | Kibo UI Registry | Components | Free | 2 deps |
