From 4337801152ecf8aae60608acdded003f3f49ea0c Mon Sep 17 00:00:00 2001 From: Nick Bland Date: Sun, 24 Apr 2022 00:19:38 +1000 Subject: [PATCH] Export toggle switch to new component file Refactored for future code maintainability. Also added in default to dark theme and adjusted some styling on the toggle switch. --- components/navbar.tsx | 48 ++------------------------------------ components/themeSwitch.tsx | 33 ++++++++++++++++++++++++++ pages/_app.tsx | 2 +- 3 files changed, 36 insertions(+), 47 deletions(-) create mode 100644 components/themeSwitch.tsx diff --git a/components/navbar.tsx b/components/navbar.tsx index cea379e..b6fd5b2 100644 --- a/components/navbar.tsx +++ b/components/navbar.tsx @@ -1,10 +1,10 @@ import React, {Fragment, ReactNode, useState} from "react"; -import {useTheme} from "next-themes"; -import {Disclosure, Switch, Transition} from "@headlessui/react"; +import {Disclosure, Transition} from "@headlessui/react"; import {XIcon, MenuIcon} from "@heroicons/react/outline" import Head from "next/head"; import Link from "next/link"; import Image from "next/image"; +import { ThemeSwitch } from "./themeSwitch"; type Props = { @@ -13,50 +13,6 @@ type Props = { description?: string } -const ThemeSwitch = () => { - const {theme, setTheme} = useTheme(); - const [enabled, setEnabled] = useState(true) - - if(enabled) { - setTheme("dark") - } else { - setTheme("light") - } - - return ( -
- {/* Light -
- - -
- Dark */} - -
- Dark Mode - - - -
-
-
- ) -} - const NavBar = ({children, title = "nickbland.dev | Home", description = "A website made by Nick Bland."}: Props) => { const [isOpen, setIsOpen] = useState(false); return ( diff --git a/components/themeSwitch.tsx b/components/themeSwitch.tsx new file mode 100644 index 0000000..1ac7bb1 --- /dev/null +++ b/components/themeSwitch.tsx @@ -0,0 +1,33 @@ +import React, { useState } from "react"; +import { useTheme } from "next-themes"; +import { Switch } from "@headlessui/react"; + +export const ThemeSwitch = () => { + const { theme, setTheme } = useTheme(); + const [enabled, setEnabled] = useState(true); + + // Use theme requires its own hook which uses Strings. Using an enabled, disabled for a bool hook and following translates that to a string. + if (enabled) { + setTheme("dark"); + } else { + setTheme("light"); + } + + return ( +
+ +
+ Dark Mode + + + +
+
+
+ ); +}; diff --git a/pages/_app.tsx b/pages/_app.tsx index bc35a96..4c893a0 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -5,7 +5,7 @@ import {ThemeProvider} from 'next-themes' function MyApp({ Component, pageProps }: AppProps) { return ( <> - +