Files
pastel-ui/lib/utils/export.ts

84 lines
2.0 KiB
TypeScript
Raw Normal View History

feat: add comprehensive export functionality for palettes Implement full palette export system with multiple formats: **Export Utilities (lib/utils/export.ts):** - exportAsCSS() - CSS custom properties (:root variables) - exportAsSCSS() - SCSS variables ($color-name format) - exportAsTailwind() - Tailwind config module export - exportAsJSON() - Structured JSON with color objects - exportAsJavaScript() - JavaScript array of colors - downloadAsFile() - Browser download helper - ExportColor interface for typed exports **ExportMenu Component:** - Format selector dropdown (5 formats) - Live preview of export code - Syntax-highlighted code display - Copy to clipboard button - Download as file button - Success feedback (Check icon + toast) - Responsive layout - Conditional rendering (empty state) **Export Formats:** 1. CSS Variables: ```css :root { --color-1: #ff0099; --color-2: #0099ff; } ``` 2. SCSS Variables: ```scss $color-1: #ff0099; $color-2: #0099ff; ``` 3. Tailwind Config: ```js module.exports = { theme: { extend: { colors: { 'color-1': '#ff0099', }, }, }, }; ``` 4. JSON: ```json { "colors": [ { "name": "color-1", "hex": "#ff0099" } ] } ``` 5. JavaScript Array: ```js const colors = ['#ff0099', '#0099ff']; ``` **Integration:** - Added to Gradient Creator page - Added to Distinct Colors page - Conditional rendering (shows only when colors exist) - Proper spacing and layout **Features:** - Real-time preview of generated code - Copy with toast notification - Download with proper file extensions - Format-specific filenames - Clean, readable output - Proper indentation and formatting **User Experience:** - Select format from dropdown - See live preview immediately - One-click copy or download - Success feedback - Professional code formatting Build successful! Export functionality working on all palette pages. Next: Keyboard shortcuts and URL sharing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-07 12:19:52 +01:00
/**
* Export utilities for color palettes
*/
export interface ExportColor {
name?: string;
hex: string;
}
/**
* Export colors as CSS variables
*/
export function exportAsCSS(colors: ExportColor[]): string {
const variables = colors
.map((color, index) => {
const name = color.name || `color-${index + 1}`;
return ` --${name}: ${color.hex};`;
})
.join('\n');
return `:root {\n${variables}\n}`;
}
/**
* Export colors as SCSS variables
*/
export function exportAsSCSS(colors: ExportColor[]): string {
return colors
.map((color, index) => {
const name = color.name || `color-${index + 1}`;
return `$${name}: ${color.hex};`;
})
.join('\n');
}
/**
* Export colors as Tailwind config
*/
export function exportAsTailwind(colors: ExportColor[]): string {
const colorEntries = colors
.map((color, index) => {
const name = color.name || `color-${index + 1}`;
return ` '${name}': '${color.hex}',`;
})
.join('\n');
return `module.exports = {\n theme: {\n extend: {\n colors: {\n${colorEntries}\n },\n },\n },\n};`;
}
/**
* Export colors as JSON
*/
export function exportAsJSON(colors: ExportColor[]): string {
const colorObjects = colors.map((color, index) => ({
name: color.name || `color-${index + 1}`,
hex: color.hex,
}));
return JSON.stringify({ colors: colorObjects }, null, 2);
}
/**
* Export colors as JavaScript array
*/
export function exportAsJavaScript(colors: ExportColor[]): string {
const colorArray = colors.map((c) => `'${c.hex}'`).join(', ');
return `const colors = [${colorArray}];`;
}
/**
* Download text as file
*/
export function downloadAsFile(content: string, filename: string, mimeType: string = 'text/plain') {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}