refactor: refactor color tool to match calculate blueprint
Rewrites all color components to use the glass panel design language, fixed-height two-panel layout, and tab-based navigation. - ColorManipulation: lg:grid-cols-5 split — left 2/5 shows ColorPicker + ColorInfo always; right 3/5 has Info/Adjust/Harmony/Gradient tabs; mobile 'Pick | Explore' switcher - ColorPicker: removes shadcn Input/Label, native input with dynamic contrast color matching the picked hue - ColorInfo: removes shadcn Button, native copy buttons on hover, metadata chips with bg-primary/5 background - ManipulationPanel: keeps Slider, replaces Button with glass action buttons, tighter spacing and muted labels - ExportMenu: keeps Select, replaces Buttons with glass action buttons, code preview in dark terminal box (#06060e) - ColorSwatch: rectangular full-width design for palette grids, hover reveals copy icon, hex label at bottom - PaletteGrid: denser grid (4→5 cols), smaller swatch height Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { ColorInfo as ColorInfoType } from '@/lib/color/api/types';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Copy } from 'lucide-react';
|
||||
import { toast } from 'sonner';
|
||||
import { cn } from '@/lib/utils/cn';
|
||||
@@ -12,79 +11,70 @@ interface ColorInfoProps {
|
||||
}
|
||||
|
||||
export function ColorInfo({ info, className }: ColorInfoProps) {
|
||||
const copyToClipboard = (value: string, label: string) => {
|
||||
const copy = (value: string, label: string) => {
|
||||
navigator.clipboard.writeText(value);
|
||||
toast.success(`Copied ${label} to clipboard`);
|
||||
toast.success(`Copied ${label}`);
|
||||
};
|
||||
|
||||
const formatRgb = (rgb: { r: number; g: number; b: number; a?: number }) => {
|
||||
if (rgb.a !== undefined && rgb.a < 1) {
|
||||
return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${rgb.a})`;
|
||||
}
|
||||
return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
|
||||
};
|
||||
const formatRgb = (rgb: { r: number; g: number; b: number; a?: number }) =>
|
||||
rgb.a !== undefined && rgb.a < 1
|
||||
? `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${rgb.a})`
|
||||
: `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
|
||||
|
||||
const formatHsl = (hsl: { h: number; s: number; l: number; a?: number }) => {
|
||||
if (hsl.a !== undefined && hsl.a < 1) {
|
||||
return `hsla(${Math.round(hsl.h)}°, ${Math.round(hsl.s * 100)}%, ${Math.round(hsl.l * 100)}%, ${hsl.a})`;
|
||||
}
|
||||
return `hsl(${Math.round(hsl.h)}°, ${Math.round(hsl.s * 100)}%, ${Math.round(hsl.l * 100)}%)`;
|
||||
};
|
||||
|
||||
const formatLab = (lab: { l: number; a: number; b: number }) => {
|
||||
return `lab(${lab.l.toFixed(1)} ${lab.a.toFixed(1)} ${lab.b.toFixed(1)})`;
|
||||
};
|
||||
|
||||
const formatOkLab = (oklab: { l: number; a: number; b: number }) => {
|
||||
return `oklab(${(oklab.l * 100).toFixed(1)}% ${oklab.a.toFixed(3)} ${oklab.b.toFixed(3)})`;
|
||||
};
|
||||
const formatHsl = (hsl: { h: number; s: number; l: number; a?: number }) =>
|
||||
hsl.a !== undefined && hsl.a < 1
|
||||
? `hsla(${Math.round(hsl.h)}°, ${Math.round(hsl.s * 100)}%, ${Math.round(hsl.l * 100)}%, ${hsl.a})`
|
||||
: `hsl(${Math.round(hsl.h)}°, ${Math.round(hsl.s * 100)}%, ${Math.round(hsl.l * 100)}%)`;
|
||||
|
||||
const formats = [
|
||||
{ label: 'Hex', value: info.hex },
|
||||
{ label: 'HEX', value: info.hex },
|
||||
{ label: 'RGB', value: formatRgb(info.rgb) },
|
||||
{ label: 'HSL', value: formatHsl(info.hsl) },
|
||||
{ label: 'Lab', value: formatLab(info.lab) },
|
||||
{ label: 'OkLab', value: formatOkLab(info.oklab) },
|
||||
{ label: 'Lab', value: `lab(${info.lab.l.toFixed(1)} ${info.lab.a.toFixed(1)} ${info.lab.b.toFixed(1)})` },
|
||||
{ label: 'OkLab', value: `oklab(${(info.oklab.l * 100).toFixed(1)}% ${info.oklab.a.toFixed(3)} ${info.oklab.b.toFixed(3)})` },
|
||||
];
|
||||
|
||||
return (
|
||||
<div className={cn('space-y-3', className)}>
|
||||
<div className="grid grid-cols-1 gap-1.5">
|
||||
{formats.map((format) => (
|
||||
{/* Format rows */}
|
||||
<div className="space-y-1">
|
||||
{formats.map((fmt) => (
|
||||
<div
|
||||
key={format.label}
|
||||
className="flex items-center justify-between px-3 py-2 bg-muted/50 rounded-md group"
|
||||
key={fmt.label}
|
||||
className="group flex items-center justify-between px-2.5 py-1.5 rounded-lg border border-transparent hover:border-border/30 hover:bg-primary/5 transition-all"
|
||||
>
|
||||
<div className="flex items-baseline gap-2 min-w-0 flex-1">
|
||||
<span className="text-[10px] uppercase tracking-wider text-muted-foreground w-10 shrink-0">{format.label}</span>
|
||||
<span className="font-mono text-xs truncate">{format.value}</span>
|
||||
<span className="text-[10px] font-semibold text-muted-foreground/50 uppercase tracking-widest w-9 shrink-0">
|
||||
{fmt.label}
|
||||
</span>
|
||||
<span className="font-mono text-xs text-foreground/80 truncate">{fmt.value}</span>
|
||||
</div>
|
||||
<Button
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
onClick={() => copyToClipboard(format.value, format.label)}
|
||||
aria-label={`Copy ${format.label} value`}
|
||||
className="opacity-0 group-hover:opacity-100 transition-opacity"
|
||||
<button
|
||||
onClick={() => copy(fmt.value, fmt.label)}
|
||||
aria-label={`Copy ${fmt.label}`}
|
||||
className="shrink-0 ml-2 p-1 rounded text-muted-foreground/30 hover:text-primary opacity-0 group-hover:opacity-100 transition-all"
|
||||
>
|
||||
<Copy className="h-3 w-3" />
|
||||
</Button>
|
||||
<Copy className="w-3 h-3" />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-3 gap-3 pt-2 border-t text-xs">
|
||||
<div>
|
||||
<div className="text-muted-foreground mb-0.5">Brightness</div>
|
||||
<div className="font-medium">{(info.brightness * 100).toFixed(1)}%</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-muted-foreground mb-0.5">Luminance</div>
|
||||
<div className="font-medium">{(info.luminance * 100).toFixed(1)}%</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-muted-foreground mb-0.5">{info.name && typeof info.name === 'string' ? 'Name' : 'Type'}</div>
|
||||
<div className="font-medium">{info.name && typeof info.name === 'string' ? info.name : (info.is_light ? 'Light' : 'Dark')}</div>
|
||||
</div>
|
||||
{/* Metadata row */}
|
||||
<div className="grid grid-cols-3 gap-2 pt-2 border-t border-border/25">
|
||||
{[
|
||||
{ label: 'Brightness', value: `${(info.brightness * 100).toFixed(1)}%` },
|
||||
{ label: 'Luminance', value: `${(info.luminance * 100).toFixed(1)}%` },
|
||||
{
|
||||
label: info.name && typeof info.name === 'string' ? 'Name' : 'Type',
|
||||
value: info.name && typeof info.name === 'string' ? info.name : (info.is_light ? 'Light' : 'Dark'),
|
||||
},
|
||||
].map((m) => (
|
||||
<div key={m.label} className="px-2.5 py-2 rounded-lg bg-primary/5 border border-border/20">
|
||||
<div className="text-[10px] text-muted-foreground/40 font-mono mb-0.5">{m.label}</div>
|
||||
<div className="text-xs font-mono font-medium text-foreground/75 truncate">{m.value}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -7,27 +7,32 @@ import { ColorInfo } from '@/components/color/ColorInfo';
|
||||
import { ManipulationPanel } from '@/components/color/ManipulationPanel';
|
||||
import { PaletteGrid } from '@/components/color/PaletteGrid';
|
||||
import { ExportMenu } from '@/components/color/ExportMenu';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { useColorInfo, useGeneratePalette, useGenerateGradient } from '@/lib/color/api/queries';
|
||||
import { Loader2, Share2, Palette, Plus, X, Layers } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import { Loader2, Share2, Plus, X, Palette, Layers } from 'lucide-react';
|
||||
import { toast } from 'sonner';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
type HarmonyType =
|
||||
| 'monochromatic'
|
||||
| 'analogous'
|
||||
| 'complementary'
|
||||
| 'triadic'
|
||||
| 'tetradic';
|
||||
type HarmonyType = 'monochromatic' | 'analogous' | 'complementary' | 'triadic' | 'tetradic';
|
||||
type RightTab = 'info' | 'adjust' | 'harmony' | 'gradient';
|
||||
type MobileTab = 'pick' | 'explore';
|
||||
|
||||
const HARMONY_OPTS: { value: HarmonyType; label: string; desc: string }[] = [
|
||||
{ value: 'monochromatic', label: 'Mono', desc: 'Single hue, varied lightness' },
|
||||
{ value: 'analogous', label: 'Analogous', desc: 'Adjacent colors ±30°' },
|
||||
{ value: 'complementary', label: 'Complement', desc: 'Opposite on wheel 180°' },
|
||||
{ value: 'triadic', label: 'Triadic', desc: 'Three equal 120° steps' },
|
||||
{ value: 'tetradic', label: 'Tetradic', desc: 'Four equal 90° steps' },
|
||||
];
|
||||
|
||||
const RIGHT_TABS: { value: RightTab; label: string }[] = [
|
||||
{ value: 'info', label: 'Info' },
|
||||
{ value: 'adjust', label: 'Adjust' },
|
||||
{ value: 'harmony', label: 'Harmony' },
|
||||
{ value: 'gradient', label: 'Gradient' },
|
||||
];
|
||||
|
||||
const actionBtn =
|
||||
'flex items-center gap-1 px-2.5 py-1 text-xs glass rounded-md border border-border/30 text-muted-foreground hover:text-primary hover:border-primary/30 hover:bg-primary/10 transition-all disabled:opacity-40 disabled:cursor-not-allowed';
|
||||
|
||||
function ColorManipulationContent() {
|
||||
const searchParams = useSearchParams();
|
||||
@@ -37,24 +42,23 @@ function ColorManipulationContent() {
|
||||
return urlColor ? `#${urlColor.replace('#', '')}` : '#ff0099';
|
||||
});
|
||||
|
||||
// Harmony state
|
||||
const [rightTab, setRightTab] = useState<RightTab>('info');
|
||||
const [mobileTab, setMobileTab] = useState<MobileTab>('pick');
|
||||
|
||||
// Harmony
|
||||
const [harmonyType, setHarmonyType] = useState<HarmonyType>('complementary');
|
||||
const [palette, setPalette] = useState<string[]>([]);
|
||||
const paletteMutation = useGeneratePalette();
|
||||
|
||||
// Gradient state
|
||||
// Gradient
|
||||
const [stops, setStops] = useState<string[]>(['#ff0099', '#0099ff']);
|
||||
const [gradientCount, setGradientCount] = useState(10);
|
||||
const [gradientResult, setGradientResult] = useState<string[]>([]);
|
||||
const gradientMutation = useGenerateGradient();
|
||||
|
||||
const { data, isLoading, isError, error } = useColorInfo({
|
||||
colors: [color],
|
||||
});
|
||||
|
||||
const { data, isLoading } = useColorInfo({ colors: [color] });
|
||||
const colorInfo = data?.colors[0];
|
||||
|
||||
// Update URL when color changes
|
||||
useEffect(() => {
|
||||
const hex = color.replace('#', '');
|
||||
if (hex.length === 6 || hex.length === 3) {
|
||||
@@ -64,301 +68,289 @@ function ColorManipulationContent() {
|
||||
|
||||
// Sync first gradient stop with active color
|
||||
useEffect(() => {
|
||||
const newStops = [...stops];
|
||||
newStops[0] = color;
|
||||
setStops(newStops);
|
||||
setStops((prev) => [color, ...prev.slice(1)]);
|
||||
}, [color]);
|
||||
|
||||
const handleShare = () => {
|
||||
const url = `${window.location.origin}/color?color=${color.replace('#', '')}`;
|
||||
navigator.clipboard.writeText(url);
|
||||
toast.success('Link copied to clipboard!');
|
||||
navigator.clipboard.writeText(`${window.location.origin}/color?color=${color.replace('#', '')}`);
|
||||
toast.success('Link copied!');
|
||||
};
|
||||
|
||||
const generateHarmony = async () => {
|
||||
try {
|
||||
const result = await paletteMutation.mutateAsync({
|
||||
base: color,
|
||||
scheme: harmonyType,
|
||||
});
|
||||
|
||||
const colors = [result.palette.primary, ...result.palette.secondary];
|
||||
setPalette(colors);
|
||||
toast.success(`Generated ${harmonyType} harmony palette`);
|
||||
} catch (error) {
|
||||
toast.error('Failed to generate harmony palette');
|
||||
console.error(error);
|
||||
}
|
||||
const result = await paletteMutation.mutateAsync({ base: color, scheme: harmonyType });
|
||||
setPalette([result.palette.primary, ...result.palette.secondary]);
|
||||
toast.success(`Generated ${harmonyType} palette`);
|
||||
} catch { toast.error('Failed to generate palette'); }
|
||||
};
|
||||
|
||||
const generateGradient = async () => {
|
||||
try {
|
||||
const result = await gradientMutation.mutateAsync({
|
||||
stops,
|
||||
count: gradientCount,
|
||||
});
|
||||
const result = await gradientMutation.mutateAsync({ stops, count: gradientCount });
|
||||
setGradientResult(result.gradient);
|
||||
toast.success(`Generated ${result.gradient.length} colors`);
|
||||
} catch (error) {
|
||||
toast.error('Failed to generate gradient');
|
||||
}
|
||||
} catch { toast.error('Failed to generate gradient'); }
|
||||
};
|
||||
|
||||
const addStop = () => {
|
||||
setStops([...stops, '#000000']);
|
||||
};
|
||||
|
||||
const removeStop = (index: number) => {
|
||||
if (index === 0) return;
|
||||
if (stops.length > 2) {
|
||||
setStops(stops.filter((_, i) => i !== index));
|
||||
}
|
||||
};
|
||||
|
||||
const updateStop = (index: number, colorValue: string) => {
|
||||
const newStops = [...stops];
|
||||
newStops[index] = colorValue;
|
||||
setStops(newStops);
|
||||
if (index === 0) setColor(colorValue);
|
||||
};
|
||||
|
||||
const harmonyDescriptions: Record<HarmonyType, string> = {
|
||||
monochromatic: 'Single color with variations',
|
||||
analogous: 'Colors adjacent on the color wheel (±30°)',
|
||||
complementary: 'Colors opposite on the color wheel (180°)',
|
||||
triadic: 'Three colors evenly spaced on the color wheel (120°)',
|
||||
tetradic: 'Four colors evenly spaced on the color wheel (90°)',
|
||||
const updateStop = (i: number, v: string) => {
|
||||
const next = [...stops];
|
||||
next[i] = v;
|
||||
setStops(next);
|
||||
if (i === 0) setColor(v);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Row 1: Workspace */}
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6 items-stretch">
|
||||
{/* Main Workspace: Color Picker and Information */}
|
||||
<div className="lg:col-span-2">
|
||||
<Card className="h-full">
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
||||
<CardTitle>Color Picker</CardTitle>
|
||||
<Button onClick={handleShare} variant="outline" size="xs">
|
||||
<Share2 className="h-3 w-3 mr-1" />
|
||||
Share
|
||||
</Button>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex flex-col md:flex-row gap-8">
|
||||
<div className="flex-shrink-0 mx-auto md:mx-0">
|
||||
<ColorPicker color={color} onChange={setColor} />
|
||||
</div>
|
||||
<div className="flex flex-col gap-4">
|
||||
|
||||
<div className="flex-1 min-w-0">
|
||||
{isLoading && (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isError && (
|
||||
<div className="p-3 bg-destructive/10 text-destructive rounded-lg text-sm">
|
||||
<p className="font-medium">Error loading color information</p>
|
||||
<p className="mt-1">{error?.message || 'Unknown error'}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{colorInfo && <ColorInfo info={colorInfo} />}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Sidebar: Color Manipulation */}
|
||||
<div className="lg:col-span-1">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>Adjustments</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ManipulationPanel color={color} onColorChange={setColor} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
{/* ── Mobile tab switcher ────────────────────────────────── */}
|
||||
<div className="flex lg:hidden glass rounded-xl p-1 gap-1">
|
||||
{(['pick', 'explore'] as MobileTab[]).map((t) => (
|
||||
<button
|
||||
key={t}
|
||||
onClick={() => setMobileTab(t)}
|
||||
className={cn(
|
||||
'flex-1 py-2.5 rounded-lg text-sm font-medium capitalize transition-all',
|
||||
mobileTab === t
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'text-muted-foreground hover:text-foreground'
|
||||
)}
|
||||
>
|
||||
{t === 'pick' ? 'Pick' : 'Explore'}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Row 2: Harmony Generator */}
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6 items-stretch">
|
||||
{/* Harmony Controls */}
|
||||
<div className="lg:col-span-1">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>Harmony</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<Select
|
||||
value={harmonyType}
|
||||
onValueChange={(value) => setHarmonyType(value as HarmonyType)}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Select harmony" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="monochromatic">Monochromatic</SelectItem>
|
||||
<SelectItem value="analogous">Analogous</SelectItem>
|
||||
<SelectItem value="complementary">Complementary</SelectItem>
|
||||
<SelectItem value="triadic">Triadic</SelectItem>
|
||||
<SelectItem value="tetradic">Tetradic (Square)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{/* ── Main layout ────────────────────────────────────────── */}
|
||||
<div
|
||||
className="grid grid-cols-1 lg:grid-cols-5 gap-4"
|
||||
style={{ height: 'calc(100svh - 220px)', minHeight: '620px' }}
|
||||
>
|
||||
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{harmonyDescriptions[harmonyType]}
|
||||
</p>
|
||||
{/* Left panel: Picker + ColorInfo */}
|
||||
<div
|
||||
className={cn(
|
||||
'lg:col-span-2 flex flex-col gap-3 overflow-hidden',
|
||||
mobileTab !== 'pick' && 'hidden lg:flex'
|
||||
)}
|
||||
>
|
||||
{/* Color picker card */}
|
||||
<div className="glass rounded-xl p-4 shrink-0">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">
|
||||
Color
|
||||
</span>
|
||||
<button onClick={handleShare} className={actionBtn}>
|
||||
<Share2 className="w-3 h-3" /> Share
|
||||
</button>
|
||||
</div>
|
||||
<ColorPicker color={color} onChange={setColor} />
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={generateHarmony}
|
||||
disabled={paletteMutation.isPending}
|
||||
className="w-full"
|
||||
>
|
||||
{paletteMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="h-3.5 w-3.5 mr-1.5 animate-spin" />
|
||||
Generating...
|
||||
</>
|
||||
) : (
|
||||
'Generate'
|
||||
)}
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Harmony Results */}
|
||||
<div className="lg:col-span-2">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
Palette {palette.length > 0 && <span className="text-muted-foreground font-normal text-sm ml-1">({palette.length})</span>}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{palette.length > 0 ? (
|
||||
<div className="space-y-5">
|
||||
<PaletteGrid colors={palette} onColorClick={setColor} />
|
||||
<div className="pt-3 border-t">
|
||||
<ExportMenu colors={palette} />
|
||||
</div>
|
||||
{/* Color info card */}
|
||||
<div className="glass rounded-xl p-4 flex flex-col flex-1 min-h-0 overflow-hidden">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest block mb-3 shrink-0">
|
||||
Info
|
||||
</span>
|
||||
<div className="flex-1 min-h-0 overflow-y-auto scrollbar-thin scrollbar-thumb-primary/20 scrollbar-track-transparent">
|
||||
{isLoading ? (
|
||||
<div className="flex justify-center py-8">
|
||||
<Loader2 className="w-4 w-4 animate-spin text-muted-foreground/40" />
|
||||
</div>
|
||||
) : (
|
||||
<div className="py-8 text-center text-muted-foreground text-xs">
|
||||
<Palette className="h-8 w-8 mx-auto mb-2 opacity-20" />
|
||||
<p>Generate a harmony palette from the current color</p>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Row 3: Gradient Generator */}
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6 items-stretch">
|
||||
{/* Gradient Controls */}
|
||||
<div className="lg:col-span-1">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>Gradient</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label className="text-xs">Color Stops</Label>
|
||||
{stops.map((stop, index) => (
|
||||
<div key={index} className="flex items-center gap-2">
|
||||
<Input
|
||||
type="color"
|
||||
value={stop}
|
||||
onChange={(e) => updateStop(index, e.target.value)}
|
||||
className="w-9 h-9 p-1 shrink-0 cursor-pointer"
|
||||
/>
|
||||
<Input
|
||||
type="text"
|
||||
value={stop}
|
||||
onChange={(e) => updateStop(index, e.target.value)}
|
||||
className="font-mono text-xs flex-1"
|
||||
/>
|
||||
{index !== 0 && stops.length > 2 && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon-xs"
|
||||
onClick={() => removeStop(index)}
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
<Button onClick={addStop} variant="outline" className="w-full">
|
||||
<Plus className="h-3.5 w-3.5 mr-1.5" />
|
||||
Add Stop
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label className="text-xs">Steps</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min={2}
|
||||
max={100}
|
||||
value={gradientCount}
|
||||
onChange={(e) => setGradientCount(parseInt(e.target.value))}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={generateGradient}
|
||||
disabled={gradientMutation.isPending}
|
||||
className="w-full"
|
||||
>
|
||||
{gradientMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="h-3.5 w-3.5 mr-1.5 animate-spin" />
|
||||
Generating...
|
||||
</>
|
||||
) : (
|
||||
'Generate'
|
||||
)}
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : colorInfo ? (
|
||||
<ColorInfo info={colorInfo} />
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Gradient Results */}
|
||||
<div className="lg:col-span-2">
|
||||
<Card className="h-full">
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
Gradient {gradientResult.length > 0 && <span className="text-muted-foreground font-normal text-sm ml-1">({gradientResult.length})</span>}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{gradientResult.length > 0 ? (
|
||||
<div className="space-y-5">
|
||||
{/* Right panel: tabbed tools */}
|
||||
<div
|
||||
className={cn(
|
||||
'lg:col-span-3 flex flex-col overflow-hidden',
|
||||
mobileTab !== 'explore' && 'hidden lg:flex'
|
||||
)}
|
||||
>
|
||||
<div className="glass rounded-xl p-4 flex flex-col flex-1 min-h-0 overflow-hidden">
|
||||
|
||||
{/* Tab switcher */}
|
||||
<div className="flex glass rounded-lg p-0.5 gap-0.5 mb-4 shrink-0">
|
||||
{RIGHT_TABS.map(({ value, label }) => (
|
||||
<button
|
||||
key={value}
|
||||
onClick={() => setRightTab(value)}
|
||||
className={cn(
|
||||
'flex-1 py-1.5 rounded-md text-xs font-medium transition-all',
|
||||
rightTab === value
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'text-muted-foreground hover:text-foreground'
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Tab content */}
|
||||
<div className="flex-1 min-h-0 overflow-y-auto scrollbar-thin scrollbar-thumb-primary/20 scrollbar-track-transparent pr-0.5">
|
||||
|
||||
{/* ── Info tab ─────────────────────────────── */}
|
||||
{rightTab === 'info' && (
|
||||
<div className="space-y-3">
|
||||
{/* Large color preview */}
|
||||
<div
|
||||
className="h-16 w-full rounded-lg border"
|
||||
style={{
|
||||
background: `linear-gradient(to right, ${gradientResult.join(', ')})`,
|
||||
}}
|
||||
className="w-full rounded-xl border border-white/8 transition-colors duration-300"
|
||||
style={{ height: '140px', background: color }}
|
||||
/>
|
||||
<PaletteGrid colors={gradientResult} onColorClick={setColor} />
|
||||
<div className="pt-3 border-t">
|
||||
<ExportMenu colors={gradientResult} />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="py-8 text-center text-muted-foreground text-xs">
|
||||
<Layers className="h-8 w-8 mx-auto mb-2 opacity-20" />
|
||||
<p>Add color stops and generate a smooth gradient</p>
|
||||
{isLoading ? (
|
||||
<div className="flex justify-center py-6">
|
||||
<Loader2 className="w-5 h-5 animate-spin text-muted-foreground/40" />
|
||||
</div>
|
||||
) : colorInfo ? (
|
||||
<ColorInfo info={colorInfo} />
|
||||
) : null}
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* ── Adjust tab ───────────────────────────── */}
|
||||
{rightTab === 'adjust' && (
|
||||
<ManipulationPanel color={color} onColorChange={setColor} />
|
||||
)}
|
||||
|
||||
{/* ── Harmony tab ──────────────────────────── */}
|
||||
{rightTab === 'harmony' && (
|
||||
<div className="space-y-4">
|
||||
{/* Scheme selector */}
|
||||
<div className="space-y-2">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">
|
||||
Scheme
|
||||
</span>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{HARMONY_OPTS.map((opt) => (
|
||||
<button
|
||||
key={opt.value}
|
||||
onClick={() => setHarmonyType(opt.value)}
|
||||
className={cn(
|
||||
'px-2.5 py-1 rounded-lg border text-xs font-mono transition-all',
|
||||
harmonyType === opt.value
|
||||
? 'bg-primary/10 border-primary/40 text-primary'
|
||||
: 'border-border/30 text-muted-foreground hover:border-primary/30 hover:text-foreground'
|
||||
)}
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<p className="text-[10px] text-muted-foreground/50 font-mono">
|
||||
{HARMONY_OPTS.find((o) => o.value === harmonyType)?.desc}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={generateHarmony}
|
||||
disabled={paletteMutation.isPending}
|
||||
className={cn(actionBtn, 'w-full justify-center py-2')}
|
||||
>
|
||||
{paletteMutation.isPending
|
||||
? <><Loader2 className="w-3 h-3 animate-spin" /> Generating…</>
|
||||
: <><Palette className="w-3 h-3" /> Generate Palette</>
|
||||
}
|
||||
</button>
|
||||
|
||||
{palette.length > 0 && (
|
||||
<div className="space-y-4">
|
||||
<PaletteGrid colors={palette} onColorClick={setColor} />
|
||||
<div className="border-t border-border/25 pt-4">
|
||||
<ExportMenu colors={palette} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* ── Gradient tab ─────────────────────────── */}
|
||||
{rightTab === 'gradient' && (
|
||||
<div className="space-y-4">
|
||||
{/* Color stops */}
|
||||
<div className="space-y-2">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">
|
||||
Stops
|
||||
</span>
|
||||
{stops.map((stop, i) => (
|
||||
<div key={i} className="flex items-center gap-2">
|
||||
<input
|
||||
type="color"
|
||||
value={stop}
|
||||
onChange={(e) => updateStop(i, e.target.value)}
|
||||
className="w-8 h-8 rounded-lg cursor-pointer border border-border/40 bg-transparent shrink-0 p-0.5"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={stop}
|
||||
onChange={(e) => updateStop(i, e.target.value)}
|
||||
className="flex-1 bg-transparent border border-border/40 rounded-lg px-2.5 py-1.5 text-xs font-mono outline-none focus:border-primary/50 transition-colors"
|
||||
/>
|
||||
{i !== 0 && stops.length > 2 && (
|
||||
<button
|
||||
onClick={() => setStops(stops.filter((_, idx) => idx !== i))}
|
||||
className="shrink-0 text-muted-foreground/35 hover:text-destructive transition-colors"
|
||||
>
|
||||
<X className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
<button
|
||||
onClick={() => setStops([...stops, '#000000'])}
|
||||
className="w-full py-1.5 rounded-lg border border-dashed border-border/30 text-xs text-muted-foreground/40 hover:text-foreground hover:border-primary/30 transition-all flex items-center justify-center gap-1"
|
||||
>
|
||||
<Plus className="w-3 h-3" /> Add stop
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Steps */}
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest shrink-0">
|
||||
Steps
|
||||
</span>
|
||||
<input
|
||||
type="number"
|
||||
min={2}
|
||||
max={100}
|
||||
value={gradientCount}
|
||||
onChange={(e) => setGradientCount(parseInt(e.target.value))}
|
||||
className="w-20 bg-transparent border border-border/40 rounded-lg px-2.5 py-1.5 text-xs font-mono text-center outline-none focus:border-primary/50 transition-colors"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={generateGradient}
|
||||
disabled={gradientMutation.isPending}
|
||||
className={cn(actionBtn, 'w-full justify-center py-2')}
|
||||
>
|
||||
{gradientMutation.isPending
|
||||
? <><Loader2 className="w-3 h-3 animate-spin" /> Generating…</>
|
||||
: <><Layers className="w-3 h-3" /> Generate Gradient</>
|
||||
}
|
||||
</button>
|
||||
|
||||
{gradientResult.length > 0 && (
|
||||
<div className="space-y-4">
|
||||
{/* Gradient preview bar */}
|
||||
<div
|
||||
className="h-12 w-full rounded-xl border border-white/8"
|
||||
style={{ background: `linear-gradient(to right, ${gradientResult.join(', ')})` }}
|
||||
/>
|
||||
<PaletteGrid colors={gradientResult} onColorClick={setColor} />
|
||||
<div className="border-t border-border/25 pt-4">
|
||||
<ExportMenu colors={gradientResult} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -369,7 +361,7 @@ export function ColorManipulation() {
|
||||
return (
|
||||
<Suspense fallback={
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground/40" />
|
||||
</div>
|
||||
}>
|
||||
<ColorManipulationContent />
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { HexColorPicker } from 'react-colorful';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { cn } from '@/lib/utils/cn';
|
||||
import { hexToRgb } from '@/lib/color/utils/color';
|
||||
|
||||
@@ -13,45 +11,23 @@ interface ColorPickerProps {
|
||||
}
|
||||
|
||||
export function ColorPicker({ color, onChange, className }: ColorPickerProps) {
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const value = e.target.value;
|
||||
// Allow partial input while typing
|
||||
onChange(value);
|
||||
};
|
||||
|
||||
// Determine text color based on background brightness
|
||||
const getContrastColor = (hex: string) => {
|
||||
const rgb = hexToRgb(hex);
|
||||
if (!rgb) return 'inherit';
|
||||
const brightness = (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;
|
||||
return brightness > 128 ? '#000000' : '#ffffff';
|
||||
};
|
||||
|
||||
const textColor = getContrastColor(color);
|
||||
const rgb = hexToRgb(color);
|
||||
const brightness = rgb ? (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000 : 0;
|
||||
const textColor = brightness > 128 ? '#000000' : '#ffffff';
|
||||
const borderColor = brightness > 128 ? 'rgba(0,0,0,0.12)' : 'rgba(255,255,255,0.2)';
|
||||
|
||||
return (
|
||||
<div className={cn('flex flex-col items-center justify-center space-y-3', className)}>
|
||||
<div className="w-full max-w-[200px] space-y-3">
|
||||
<HexColorPicker color={color} onChange={onChange} className="!w-full" />
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="color-input" className="text-xs">
|
||||
Hex Value
|
||||
</Label>
|
||||
<Input
|
||||
id="color-input"
|
||||
type="text"
|
||||
value={color}
|
||||
onChange={handleInputChange}
|
||||
placeholder="#ff0099"
|
||||
className="font-mono text-xs transition-colors duration-200"
|
||||
style={{
|
||||
backgroundColor: color,
|
||||
color: textColor,
|
||||
borderColor: textColor === '#000000' ? 'rgba(0,0,0,0.1)' : 'rgba(255,255,255,0.2)'
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className={cn('flex flex-col gap-3', className)}>
|
||||
<HexColorPicker color={color} onChange={onChange} className="!w-full" />
|
||||
<input
|
||||
type="text"
|
||||
value={color}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
placeholder="#ff0099"
|
||||
className="w-full font-mono text-xs rounded-lg px-3 py-2 outline-none transition-colors duration-200 border"
|
||||
style={{ backgroundColor: color, color: textColor, borderColor }}
|
||||
spellCheck={false}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -13,54 +13,43 @@ interface ColorSwatchProps {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function ColorSwatch({
|
||||
color,
|
||||
size = 'md',
|
||||
showLabel = true,
|
||||
onClick,
|
||||
className,
|
||||
}: ColorSwatchProps) {
|
||||
export function ColorSwatch({ color, size = 'md', showLabel = true, onClick, className }: ColorSwatchProps) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
const sizeClasses = {
|
||||
sm: 'h-12 w-12',
|
||||
md: 'h-16 w-16',
|
||||
lg: 'h-24 w-24',
|
||||
};
|
||||
|
||||
const handleCopy = (e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
const handleClick = () => {
|
||||
if (onClick) { onClick(); return; }
|
||||
navigator.clipboard.writeText(color);
|
||||
setCopied(true);
|
||||
toast.success(`Copied ${color}`);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
setTimeout(() => setCopied(false), 1500);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn('flex flex-col items-center gap-2', className)}>
|
||||
<button
|
||||
className={cn(
|
||||
'relative rounded-lg ring-2 ring-border transition-all duration-200',
|
||||
'hover:scale-110 hover:ring-primary hover:shadow-lg',
|
||||
'focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
|
||||
'group active:scale-95',
|
||||
sizeClasses[size]
|
||||
)}
|
||||
style={{ backgroundColor: color }}
|
||||
onClick={onClick || handleCopy}
|
||||
aria-label={`Color ${color}`}
|
||||
>
|
||||
<div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-all duration-200 bg-black/30 rounded-lg backdrop-blur-sm">
|
||||
{copied ? (
|
||||
<Check className="h-5 w-5 text-white animate-scale-in" />
|
||||
) : (
|
||||
<Copy className="h-5 w-5 text-white" />
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
{showLabel && (
|
||||
<span className="text-xs font-mono text-muted-foreground">{color}</span>
|
||||
<button
|
||||
onClick={handleClick}
|
||||
title={color}
|
||||
aria-label={`Color ${color}`}
|
||||
className={cn(
|
||||
'group relative w-full rounded-lg overflow-hidden border border-white/8 transition-all',
|
||||
'hover:scale-[1.04] hover:border-white/20 hover:shadow-lg hover:shadow-black/20',
|
||||
size === 'sm' && 'h-10',
|
||||
size === 'md' && 'h-14',
|
||||
size === 'lg' && 'h-20',
|
||||
className
|
||||
)}
|
||||
</div>
|
||||
style={{ backgroundColor: color }}
|
||||
>
|
||||
<div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity bg-black/25">
|
||||
{copied
|
||||
? <Check className="w-3.5 h-3.5 text-white drop-shadow" />
|
||||
: <Copy className="w-3.5 h-3.5 text-white drop-shadow" />
|
||||
}
|
||||
</div>
|
||||
{showLabel && (
|
||||
<div className="absolute bottom-0 inset-x-0 px-1 py-0.5 text-[9px] font-mono text-white/70 bg-black/25 truncate text-center leading-tight">
|
||||
{color}
|
||||
</div>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useState, useEffect } from 'react';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Download, Copy, Check, Loader2 } from 'lucide-react';
|
||||
import { toast } from 'sonner';
|
||||
import {
|
||||
@@ -21,6 +20,7 @@ import {
|
||||
type ExportColor,
|
||||
} from '@/lib/color/utils/export';
|
||||
import { colorAPI } from '@/lib/color/api/client';
|
||||
import { cn } from '@/lib/utils/cn';
|
||||
|
||||
interface ExportMenuProps {
|
||||
colors: string[];
|
||||
@@ -30,6 +30,9 @@ interface ExportMenuProps {
|
||||
type ExportFormat = 'css' | 'scss' | 'tailwind' | 'json' | 'javascript';
|
||||
type ColorSpace = 'hex' | 'rgb' | 'hsl' | 'lab' | 'oklab' | 'lch' | 'oklch';
|
||||
|
||||
const actionBtn =
|
||||
'flex items-center gap-1.5 px-3 py-1.5 text-xs glass rounded-md border border-border/30 text-muted-foreground hover:text-primary hover:border-primary/30 hover:bg-primary/10 transition-all disabled:opacity-40 disabled:cursor-not-allowed';
|
||||
|
||||
export function ExportMenu({ colors, className }: ExportMenuProps) {
|
||||
const [format, setFormat] = useState<ExportFormat>('css');
|
||||
const [colorSpace, setColorSpace] = useState<ColorSpace>('hex');
|
||||
@@ -39,152 +42,105 @@ export function ExportMenu({ colors, className }: ExportMenuProps) {
|
||||
|
||||
useEffect(() => {
|
||||
async function convertColors() {
|
||||
if (colorSpace === 'hex') {
|
||||
setConvertedColors(colors);
|
||||
return;
|
||||
}
|
||||
|
||||
if (colorSpace === 'hex') { setConvertedColors(colors); return; }
|
||||
setIsConverting(true);
|
||||
try {
|
||||
const response = await colorAPI.convertFormat({
|
||||
colors,
|
||||
format: colorSpace,
|
||||
});
|
||||
|
||||
const response = await colorAPI.convertFormat({ colors, format: colorSpace });
|
||||
if (response.success) {
|
||||
setConvertedColors(response.data.conversions.map(c => c.output));
|
||||
setConvertedColors(response.data.conversions.map((c) => c.output));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to convert colors:', error);
|
||||
toast.error('Failed to convert colors to selected space');
|
||||
} catch {
|
||||
toast.error('Failed to convert colors');
|
||||
} finally {
|
||||
setIsConverting(false);
|
||||
}
|
||||
}
|
||||
|
||||
convertColors();
|
||||
}, [colors, colorSpace]);
|
||||
|
||||
const exportColors: ExportColor[] = convertedColors.map((value) => ({ value }));
|
||||
|
||||
const getExportContent = (): string => {
|
||||
const getContent = (): string => {
|
||||
switch (format) {
|
||||
case 'css':
|
||||
return exportAsCSS(exportColors);
|
||||
case 'scss':
|
||||
return exportAsSCSS(exportColors);
|
||||
case 'tailwind':
|
||||
return exportAsTailwind(exportColors);
|
||||
case 'json':
|
||||
return exportAsJSON(exportColors);
|
||||
case 'javascript':
|
||||
return exportAsJavaScript(exportColors);
|
||||
case 'css': return exportAsCSS(exportColors);
|
||||
case 'scss': return exportAsSCSS(exportColors);
|
||||
case 'tailwind': return exportAsTailwind(exportColors);
|
||||
case 'json': return exportAsJSON(exportColors);
|
||||
case 'javascript': return exportAsJavaScript(exportColors);
|
||||
}
|
||||
};
|
||||
|
||||
const getFileExtension = (): string => {
|
||||
switch (format) {
|
||||
case 'css':
|
||||
return 'css';
|
||||
case 'scss':
|
||||
return 'scss';
|
||||
case 'tailwind':
|
||||
return 'js';
|
||||
case 'json':
|
||||
return 'json';
|
||||
case 'javascript':
|
||||
return 'js';
|
||||
}
|
||||
};
|
||||
const getExt = () => ({ css: 'css', scss: 'scss', tailwind: 'js', json: 'json', javascript: 'js' }[format]);
|
||||
|
||||
const handleCopy = () => {
|
||||
const content = getExportContent();
|
||||
navigator.clipboard.writeText(content);
|
||||
navigator.clipboard.writeText(getContent());
|
||||
setCopied(true);
|
||||
toast.success('Copied to clipboard!');
|
||||
toast.success('Copied!');
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
};
|
||||
|
||||
const handleDownload = () => {
|
||||
const content = getExportContent();
|
||||
const extension = getFileExtension();
|
||||
downloadAsFile(content, `palette.${extension}`, 'text/plain');
|
||||
downloadAsFile(getContent(), `palette.${getExt()}`, 'text/plain');
|
||||
toast.success('Downloaded!');
|
||||
};
|
||||
|
||||
if (colors.length === 0) {
|
||||
return null;
|
||||
}
|
||||
if (colors.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<div className="space-y-3">
|
||||
<div className="flex flex-col md:flex-row gap-3">
|
||||
<Select
|
||||
value={format}
|
||||
onValueChange={(value) => setFormat(value as ExportFormat)}
|
||||
>
|
||||
<SelectTrigger className="w-full md:flex-1">
|
||||
<SelectValue placeholder="Format" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="css">CSS Variables</SelectItem>
|
||||
<SelectItem value="scss">SCSS Variables</SelectItem>
|
||||
<SelectItem value="tailwind">Tailwind Config</SelectItem>
|
||||
<SelectItem value="json">JSON</SelectItem>
|
||||
<SelectItem value="javascript">JavaScript Array</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<div className={cn('space-y-3', className)}>
|
||||
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">Export</span>
|
||||
|
||||
<Select
|
||||
value={colorSpace}
|
||||
onValueChange={(value) => setColorSpace(value as ColorSpace)}
|
||||
>
|
||||
<SelectTrigger className="w-full md:flex-1">
|
||||
<SelectValue placeholder="Space" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="hex">Hex</SelectItem>
|
||||
<SelectItem value="rgb">RGB</SelectItem>
|
||||
<SelectItem value="hsl">HSL</SelectItem>
|
||||
<SelectItem value="lab">Lab</SelectItem>
|
||||
<SelectItem value="oklab">OkLab</SelectItem>
|
||||
<SelectItem value="lch">LCH</SelectItem>
|
||||
<SelectItem value="oklch">OkLCH</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{/* Selectors */}
|
||||
<div className="flex gap-2">
|
||||
<Select value={format} onValueChange={(v) => setFormat(v as ExportFormat)}>
|
||||
<SelectTrigger className="flex-1 h-7 text-xs border-border/30 bg-transparent hover:border-primary/30 transition-colors">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="css">CSS Vars</SelectItem>
|
||||
<SelectItem value="scss">SCSS</SelectItem>
|
||||
<SelectItem value="tailwind">Tailwind</SelectItem>
|
||||
<SelectItem value="json">JSON</SelectItem>
|
||||
<SelectItem value="javascript">JS Array</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select value={colorSpace} onValueChange={(v) => setColorSpace(v as ColorSpace)}>
|
||||
<SelectTrigger className="flex-1 h-7 text-xs border-border/30 bg-transparent hover:border-primary/30 transition-colors">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{['hex', 'rgb', 'hsl', 'lab', 'oklab', 'lch', 'oklch'].map((s) => (
|
||||
<SelectItem key={s} value={s} className="font-mono text-xs">{s}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="p-3 bg-muted/50 rounded-lg relative min-h-[80px]">
|
||||
{isConverting ? (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-muted/50 backdrop-blur-sm rounded-lg z-10">
|
||||
<Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
) : null}
|
||||
<pre className="text-[11px] overflow-x-auto leading-relaxed">
|
||||
<code>{getExportContent()}</code>
|
||||
</pre>
|
||||
</div>
|
||||
{/* Code preview */}
|
||||
<div
|
||||
className="relative rounded-xl overflow-hidden border border-white/5 min-h-[80px]"
|
||||
style={{ background: '#06060e' }}
|
||||
>
|
||||
{isConverting && (
|
||||
<div className="absolute inset-0 flex items-center justify-center z-10 bg-black/30">
|
||||
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)}
|
||||
<pre className="p-3 text-[10px] font-mono text-white/60 overflow-x-auto leading-relaxed">
|
||||
<code>{getContent()}</code>
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col md:flex-row gap-3">
|
||||
<Button onClick={handleCopy} variant="outline" className="w-full md:flex-1" disabled={isConverting}>
|
||||
{copied ? (
|
||||
<>
|
||||
<Check className="h-3.5 w-3.5 mr-1.5" />
|
||||
Copied
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Copy className="h-3.5 w-3.5 mr-1.5" />
|
||||
Copy
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
<Button onClick={handleDownload} variant="default" className="w-full md:flex-1" disabled={isConverting}>
|
||||
<Download className="h-3.5 w-3.5 mr-1.5" />
|
||||
Download
|
||||
</Button>
|
||||
</div>
|
||||
{/* Actions */}
|
||||
<div className="flex gap-2">
|
||||
<button onClick={handleCopy} disabled={isConverting} className={cn(actionBtn, 'flex-1 justify-center')}>
|
||||
{copied ? <Check className="w-3 h-3" /> : <Copy className="w-3 h-3" />}
|
||||
{copied ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
<button onClick={handleDownload} disabled={isConverting} className={cn(actionBtn, 'flex-1 justify-center')}>
|
||||
<Download className="w-3 h-3" />
|
||||
Download
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -2,34 +2,25 @@
|
||||
|
||||
import { useState } from 'react';
|
||||
import { Slider } from '@/components/ui/slider';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
useLighten,
|
||||
useDarken,
|
||||
useSaturate,
|
||||
useDesaturate,
|
||||
useRotate,
|
||||
useComplement
|
||||
useComplement,
|
||||
} from '@/lib/color/api/queries';
|
||||
import { toast } from 'sonner';
|
||||
import { Sun, Moon, Droplets, Droplet, RotateCcw, ArrowLeftRight } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils/cn';
|
||||
|
||||
interface ManipulationPanelProps {
|
||||
color: string;
|
||||
onColorChange: (color: string) => void;
|
||||
}
|
||||
|
||||
interface ManipulationRow {
|
||||
label: string;
|
||||
icon: React.ReactNode;
|
||||
value: number;
|
||||
setValue: (v: number) => void;
|
||||
format: (v: number) => string;
|
||||
min: number;
|
||||
max: number;
|
||||
step: number;
|
||||
onApply: () => Promise<void>;
|
||||
}
|
||||
const actionBtn =
|
||||
'shrink-0 px-3 py-1 text-[10px] font-mono glass rounded-md border border-border/30 text-muted-foreground hover:text-primary hover:border-primary/30 hover:bg-primary/10 transition-all disabled:opacity-40 disabled:cursor-not-allowed';
|
||||
|
||||
export function ManipulationPanel({ color, onColorChange }: ManipulationPanelProps) {
|
||||
const [lightenAmount, setLightenAmount] = useState(0.2);
|
||||
@@ -53,150 +44,104 @@ export function ManipulationPanel({ color, onColorChange }: ManipulationPanelPro
|
||||
rotateMutation.isPending ||
|
||||
complementMutation.isPending;
|
||||
|
||||
const handleMutation = async (
|
||||
mutationFn: (params: any) => Promise<any>,
|
||||
const applyMutation = async (
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
mutationFn: (p: any) => Promise<{ colors: { output: string }[] }>,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
params: any,
|
||||
successMsg: string,
|
||||
errorMsg: string
|
||||
msg: string
|
||||
) => {
|
||||
try {
|
||||
const result = await mutationFn(params);
|
||||
if (result.colors[0]) {
|
||||
onColorChange(result.colors[0].output);
|
||||
toast.success(successMsg);
|
||||
toast.success(msg);
|
||||
}
|
||||
} catch {
|
||||
toast.error(errorMsg);
|
||||
toast.error('Failed to apply');
|
||||
}
|
||||
};
|
||||
|
||||
const rows: ManipulationRow[] = [
|
||||
const rows = [
|
||||
{
|
||||
label: 'Lighten',
|
||||
icon: <Sun className="h-3.5 w-3.5" />,
|
||||
value: lightenAmount,
|
||||
setValue: setLightenAmount,
|
||||
format: (v) => `${(v * 100).toFixed(0)}%`,
|
||||
label: 'Lighten', icon: <Sun className="w-3 h-3" />,
|
||||
value: lightenAmount, setValue: setLightenAmount,
|
||||
display: `${(lightenAmount * 100).toFixed(0)}%`,
|
||||
min: 0, max: 1, step: 0.05,
|
||||
onApply: () => handleMutation(
|
||||
lightenMutation.mutateAsync,
|
||||
{ colors: [color], amount: lightenAmount },
|
||||
`Lightened by ${(lightenAmount * 100).toFixed(0)}%`,
|
||||
'Failed to lighten color'
|
||||
),
|
||||
onApply: () => applyMutation(lightenMutation.mutateAsync, { colors: [color], amount: lightenAmount }, `Lightened ${(lightenAmount * 100).toFixed(0)}%`),
|
||||
},
|
||||
{
|
||||
label: 'Darken',
|
||||
icon: <Moon className="h-3.5 w-3.5" />,
|
||||
value: darkenAmount,
|
||||
setValue: setDarkenAmount,
|
||||
format: (v) => `${(v * 100).toFixed(0)}%`,
|
||||
label: 'Darken', icon: <Moon className="w-3 h-3" />,
|
||||
value: darkenAmount, setValue: setDarkenAmount,
|
||||
display: `${(darkenAmount * 100).toFixed(0)}%`,
|
||||
min: 0, max: 1, step: 0.05,
|
||||
onApply: () => handleMutation(
|
||||
darkenMutation.mutateAsync,
|
||||
{ colors: [color], amount: darkenAmount },
|
||||
`Darkened by ${(darkenAmount * 100).toFixed(0)}%`,
|
||||
'Failed to darken color'
|
||||
),
|
||||
onApply: () => applyMutation(darkenMutation.mutateAsync, { colors: [color], amount: darkenAmount }, `Darkened ${(darkenAmount * 100).toFixed(0)}%`),
|
||||
},
|
||||
{
|
||||
label: 'Saturate',
|
||||
icon: <Droplets className="h-3.5 w-3.5" />,
|
||||
value: saturateAmount,
|
||||
setValue: setSaturateAmount,
|
||||
format: (v) => `${(v * 100).toFixed(0)}%`,
|
||||
label: 'Saturate', icon: <Droplets className="w-3 h-3" />,
|
||||
value: saturateAmount, setValue: setSaturateAmount,
|
||||
display: `${(saturateAmount * 100).toFixed(0)}%`,
|
||||
min: 0, max: 1, step: 0.05,
|
||||
onApply: () => handleMutation(
|
||||
saturateMutation.mutateAsync,
|
||||
{ colors: [color], amount: saturateAmount },
|
||||
`Saturated by ${(saturateAmount * 100).toFixed(0)}%`,
|
||||
'Failed to saturate color'
|
||||
),
|
||||
onApply: () => applyMutation(saturateMutation.mutateAsync, { colors: [color], amount: saturateAmount }, `Saturated ${(saturateAmount * 100).toFixed(0)}%`),
|
||||
},
|
||||
{
|
||||
label: 'Desaturate',
|
||||
icon: <Droplet className="h-3.5 w-3.5" />,
|
||||
value: desaturateAmount,
|
||||
setValue: setDesaturateAmount,
|
||||
format: (v) => `${(v * 100).toFixed(0)}%`,
|
||||
label: 'Desaturate', icon: <Droplet className="w-3 h-3" />,
|
||||
value: desaturateAmount, setValue: setDesaturateAmount,
|
||||
display: `${(desaturateAmount * 100).toFixed(0)}%`,
|
||||
min: 0, max: 1, step: 0.05,
|
||||
onApply: () => handleMutation(
|
||||
desaturateMutation.mutateAsync,
|
||||
{ colors: [color], amount: desaturateAmount },
|
||||
`Desaturated by ${(desaturateAmount * 100).toFixed(0)}%`,
|
||||
'Failed to desaturate color'
|
||||
),
|
||||
onApply: () => applyMutation(desaturateMutation.mutateAsync, { colors: [color], amount: desaturateAmount }, `Desaturated ${(desaturateAmount * 100).toFixed(0)}%`),
|
||||
},
|
||||
{
|
||||
label: 'Rotate',
|
||||
icon: <RotateCcw className="h-3.5 w-3.5" />,
|
||||
value: rotateAmount,
|
||||
setValue: setRotateAmount,
|
||||
format: (v) => `${v}°`,
|
||||
label: 'Rotate Hue', icon: <RotateCcw className="w-3 h-3" />,
|
||||
value: rotateAmount, setValue: setRotateAmount,
|
||||
display: `${rotateAmount}°`,
|
||||
min: -180, max: 180, step: 5,
|
||||
onApply: () => handleMutation(
|
||||
rotateMutation.mutateAsync,
|
||||
{ colors: [color], amount: rotateAmount },
|
||||
`Rotated hue by ${rotateAmount}°`,
|
||||
'Failed to rotate hue'
|
||||
),
|
||||
onApply: () => applyMutation(rotateMutation.mutateAsync, { colors: [color], amount: rotateAmount }, `Rotated ${rotateAmount}°`),
|
||||
},
|
||||
];
|
||||
|
||||
const handleComplement = async () => {
|
||||
try {
|
||||
const result = await complementMutation.mutateAsync([color]);
|
||||
if (result.colors[0]) {
|
||||
onColorChange(result.colors[0].output);
|
||||
toast.success('Generated complementary color');
|
||||
}
|
||||
} catch {
|
||||
toast.error('Failed to generate complement');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{rows.map((row) => (
|
||||
<div key={row.label} className="space-y-2">
|
||||
<div key={row.label} className="space-y-1.5">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-1.5 text-xs font-medium">
|
||||
<div className="flex items-center gap-1.5 text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">
|
||||
{row.icon}
|
||||
<span>{row.label}</span>
|
||||
</div>
|
||||
<span className="text-[10px] text-muted-foreground tabular-nums">{row.format(row.value)}</span>
|
||||
<span className="text-[10px] text-muted-foreground/40 font-mono tabular-nums">{row.display}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Slider
|
||||
min={row.min}
|
||||
max={row.max}
|
||||
step={row.step}
|
||||
min={row.min} max={row.max} step={row.step}
|
||||
value={[row.value]}
|
||||
onValueChange={(vals) => row.setValue(vals[0])}
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button
|
||||
onClick={row.onApply}
|
||||
disabled={isLoading}
|
||||
variant="outline"
|
||||
className="shrink-0 w-16"
|
||||
>
|
||||
<button onClick={row.onApply} disabled={isLoading} className={actionBtn}>
|
||||
Apply
|
||||
</Button>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
<div className="pt-3 border-t">
|
||||
<Button
|
||||
onClick={handleComplement}
|
||||
<div className="pt-3 border-t border-border/25">
|
||||
<button
|
||||
onClick={async () => {
|
||||
try {
|
||||
const result = await complementMutation.mutateAsync([color]);
|
||||
if (result.colors[0]) {
|
||||
onColorChange(result.colors[0].output);
|
||||
toast.success('Complementary color applied');
|
||||
}
|
||||
} catch { toast.error('Failed'); }
|
||||
}}
|
||||
disabled={isLoading}
|
||||
variant="outline"
|
||||
className="w-full"
|
||||
className={cn(actionBtn, 'w-full justify-center flex items-center gap-1.5 py-2')}
|
||||
>
|
||||
<ArrowLeftRight className="h-3.5 w-3.5 mr-1.5" />
|
||||
<ArrowLeftRight className="w-3 h-3" />
|
||||
Complementary Color
|
||||
</Button>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -19,16 +19,12 @@ export function PaletteGrid({ colors, onColorClick, className }: PaletteGridProp
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'grid grid-cols-3 sm:grid-cols-4 md:grid-cols-5 lg:grid-cols-6 gap-4',
|
||||
className
|
||||
)}
|
||||
>
|
||||
<div className={cn('grid grid-cols-4 sm:grid-cols-5 gap-2', className)}>
|
||||
{colors.map((color, index) => (
|
||||
<ColorSwatch
|
||||
key={`${color}-${index}`}
|
||||
color={color}
|
||||
size="sm"
|
||||
onClick={onColorClick ? () => onColorClick(color) : undefined}
|
||||
/>
|
||||
))}
|
||||
|
||||
Reference in New Issue
Block a user