theme-toggle (#52)

* default to system preferred theme

* theme svg in footer

* accounted for undefined window
This commit is contained in:
Owen Rees
2023-07-04 22:40:39 +02:00
committed by GitHub
parent 8a4f5f08d2
commit 96fda01929
4 changed files with 39 additions and 27 deletions
+8 -1
View File
@@ -1,9 +1,16 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
function useDarkMode(): [string, Dispatch<SetStateAction<string>>] {
const prefersDarkMode =
typeof window !== "undefined" &&
window.matchMedia("(prefers-color-scheme: dark)").matches;
const [theme, setTheme] = useState(
typeof window !== "undefined" ? localStorage.theme : "dark"
typeof window !== "undefined"
? localStorage.theme || (prefersDarkMode ? "dark" : "light")
: "dark"
);
const colorTheme = theme === "dark" ? "light" : "dark";
useEffect(() => {