GdzieWjade/src/lib/settings.ts

30 lines
995 B
TypeScript

import type { Lang } from './types';
export interface Settings {
lang: Lang;
textScale: 100 | 125 | 150;
highContrast: boolean;
reduceMotion: boolean;
}
export const defaultSettings = (lang: Lang): Settings => ({
lang, textScale: 100, highContrast: false, reduceMotion: false,
});
export function isSettings(v: unknown): v is Settings {
if (typeof v !== 'object' || v === null) return false;
const s = v as Record<string, unknown>;
return (
(s.lang === 'pl' || s.lang === 'en') && (s.textScale === 100 || s.textScale === 125 || s.textScale === 150) &&
typeof s.highContrast === 'boolean' && typeof s.reduceMotion === 'boolean'
);
}
/** Applies display settings to the document root. */
export function applySettings(s: Settings) {
const root = document.documentElement;
root.lang = s.lang;
root.style.fontSize = `${s.textScale}%`;
root.dataset.contrast = s.highContrast ? 'high' : 'default';
root.dataset.motion = s.reduceMotion ? 'reduce' : 'default';
}