|
| 1 | +import { computed } from 'vue' |
| 2 | +import { useLocalStorage } from '@vueuse/core' |
| 3 | + |
| 4 | +export interface AccentColor { |
| 5 | + id: string |
| 6 | + name: string |
| 7 | + value: string |
| 8 | +} |
| 9 | + |
| 10 | +export const ACCENT_COLORS = [ |
| 11 | + { id: 'rose', name: 'Rose', value: '#e9aeba' }, |
| 12 | + { id: 'amber', name: 'Amber', value: '#fbbf24' }, |
| 13 | + { id: 'emerald', name: 'Emerald', value: '#34d399' }, |
| 14 | + { id: 'sky', name: 'Sky', value: '#38bdf8' }, |
| 15 | + { id: 'violet', name: 'Violet', value: '#a78bfa' }, |
| 16 | + { id: 'coral', name: 'Coral', value: '#fb7185' }, |
| 17 | +] as const satisfies readonly AccentColor[] |
| 18 | + |
| 19 | +export type ColorId = (typeof ACCENT_COLORS)[number]['id'] |
| 20 | + |
| 21 | +function applyColorToDocument(color: string | null) { |
| 22 | + if (color) { |
| 23 | + document.documentElement.style.setProperty('--accent-color', color) |
| 24 | + } else { |
| 25 | + document.documentElement.style.removeProperty('--accent-color') |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export function useAccentColor() { |
| 30 | + const accentColorId = useLocalStorage<string | null>('app-accent', null) |
| 31 | + |
| 32 | + function setAccentColor(id: ColorId | null) { |
| 33 | + const chosenColor = ACCENT_COLORS.find(color => color.id === id)?.value ?? null |
| 34 | + applyColorToDocument(chosenColor) |
| 35 | + accentColorId.value = id |
| 36 | + } |
| 37 | + |
| 38 | + return { |
| 39 | + accentColorId: computed(() => accentColorId.value), |
| 40 | + setAccentColor, |
| 41 | + } |
| 42 | +} |
0 commit comments