-
-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
70 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,62 @@ | ||
(() => { | ||
function getTheme() { | ||
return localStorage.getItem('theme') || 'auto'; | ||
} | ||
|
||
function isDark() { | ||
if (theme === 'auto') { | ||
return window.matchMedia('(prefers-color-scheme: dark)').matches; | ||
} else { | ||
return theme === 'dark'; | ||
} | ||
} | ||
|
||
function setTheme(newTheme) { | ||
const noTransitions = Object.assign(document.createElement('style'), { | ||
textContent: '* { transition: none !important; }' | ||
}); | ||
|
||
theme = newTheme; | ||
localStorage.setItem('theme', theme); | ||
|
||
// Update the UI | ||
[...menu.querySelectorAll('sl-menu-item')].map(item => (item.checked = item.getAttribute('value') === theme)); | ||
menuIcon.name = isDark() ? 'moon' : 'sun'; | ||
|
||
// Toggle the dark mode class without transitions | ||
document.body.appendChild(noTransitions); | ||
requestAnimationFrame(() => { | ||
document.documentElement.classList.toggle('sl-theme-dark', isDark()); | ||
requestAnimationFrame(() => document.body.removeChild(noTransitions)); | ||
}); | ||
} | ||
|
||
let theme = getTheme(); | ||
|
||
// Generate the theme picker dropdown | ||
const dropdown = document.createElement('sl-dropdown'); | ||
dropdown.classList.add('theme-picker'); | ||
dropdown.innerHTML = ` | ||
<sl-button size="small" slot="trigger" caret> | ||
<sl-icon name="sun" label="Select Theme"></sl-icon> | ||
</sl-button> | ||
<sl-menu> | ||
<sl-menu-item value="light">Light</sl-menu-item> | ||
<sl-menu-item value="dark">Dark</sl-menu-item> | ||
<sl-menu-divider></sl-menu-divider> | ||
<sl-menu-item value="auto">Auto</sl-menu-item> | ||
</sl-menu> | ||
`; | ||
document.body.prepend(dropdown); | ||
|
||
// Listen for selections | ||
const menu = dropdown.querySelector('sl-menu'); | ||
const menuIcon = dropdown.querySelector('sl-icon'); | ||
menu.addEventListener('sl-select', event => setTheme(event.detail.item.value)); | ||
|
||
// Update the theme when the preference changes | ||
window.matchMedia('(prefers-color-scheme: dark)').addListener(event => setTheme(theme)); | ||
|
||
// Set the intial theme and sync the UI | ||
setTheme(theme); | ||
})(); |
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