| 1 | export const TEXT_SIZES = ["small", "default", "large", "xlarge", "xxlarge"] as const; |
| 2 | |
| 3 | export type TextSize = (typeof TEXT_SIZES)[number]; |
| 4 | |
| 5 | export const DEFAULT_TEXT_SIZE: TextSize = "default"; |
| 6 | |
| 7 | const TEXT_SIZE_KEY = "reasonix-text-size"; |
| 8 | |
| 9 | export function isTextSize(value: unknown): value is TextSize { |
| 10 | return typeof value === "string" && (TEXT_SIZES as readonly string[]).includes(value); |
| 11 | } |
| 12 | |
| 13 | export function nextTextSize(current: TextSize, delta: -1 | 1): TextSize { |
| 14 | const index = TEXT_SIZES.indexOf(current); |
| 15 | const nextIndex = Math.min(TEXT_SIZES.length - 1, Math.max(0, index + delta)); |
| 16 | return TEXT_SIZES[nextIndex]; |
| 17 | } |
| 18 | |
| 19 | export function getTextSize(): TextSize { |
| 20 | const stored = typeof localStorage !== "undefined" ? localStorage.getItem(TEXT_SIZE_KEY) : null; |
| 21 | return isTextSize(stored) ? stored : DEFAULT_TEXT_SIZE; |
| 22 | } |
| 23 | |
| 24 | export function applyTextSize(size: TextSize): void { |
| 25 | if (typeof document === "undefined") return; |
| 26 | const root = document.documentElement; |
| 27 | if (size === DEFAULT_TEXT_SIZE) root.removeAttribute("data-text-size"); |
| 28 | else root.setAttribute("data-text-size", size); |
| 29 | try { |
| 30 | localStorage.setItem(TEXT_SIZE_KEY, size); |
| 31 | } catch { |
| 32 | /* private mode / no storage - the in-DOM attribute still applies */ |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export function initTextSize(): void { |
| 37 | applyTextSize(getTextSize()); |
| 38 | } |
| 39 |