|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { useThemeCustomization } from '@/hooks/useThemeCustomization'; |
| 5 | +import { DEFAULT_THEME } from '@/utils/themeUtils'; |
| 6 | + |
| 7 | +export default function ThemeCustomizer() { |
| 8 | + const { theme, setTheme, reset, exportTheme, importTheme } = useThemeCustomization(); |
| 9 | + const [local, setLocal] = React.useState(theme); |
| 10 | + |
| 11 | + React.useEffect(() => setLocal(theme), [theme]); |
| 12 | + |
| 13 | + function updateColor(key: keyof typeof theme.colors, value: string) { |
| 14 | + setLocal((prev) => ({ ...prev, colors: { ...prev.colors, [key]: value } })); |
| 15 | + } |
| 16 | + |
| 17 | + function apply() { |
| 18 | + setTheme(local); |
| 19 | + } |
| 20 | + |
| 21 | + async function handleImportFile(e: React.ChangeEvent<HTMLInputElement>) { |
| 22 | + const f = e.target.files?.[0]; |
| 23 | + if (!f) return; |
| 24 | + const txt = await f.text(); |
| 25 | + const res = importTheme(txt); |
| 26 | + if (!res.ok) { |
| 27 | + // simple alert for now |
| 28 | + alert('Failed to import theme: ' + (res.error || 'invalid')); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="p-4 border rounded-md bg-white"> |
| 34 | + <h2 className="text-lg font-semibold mb-2">Theme Customizer</h2> |
| 35 | + <div className="grid grid-cols-2 gap-3"> |
| 36 | + <label className="flex flex-col text-sm"> |
| 37 | + Primary |
| 38 | + <input |
| 39 | + type="color" |
| 40 | + value={local.colors.primary} |
| 41 | + onChange={(e) => updateColor('primary', e.target.value)} |
| 42 | + /> |
| 43 | + </label> |
| 44 | + <label className="flex flex-col text-sm"> |
| 45 | + Accent |
| 46 | + <input |
| 47 | + type="color" |
| 48 | + value={local.colors.accent} |
| 49 | + onChange={(e) => updateColor('accent', e.target.value)} |
| 50 | + /> |
| 51 | + </label> |
| 52 | + <label className="flex flex-col text-sm"> |
| 53 | + Background |
| 54 | + <input |
| 55 | + type="color" |
| 56 | + value={local.colors.background} |
| 57 | + onChange={(e) => updateColor('background', e.target.value)} |
| 58 | + /> |
| 59 | + </label> |
| 60 | + <label className="flex flex-col text-sm"> |
| 61 | + Foreground |
| 62 | + <input |
| 63 | + type="color" |
| 64 | + value={local.colors.foreground} |
| 65 | + onChange={(e) => updateColor('foreground', e.target.value)} |
| 66 | + /> |
| 67 | + </label> |
| 68 | + </div> |
| 69 | + |
| 70 | + <div className="mt-4 flex gap-2"> |
| 71 | + <button onClick={apply} className="px-3 py-1 rounded bg-blue-600 text-white"> |
| 72 | + Apply |
| 73 | + </button> |
| 74 | + <button onClick={() => setTheme(DEFAULT_THEME as any)} className="px-3 py-1 rounded border"> |
| 75 | + Reset |
| 76 | + </button> |
| 77 | + <button |
| 78 | + onClick={() => { |
| 79 | + const json = exportTheme(); |
| 80 | + navigator.clipboard?.writeText(json); |
| 81 | + alert('Theme JSON copied to clipboard'); |
| 82 | + }} |
| 83 | + className="px-3 py-1 rounded border" |
| 84 | + > |
| 85 | + Copy JSON |
| 86 | + </button> |
| 87 | + <label className="px-3 py-1 rounded border cursor-pointer"> |
| 88 | + Import |
| 89 | + <input |
| 90 | + type="file" |
| 91 | + accept="application/json" |
| 92 | + onChange={handleImportFile} |
| 93 | + className="hidden" |
| 94 | + /> |
| 95 | + </label> |
| 96 | + </div> |
| 97 | + |
| 98 | + <div className="mt-4"> |
| 99 | + <div |
| 100 | + className="p-3 rounded" |
| 101 | + style={{ |
| 102 | + background: 'var(--background)', |
| 103 | + color: 'var(--foreground)', |
| 104 | + border: '1px solid rgba(0,0,0,0.06)', |
| 105 | + }} |
| 106 | + > |
| 107 | + <strong>Live preview</strong> |
| 108 | + <p className="mt-2"> |
| 109 | + Primary color sample:{' '} |
| 110 | + <span style={{ color: 'var(--color-primary)' }}>{local.colors.primary}</span> |
| 111 | + </p> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
0 commit comments