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-infra] Fix dark/light mode #39076

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 29 additions & 7 deletions docs/src/modules/components/MarkdownDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import PropTypes from 'prop-types';
import { useRouter } from 'next/router';
import { useTheme } from '@mui/system';
import { exactProp } from '@mui/utils';
import { CssVarsProvider as JoyCssVarsProvider, useColorScheme } from '@mui/joy/styles';
import {
CssVarsProvider as JoyCssVarsProvider,
useColorScheme as useJoyColorScheme,
} from '@mui/joy/styles';
import {
Experimental_CssVarsProvider as MuiCssVarsProvider,
useColorScheme as useMuiColorScheme,
} from '@mui/material/styles';
import RichMarkdownElement from 'docs/src/modules/components/RichMarkdownElement';
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
import AppLayoutDocs from 'docs/src/modules/components/AppLayoutDocs';
Expand All @@ -12,16 +19,17 @@ import BrandingProvider from 'docs/src/BrandingProvider';
import Ad from 'docs/src/modules/components/Ad';
import AdGuest from 'docs/src/modules/components/AdGuest';

function JoyModeObserver({ mode }) {
function ModeObserver({ mode, useColorScheme }) {
const { setMode } = useColorScheme();
React.useEffect(() => {
setMode(mode);
}, [mode, setMode]);
return null;
}

JoyModeObserver.propTypes = {
ModeObserver.propTypes = {
mode: PropTypes.oneOf(['light', 'dark']),
useColorScheme: PropTypes.func,
};

export default function MarkdownDocs(props) {
Expand All @@ -46,10 +54,22 @@ export default function MarkdownDocs(props) {
const localizedDoc = docs[userLanguage] || docs.en;

const isJoy = canonicalAs.startsWith('/joy-ui/') && !disableCssVarsProvider;
const CssVarsProvider = isJoy ? JoyCssVarsProvider : React.Fragment;
const Wrapper = isJoy ? BrandingProvider : React.Fragment;
const isMui = canonicalAs.startsWith('/material-ui/') && !disableCssVarsProvider;
let CssVarsProvider = React.Fragment;
let useColorScheme;
if (isJoy) {
CssVarsProvider = JoyCssVarsProvider;
useColorScheme = useJoyColorScheme;
}

if (isMui) {
CssVarsProvider = MuiCssVarsProvider;
useColorScheme = useMuiColorScheme;
}

const Wrapper = isJoy || isMui ? BrandingProvider : React.Fragment;
const wrapperProps = {
...(isJoy && { mode: theme.palette.mode }),
...((isJoy || isMui) && { mode: theme.palette.mode }),
};

return (
Expand All @@ -69,7 +89,9 @@ export default function MarkdownDocs(props) {
</BrandingProvider>
)}
<CssVarsProvider>
{isJoy && <JoyModeObserver mode={theme.palette.mode} />}
{(isJoy || isMui) && (
<ModeObserver mode={theme.palette.mode} useColorScheme={useColorScheme} />
)}
{localizedDoc.rendered.map((renderedMarkdownOrDemo, index) => (
<RichMarkdownElement
key={`demos-section-${index}`}
Expand Down
Loading