-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathindex.jsx
More file actions
64 lines (54 loc) · 1.42 KB
/
index.jsx
File metadata and controls
64 lines (54 loc) · 1.42 KB
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
import React from "react";
import styles from "./DarkMode.module.css";
import classNames from "classnames";
let prefersDark = false
const DarkMode = () => {
const [theme, setTheme] = React.useState('light')
const setDark = () => {
document.documentElement.setAttribute("data-theme", "dark")
}
const setLight = () => {
document.documentElement.setAttribute("data-theme", "light")
}
if(typeof window !== 'undefined') {
prefersDark =
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
}
const defaultDark =
theme === "dark" || (theme === null && prefersDark)
if (defaultDark) {
setDark();
}
const toggleTheme = (event) => {
if (event.currentTarget.dataset.theme === 'light') {
setDark();
} else {
setLight();
}
}
return (
<div
className={classNames({
[styles.darkMode]: true,
"p-1 rounded-lg": true,
})}
data-theme={theme}
onClick={(event) => {
setTheme(currentTheme => ((currentTheme === 'light') ? 'dark' : 'light'))
toggleTheme(event)
}}
>
<img
src={`${(theme === 'light') ? '/dark-mode.webp' : '/moon.webp'}`}
alt={"switch"}
className={classNames({
"w-7 h-7": true,
"mobile:w-6 mobile:h-6": true,
})}
style={{ fontSize: '9px' }}
/>
</div>
)
};
export default DarkMode;