-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
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 |
---|---|---|
|
@@ -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'; | ||
|
||
// You can find a benchmark of the available CSS minifiers under | ||
// https://github.com/GoalSmashers/css-minification-benchmark | ||
|
@@ -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
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. We have used the JavaScript syntax for them until now. |
||
/> | ||
</Head> | ||
<body> | ||
<Main /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
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 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 |
||
|
||
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) { | ||
|
@@ -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); | ||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
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 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}} --> | ||
|
||
|
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.
The other components in the docs are importing from: