Created
August 16, 2022 00:14
-
-
Save lawrencecchen/b9dd8f9558e9e0c8feac74d49f73dfdf to your computer and use it in GitHub Desktop.
Revisions
-
lawrencecchen created this gist
Aug 16, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ // npm i usehooks-ts import { useEffect } from "react"; import { useDarkMode } from "usehooks-ts"; export function useTheme() { const theme = useDarkMode(); useEffect(() => { if (theme.isDarkMode) { document.body.classList.add("dark"); } else { document.body.classList.remove("dark"); } }, [theme.isDarkMode]); return theme; } // <body> // <script> // if (!!window.localStorage.getItem("usehooks-ts-dark-mode")) { // document.body.classList.add("dark"); // } // </script> // </body>