Skip to content

Commit

Permalink
fix: start with saved theme or preferred theme
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Aug 26, 2024
1 parent 8dadde7 commit 80d9671
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/providers/src/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ function getPreferredTheme() {
return window.matchMedia(PREFERS_LIGHT_MQ).matches ? Theme.light : Theme.dark;
}

const THEME_KEY = 'myst:theme';

const CLIENT_THEME_SOURCE = `
const savedTheme = localStorage.getItem(${JSON.stringify(THEME_KEY)});
const theme = window.matchMedia(${JSON.stringify(PREFERS_LIGHT_MQ)}).matches ? 'light' : 'dark';
const classes = document.documentElement.classList;
const hasAnyTheme = classes.contains('light') || classes.contains('dark');
if (!hasAnyTheme) classes.add(theme);
if (!hasAnyTheme) classes.add(savedTheme ?? theme);
`;

/**
Expand All @@ -79,7 +82,6 @@ export function BlockingThemeLoader() {
return <script dangerouslySetInnerHTML={{ __html: CLIENT_THEME_SOURCE }} />;
}

const THEME_KEY = 'myst:theme';

export function ThemeProvider({
children,
Expand Down

0 comments on commit 80d9671

Please sign in to comment.