Skip to content

Commit

Permalink
add dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SultaniGithub committed May 17, 2024
1 parent 672e0ab commit 0d349f6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import Header from "@/components/header";
import Provider from "./provider";
const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
Expand All @@ -15,10 +16,13 @@ export default function RootLayout({
children: React.ReactNode;
}>) {
return (

<html lang="en">
<body className={inter.className}>
<Provider>
<Header/>
{children}
</Provider>
</body>
</html>
);
Expand Down
13 changes: 13 additions & 0 deletions app/provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ThemeProvider } from "next-themes";
import { ReactNode } from "react";

const Provider=({children}:{children:ReactNode})=>{
return (
<ThemeProvider attribute="class">
<div className="text-gray-700 dark:text-gray-200 dark:bg-gray-700 min-h-screen select-none transition-colors duration-300">
{children}
</div>
</ThemeProvider>
)
}
export default Provider
22 changes: 22 additions & 0 deletions components/darkModeSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client"
import { MdDarkMode } from "react-icons/md";
import { CiDark } from "react-icons/ci";
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";

const DarkModeSwitch=()=>{

const {theme,setTheme,systemTheme}=useTheme()
const currentTheme= theme=== systemTheme ? 'system': theme
const [mounted,setMounted]=useState(false)
useEffect(()=>{setMounted(true)})


return(
<div>
{mounted && (currentTheme==='dark'? <span className="text-xl cursor-pointer hover:text-amber-500"><div onClick={()=>setTheme('light')}><MdDarkMode /></div></span> : <span className="text-xl cursor-pointer hover:text-amber-500"><div onClick={()=>setTheme('dark')}><CiDark/></div></span>)}
</div>
)
}

export default DarkModeSwitch
6 changes: 5 additions & 1 deletion components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import Link from "next/link";
import Menu from "./menu"
import { AiFillHome } from "react-icons/ai";
import { BsFillInfoCircleFill } from "react-icons/bs";
import DarkModeSwitch from "./darkModeSwitch";
const Header=()=>{
return (
<div className="flex justify-between max-w-6xl m-auto p-4">
<div className="flex gap-4">
<Menu title="home" address="/" Icon={AiFillHome} />
<Menu title="about" address="/about" Icon={BsFillInfoCircleFill} />
</div>
<div className="flex items-center gap-2">
<DarkModeSwitch/>
<Link href="/" className="flex gap-1 items-center ">
<span className="bg-amber-500 rounded-lg font-bold px-2 py-1">IDMB</span>
<span className="text-xl bg-amber-500 rounded-lg font-bold px-2 py-1">IDMB</span>
<span className="hidden sm:inline">Clone</span>
</Link>
</div>
</div>
)
}
Expand Down
1 change: 1 addition & 0 deletions tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ const config: Config = {
},
},
plugins: [],
darkMode:"class"
};
export default config;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "components/header.tsx"],
"exclude": ["node_modules"]
"exclude": ["node_modules"],

}

0 comments on commit 0d349f6

Please sign in to comment.