JSFiddle - React, Tailwind, and code Playground
by Nikkoin
HTML
<div id="theme-switch">
<label class="switch">
<input type="checkbox" id="dark-mode-toggle" onclick="toggleDarkMode()">
<span class="slider"></span>
</label>
</div>
CSS
/* Estilos para o switch de tema */
#theme-switch {
margin: 10px;
}
#theme-switch label {
margin-left: 5px;
cursor: pointer;
}
/* Estilização do switch */
.switch {
position: relative;
display: inline-block;
width: 50px;
height: 24px;
}
/* Esconder o input padrão do switch */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* Estilização da alça do switch */
.slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius: 24px;
background-color: #2196F3;
transition: 0.4s;
}
/* Estilização da alça do switch no modo escuro */
.dark-mode .slider {
background-color: #dede4d;
}
/* Estilização do indicador do switch */
.slider:before {
position: absolute;
content: "";
height: 20px;
width: 20px;
left: 2px;
bottom: 2px;
border-radius: 50%;
background-color: #fff;
transition: 0.4s;
}
/* Estilização do indicador do switch no modo escuro */
.dark-mode .slider:before {
background-color: #333;
}
/* Estilização do switch quando ativado */
input:checked+.slider {
background-color: #2196F3;
}
/* Estilização do indicador do switch quando ativado */
input:checked+.slider:before {
transform: translateX(26px);
}
JavaScript
// Função para alternar entre o modo claro e o modo escuro
function toggleDarkMode() {
const body = document.body;
body.classList.toggle("dark-mode");
}