Skip to content

Commit

Permalink
fix: allow multiple attributes in the injection script
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian24Design committed Aug 31, 2024
1 parent bf0c5a4 commit a4589b0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions next-themes/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ export const script = (
) => {
const el = document.documentElement
const systemThemes = ['light', 'dark']
const isClass = attribute === 'class'
const classes = isClass && value ? themes.map(t => value[t] || t) : themes

function updateDOM(theme: string) {
if (isClass) {
el.classList.remove(...classes)
el.classList.add(theme)
} else {
el.setAttribute(attribute, theme)
}
const attributes = Array.isArray(attribute) ? attribute : [attribute]

attributes.forEach(attr => {
const isClass = attr === 'class'
const classes = isClass && value ? themes.map(t => value[t] || t) : themes
if (isClass) {
el.classList.remove(...classes)
el.classList.add(theme)
} else {
el.setAttribute(attr, theme)
}
})

setColorScheme(theme)
}
Expand Down

0 comments on commit a4589b0

Please sign in to comment.