Files
Momento/memento-note/components/theme-initializer.tsx

62 lines
1.7 KiB
TypeScript

'use client'
import { useEffect } from 'react'
import { applyDocumentTheme, normalizeThemeId } from '@/lib/apply-document-theme'
interface ThemeInitializerProps {
theme?: string
fontSize?: string
fontFamily?: string
}
export function ThemeInitializer({ theme, fontSize, fontFamily }: ThemeInitializerProps) {
useEffect(() => {
const applyFontSize = (s?: string) => {
const size = s || 'medium'
const fontSizeMap: Record<string, string> = {
small: '14px',
medium: '16px',
large: '18px',
'extra-large': '20px',
}
const fontSizeFactorMap: Record<string, number> = {
small: 0.95,
medium: 1.0,
large: 1.1,
'extra-large': 1.25,
}
const root = document.documentElement
root.style.setProperty('--user-font-size', fontSizeMap[size] || '16px')
root.style.setProperty('--user-font-size-factor', (fontSizeFactorMap[size] || 1).toString())
}
const localTheme = localStorage.getItem('theme-preference')
const effectiveTheme = localTheme || theme || 'light'
applyDocumentTheme(effectiveTheme)
if (!localTheme && theme) {
localStorage.setItem('theme-preference', normalizeThemeId(theme))
}
applyFontSize(fontSize)
const localFontFamily = localStorage.getItem('font-family')
const effectiveFontFamily = localFontFamily || fontFamily || 'inter'
const root = document.documentElement
if (effectiveFontFamily === 'system') {
root.classList.add('font-system')
} else {
root.classList.remove('font-system')
}
if (!localFontFamily && fontFamily) {
localStorage.setItem('font-family', fontFamily)
}
}, [theme, fontSize, fontFamily])
return null
}