Skip to content

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Mar 24, 2025

The mode toggle explanation: https://deploy-preview-45661--material-ui.netlify.app/experiments/docs/demos/#isolated-demo

Changes

  • Sync the mode from page to demos at docs/src/theming.tsx(test added), see this commit.
  • Fix iframe to work with CSS variables
  • Simplify the codebase by moving Joy related logic from MarkdownDocs (v1 and v2) to docs/src/theming.tsx.
    • Removed disableCssVarsProvider
    • Removed usesCssVarsTheme
  • No longer need to separate DemoContainer between Joy and Material.

New:

  • Added isolated flag for mini-app demos. For example:
  • Update experiment demos page to show how to build demos with theme customization and added e2e-website tests to ensure that those demos are working correctly.

Test on X

To test this PR on MUI X repo:

  • Update monorepo in package.json to this commit f26f8aa4381493c58d296e5ce997343b064985a7
  • Update docs/package.json to
"@mui/material": "https://pkg.csb.dev/mui/material-ui/commit/35efdb00/@mui/material",
"@mui/system": "https://pkg.csb.dev/mui/material-ui/commit/35efdb00/@mui/system",

Please delete the node_modules and run pnpm install before pnpm docs:dev.


@siriwatknp siriwatknp added the docs Improvements or additions to the documentation. label Mar 24, 2025
@mui-bot
Copy link

mui-bot commented Mar 24, 2025

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Mar 24, 2025
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Mar 25, 2025
<Typography
endDecorator={<Link href="/sign-up">Sign up</Link>}
sx={{ fontSize: 'sm', alignSelf: 'center' }}
<CssVarsProvider {...props}>
Copy link
Member Author

@siriwatknp siriwatknp Mar 25, 2025

Choose a reason for hiding this comment

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

This demo is an iframe with mode toggle, so need to add dedicated CssVarsProvider (ThemeProvider) to follow the new demo guide.

@siriwatknp siriwatknp marked this pull request as ready for review March 25, 2025 07:09
@siriwatknp siriwatknp changed the title [docs][WIP] Sync the mode from page to demos [docs] Sync the mode from page to demos Mar 25, 2025
Comment on lines 147 to 160
function IsolatedDemo({ root, children }) {
return (
<SystemThemeProvider
theme={(upperTheme) => ({
direction: upperTheme.direction,
})}
>
{React.cloneElement(children, {
disableNestedContext: true,
storageManager: null,
colorSchemeNode: root,
})}
</SystemThemeProvider>
);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure tu undersand why wee need both:

  • the SystemThemeProvider that overrides the theme with a default one.
  • Passing props toe the children theme.

This props propagation looks weird to me.

From an outsider point of view, it feels more natural to write a demo with a wrapper like that

<ThemeProvider 
        theme={(upperTheme) => ({ direction: upperTheme.direction })}
	disableNestedContext
        storageManager={null}
        colorSchemeNode={ } // Here is the only bloking point because we woudl need the demoId
>

Compared to having to understand that

{{"demo": "Xxx.js", "isolated": true}} pass some props that should be passed to the theme provider of the demo.

But not sure it's doable

Copy link
Member Author

@siriwatknp siriwatknp Mar 26, 2025

Choose a reason for hiding this comment

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

From an outsider point of view, it feels more natural to write a demo with a wrapper like that

Yes, that's the point. Those props must be passed to the demo to work within the docs context.

When users open the demo on CodeSandbox or Stackblitz, the props will be empty which fallbacks to the initial values of the ThemeProvider.

I think it's a better experience for both maintainer and user. They don't need to know about disableNestedContext, storageManager, or colorSchemeNode to build demos.

@alexfauquette
Copy link
Member

I'm a bit lost because your first commit does not appear in the "files changed"
I don't see modification of the packages/mui-material in "files changed". And I don't see it being reverted in any commit 😲
Either a GitHub bug, or it got removed in a merge

image
image

@siriwatknp
Copy link
Member Author

siriwatknp commented Mar 26, 2025

Comment on lines +217 to +237
{isolated ? (
// Place ThemeProvider from MUI System here to disconnect the theme inheritance for Material UI and Joy UI
// The demo will need to handle the ThemeProvider itself.
<SystemThemeProvider
theme={(upperTheme) => ({
direction: upperTheme.direction, // required for internal ThemeProvider
vars: upperTheme.vars, // required for styling Iframe
})}
>
{iframe ? (
<DemoIframe name={name} isJoy={isJoy} isolated={isolated} {...other}>
{/* `children` needs to be a child of `DemoIframe` since the iframe implementation rely on `cloneElement`. */}
{/* the `colorSchemeNode` will be provided by DemoIframe through `window` prop */}
<IsolatedDemo cssVarPrefix={name}>{children}</IsolatedDemo>
</DemoIframe>
) : (
<IsolatedDemo cssVarPrefix={name} colorSchemeNode={root}>
{children}
</IsolatedDemo>
)}
</SystemThemeProvider>
Copy link
Member Author

@siriwatknp siriwatknp Mar 26, 2025

Choose a reason for hiding this comment

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

@alexfauquette The new isolated flag is to support these kind of demos:

These are considered mini apps within the docs, so they have to be isolated from the page's theme and color mode.

Screen.Recording.2568-03-26.at.15.27.38.mov

The <SystemThemeProvider> is used as an internal workaround to disconnect the theme from page and these mini apps.

Note: I don't think MUI X needs this kind of demo, that's why I added it as a new flag so that it does not impact MUI X.

@siriwatknp
Copy link
Member Author

Not sure why the code coverage fails even though I added more tests in this PR 🥲

Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

Except the weird 1px tranpsarent border I don't understand, I'm ok with this

@@ -16,7 +16,7 @@ const StyledMarkdownElement = styled(MarkdownElement)(({ theme }) => [
overflow: 'auto',
marginTop: -1,
backgroundColor: 'hsl(210, 25%, 9%)', // a special, one-off, color tailored for the code blocks using MUI's branding theme blue palette as the starting point. It has a less saturaded color but still maintaining a bit of the blue tint.
border: 0,
border: '1px solid transparent',
Copy link
Member

Choose a reason for hiding this comment

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

That's weird

Copy link
Member Author

@siriwatknp siriwatknp Mar 26, 2025

Choose a reason for hiding this comment

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

To fix the layout shift caused by border width (in light mode was no border but in dark mode the border was 1px). This is noticeable in pages that have a lot of code block.

Before (notice the text at the bottom of the clip):

Screen.Recording.2568-03-26.at.18.31.18.mov

After (no longer shift):

Screen.Recording.2568-03-26.at.18.31.45.mov

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Nice job creating the experiments page, it helped a lot with the review.

Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

Great work on massive improvements. 💯

@LukasTy
Copy link
Member

LukasTy commented Mar 26, 2025

In regards to the codecov results, there is an indirect change detected:
Screenshot 2025-03-26 at 15 17 02

https://app.codecov.io/gh/mui/material-ui/pull/45661/indirect-changes

@siriwatknp siriwatknp enabled auto-merge (squash) March 26, 2025 13:28
@siriwatknp siriwatknp merged commit 6382bd0 into mui:master Mar 26, 2025
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants