color-swatch
diceui-radix/color-swatchFreeComponent
diceui/radix publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/sadmann7/diceui/HEAD/docs/public/r/styles/radix-nova/color-swatch.jsonSource
1"use client";23import { cva, type VariantProps } from "class-variance-authority";4import { Slot as SlotPrimitive } from "radix-ui";5import * as React from "react";6import { cn } from "@/lib/utils";78const colorSwatchVariants = cva(9 "box-border rounded-sm border bg-clip-padding shadow-sm data-disabled:pointer-events-none data-disabled:opacity-50",10 {11 variants: {12 size: {13 default: "size-8",14 sm: "size-6",15 lg: "size-12",16 },17 },18 defaultVariants: {19 size: "default",20 },21 },22);2324function getIsCssColor(v: string): boolean {25 try {26 return typeof CSS !== "undefined" && typeof CSS.supports === "function"27 ? CSS.supports("color", v)28 : true;29 } catch {30 return false;31 }32}3334function getHasAlpha(v: string): boolean {35 const s = v.trim().toLowerCase();3637 if (s === "transparent") return true;3839 if (/^#(?:[0-9a-f]{4}|[0-9a-f]{8})$/i.test(s)) return true;4041 if (/\b(?:rgba|hsla)\s*\(/i.test(s)) return true;4243 if (44 /\b(?:rgb|hsl|lab|lch|oklab|oklch|color)\s*\([^)]*\/\s*[\d.]+%?\s*\)/i.test(45 s,46 )47 ) {48 return true;49 }5051 return false;52}5354interface ColorSwatchProps55 extends Omit<React.ComponentProps<"div">, "children">,56 VariantProps<typeof colorSwatchVariants> {57 color?: string;58 asChild?: boolean;59 disabled?: boolean;60 withoutTransparency?: boolean;61}6263function ColorSwatch({64 color,65 size = "default",66 asChild = false,67 disabled = false,68 withoutTransparency = false,69 className,70 style,71 ...props72}: ColorSwatchProps) {73 const colorValue = color?.trim();7475 const backgroundStyle = React.useMemo<React.CSSProperties>(() => {76 if (!colorValue) {77 return {78 background:79 "linear-gradient(to bottom right, transparent calc(50% - 1px), hsl(var(--destructive)) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px)) no-repeat",80 };81 }8283 if (!getIsCssColor(colorValue)) {84 return { backgroundColor: "transparent" };85 }8687 if (!withoutTransparency && getHasAlpha(colorValue)) {88 return {89 background: `linear-gradient(${colorValue}, ${colorValue}), repeating-conic-gradient(#ccc 0% 25%, #fff 0% 50%) 0% 50% / 10px 10px`,90 };91 }9293 return { backgroundColor: colorValue };94 }, [colorValue, withoutTransparency]);9596 const ariaLabel = !colorValue97 ? "No color selected"98 : `Color swatch: ${colorValue}`;99100 const Primitive = asChild ? SlotPrimitive.Slot : "div";101102 return (103 <Primitive104 role="img"105// … truncated
What it pulls in
npm packages
Files it writes
More from diceui/radix
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| action-baraction-bar | diceui/radix | Components | Free | 1 dep · 2 files | |
| angle-sliderangle-slider | diceui/radix | Components | Free | 1 dep · 3 files | |
| avatar-groupavatar-group | diceui/radix | Components | Free | 1 dep | |
| badge-overflowbadge-overflow | diceui/radix | Components | Free | 1 dep · 2 files | |
| bannerbanner | diceui/radix | Components | Free | 1 dep | |
| checkbox-groupcheckbox-group | diceui/radix | Components | Free | 1 dep | |
| circular-progresscircular-progress | diceui/radix | Components | Free | 1 dep | |
| client-onlyclient-only | diceui/radix | Components | Free | no deps |
