Skip to content

[website] Fix home page dark mode flicker#33545

Merged
siriwatknp merged 144 commits intomui:masterfrom
siriwatknp:docs/home-improve
Oct 18, 2022
Merged

[website] Fix home page dark mode flicker#33545
siriwatknp merged 144 commits intomui:masterfrom
siriwatknp:docs/home-improve

Conversation

@siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Jul 17, 2022

Before: https://master--material-ui.netlify.app/
After: https://deploy-preview-33545--material-ui.netlify.app/

Changes

  • Most of the changes are refactoring existing styles to support CSS variables

    // example
    // From
    backgroundColor:
      theme.palette.mode === 'dark'
        ? alpha(theme.palette.primaryDark[900], 0.72)
        : 'rgba(255,255,255,0.72)',
        
    // to
    backgroundColor: 'rgba(255,255,255,0.72)',
    ...theme.applyDarkStyles({
      backgroundColor: alpha(theme.palette.primaryDark[900], 0.72)
    })
  • Add theme.applyDarkStyles() to docs theme: A utility for creating styles for dark mode. It will check the theme CSS variables and apply the proper styles for dark mode.

    e.g.

    theme.applyDarkStyles({
      color: '#fff'
    })
    
    // Result
    // when there are no CSS variables, fallback to object spread
    ...theme.palette.mode === 'dark' && {
      color: '#fff',
    }
    
    // For CSS variables, it uses data attribute
    ':where([data-muidocs-color-scheme="dark"]) &': {
      color: '#fff',
    }

    use :where() so that it does not create CSS specificity

    This utility is specific to our docs because there are pages that are not wrapped with CssVarsProvider. This way developers (us) don't need to write both syntaxes to apply dark mode styles.

  • Wrap the home page with CssVarsProvider and set CSS variable prefix to --muidocs so that it does not interfere with Material UI demos in the future.

  • Theme tokens do not change (copy from the existing implementation)

  • Workaround for :where() selector. This is an existing issue in emotion (as well as styled-components)


Part of #15588

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

website Pages that are not documentation-related, marketing-focused.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants