variant-color-selector-basic
stackzero/variant-color-selector-basicFreeComponent
stackzero publishes no description for this item.
Install it
npx shadcn@latest add https://ui.stackzero.co/r/variant-color-selector-basic.jsonSource
1"use client";23import * as React from "react";4import * as RadioGroupPrimitive from "@radix-ui/react-radio-group";5import { Circle } from "lucide-react";67import { cn } from "@/lib/utils";89export interface ColorVariantItem {10 id: string;11 value: string;12 color: string;13 label: string;14}1516interface VariantColorSelectorBasicProps {17 value: string;18 onValueChange: (value: string) => void;19 variants: ColorVariantItem[];20 className?: string;21}2223const VariantColorSelectorBasic = ({24 className,25 onValueChange,26 value,27 variants,28}: VariantColorSelectorBasicProps) => {29 return (30 <RadioGroupPrimitive.Root31 className={cn("flex flex-wrap gap-3", className)}32 value={value}33 onValueChange={onValueChange}34 >35 {variants.map((variant) => (36 <label37 key={variant.id}38 className="relative flex cursor-pointer flex-col items-center gap-2"39 >40 <RadioGroupPrimitive.Item41 value={variant.value}42 className={cn(43 "ring-offset-background focus-visible:ring-ring aspect-square size-6 rounded-full border-2 focus:outline-hidden focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",44 "has-data-[state=checked]:ring-4",45 "has-data-[state=checked]:border-0"46 )}47 style={{48 backgroundColor: variant.color,49 color: getContrastYIQ(variant.color),50 }}51 >52 <RadioGroupPrimitive.Indicator className="flex h-full items-center justify-center">53 <Circle className="h-2.5 w-2.5 fill-current text-current" />54 </RadioGroupPrimitive.Indicator>55 </RadioGroupPrimitive.Item>56 <span className="text-foreground text-sm leading-none font-medium">57 {variant.label}58 </span>59 </label>60 ))}61 </RadioGroupPrimitive.Root>62 );63};6465export default VariantColorSelectorBasic;6667/**68 * Choose the best color for text based on the background color69 * Supports HEX and RGB colors70 * @param color71 * @returns72 */73function getContrastYIQ(color: string) {74 let r, g, b;7576 if (color.startsWith("#")) {77 // HEX color78 color = color.replace("#", "");79 r = parseInt(color.substring(0, 2), 16);80 g = parseInt(color.substring(2, 4), 16);81 b = parseInt(color.substring(4, 6), 16);82 } else if (color.startsWith("rgb")) {83 // RGB color84 const rgbValues = color.match(/\d+/g);85// … truncated
What it pulls in
npm packages
Files it writes
More from stackzero
All 112 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| face-rating-basicface-rating-basic | stackzero | Components | Free | 1 dep | |
| face-rating-gradientface-rating-gradient | stackzero | Components | Free | 2 deps | |
| image-carousel-basicimage-carousel-basic | stackzero | Components | Free | 5 deps | |
| image-viewer-basicimage-viewer-basic | stackzero | Components | Free | 3 deps | |
| image-viewer-basic-ex-01image-viewer-basic-ex-01 | stackzero | Components | Free | 1 dep | |
| image-viewer-basic-ex-02image-viewer-basic-ex-02 | stackzero | Components | Free | 1 dep | |
| image-viewer-motionimage-viewer-motion | stackzero | Components | Free | 4 deps | |
| image-viewer-motion-ex-01image-viewer-motion-ex-01 | stackzero | Components | Free | 1 dep |
