mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
fix(client): initialize theme during gatsby SSR (#61922)
This commit is contained in:
@@ -62,3 +62,36 @@ export function getPostBodyComponents(superblock: string): JSX.Element[] {
|
||||
|
||||
return scripts.filter(Boolean);
|
||||
}
|
||||
|
||||
export function getPreBodyThemeScript(): JSX.Element[] {
|
||||
const script = (
|
||||
<script
|
||||
key='prebody-theme-init'
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `
|
||||
(function(){
|
||||
let theme = 'light';
|
||||
const localTheme = localStorage.getItem('theme');
|
||||
|
||||
if (localTheme !== null) {
|
||||
theme = localTheme === 'dark' ? 'dark' : 'light';
|
||||
} else if (
|
||||
window.matchMedia &&
|
||||
window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
) {
|
||||
theme = 'dark';
|
||||
}
|
||||
|
||||
const bodyEl = document && document.body;
|
||||
|
||||
if (bodyEl && bodyEl.classList) {
|
||||
bodyEl.classList.remove('light-palette');
|
||||
bodyEl.classList.remove('dark-palette');
|
||||
bodyEl.classList.add(theme + '-palette');
|
||||
}
|
||||
})();`
|
||||
}}
|
||||
/>
|
||||
);
|
||||
return [script];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user