Im quite new to HTML/CSS/JS... I have a light/dark mode toggle switch that activates dark mode on my asp.net core site. Everything works fine and i am using localhost to save the dark/light theme on page refresh or load. The problem is my toggle switch keeps reverting back to light mode. Ive tried a few options and i cant seem to figure out what needs to be done to keep the toggle switch in the correct position on page load.
Here is my code
HTML checkbox:
<div class="toggle-container">
<input type="checkbox" id="dark-mode-switch" name="theme" /><label for="dark-mode-switch">Toggle</label>
</div>
CSS:
.switch {
align-self: self-end;
margin: inherit;
padding: inherit;
border-radius: 15px;
display: grid;
grid-template-columns: 90px auto;
grid-template-rows: auto auto;
grid-template-areas: "title switch" "content content";
}
.switch h1 {
margin: auto;
color: var(--color-headings);
font-size: 16px;
line-height: inherit;
}
input[type=checkbox] {
height: 0;
width: 0;
visibility: hidden;
}
label {
cursor: pointer;
text-indent: -9999px;
width: 52px;
height: 27px;
background: grey;
float: right;
border-radius: 100px;
position: relative;
}
label:after {
content: '';
position: absolute;
top: 3px;
left: 3px;
width: 20px;
height: 20px;
background: #fff;
border-radius: 90px;
transition: 0.4s;
}
input:checked + label{
background: var(--color-headings);
}
input:checked + label:after {
left: calc(100% - 5px);
transform: translateX(-100%);
}
label:active:after {
width: 45px;
}
html.transition,
html.transition *,
html.transition *:before,
html.transition *:after {
transition: all 750ms !important;
transition-delay: 0 !important;
JS
let darkMode = localStorage.getItem('data-theme');
const checkbox = document.querySelector('input[name=theme]');
if (darkMode === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark')
localStorage.setItem('data-theme', 'dark')
}
checkbox.addEventListener('change', function () {
if (this.checked) {
trans()
document.documentElement.setAttribute('data-theme', 'dark')
localStorage.setItem('data-theme', 'dark')
} else {
trans()
document.documentElement.setAttribute('data-theme', 'light')
localStorage.setItem('data-theme', 'light')
}
})
let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() => {
document.documentElement.classList.remove('transition')
}, 1000)
}