-
Notifications
You must be signed in to change notification settings - Fork 8
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
20 changed files
with
327 additions
and
263 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/*! | ||
* Minimal theme switcher | ||
* | ||
* Pico.css - https://picocss.com | ||
* Copyright 2019-2024 - Licensed under MIT | ||
* | ||
* @see https://x4qtf8.csb.app/js/minimal-theme-switcher.js | ||
*/ | ||
|
||
const themeSwitcher = { | ||
// Config | ||
_scheme: "auto", | ||
menuTarget: "details.dropdown", | ||
buttonsTarget: "a[data-theme-switcher]", | ||
buttonAttribute: "data-theme-switcher", | ||
rootAttribute: "data-theme", | ||
localStorageKey: "picoPreferredColorScheme", | ||
|
||
// Init | ||
init() { | ||
this.scheme = this.schemeFromLocalStorage; | ||
this.initSwitchers(); | ||
}, | ||
|
||
// Get color scheme from local storage | ||
get schemeFromLocalStorage() { | ||
return window.localStorage?.getItem(this.localStorageKey) ?? this._scheme; | ||
}, | ||
|
||
// Preferred color scheme | ||
get preferredColorScheme() { | ||
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; | ||
}, | ||
|
||
// Init switchers | ||
initSwitchers() { | ||
const buttons = document.querySelectorAll(this.buttonsTarget); | ||
buttons.forEach((button) => { | ||
button.addEventListener( | ||
"click", | ||
(event) => { | ||
event.preventDefault(); | ||
// Set scheme | ||
this.scheme = button.getAttribute(this.buttonAttribute); | ||
// Close dropdown | ||
document.querySelector(this.menuTarget)?.removeAttribute("open"); | ||
}, | ||
false | ||
); | ||
}); | ||
}, | ||
|
||
// Set scheme | ||
set scheme(scheme) { | ||
if (scheme === "auto") { | ||
this._scheme = this.preferredColorScheme; | ||
} else if (scheme === "dark" || scheme === "light") { | ||
this._scheme = scheme; | ||
} | ||
this.applyScheme(); | ||
this.schemeToLocalStorage(); | ||
}, | ||
|
||
// Get scheme | ||
get scheme() { | ||
return this._scheme; | ||
}, | ||
|
||
// Apply scheme | ||
applyScheme() { | ||
document.querySelector("html")?.setAttribute(this.rootAttribute, this.scheme); | ||
}, | ||
|
||
// Store scheme to local storage | ||
schemeToLocalStorage() { | ||
window.localStorage?.setItem(this.localStorageKey, this.scheme); | ||
}, | ||
}; | ||
|
||
// Init | ||
themeSwitcher.init(); |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 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
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
Oops, something went wrong.