All files / src/hooks useAccessibilitySettings.ts

100% Statements 54/54
100% Branches 18/18
100% Functions 20/20
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                      11x 11x                                   11x 11x           1x 1x 1x 1x       2x 2x 2x 2x       4x 4x 4x 4x       2x 2x 2x 2x       5x 5x 5x 3x                   9x 9x                 124x       11x 10x 8x       11x 10x 8x       127x   1x   1x     125x             127x   1x   1x     125x               124x 124x 124x 124x 249x           124x    
import { useMemo } from "react";
import { create } from "zustand";
import {
  getCachedAccessibilityPrefs,
  setCachedAccessibilityPrefs,
} from "../services/accessibilitySettingsCache";
 
export type FontSizeOption = "small" | "medium" | "large";
export type FontWeightOption = "light" | "regular" | "bold";
 
/** Shared option lists (avoids duplicated `as const` assertions in UI). */
export const FONT_SIZE_OPTIONS: FontSizeOption[] = ["small", "medium", "large"];
export const FONT_WEIGHT_OPTIONS: FontWeightOption[] = [
  "light",
  "regular",
  "bold",
];
 
interface AccessibilitySettingsState {
  colorBlindMode: boolean;
  wheelchairUser: boolean;
  fontSize: FontSizeOption;
  fontWeight: FontWeightOption;
  toggleColorBlindMode: () => void;
  toggleWheelchairUser: () => void;
  setFontSize: (value: FontSizeOption) => void;
  setFontWeight: (value: FontWeightOption) => void;
  hydrateFromStorage: () => Promise<void>;
}
 
export const useAccessibilitySettingsStore = create<AccessibilitySettingsState>(
  (set, get) => ({
    colorBlindMode: false,
    wheelchairUser: false,
    fontSize: "medium",
    fontWeight: "regular",
    toggleColorBlindMode: () => {
      set((state) => {
        const next = { ...state, colorBlindMode: !state.colorBlindMode };
        void persist(next);
        return next;
      });
    },
    toggleWheelchairUser: () => {
      set((state) => {
        const next = { ...state, wheelchairUser: !state.wheelchairUser };
        void persist(next);
        return next;
      });
    },
    setFontSize: (fontSize) => {
      set((state) => {
        const next = { ...state, fontSize };
        void persist(next);
        return next;
      });
    },
    setFontWeight: (fontWeight) => {
      set((state) => {
        const next = { ...state, fontWeight };
        void persist(next);
        return next;
      });
    },
    hydrateFromStorage: async () => {
      try {
        const cached = await getCachedAccessibilityPrefs();
        if (!cached) return;
        set((state) => ({
          ...state,
          ...cached,
        }));
      } catch {}
    },
  }),
);
 
async function persist(state: AccessibilitySettingsState) {
  const { colorBlindMode, wheelchairUser, fontSize, fontWeight } = state;
  await setCachedAccessibilityPrefs({
    colorBlindMode,
    wheelchairUser,
    fontSize,
    fontWeight,
  });
}
 
export function useAccessibilitySettings() {
  return useAccessibilitySettingsStore();
}
 
export function getFontSizeLabel(size: FontSizeOption) {
  if (size === "small") return "Small";
  if (size === "large") return "Large";
  return "Medium";
}
 
export function getFontWeightLabel(weight: FontWeightOption) {
  if (weight === "light") return "Light";
  if (weight === "bold") return "Bold";
  return "Regular";
}
 
export function getFontScale(size: FontSizeOption): number {
  switch (size) {
    case "small":
      return 0.875;
    case "large":
      return 1.25;
    case "medium":
    default:
      return 1;
  }
}
 
export function getFontWeightValue(
  weight: FontWeightOption,
): "400" | "500" | "700" {
  switch (weight) {
    case "light":
      return "400";
    case "bold":
      return "700";
    case "regular":
    default:
      return "500";
  }
}
 
/**
 * Scaled text style for dynamic type (font size + weight from accessibility settings).
 */
export function useAccessibleTypography() {
  const { fontSize, fontWeight } = useAccessibilitySettings();
  const fontScale = getFontScale(fontSize);
  const weightValue = getFontWeightValue(fontWeight);
  const textStyle = useMemo(
    () => (baseSize: number) => ({
      fontSize: Math.round(baseSize * fontScale),
      fontWeight: weightValue,
    }),
    [fontScale, weightValue],
  );
  return { fontScale, weightValue, textStyle };
}