Skip to content

Commit f99ee8c

Browse files
committed
added ThemeSwitch component and used it in Header component
1 parent 858ebb6 commit f99ee8c

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/app/components/ThemeSwitch.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use client'
2+
3+
import { Moon, Sun } from 'lucide-react'
4+
import { useState, useEffect } from 'react'
5+
import { useTheme } from 'next-themes'
6+
import Image from "next/image"
7+
8+
export default function ThemeSwitch() {
9+
const [mounted, setMounted] = useState(false)
10+
const { setTheme, resolvedTheme } = useTheme()
11+
12+
useEffect(() => setMounted(true), [])
13+
14+
if (!mounted) return (
15+
<img
16+
src="data:image/svg+xml;base64,PHN2ZyBzdHJva2U9IiNGRkZGRkYiIGZpbGw9IiNGRkZGRkYiIHN0cm9rZS13aWR0aD0iMCIgdmlld0JveD0iMCAwIDI0IDI0IiBoZWlnaHQ9IjIwMHB4IiB3aWR0aD0iMjAwcHgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHJlY3Qgd2lkdGg9IjIwIiBoZWlnaHQ9IjIwIiB4PSIyIiB5PSIyIiBmaWxsPSJub25lIiBzdHJva2Utd2lkdGg9IjIiIHJ4PSIyIj48L3JlY3Q+PC9zdmc+Cg=="
17+
width={36}
18+
height={36}
19+
sizes="36x36"
20+
alt="Loading Light/Dark Toggle"
21+
title="Loading Light/Dark Toggle"
22+
/>
23+
// priority={false}
24+
)
25+
26+
if (resolvedTheme === 'dark') {
27+
return <Sun onClick={() => setTheme('light')} />
28+
}
29+
if (resolvedTheme === 'light') {
30+
return <Moon onClick={() => setTheme('dark')} />
31+
}
32+
}

src/app/components/header.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Moon, Sun, Github, Linkedin, Mail, StickyNote } from 'lucide-react'
1+
import { Github, Linkedin, Mail, StickyNote } from 'lucide-react'
2+
import ThemeSwitch from './ThemeSwitch'
23
import Image from 'next/image'
34

45
export function Header() {
@@ -19,13 +20,7 @@ export function Header() {
1920
<div className="flex-grow text-center md:text-left">
2021
<div className="flex items-center justify-center md:justify-start">
2122
<h1 className="text-3xl md:text-4xl lg:text-5xl font-bold mr-4">Himank Dave</h1>
22-
{/* <button
23-
onClick={() => setDarkMode(!darkMode)}
24-
className="p-2 rounded-full bg-gray-300 dark:bg-gray-700"
25-
aria-label="Toggle dark mode"
26-
>
27-
{darkMode ? <Sun className="w-6 h-6" /> : <Moon className="w-6 h-6" />}
28-
</button> */}
23+
<ThemeSwitch />
2924
</div>
3025
<div className="flex justify-center md:justify-start mt-4 space-x-4">
3126
<a href={githubLink} target="_blank" rel="noopener noreferrer" aria-label="GitHub">

0 commit comments

Comments
 (0)