GdzieWjade/src/lib/settings.ts

32 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-10-03 14:29:28 +02:00
import type { Lang } from './types';
export interface Settings {
lang: Lang;
textScale: 100 | 125 | 150;
highContrast: boolean;
reduceMotion: boolean;
showDemoPlaces: boolean;
}
export const defaultSettings = (lang: Lang): Settings => ({
lang, textScale: 100, highContrast: false, reduceMotion: false, showDemoPlaces: true,
});
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' && typeof s.showDemoPlaces === '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';
}