Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

3.0 KB
Raw Blame
/*
 * Pre-paint appearance boot — kills the flash of default/wrong theme (FOUC).
 *
 * Loaded as an external, render-blocking CLASSIC script in <head> (NOT a module)
 * so it runs before first paint AND complies with the production CSP
 * (script-src 'self'; inline scripts are blocked). It reads the compact snapshot
 * written by client/src/theme/applyAppearance.ts and applies it verbatim. Keep
 * this in sync with that module's snapshot shape + apply logic.
 *
 * It must never throw — any failure silently falls back to the default look.
 */
(function () {
  try {
    var raw = localStorage.getItem('trek_appearance');
    // Inside the iOS and Android app, before anyone signed in on this server
    // from here: the page carries on the address screen's colour until the
    // sign-in screen is up (native/appBoot.ts), instead of flashing white.
    // The app's bridge is in place before this script runs.
    var w = window;
    var inApp = !!(w.androidBridge || (w.webkit && w.webkit.messageHandlers && w.webkit.messageHandlers.bridge));
    if (inApp && !raw) {
      document.documentElement.setAttribute('data-app-boot', '');
      document.documentElement.style.backgroundColor = '#fffaf5';
    }
    if (!raw) return;
    var s = JSON.parse(raw);
    if (!s || s.v !== 1) return;

    var root = document.documentElement;
    var path = location.pathname;
    var isShared = path.indexOf('/shared/') === 0 || path.indexOf('/public/') === 0;

    var dark;
    if (isShared) dark = false;
    else if (s.darkMode === 'auto') dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
    else dark = s.darkMode === true || s.darkMode === 'dark';
    root.classList.toggle('dark', dark);

    var scheme = isShared ? 'default' : s.scheme;
    if (scheme && scheme !== 'default') root.setAttribute('data-scheme', scheme);
    if (!isShared && s.noTransparency) root.setAttribute('data-no-transparency', '');
    if (s.density === 'compact') root.setAttribute('data-density', 'compact');
    if (s.reduceMotion) root.setAttribute('data-reduce-motion', '');

    if (!isShared && scheme === 'custom' && s.accent) {
      root.style.setProperty('--accent-custom-light', s.accent.light);
      root.style.setProperty('--accent-custom-dark', s.accent.dark);
      if (s.accentText) {
        root.style.setProperty('--accent-custom-text-light', s.accentText.light);
        root.style.setProperty('--accent-custom-text-dark', s.accentText.dark);
      }
    }

    var ts = s.typeScale || {};
    var fs = typeof s.fontScale === 'number' ? s.fontScale : 1;
    setScale('--fs-scale-title', fs * (ts.title || 1));
    setScale('--fs-scale-subtitle', fs * (ts.subtitle || 1));
    setScale('--fs-scale-body', fs * (ts.body || 1));
    setScale('--fs-scale-caption', fs * (ts.caption || 1));
    if (fs !== 1) root.style.fontSize = fs * 100 + '%';

    function setScale(name, v) {
      if (typeof v === 'number' && v !== 1) root.style.setProperty(name, String(v));
    }
  } catch (e) {
    /* never block boot */
  }
})();