Skip to content

Commit

Permalink
fix: update theme correctly and show dropdowns correctly (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
VmMad authored Feb 1, 2024
1 parent 9e87443 commit 3ead1e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions client/src/app/components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,30 @@ export default function Header({ rootPath, currentNetwork, networks, history, ac
const isMarketed = isMarketedNetwork(currentNetwork?.network);

useEffect(() => {
toggleModeClass();
}, []);
saveThemeAndDispatchEvent(darkMode);
toggleBodyThemeClass();
}, [darkMode]);

function saveThemeAndDispatchEvent(newDarkMode: boolean): void {
settingsService.saveSingle("darkMode", newDarkMode);
const event = new CustomEvent("theme-change", { detail: { darkMode: newDarkMode } });
window.dispatchEvent(event);
}

/**
* Toggle the display mode.
*/
function toggleMode(): void {
setDarkMode((darkMode) => !darkMode);
settingsService.saveSingle("darkMode", darkMode);
const event = new CustomEvent("theme-change", { detail: { darkMode: darkMode } });
window.dispatchEvent(event);
toggleModeClass();
function handleThemeChange(): void {
setDarkMode((darkMode) => {
const newDarkMode = !darkMode;
saveThemeAndDispatchEvent(darkMode);
return newDarkMode;
});

toggleBodyThemeClass();
}

function toggleModeClass(): void {
function toggleBodyThemeClass(): void {
const body = document.querySelector("body");
if (body) {
body.classList.toggle("darkmode", darkMode);
Expand Down Expand Up @@ -152,7 +161,7 @@ export default function Header({ rootPath, currentNetwork, networks, history, ac
</div>

{/* Theme Button */}
<button type="button" className="button--unstyled theme-toggle" onClick={() => toggleMode()}>
<button type="button" className="button--unstyled theme-toggle" onClick={() => handleThemeChange()}>
{darkMode ? <span className="material-icons">light_mode</span> : <span className="material-icons">dark_mode</span>}
</button>

Expand Down
2 changes: 1 addition & 1 deletion client/src/app/components/header/HeaderDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const MobileDropdown = ({

return (
<>
{disabled && (
{!disabled && (
<>
<li
className={classNames("menu--expanded__item", {
Expand Down

0 comments on commit 3ead1e2

Please sign in to comment.