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 for theme switching when jumping between docs pages and marketing… #408

Merged
merged 2 commits into from
Jan 4, 2024
Merged
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
3 changes: 1 addition & 2 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ const config = {
// Replace with your project's social card
image: "img/uid2-social-card.jpg",
colorMode: {
defaultMode: "dark",
disableSwitch: true,
defaultMode: "light"
},

navbar: {
Expand Down
11 changes: 9 additions & 2 deletions src/components/HomepageHero/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ import { useIsJapanese } from "@site/src/utils/isJapanese";
import { useColorMode } from "@docusaurus/theme-common";
export default function HomepageHero(): JSX.Element {
const isJapanese = useIsJapanese();
const { setColorMode } = useColorMode();
const { setColorMode, colorMode } = useColorMode();

//quick fix for updating color mode on page load breaking after theme upgrade
React.useEffect(() => {
setColorMode("dark");
const originalTheme = colorMode;

// The second parameter exists, it's just not on the type :(
setColorMode("dark", { persist: false });

return () => {
setColorMode(originalTheme);
};
}, []);

return (
Expand Down
40 changes: 40 additions & 0 deletions src/theme/Layout/Provider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useEffect } from "react";
import { composeProviders, useColorMode } from "@docusaurus/theme-common";
import { useLocation } from "@docusaurus/router";
import {
ColorModeProvider,
AnnouncementBarProvider,
DocsPreferredVersionContextProvider,
ScrollControllerProvider,
NavbarProvider,
PluginHtmlClassNameProvider,
} from "@docusaurus/theme-common/internal";
const Provider = composeProviders([
ColorModeProvider,
AnnouncementBarProvider,
ScrollControllerProvider,
DocsPreferredVersionContextProvider,
PluginHtmlClassNameProvider,
NavbarProvider,
]);

function RestoreTheme() {
const { setColorMode } = useColorMode();
useEffect(() => {
const storedTheme = localStorage.getItem("theme");
setColorMode(storedTheme);
}, []);
return <></>;
}

export default function LayoutProvider({ children }) {
const location = useLocation();
let isDocsPage = location.pathname.includes("/docs/");

return (
<Provider>
{isDocsPage ? <RestoreTheme></RestoreTheme> : null}
{children}
</Provider>
);
}
15 changes: 2 additions & 13 deletions src/theme/Navbar/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { type ComponentProps } from "react";
import clsx from "clsx";
import { useThemeConfig, useColorMode } from "@docusaurus/theme-common";
import { useThemeConfig } from "@docusaurus/theme-common";
import {
useHideableNavbar,
useNavbarMobileSidebar,
Expand Down Expand Up @@ -28,17 +28,6 @@ export default function NavbarLayout({ children }: Props): JSX.Element {
} = useThemeConfig();
const mobileSidebar = useNavbarMobileSidebar();
const { navbarRef, isNavbarVisible } = useHideableNavbar(hideOnScroll);
const { colorMode, setColorMode } = useColorMode();

React.useEffect(() => {
const isMarketingPage =
document.documentElement.classList.contains("plugin-pages");
if (isMarketingPage) {
setColorMode("dark");
} else {
setColorMode("light");
}
}, []);

return (
<nav
Expand All @@ -59,7 +48,7 @@ export default function NavbarLayout({ children }: Props): JSX.Element {
"navbar--dark": style === "dark",
"navbar--primary": style === "primary",
"navbar-sidebar--show": mobileSidebar.shown,
}
},
)}
>
{children}
Expand Down