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

[system] Add unstable_createCssVarsProvider api #28965

Merged
merged 24 commits into from Oct 15, 2021

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Oct 10, 2021

Codesandbox preview

the idea behind this PR is that Joy & Material will use unstable_createCssVarsProvider (similar to createStyled) to provide CSS variables that developers can extend and/or override the default theme.

CssVarsProvider at its core is a ThemeProvider + a function that compute theme input to create CSS variables.

The picture below describes the high-level flow that this API is responsible for.

Screen Shot 2564-10-11 at 11 43 32

How to use this API
First, the design system has to create the Provider with the default theme and then exports it. The theme structure needs to follow the palette structure (the keys will be the mode name and the value can be any palette structure). To see how type is defined, check out spec file

// in `packages/mui-joy`,

const ThemeContext = React.createContext();

const { CssVarsProvider, getInitModeScript } = createCssVarsProvider(
  ThemeContext,
  {
    defaultColorScheme: 'light',
    theme: {
      colorSchemes: {
        light: {
          palette: {
            brand: '',
            appBg: '',
          }
        },
        dark: {
          palette: {
            brand: '',
            appBg: '',
          }
        }
      },
      fontSize: {
        md: '1rem',
        // ...
      },
      // ...the rest theme structure
    },
  }
);

export { CssVarsProvider, getInitModeScript };

Then, developers can import the Provider from the package. (If they don't want to use CSS variables, they can import the ThemeProvider as usual)

import { CssVarsProvider, useTheme } from '@mui/joy';

function App() {
  return (
    <CssVarsProvider
       // theme={{ }} // they can override the default theme
       // theme={{ colorSchemes: { comfort: { ... } } }} // or they can extends the color schemes
    />
  )
}

const CustomComponent = () => {
  const theme = useTheme(); // the theme is the same as in `styled` API
  return (
    <button
      style={{
        // `vars` is the computed object that reference to CSS variables
        color: theme.vars.palette.brand, // the result is `var(--palette-brand)`
      }}>Foo</button>
  )
}

@mui-pr-bot
Copy link

mui-pr-bot commented Oct 10, 2021

Details of bundle changes

@material-ui/system: parsed: +5.81% , gzip: +5.80%

Generated by 🚫 dangerJS against bead5a9

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 11, 2021
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 11, 2021
@siriwatknp siriwatknp marked this pull request as ready for review October 11, 2021 04:47
},
};

export const createCssVarsParser = <Css = NestedRecord<string>, Vars = NestedRecord<string>>(
Copy link
Member

Choose a reason for hiding this comment

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

What is being parsed into what and why do we need it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have changed it a bit to make it easier. The cssVarsParser is responsible for creating { css, vars } from theme.

the css will be applied to <GlobalStyles styles={css} /> and vars will be attached to theme in react context so that the components can read and reference to css variable.

const Button = styled('button')(({ theme }) => ({
  color: theme.vars.palette.primary.main, // 'var(--palette-primary-main)'
}))

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.

I will need to spend more time testing this locally, but this is my first round of review :) Really happy to see this 🤩

packages/mui-system/src/cssVars/getDataset.ts Outdated Show resolved Hide resolved
packages/mui-system/src/cssVars/createCssVarsProvider.js Outdated Show resolved Hide resolved
packages/mui-system/src/cssVars/cssVarsParser.ts Outdated Show resolved Hide resolved
packages/mui-system/src/cssVars/cssVarsParser.test.ts Outdated Show resolved Hide resolved
packages/mui-system/src/cssVars/cssVarsParser.test.ts Outdated Show resolved Hide resolved
packages/mui-system/src/cssVars/createCssVarsProvider.js Outdated Show resolved Hide resolved
Copy link
Member

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

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

I love it! I played with it a bit. Just to confirm my understanding, it is expected that theme.vars is undefined yet as useTheme for joy isn't in the scope of this PR, right? Code sandbox

@siriwatknp
Copy link
Member Author

siriwatknp commented Oct 11, 2021

I love it! I played with it a bit. Just to confirm my understanding, it is expected that theme.vars is undefined yet as useTheme for joy isn't in the scope of this PR, right? Code sandbox

Yes, correct. This PR is from them system perspective only.

@siriwatknp siriwatknp mentioned this pull request Oct 13, 2021
16 tasks
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.

Some small details :)

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.

Awesome 🔥 I don't have anything else to add

@siriwatknp siriwatknp merged commit d618108 into mui:master Oct 15, 2021
@zannager zannager added the package: system Specific to @mui/system label Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: system Specific to @mui/system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants