|
| 1 | +"use client" |
| 2 | + |
| 3 | +import * as React from "react" |
| 4 | + |
| 5 | +import { getColorFormat, type Color } from "@/lib/colors" |
| 6 | +import { cn } from "@/lib/utils" |
| 7 | +import { useColors } from "@/hooks/use-colors" |
| 8 | +import { |
| 9 | + Select, |
| 10 | + SelectContent, |
| 11 | + SelectItem, |
| 12 | + SelectTrigger, |
| 13 | + SelectValue, |
| 14 | +} from "@/components/ui/select" |
| 15 | +import { Skeleton } from "@/components/ui/skeleton" |
| 16 | + |
| 17 | +export function ColorFormatSelector({ |
| 18 | + color, |
| 19 | + className, |
| 20 | + ...props |
| 21 | +}: Omit<React.ComponentProps<typeof SelectTrigger>, "color"> & { |
| 22 | + color: Color |
| 23 | +}) { |
| 24 | + const { format, setFormat, isLoading } = useColors() |
| 25 | + const formats = React.useMemo(() => getColorFormat(color), [color]) |
| 26 | + |
| 27 | + if (isLoading) { |
| 28 | + return <ColorFormatSelectorSkeleton /> |
| 29 | + } |
| 30 | + |
| 31 | + return ( |
| 32 | + <Select value={format} onValueChange={setFormat}> |
| 33 | + <SelectTrigger |
| 34 | + className={cn("h-7 w-auto gap-1.5 rounded-lg pr-2 text-xs", className)} |
| 35 | + {...props} |
| 36 | + > |
| 37 | + <span className="font-medium">Format: </span> |
| 38 | + <span className="font-mono text-xs text-muted-foreground"> |
| 39 | + {format} |
| 40 | + </span> |
| 41 | + </SelectTrigger> |
| 42 | + <SelectContent align="end" className="rounded-xl"> |
| 43 | + {Object.entries(formats).map(([format, value]) => ( |
| 44 | + <SelectItem |
| 45 | + key={format} |
| 46 | + value={format} |
| 47 | + className="gap-2 rounded-lg [&>span]:flex [&>span]:items-center [&>span]:gap-2" |
| 48 | + > |
| 49 | + <span className="font-medium">{format}</span> |
| 50 | + <span className="font-mono text-xs text-muted-foreground"> |
| 51 | + {value} |
| 52 | + </span> |
| 53 | + </SelectItem> |
| 54 | + ))} |
| 55 | + </SelectContent> |
| 56 | + </Select> |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +export function ColorFormatSelectorSkeleton({ |
| 61 | + className, |
| 62 | + ...props |
| 63 | +}: React.ComponentProps<typeof Skeleton>) { |
| 64 | + return ( |
| 65 | + <Skeleton |
| 66 | + className={cn("h-7 w-[116px] gap-1.5 rounded-lg", className)} |
| 67 | + {...props} |
| 68 | + /> |
| 69 | + ) |
| 70 | +} |
0 commit comments