From 1b9d1cc8e67c0d258c048e7499d28d89eb5c71b9 Mon Sep 17 00:00:00 2001 From: Arne Pedersen Date: Sun, 25 Dec 2022 14:12:58 +0100 Subject: [PATCH] Add next-themes logic + icons. --- src/components/header/index.tsx | 51 ++++++++++++++++++++++++++++++++- src/pages/_app.tsx | 7 ++++- tailwind.config.js | 17 +++++------ 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/components/header/index.tsx b/src/components/header/index.tsx index 38f5b87..5655b69 100644 --- a/src/components/header/index.tsx +++ b/src/components/header/index.tsx @@ -1,9 +1,57 @@ -import React from "react" +import React, { useEffect, useState } from "react" import { DiGithubBadge } from "react-icons/di" import { MdSettings } from "react-icons/md" import Link from "next/link" +import { useTheme } from "next-themes" export function Header() { + const { theme, systemTheme, setTheme } = useTheme() + const [mounted, setMounted] = useState(false) + + useEffect(() => { + setMounted(true) + }, []) + + const renderThemeToggle = () => { + if (!mounted) return null + + const currentTheme = theme === "system" ? systemTheme : theme + + if (currentTheme === "dark") { + return ( + + ) + } else { + return ( + + ) + } + } + return (
@@ -24,6 +72,7 @@ export function Header() {
+ {renderThemeToggle()}
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 674ffd6..1293275 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -1,6 +1,7 @@ import "../styles/global.css" import { AppProps } from "next/app" import { useEffect, useState } from "react" +import { ThemeProvider } from "next-themes" export default function App({ Component, pageProps }: AppProps) { const [keys, setKeys] = useState<{ @@ -17,5 +18,9 @@ export default function App({ Component, pageProps }: AppProps) { })() }, []) - return + return ( + + + + ) } diff --git a/tailwind.config.js b/tailwind.config.js index f4132ae..d31caa7 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -1,18 +1,19 @@ /** @type {import('tailwindcss').Config} */ module.exports = { content: [ - "./src/pages/**/*.{js,ts,jsx,tsx}", - "./src/components/**/*.{js,ts,jsx,tsx}", - "./src/views/**/*.{js,ts,jsx,tsx}", + "./src/pages/**/*.{js,ts,jsx,tsx}", + "./src/components/**/*.{js,ts,jsx,tsx}", + "./src/views/**/*.{js,ts,jsx,tsx}", ], + darkMode: "class", theme: { extend: { colors: { - 'custom-green-light': '#6b9370', - 'custom-green-dark': '#4D6A51', - 'custom-black': '#3C3744' - } - } + "custom-green-light": "#6b9370", + "custom-green-dark": "#4D6A51", + "custom-black": "#3C3744", + }, + }, }, plugins: [], }