forked from pacocoursey/next-themes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved inline script minification (pacocoursey#262)
* move inline script to a separate file for free minificaiton * cleanup * simplify
- Loading branch information
1 parent
83ad33a
commit f5f06c8
Showing
5 changed files
with
87 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export const script = ( | ||
attribute, | ||
storageKey, | ||
defaultTheme, | ||
forcedTheme, | ||
themes, | ||
value, | ||
enableSystem, | ||
enableColorScheme | ||
) => { | ||
const el = document.documentElement | ||
const media = `(prefers-color-scheme: dark)` | ||
const systemThemes = ['light', 'dark'] | ||
const isClass = attribute === 'class' | ||
const classes = | ||
isClass && !!value | ||
? themes.map(t => { | ||
return value[t] || t | ||
}) | ||
: themes | ||
|
||
function updateDOM(theme: string) { | ||
if (isClass) { | ||
el.classList.remove(...classes) | ||
el.classList.add(theme) | ||
} else { | ||
el.setAttribute(attribute, theme) | ||
} | ||
|
||
setColorScheme(theme) | ||
} | ||
|
||
function setColorScheme(theme: string) { | ||
if (enableColorScheme && systemThemes.includes(theme)) { | ||
el.style.colorScheme = theme | ||
} | ||
} | ||
|
||
function getSystemTheme() { | ||
return window.matchMedia(media).matches ? 'dark' : 'light' | ||
} | ||
|
||
;(function () { | ||
if (forcedTheme) { | ||
updateDOM(forcedTheme) | ||
} else { | ||
try { | ||
const e = localStorage.getItem(storageKey) | ||
const themeName = e || defaultTheme | ||
const isSystem = enableSystem && themeName === 'system' ? true : false | ||
|
||
const theme = isSystem ? getSystemTheme() : e || defaultTheme | ||
updateDOM(theme) | ||
} catch (e) { | ||
// | ||
} | ||
} | ||
})() | ||
} |