-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSwitch.js
More file actions
26 lines (21 loc) · 828 Bytes
/
Switch.js
File metadata and controls
26 lines (21 loc) · 828 Bytes
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
import { useState, useEffect } from 'react'
import { useTheme } from 'next-themes'
import Image from 'next/image'
export default function Switch() {
const [mounted, setMounted] = useState(false)
const { theme, setTheme } = useTheme()
// When mounted on client, now we can show the UI
useEffect(() => setMounted(true), [])
if (!mounted) return null
const switchOnClick = () => {
setTheme(theme === 'dark' ? 'light' : 'dark')
}
return (
<div className="theme-switch">
<div className={`switch-container ${theme}`} onClick={switchOnClick}>
<Image src="/images/sun.svg" className="switch-icon sun" alt="light mode" height={15} width={15} />
<Image src="/images/sponsored/moon.svg" className="switch-icon moon" alt="dark mode" height={15} width={15} />
</div>
</div>
)
}