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: Adds ability to persist theming to local storage. #4367

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ const HeaderTools = ({

const toggleDarkTheme = (_evt, selected) => {
const darkThemeToggleClicked = !selected === isDarkTheme;
document.querySelector('html').classList.toggle('pf-v6-theme-dark', darkThemeToggleClicked);
setIsDarkTheme(darkThemeToggleClicked);
};

Expand Down Expand Up @@ -247,17 +246,32 @@ export const SideNavLayout = ({ children, groupedRoutes, navOpen: navOpenProp })
const [isRTL, setIsRTL] = useState(false);
const [isDarkTheme, setIsDarkTheme] = React.useState(false);

const handleThemeChange = (darkThemeEnabled) => {
document.querySelector('html').classList.toggle('pf-v6-theme-dark', darkThemeEnabled);
localStorage.setItem('dark-theme', darkThemeEnabled);
setIsDarkTheme(darkThemeEnabled);
}

useEffect(() => {
localStorage ? handleThemeChange(localStorage.getItem('dark-theme') === 'true') : handleThemeChange(false);

document.addEventListener('visibilitychange', () => handleThemeChange(localStorage.getItem('dark-theme') === 'true'));

if (typeof window === 'undefined') {
return;
}

if (hasVersionSwitcher && window.fetch) {
fetch('/versions.json').then((res) => {
if (res.ok) {
res.json().then((json) => setVersions(json));
}
});
}

return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
}
}, []);

const SideBar = (
Expand Down Expand Up @@ -339,7 +353,7 @@ export const SideNavLayout = ({ children, groupedRoutes, navOpen: navOpenProp })
isRTL={isRTL}
setIsRTL={setIsRTL}
isDarkTheme={isDarkTheme}
setIsDarkTheme={setIsDarkTheme}
setIsDarkTheme={handleThemeChange}
/>
)}
</MastheadContent>
Expand Down
2 changes: 1 addition & 1 deletion packages/documentation-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"screenshots": "pf-docs-framework screenshots"
},
"dependencies": {
"@patternfly/documentation-framework": "6.0.1",
"@patternfly/documentation-framework": "6.0.5",
"@patternfly/react-catalog-view-extension": "6.0.0",
"@patternfly/react-console": "6.0.0",
"@patternfly/react-docs": "7.0.0",
Expand Down
Loading