mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-02-20 07:44:01 -05:00
* Add font preference selector in settings
* Consolidate font family config into shared global constant
* Revert "Consolidate font family config into shared global constant"
This reverts commit 789ddc98e1.
* Fix
38 lines
1.3 KiB
TypeScript
38 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'
|
|
return JSON.parse(localStorage.getItem('font-preference') ?? 'null') ?? 'atkinson'
|
|
}
|