-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[blog] Fix dark mode support #35969
[blog] Fix dark mode support #35969
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ import { | |
getThemedComponents, | ||
getMetaThemeColor, | ||
} from 'docs/src/modules/brandingTheme'; | ||
import PageContext from './PageContext'; | ||
|
||
const languageMap = { | ||
en: enUS, | ||
|
@@ -109,11 +108,8 @@ if (process.env.NODE_ENV !== 'production') { | |
|
||
export function ThemeProvider(props) { | ||
const { children } = props; | ||
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)'); | ||
const pageContextValue = React.useContext(PageContext); | ||
// `activePage` does not exist for playground pages | ||
// forcing light mode in playground avoids the need for a wrapping theme in playground pages | ||
Comment on lines
-114
to
-115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that a better solution is to move with this direction https://github.com/mui/material-ui/pull/35330/files#diff-41c21e2dc42b8c223fc2fa17580e58041afa590fe34d9fbe9a1ffbe5e17d318b |
||
const preferredMode = pageContextValue.activePage && prefersDarkMode ? 'dark' : 'light'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the fix, blog posts don't have active pages, like many other marketing pages. |
||
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)', { noSsr: true }); | ||
const preferredMode = prefersDarkMode ? 'dark' : 'light'; | ||
|
||
const [themeOptions, dispatch] = React.useReducer( | ||
(state, action) => { | ||
|
@@ -162,34 +158,41 @@ export function ThemeProvider(props) { | |
throw new Error(`Unrecognized type ${action.type}`); | ||
} | ||
}, | ||
{ ...themeInitialOptions, paletteMode: preferredMode }, | ||
{ ...themeInitialOptions, paletteMode: 'light' }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the default value more explicit. |
||
); | ||
|
||
const userLanguage = useUserLanguage(); | ||
const { dense, direction, paletteColors, paletteMode, spacing } = themeOptions; | ||
|
||
useLazyCSS('/static/styles/prism-okaidia.css', '#prismjs'); | ||
|
||
React.useEffect(() => { | ||
if (typeof window !== 'undefined') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is useless, removed |
||
const nextPaletteColors = JSON.parse(getCookie('paletteColors') || 'null'); | ||
let nextPaletteMode = localStorage.getItem('mui-mode') || preferredMode; // syncing with homepage, can be removed once all pages are migrated to CSS variables | ||
if (nextPaletteMode === 'system') { | ||
nextPaletteMode = preferredMode; | ||
} | ||
|
||
dispatch({ | ||
type: 'CHANGE', | ||
payload: { paletteColors: nextPaletteColors, paletteMode: nextPaletteMode }, | ||
}); | ||
useEnhancedEffect(() => { | ||
const nextPaletteColors = JSON.parse(getCookie('paletteColors') || 'null'); | ||
let nextPaletteMode = localStorage.getItem('mui-mode') || preferredMode; // syncing with homepage, can be removed once all pages are migrated to CSS variables | ||
if (nextPaletteMode === 'system') { | ||
nextPaletteMode = preferredMode; | ||
} | ||
|
||
dispatch({ | ||
type: 'CHANGE', | ||
payload: { paletteColors: nextPaletteColors, paletteMode: nextPaletteMode }, | ||
}); | ||
}, [preferredMode]); | ||
|
||
useEnhancedEffect(() => { | ||
document.body.dir = direction; | ||
}, [direction]); | ||
|
||
React.useEffect(() => { | ||
useEnhancedEffect(() => { | ||
// To support light and dark mode images in the docs | ||
if (paletteMode === 'dark') { | ||
document.body.classList.remove('mode-light'); | ||
document.body.classList.add('mode-dark'); | ||
} else { | ||
document.body.classList.remove('mode-dark'); | ||
document.body.classList.add('mode-light'); | ||
} | ||
|
||
const metas = document.querySelectorAll('meta[name="theme-color"]'); | ||
metas.forEach((meta) => { | ||
meta.setAttribute('content', getMetaThemeColor(paletteMode)); | ||
|
@@ -238,23 +241,10 @@ export function ThemeProvider(props) { | |
|
||
React.useEffect(() => { | ||
// Expose the theme as a global variable so people can play with it. | ||
if (typeof window !== 'undefined') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is useless, removed |
||
window.theme = theme; | ||
window.createTheme = createTheme; | ||
} | ||
window.theme = theme; | ||
window.createTheme = createTheme; | ||
}, [theme]); | ||
|
||
useEnhancedEffect(() => { | ||
// To support light and dark mode images in the docs | ||
if (theme.palette.mode === 'dark') { | ||
document.body.classList.remove('mode-light'); | ||
document.body.classList.add('mode-dark'); | ||
} else { | ||
document.body.classList.remove('mode-dark'); | ||
document.body.classList.add('mode-light'); | ||
} | ||
}, [theme.palette.mode]); | ||
Comment on lines
-247
to
-256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved up and grouped with the update of |
||
|
||
return ( | ||
<MuiThemeProvider theme={theme}> | ||
<DispatchContext.Provider value={dispatch}>{children}</DispatchContext.Provider> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,14 +108,14 @@ export default function createCssVarsProvider(options) { | |
} | ||
|
||
const calculatedMode = (() => { | ||
if (!mode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverse the logic to be easier to understand, it groups a related test with its outcome. |
||
// This scope occurs on the server | ||
if (defaultMode === 'system') { | ||
return designSystemMode; | ||
} | ||
return defaultMode; | ||
if (mode) { | ||
return mode; | ||
} | ||
// This scope occurs on the server | ||
if (defaultMode === 'system') { | ||
return designSystemMode; | ||
} | ||
return mode; | ||
return defaultMode; | ||
})(); | ||
const calculatedColorScheme = (() => { | ||
if (!colorScheme) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off-topic. This solves a hydration mismatch otherwise when the page is in dark mode.
http://0.0.0.0:3000/blog/v6-beta-pickers/