mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 01:51:37 -04:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import {useEffect} from 'react'
|
|
import {usePersistentLocalState} from 'web/hooks/use-persistent-local-state'
|
|
|
|
export type FontOption = 'atkinson' | 'system-sans' | 'classic-serif'
|
|
|
|
const FONT_VARIABLES: Record<FontOption, string> = {
|
|
atkinson: '"Atkinson Hyperlegible Next", Georgia, "Times New Roman", Times, serif',
|
|
'system-sans':
|
|
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif',
|
|
'classic-serif': 'Georgia, "Times New Roman", Times, serif',
|
|
}
|
|
|
|
export const useFontPreference = () => {
|
|
const [font, setFontState] = usePersistentLocalState<FontOption>('atkinson', 'font-preference')
|
|
|
|
const setFont = (newFont: FontOption) => {
|
|
setFontState(newFont)
|
|
applyFontPreference(newFont)
|
|
}
|
|
|
|
return {font, setFont}
|
|
}
|
|
|
|
export const useFontPreferenceManager = () => {
|
|
useEffect(() => {
|
|
applyFontPreference(getStoredFontPreference())
|
|
}, [])
|
|
}
|
|
|
|
export const applyFontPreference = (font: FontOption) => {
|
|
const fontFamily = FONT_VARIABLES[font] ?? FONT_VARIABLES.atkinson
|
|
document.documentElement.style.setProperty('--font-main', fontFamily)
|
|
}
|
|
|
|
const getStoredFontPreference = (): FontOption => {
|
|
if (typeof window === 'undefined') return 'atkinson'
|
|
const font = JSON.parse(localStorage.getItem('font-preference') ?? 'null') ?? 'atkinson'
|
|
return font.value ?? (font as FontOption)
|
|
}
|