All files / js theme-init.js

0% Statements 0/11
0% Branches 0/10
0% Functions 0/1
0% Lines 0/8

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                                           
/**
 * Anti-flash theme initialiser for Riksdagsmonitor news articles.
 *
 * Runs synchronously before first paint to set the correct `data-theme`
 * attribute on `<html>` and prevent a flash of the wrong theme.
 *
 * Uses the same storage key (`riksdagsmonitor-theme`) as theme-toggle.js
 * so that all theme state remains consistent across the app.
 *
 * @module js/theme-init
 */
(function () {
  var key = 'riksdagsmonitor-theme';
  var t = null;
  try { t = localStorage.getItem(key); } catch (_e) { /* ignore storage errors */ }
  if (t !== 'dark' && t !== 'light') {
    if (t !== null) { try { localStorage.removeItem(key); } catch (_e) { /* ignore storage errors */ } }
    t = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ? 'dark' : 'light';
  }
  document.documentElement.setAttribute('data-theme', t);
}());