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

[blog] Fix dark mode support #35969

Merged
merged 2 commits into from
Feb 4, 2023

Conversation

oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Jan 28, 2023

Fix #35948 (comment)

This bug was discrediting us among new users. I mean, ~1/3 of the sessions are dark mode, so, imagine we have a blog post that is meant for SEO ranking, with tone or first time visitors. Or a blog post that gets viral 🙃.

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work regression A bug, but worse blog labels Jan 28, 2023
@mui-bot
Copy link

mui-bot commented Jan 28, 2023

Messages
📖 Netlify deploy preview: https://deploy-preview-35969--material-ui.netlify.app/

No bundle size changes

Generated by 🚫 dangerJS against 3ab92a9

@@ -6,7 +6,7 @@ import { useTranslate } from 'docs/src/modules/utils/i18n';
const StyledLink = styled(MuiLink)(({ theme }) => ({
position: 'fixed',
padding: theme.spacing(1),
background: theme.palette.background.paper,
background: (theme.vars || theme).palette.background.paper,
Copy link
Member Author

@oliviertassinari oliviertassinari Jan 28, 2023

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.

Screenshot 2023-01-28 at 01 59 16

http://0.0.0.0:3000/blog/v6-beta-pickers/

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
const preferredMode = pageContextValue.activePage && prefersDarkMode ? 'dark' : 'light';
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

@@ -162,34 +158,42 @@ export function ThemeProvider(props) {
throw new Error(`Unrecognized type ${action.type}`);
}
},
{ ...themeInitialOptions, paletteMode: preferredMode },
{ ...themeInitialOptions, paletteMode: 'light' },
Copy link
Member Author

Choose a reason for hiding this comment

The 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') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is useless, removed

@@ -238,23 +242,10 @@ export function ThemeProvider(props) {

React.useEffect(() => {
// Expose the theme as a global variable so people can play with it.
if (typeof window !== 'undefined') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is useless, removed

@@ -108,14 +108,14 @@ export default function createCssVarsProvider(options) {
}

const calculatedMode = (() => {
if (!mode) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Comment on lines -247 to -256
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]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved up and grouped with the update of meta[name="theme-color"].

Comment on lines -114 to -115
// `activePage` does not exist for playground pages
// forcing light mode in playground avoids the need for a wrapping theme in playground pages
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliviertassinari oliviertassinari merged commit 163b187 into mui:master Feb 4, 2023
@oliviertassinari oliviertassinari deleted the blog-fix-dark branch February 4, 2023 17:53
@cherniavskii
Copy link
Member

Nice job fixing this issue! 🎉
I noticed it today and it looks like it's the same one that was reported by @samuelsycamore and in https://twitter.com/alejandrojsx/status/1565374949572988929

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blog bug 🐛 Something doesn't work regression A bug, but worse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants