-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdark-light-mode-toggle.html
156 lines (137 loc) · 3.82 KB
/
dark-light-mode-toggle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dark | Light Mode with Toggle</title>
<style>
*,
*::after,
*::before {
box-sizing: border-box;
}
html {
height: 100%;
font-family: 'Montserrat';
display: grid;
align-items: center;
justify-items: center;
--bg: #fcfcfc;
--bg-panel: #ebebeb;
--color-headings: #0077ff;
--color-text: #333333;
}
html[data-theme='dark'] {
--bg: #333333;
--bg-panel: #434343;
--color-headings: #3694ff;
--color-text: #b5b5b5;
}
body {
background-color: var(--bg);
}
.container {
background-color: var(--bg-panel);
margin: 5em;
padding: 5em;
border-radius: 15px;
display: grid;
grid-template-columns: 80% auto;
grid-template-rows: auto auto;
grid-template-areas:
'title switch'
'content content';
}
h1 {
margin: 0;
color: var(--color-headings);
}
p {
color: var(--color-text);
grid-area: content;
font-size: 1.1em;
line-height: 1.8em;
margin-top: 2em;
}
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.3s;
}
input:checked + label {
background: var(--color-headings);
}
input:checked + label:after {
left: calc(100% - 5px);
transform: translateX(-100%);
}
label:active:after {
width: 45px;
}
/* html AND .transition selector */
html.theme-transition,
html.theme-transition *,
html.theme-transition *:before,
html.theme-transition *:after {
transition: all 750ms !important;
transition-delay: 0 !important;
}
</style>
</head>
<body>
<div class="container">
<h1>Light / Dark Mode</h1>
<div class="toggle-container">
<input type="checkbox" id="switch" name="theme" /><label for="switch">Toggle</label>
</div>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos ducimus repellendus dolorem eum consequatur id
exercitationem nesciunt, inventore modi perferendis impedit esse, tempora officia, ipsam quae libero. Nostrum,
alias dignissimos.
</p>
</div>
<script>
var checkbox = document.querySelector('input[name=theme]')
checkbox.addEventListener('change', function () {
if (this.checked) {
trans()
document.documentElement.setAttribute('data-theme', 'dark')
} else {
trans()
document.documentElement.setAttribute('data-theme', 'light')
}
})
let trans = () => {
// documentElement is the HTML tag
//set the theme-transition class immediately before mode switch
document.documentElement.classList.add('theme-transition')
window.setTimeout(() => {
// then remove class as soon as the css transition is over (so it does not interfere with other css transitions)
document.documentElement.classList.remove('theme-transition')
}, 1000)
}
</script>
</body>
</html>