{"version":3,"file":"blocking.js","mappings":"MAcA,SAASA,EAAgBC,GAAOC,UAAEA,GAAY,GAAU,CAAC,GACvD,MAAMC,EAAO,OACPC,EAAQ,QACRC,EAAc,gBAEpB,IAAIC,EACAC,EACAC,EAGJ,IACEA,EAAiBC,aAAaC,QAAQL,EACxC,CAAE,MAAOM,GAEPC,QAAQC,KAAK,yCAA0CF,EACzD,CA0BA,GArBEL,EAFEE,EAEYA,IAAmBJ,EAAQA,EAAQD,EAGzBW,OAAOC,WAC7B,+BACAC,QAE8Bb,EAAOC,EAKvCG,EADEL,EACUI,IAAgBH,EAAOA,EAAOC,EAE9BE,IAAgBH,EAAOC,EAAQD,EAI7Cc,SAASC,KAAKC,UAAUC,OAAO,aAAcb,IAAcJ,GAGvDK,GAAkBP,EACpB,IACEQ,aAAaY,QAAQhB,EAAaE,EACpC,CAAE,MAAOI,GAEPC,QAAQC,KAAK,wCAAyCF,EACxD,CAEJ,CAOAX,EAAgB,KAAM,CAAEE,WAAW,IAKnCe,SAASK,iBAAiB,0BAA2BtB,EAAiB,CACpEuB,SAAS","sources":["webpack://sphinx-wagtail-theme/./js/blocking.js"],"sourcesContent":["/**\n * IMPORTANT: This script will block the DOM from loading, only add critical path JS here.\n */\n\n/**\n * Set and store dark/light (theme) mode.\n *\n * Mode will be set to be one of \"light\" or \"dark\", by default the resolved\n * theme mode will be flipped (e.g. light to dark).\n *\n * @param {Event} event - is for usage with event listeners, and indicates the user has manually clicked a button\n * @param {object?} options - additional options param when called explicitly\n * @param {boolean?} options.isInitial - if true the current value will be resolved & set NOT toggled\n */\nfunction updateThemeMode(event, { isInitial = false } = {}) {\n  const DARK = 'dark';\n  const LIGHT = 'light';\n  const STORAGE_KEY = 'wagtail-theme';\n\n  let currentMode;\n  let applyMode;\n  let savedThemeMode;\n\n  // safely request local storage - if cookies/storage is disabled it should not error\n  try {\n    savedThemeMode = localStorage.getItem(STORAGE_KEY);\n  } catch (error) {\n    // eslint-disable-next-line no-console\n    console.warn('Unable to read theme from localStorage', error);\n  }\n\n  // find the current mode from existing storage or browser preference\n  if (savedThemeMode) {\n    // note - do not assume correct format of local storage\n    currentMode = savedThemeMode === LIGHT ? LIGHT : DARK;\n  } else {\n    // fall back on browser media for first toggle\n    const prefersDarkMode = window.matchMedia(\n      '(prefers-color-scheme:dark)',\n    ).matches;\n\n    currentMode = prefersDarkMode ? DARK : LIGHT;\n  }\n\n  // if running initially - do not 'flip' - instead apply current mode\n  if (isInitial) {\n    applyMode = currentMode === DARK ? DARK : LIGHT;\n  } else {\n    applyMode = currentMode === DARK ? LIGHT : DARK;\n  }\n\n  // set applied mode to the DOM\n  document.body.classList.toggle('theme-dark', applyMode === DARK);\n\n  // only store value if already stored OR was triggered by an actual click\n  if (savedThemeMode || event) {\n    try {\n      localStorage.setItem(STORAGE_KEY, applyMode);\n    } catch (error) {\n      // eslint-disable-next-line no-console\n      console.warn('Unable to store theme in localStorage', error);\n    }\n  }\n}\n\n/**\n * Apply user's saved light/dark preference, or fall back to browser setting.\n * Load light/dark preference before DOMContentLoaded so the user does not see a\n * flash during theme initialisation.\n */\nupdateThemeMode(null, { isInitial: true });\n\n/**\n * Set up event listener for other manual toggling.\n */\ndocument.addEventListener('theme:toggle-theme-mode', updateThemeMode, {\n  passive: true,\n});\n"],"names":["updateThemeMode","event","isInitial","DARK","LIGHT","STORAGE_KEY","currentMode","applyMode","savedThemeMode","localStorage","getItem","error","console","warn","window","matchMedia","matches","document","body","classList","toggle","setItem","addEventListener","passive"],"sourceRoot":""}