Skip to content

Commit

Permalink
Updated DarkModeToggle
Browse files Browse the repository at this point in the history
  • Loading branch information
adriandelgg committed Jun 30, 2022
1 parent bf819f0 commit 2d13564
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions frontend/src/helpers/DarkModeToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import { useState } from "react";
import { DarkModeSwitch } from "react-toggle-dark-mode";
import { useLayoutEffect, useState } from 'react';
import { DarkModeSwitch } from 'react-toggle-dark-mode';

export const DarkModeToggle = () => {
const [isDarkMode, setDarkMode] = useState(false);
const [isDarkMode, setDarkMode] = useState(true);

// Checks local storage for dark mode
useLayoutEffect(() => {
const darkMode = globalThis.localStorage.getItem('darkMode');

const isDark = darkMode && JSON.parse(darkMode);

if (!isDark) {
globalThis.document.documentElement.classList.add('dark');
} else {
setDarkMode(isDark);
isDark
? globalThis.document.documentElement.classList.add('dark')
: globalThis.document.documentElement.classList.remove('dark');
}
}, []);

const toggleDarkMode = (checked: boolean) => {
globalThis.localStorage.setItem('darkMode', JSON.stringify(checked));
setDarkMode(checked);
isDarkMode
? document.documentElement.classList.remove("dark")
: document.documentElement.classList.add("dark");
checked
? globalThis.document.documentElement.classList.add('dark')
: globalThis.document.documentElement.classList.remove('dark');
};

return (
Expand Down

0 comments on commit 2d13564

Please sign in to comment.