Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove duplicate theme logic #216

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,5 @@
<div id="root"></div>
<!-- Main JavaScript file -->
<script type="module" src="/src/main.tsx"></script>
<!-- JavaScript to toggle dark mode -->
<script>
// Check user's theme preference or default to system preference
const themePreference =
localStorage.getItem('themePreference') ||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
// Get the root HTML element
const rootElement = document.documentElement;
// Apply dark mode class if preferred theme is dark
if (themePreference === 'dark') {
rootElement.classList.add('dark-mode');
} else {
rootElement.classList.remove('dark-mode');
}
</script>
</body>
</html>
38 changes: 2 additions & 36 deletions src/components/Navigation/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Link,
useLocation,
} from 'react-router-dom';
import { DropDownSearch, useAuth } from '../../index';
import { DropDownSearch, useAuth, useTheme } from '../../index';
import { fetchAdvancedSearch, type Anime } from '../..';
import { FiSun, FiMoon, FiX /* FiMenu */ } from 'react-icons/fi';
import { GoCommandPalette } from 'react-icons/go';
Expand Down Expand Up @@ -200,30 +200,6 @@ const SlashToggleBtn = styled.div<{ $isFocused: boolean }>`
}
`;

const detectUserTheme = () => {
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
return true;
}
return false;
};

const saveThemePreference = (isDarkMode: boolean) => {
localStorage.setItem('themePreference', isDarkMode ? 'dark' : 'light');
};

const getInitialThemePreference = () => {
const storedThemePreference = localStorage.getItem('themePreference');

if (storedThemePreference) {
return storedThemePreference === 'dark';
}

return detectUserTheme();
};

export const Navbar = () => {
const { isLoggedIn, userData } = useAuth();
const [isPaddingExtended, setIsPaddingExtended] = useState(false);
Expand Down Expand Up @@ -288,17 +264,7 @@ export const Navbar = () => {
};
});

const [isDarkMode, setIsDarkMode] = useState(getInitialThemePreference());

useEffect(() => {
document.documentElement.classList.toggle('dark-mode', isDarkMode);
}, [isDarkMode]);

const toggleTheme = useCallback(() => {
const newIsDarkMode = !isDarkMode;
setIsDarkMode(newIsDarkMode);
saveThemePreference(newIsDarkMode);
}, [isDarkMode, setIsDarkMode]);
const { isDarkMode, toggleTheme } = useTheme();

const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
Expand Down