-
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.
- Loading branch information
Showing
4 changed files
with
162 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--- | ||
--- | ||
|
||
<script> | ||
const sun = document.getElementById('iconSun'); | ||
const moon = document.getElementById('iconMoon'); | ||
const state = document.getElementById('debugState'); | ||
const close = document.getElementById('closeB'); | ||
let checky = localStorage.getItem('theme'); | ||
const dataTheme = document.documentElement; | ||
|
||
const button = document.getElementById('themeSwitcher'); | ||
// const theme = dataTheme.getAttribute('data-test'); | ||
|
||
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches | ||
? "dark" | ||
: "light"; | ||
|
||
// Check local storage for 'theme' | ||
// if defined already, make sure that <html> data-theme corresponds with localStorage. | ||
// If not defined, set a new entry in localStorage based on the system's Theme. | ||
switch(localStorage.getItem('theme')) { | ||
case 'dark': | ||
document.documentElement.setAttribute('data-theme', 'dark') | ||
break; | ||
case 'light': | ||
document.documentElement.setAttribute('data-theme', 'light') | ||
break; | ||
default: | ||
console.warn('No theme set, creating & storing theme based on system preference: ', systemTheme); | ||
localStorage.setItem('theme', systemTheme); | ||
updateTheme(localStorage.getItem('theme')) | ||
break; | ||
} | ||
|
||
|
||
function updateTheme(value) { | ||
const theme = value === "system" ? systemTheme : value; | ||
document.documentElement.setAttribute('data-theme', theme) | ||
} | ||
|
||
button.addEventListener('click', () => { | ||
switch(localStorage.getItem('theme')) { | ||
case 'dark': | ||
console.log('dark') | ||
localStorage.setItem('theme', 'light'); | ||
state.innerHTML = 'light'; | ||
moon.classList.add('hide'); | ||
sun.classList.remove('hide'); | ||
document.documentElement.setAttribute('data-theme', 'light') | ||
break; | ||
case 'light': | ||
console.log('light') | ||
localStorage.setItem('theme', 'dark'); | ||
state.innerHTML = 'dark'; | ||
sun.classList.add('hide'); | ||
moon.classList.remove('hide'); | ||
document.documentElement.setAttribute('data-theme', 'dark') | ||
break; | ||
} | ||
}) | ||
let wrapper = document.querySelectorAll('.hideMe') | ||
|
||
|
||
close.addEventListener('click', () => { | ||
for(const item of wrapper) { | ||
item.classList.add('hide'); | ||
} | ||
}) | ||
</script> | ||
|
||
<div class="hideMe"> | ||
<button id="themeSwitcher"> | ||
<svg id="iconSun" data-slot="icon" aria-hidden="true" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> | ||
<path d="M12 2.25a.75.75 0 0 1 .75.75v2.25a.75.75 0 0 1-1.5 0V3a.75.75 0 0 1 .75-.75ZM7.5 12a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM18.894 6.166a.75.75 0 0 0-1.06-1.06l-1.591 1.59a.75.75 0 1 0 1.06 1.061l1.591-1.59ZM21.75 12a.75.75 0 0 1-.75.75h-2.25a.75.75 0 0 1 0-1.5H21a.75.75 0 0 1 .75.75ZM17.834 18.894a.75.75 0 0 0 1.06-1.06l-1.59-1.591a.75.75 0 1 0-1.061 1.06l1.59 1.591ZM12 18a.75.75 0 0 1 .75.75V21a.75.75 0 0 1-1.5 0v-2.25A.75.75 0 0 1 12 18ZM7.758 17.303a.75.75 0 0 0-1.061-1.06l-1.591 1.59a.75.75 0 0 0 1.06 1.061l1.591-1.59ZM6 12a.75.75 0 0 1-.75.75H3a.75.75 0 0 1 0-1.5h2.25A.75.75 0 0 1 6 12ZM6.697 7.757a.75.75 0 0 0 1.06-1.06l-1.59-1.591a.75.75 0 0 0-1.061 1.06l1.59 1.591Z"></path> | ||
</svg> | ||
<svg id="iconMoon" class="hide" data-slot="icon" aria-hidden="true" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> | ||
<path clip-rule="evenodd" d="M9.528 1.718a.75.75 0 0 1 .162.819A8.97 8.97 0 0 0 9 6a9 9 0 0 0 9 9 8.97 8.97 0 0 0 3.463-.69.75.75 0 0 1 .981.98 10.503 10.503 0 0 1-9.694 6.46c-5.799 0-10.5-4.7-10.5-10.5 0-4.368 2.667-8.112 6.46-9.694a.75.75 0 0 1 .818.162Z" fill-rule="evenodd"></path> | ||
</svg> | ||
</button> | ||
</div> | ||
<div class="hideMe"> | ||
<span id="debugState"> | ||
light | ||
</span> | ||
<span id="closeB"> | ||
<br>debug | ||
<br>[x]close | ||
</span> | ||
</div> | ||
|
||
<style> | ||
button { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
width: 3rem; | ||
aspect-ratio: 1; | ||
height: 3rem; | ||
|
||
@apply rounded-full; | ||
} | ||
|
||
.hide { | ||
display: none; | ||
} | ||
|
||
svg { | ||
height: 2rem; | ||
color: #1E1F22; | ||
} | ||
span { | ||
font-size: 0.62rem !important; | ||
font-family: monospace; | ||
} | ||
|
||
</style> |
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