-
Notifications
You must be signed in to change notification settings - Fork 0
/
dark-light-mode-figma.html
100 lines (89 loc) · 2.39 KB
/
dark-light-mode-figma.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
<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 switch</title>
<meta name="color-scheme" content="dark light">
<meta name="theme-color" content="">
<link rel="icon" content="">
<style rel="stylesheet">
:root {
--duration: 0.5s;
--timing: ease;
}
[data-preferred-theme=dark] {
--font-color: #fff;
--body-bg: #000;
}
[data-preferred-theme=light] {
--font-color: #666;
--body-bg: #fff;
}
body {
padding: 0;
margin: 0;
transition:
color var(--duration) var(--timing),
background-color var(--duration) var(--timing);
background-color: var(--body-bg);
font-family: PingFangSC, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji",
"Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}
.box {
display: flex;
flex-flow: column nowrap;
align-items: center;
justify-content: center;
min-height: 100vh;
max-height: 100vh;
font-size: 26px;
color: var(--font-color);
}
.switch {
position: fixed;
top: 10px;
right: 16px;
padding: 0;
margin: 0;
list-style: none;
display: flex;
flex-flow: row nowrap;
justify-content: flex-end;
}
.switch li {
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
margin: 0 5px 0 0;
}
</style>
</head>
<body data-preferred-theme="light">
<ul class="switch">
<li data-theme="light" data-id="switch-light">🌞</li>
<li data-theme="dark" data-id="switch-dark">🌙</li>
</ul>
<div class="box">Mode Switch</div>
</body>
<script type="text/javascript">
const changeTheme = (theme) => {
document.body.setAttribute('data-preferred-theme', theme);
}
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
changeTheme('dark');
} else {
changeTheme('light');
}
document.querySelectorAll('.switch li').forEach(element => {
element.addEventListener('click', function (e) {
const colorScheme = element.dataset.theme;
changeTheme(colorScheme);
})
})
</script>
</html>