Skip to content

Commit

Permalink
perf: use minimiser for window.localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 19, 2024
1 parent dda8469 commit e880c93
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// Global variable minimizers
const w = window
const de = document.documentElement
const ls = window.localStorage

const knownColorSchemes = ['dark', 'light']

const preference = (window && window.localStorage && window.localStorage.getItem && window.localStorage.getItem('<%= options.storageKey %>')) || '<%= options.preference %>'
const preference = (ls && ls.getItem && ls.getItem('<%= options.storageKey %>')) || '<%= options.preference %>'
let value = preference === 'system' ? getColorScheme() : preference
// Applied forced color mode
const forcedColorMode = de.getAttribute('data-color-mode-forced')
Expand All @@ -18,7 +19,6 @@

addColorScheme(value)


w['<%= options.globalName %>'] = {
preference,
value,
Expand Down Expand Up @@ -63,8 +63,8 @@
function getColorScheme () {
if (
// @ts-expect-error TS assumes matchMedia is always defined
w.matchMedia
&& prefersColorScheme('').media !== 'not all') {
w.matchMedia &&
prefersColorScheme('').media !== 'not all') {
for (const colorScheme of knownColorSchemes) {
if (prefersColorScheme(':' + colorScheme).matches) {
return colorScheme
Expand Down

0 comments on commit e880c93

Please sign in to comment.