Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify/minify script.ts #264

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions next-themes/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ export const script = (
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
})
isClass && value
? themes.map(t => value[t] || t)
: themes

function updateDOM(theme: string) {
Expand All @@ -37,19 +34,17 @@ export const script = (
}

function getSystemTheme() {
return window.matchMedia(media).matches ? 'dark' : 'light'
return window.matchMedia('(prefers-color-scheme: dark)').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
const themeName = localStorage.getItem(storageKey) || defaultTheme
const isSystem = enableSystem && themeName === 'system'
const theme = isSystem ? getSystemTheme() : themeName
updateDOM(theme)
} catch (e) {
//
Expand Down
Loading