Replace shadcn Select → native <select>: - ascii/FontPreview.tsx: comment-style picker → glass pill wrapper with MessageSquareCode icon + native select - color/ExportMenu.tsx: format + color-space pickers → native select with shared selectCls - units/MainConverter.tsx: from/to unit pickers → native select Delete dead code: - components/media/FormatSelector.tsx (not imported anywhere, used shadcn Input + Label + Card) - components/ui/select.tsx — now unused - components/ui/input.tsx — now unused - components/ui/label.tsx — now unused - components/ui/card.tsx — now unused Remaining components/ui/: slider.tsx, tooltip.tsx (TooltipProvider in Providers.tsx), slider-row.tsx, color-input.tsx Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
142 lines
4.9 KiB
TypeScript
142 lines
4.9 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import { Download, Copy, Check, Loader2 } from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import {
|
|
exportAsCSS,
|
|
exportAsSCSS,
|
|
exportAsTailwind,
|
|
exportAsJSON,
|
|
exportAsJavaScript,
|
|
downloadAsFile,
|
|
type ExportColor,
|
|
} from '@/lib/color/utils/export';
|
|
import { colorAPI } from '@/lib/color/api/client';
|
|
import { cn } from '@/lib/utils/cn';
|
|
|
|
interface ExportMenuProps {
|
|
colors: string[];
|
|
className?: string;
|
|
}
|
|
|
|
type ExportFormat = 'css' | 'scss' | 'tailwind' | 'json' | 'javascript';
|
|
type ColorSpace = 'hex' | 'rgb' | 'hsl' | 'lab' | 'oklab' | 'lch' | 'oklch';
|
|
|
|
const selectCls =
|
|
'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 text-foreground/80 cursor-pointer';
|
|
|
|
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');
|
|
const [convertedColors, setConvertedColors] = useState<string[]>(colors);
|
|
const [isConverting, setIsConverting] = useState(false);
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
useEffect(() => {
|
|
async function convertColors() {
|
|
if (colorSpace === 'hex') { setConvertedColors(colors); return; }
|
|
setIsConverting(true);
|
|
try {
|
|
const response = await colorAPI.convertFormat({ colors, format: colorSpace });
|
|
if (response.success) {
|
|
setConvertedColors(response.data.conversions.map((c) => c.output));
|
|
}
|
|
} catch {
|
|
toast.error('Failed to convert colors');
|
|
} finally {
|
|
setIsConverting(false);
|
|
}
|
|
}
|
|
convertColors();
|
|
}, [colors, colorSpace]);
|
|
|
|
const exportColors: ExportColor[] = convertedColors.map((value) => ({ value }));
|
|
|
|
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);
|
|
}
|
|
};
|
|
|
|
const getExt = () => ({ css: 'css', scss: 'scss', tailwind: 'js', json: 'json', javascript: 'js' }[format]);
|
|
|
|
const handleCopy = () => {
|
|
navigator.clipboard.writeText(getContent());
|
|
setCopied(true);
|
|
toast.success('Copied!');
|
|
setTimeout(() => setCopied(false), 2000);
|
|
};
|
|
|
|
const handleDownload = () => {
|
|
downloadAsFile(getContent(), `palette.${getExt()}`, 'text/plain');
|
|
toast.success('Downloaded!');
|
|
};
|
|
|
|
if (colors.length === 0) return null;
|
|
|
|
return (
|
|
<div className={cn('space-y-3', className)}>
|
|
<span className="text-[10px] font-semibold text-muted-foreground uppercase tracking-widest">Export</span>
|
|
|
|
{/* Selectors */}
|
|
<div className="flex gap-2">
|
|
<select
|
|
value={format}
|
|
onChange={(e) => setFormat(e.target.value as ExportFormat)}
|
|
className={selectCls}
|
|
>
|
|
<option value="css">CSS Vars</option>
|
|
<option value="scss">SCSS</option>
|
|
<option value="tailwind">Tailwind</option>
|
|
<option value="json">JSON</option>
|
|
<option value="javascript">JS Array</option>
|
|
</select>
|
|
<select
|
|
value={colorSpace}
|
|
onChange={(e) => setColorSpace(e.target.value as ColorSpace)}
|
|
className={selectCls}
|
|
>
|
|
{['hex', 'rgb', 'hsl', 'lab', 'oklab', 'lch', 'oklch'].map((s) => (
|
|
<option key={s} value={s}>{s}</option>
|
|
))}
|
|
</select>
|
|
</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>
|
|
|
|
{/* 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>
|
|
);
|
|
}
|