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

[docs] Dark mode conditional content rendering #28665

Merged
merged 2 commits into from
Oct 20, 2021
Merged
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
17 changes: 17 additions & 0 deletions docs/pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LANGUAGES_SSR } from 'docs/src/modules/constants';
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
import createEmotionCache from 'docs/src/createEmotionCache';
import { getMetaThemeColor } from 'docs/src/modules/brandingTheme';
import { GlobalStyles } from '@mui/styled-engine';
Copy link
Member

Choose a reason for hiding this comment

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

The other components in the docs are importing from:

Suggested change
import { GlobalStyles } from '@mui/styled-engine';
import GlobalStyles from '@mui/material/GlobalStyles';


// You can find a benchmark of the available CSS minifiers under
// https://github.com/GoalSmashers/css-minification-benchmark
Expand Down Expand Up @@ -113,6 +114,22 @@ export default class MyDocument extends Document {
__html: `@font-face{font-family:'IBM Plex Sans';src:url(/static/fonts/IBMPlexSans-Regular.woff2) format('woff2'),url(/static/fonts/IBMPlexSans-Regular.woff) format('woff'),url(/static/fonts/IBMPlexSans-Regular.ttf) format('truetype');font-weight:400;font-style:normal;font-display:swap}@font-face{font-family:'IBM Plex Sans';src:url(/static/fonts/IBMPlexSans-Medium.woff2) format('woff2'),url(/static/fonts/IBMPlexSans-Medium.woff) format('woff'),url(/static/fonts/IBMPlexSans-Medium.ttf) format('truetype');font-weight:500;font-style:normal;font-display:swap}@font-face{font-family:'IBM Plex Sans';src:url(/static/fonts/IBMPlexSans-Bold.woff2) format('woff2'),url(/static/fonts/IBMPlexSans-Bold.woff) format('woff'),url(/static/fonts/IBMPlexSans-Bold.ttf) format('truetype');font-weight:700;font-style:normal;font-display:swap}@font-face{font-family:'IBM Plex Mono';src:url(/static/fonts/IBMPlexMono-Regular.woff2) format('woff2'),url(/static/fonts/IBMPlexMono-Regular.woff) format('woff'),url(/static/fonts/IBMPlexMono-Regular.ttf) format('truetype');font-weight:400;font-style:normal;font-display:swap}@font-face{font-family:'IBM Plex Mono';src:url(/static/fonts/IBMPlexMono-SemiBold.woff2) format('woff2'),url(/static/fonts/IBMPlexMono-SemiBold.woff) format('woff'),url(/static/fonts/IBMPlexMono-SemiBold.ttf) format('truetype');font-weight:600;font-style:normal;font-display:swap}`,
}}
/>
<GlobalStyles
styles={`
.only-light-mode {
display: none;
}
.mode-light .only-light-mode {
display: block;
}
.only-dark-mode {
display: none;
}
.mode-dark .only-dark-mode {
display: block;
}
`}
Comment on lines +118 to +131
Copy link
Member

Choose a reason for hiding this comment

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

We have used the JavaScript syntax for them until now.

/>
</Head>
<body>
<Main />
Expand Down
1 change: 1 addition & 0 deletions docs/public/static/images/font-size-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/public/static/images/font-size.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 16 additions & 2 deletions docs/src/modules/components/AppSettingsDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,21 @@ function AppSettingsDrawer(props) {
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
const preferredMode = prefersDarkMode ? 'dark' : 'light';

const updateBodyClass = (currentMode) => {
if (currentMode === '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');
}
};
Comment on lines +45 to +53
Copy link
Member

@oliviertassinari oliviertassinari Oct 31, 2021

Choose a reason for hiding this comment

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

I don't think that this logic should be here. This component one of the UI controls that sets the mode. The core of the logic is in /docs/src/modules/components/ThemeContext.js.


React.useEffect(() => {
setMode(getCookie('paletteMode') || 'system');
}, [setMode]);
const initialMode = getCookie('paletteMode') || 'system';
setMode(initialMode);
updateBodyClass(initialMode === 'system' ? preferredMode : initialMode);
}, [setMode, preferredMode]);

const handleChangeThemeMode = (event, paletteMode) => {
if (paletteMode === null) {
Expand All @@ -56,9 +68,11 @@ function AppSettingsDrawer(props) {
if (paletteMode === 'system') {
document.cookie = `paletteMode=;path=/;max-age=31536000`;
changeTheme({ paletteMode: preferredMode });
updateBodyClass(preferredMode);
} else {
document.cookie = `paletteMode=${paletteMode};path=/;max-age=31536000`;
changeTheme({ paletteMode });
updateBodyClass(paletteMode);
}
};

Expand Down
7 changes: 6 additions & 1 deletion docs/src/pages/customization/typography/typography.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ const theme = createTheme({

The computed font size by the browser follows this mathematical equation:

<img src="/static/images/font-size.svg" alt="font size calculation" style="width: 458px;" />
<div class="only-light-mode">
<img src="/static/images/font-size.svg" alt="font size calculation" style="width: 458px;" />
</div>
<div class="only-dark-mode">
<img src="/static/images/font-size-dark.svg" alt="font size calculation" style="width: 458px;" />
</div>
Comment on lines +108 to +113
Copy link
Member

Choose a reason for hiding this comment

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

This creates a layout shift when loading. Try https://mui.com/customization/typography/#font-size


<!-- https://latex.codecogs.com/svg.latex?\dpi{200}&space;\text{computed}&space;=&space;\text{specification}\cdot\frac{\text{typography.fontSize}}{14}\cdot\frac{\text{html&space;fontsize}}{\text{typography.htmlFontSize}} -->

Expand Down