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] Restructure docs theme context to CSS variables #45386

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Feb 24, 2025

Part of #44534

Preview: https://deploy-preview-45386--material-ui.netlify.app/

This PR aims to fix 2 things:

  • The dark mode flicker on component pages
  • The toggle speed when switching between light and dark modes

Not possible to split the PR because they are dependent (the _app.js ThemeProvider has to be removed)

Root cause

Dark mode flicker

This is because component (demo) pages are not using CSS variables

Toggle speed

Switching between light and dark mode cause the whole page to rerender.
This is not only about using CSS variables as you can see in the recording below.

The homepage, using CSS variables, still rerender all components because the theme values changes between light and dark modes (the theme.palette.mode value).

This cause component to rerender because all Material UI is built with styled which subscribe to the ThemeContext.

Screen.Recording.2568-02-25.at.16.04.19.mov

Solution

The main changes of this PR are:

  • Remove the _app.js ThemeProvider that causes the change between modes and migrate to CSS theme variables within each page.
  • Freeze the theme between modes so that most component does not rerender. This is possible with CSS theme variables because both light and dark tokens/styles are created initially without runtime change.
Screen.Recording.2568-02-25.at.16.03.36.mov

Side benefits

  • Creating new pages, e.g. a playground, no longer include branding theme.
  • Reduced complexity of the docs because Joy UI Wrapper is not needed, thanks to the theme scoping.

@siriwatknp siriwatknp added the scope: docs-infra Specific to the docs-infra product label Feb 24, 2025
@siriwatknp siriwatknp changed the title [docs-infra] Restructure docs theme context [docs-infra] Restructure docs theme context to CSS variables Feb 24, 2025
@mui-bot
Copy link

mui-bot commented Feb 24, 2025

Netlify deploy preview

https://deploy-preview-45386--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 459d515

@siriwatknp siriwatknp force-pushed the docs-infra/demo-cssvars2 branch from baa6e59 to 97b90da Compare February 24, 2025 06:28

export function resetDocsColor() {
if (typeof document !== 'undefined') {
document.documentElement.removeAttribute('style');
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure there are no other styles added on this element? Would id be better to create a style tag with specific id and remove it instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, it's a quick approach to test if it works.

<SettingsIcon fontSize="small" />
</IconButton>
</Tooltip>
<BrandingCssVarsProvider {...themeOptions}>
Copy link
Member

Choose a reason for hiding this comment

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

Have you checked if there are other places where this needs to be done? This AppFrame as far as I know is used only on subset of the pages. (I may be wrong with this assumption)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I checked. AppFrame only used on demo pages.

Copy link
Member

Choose a reason for hiding this comment

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

So it's the plan to add this on the other pages as well directly?

Copy link
Member Author

Choose a reason for hiding this comment

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

They are all added. Marketing, blog, and API pages already contain BrandingCssVarsProvider.

Might need to adjust the API pages a bit because it's also using AppFrame.

@siriwatknp siriwatknp added the on hold There is a blocker, we need to wait label Mar 3, 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 11, 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 11, 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 12, 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 12, 2025
@siriwatknp siriwatknp removed the on hold There is a blocker, we need to wait label Mar 12, 2025
@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: out-of-date The pull request has merge conflicts and can't be merged scope: docs-infra Specific to the docs-infra product
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants