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

[pickers] Support theme.vars #6778

Merged
merged 11 commits into from
Nov 23, 2022
Merged

[pickers] Support theme.vars #6778

merged 11 commits into from
Nov 23, 2022

Conversation

alexfauquette
Copy link
Member

Fix #6711

@alexfauquette alexfauquette added the customization: css Design CSS customizability label Nov 8, 2022
@mui-bot
Copy link

mui-bot commented Nov 8, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-6778--material-ui-x.netlify.app/

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 669.4 1,030.1 698.4 814 139.556
Sort 100k rows ms 598.7 1,049.4 598.7 880.12 158.528
Select 100k rows ms 203.3 487.5 258.4 291.92 101.734
Deselect 100k rows ms 143.4 280.4 202 217.28 50.213

Generated by 🚫 dangerJS against aa08209

@alexfauquette
Copy link
Member Author

@siriwatknp I'm not sure about how to extend the theme types. For now, I put the

import type {} from '@mui/material/themeCssVarsAugmentation';

Into the root index.ts file. Would you have some recommendation about it?

backgroundColor: alpha(theme.palette.primary.light, 0.6),
color: (theme.vars || theme).palette.primary.contrastText,
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.primary.lightChannel} / ${0.6})`
Copy link
Member

@siriwatknp siriwatknp Nov 8, 2022

Choose a reason for hiding this comment

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

Suggested change
? `rgba(${theme.vars.palette.primary.lightChannel} / ${0.6})`
? `rgba(${theme.vars.palette.primary.lightChannel} / 0.6)`

The curry bracket is not needed (x the others).

@@ -1,3 +1,5 @@
import type {} from '@mui/material/themeCssVarsAugmentation';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import type {} from '@mui/material/themeCssVarsAugmentation';

This should not be enabled by the library, it should be done by the consumer instead.

Can you try importing it into the MUI X global project 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.

what do you mean by

importing it into the MUI X global project

Copy link
Member

Choose a reason for hiding this comment

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

Without it, TypeScript complains about theme.vars as it's not available on the standard Theme interface.
Can we solve it somehow without importing themeCssVarsAugmentation?

Copy link
Member

Choose a reason for hiding this comment

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

Even if we fix it on our side, make sure to test a bundled version of our package with skipLIbCheck: false because I suspect it will fail without this import.

Doesn't it make sense for @mui/x-date-pickers to import the module augmentation that it uses internally ?

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't it make sense for @mui/x-date-pickers to import the module augmentation that it uses internally

I am open to any option but the developers have to opt-in to get the type. Meaning when they install @mui/x-date-pickers and @mui/material, they should not see theme.vars.

If they want to have theme.vars, they have to opt-in (either import from @mui/material or @mui/x-date-pickers).

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 struggle to understand how we could use theme.vars in our files, and hide the theme.vars type from users

Comment on lines 228 to 232
color: theme.palette.getContrastText(
theme.vars
? `rgba(${theme.vars.palette.primary.lightChannel} / ${0.6})`
: alpha(theme.palette.primary.light, 0.6),
),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
color: theme.palette.getContrastText(
theme.vars
? `rgba(${theme.vars.palette.primary.lightChannel} / ${0.6})`
: alpha(theme.palette.primary.light, 0.6),
),
color: theme.vars ? theme.vars.palette.text.primary : theme.palette.getContrastText(alpha(theme.palette.primary.light, 0.6)),

I think this is the closest result because alpha(theme.palette.primary.light, 0.6) mostly gives very light color.


export type ExtendedTheme = Theme | CssVarsTheme;

export const getThemePalette = (theme: ExtendedTheme): Theme['palette'] | ThemeVars['palette'] => {
Copy link
Member

Choose a reason for hiding this comment

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

I do like this solution a lot

Copy link
Member Author

Choose a reason for hiding this comment

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

@siriwatknp If you're good with this solution I will do the same in the data-grid

Copy link
Member

Choose a reason for hiding this comment

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

Honestly, I don't want to go that way because the issue is just about typings. I found a way to make TypeScript happy by adding the themeCssVarsAugmentation to tsconfig.json.

{
  "extends": "../../../tsconfig.json",
  "compilerOptions": {
    "types": ["react", "mocha", "node"],
    "noImplicitAny": true
  },
  "include": [
    "src/**/*",
    "../../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
+    "../../../node_modules/@mui/material/themeCssVarsAugmentation"
  ]
}

Note: you will need to add it to all tsconfig.json and tsconfig.build.json in all packages/*.

@alexfauquette @flaviendelangle What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Screen Shot 2565-11-16 at 11 23 48

Seems to work.

Copy link
Member

Choose a reason for hiding this comment

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

Confirmed, you need to add it to tsconfig.build.json too, otherwise, yarn build fails.

Copy link
Member Author

Choose a reason for hiding this comment

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

clearly less hacky, and I confirm yarn release:build succeed

Copy link
Member

Choose a reason for hiding this comment

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

Seems like apps using @mui/x-date-pickers with this approach also works and don't have access to theme.vars

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 15, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 16, 2022
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

Left one last comment, the rest looks good.

…ckerDay.tsx

Co-authored-by: Siriwat K <siriwatkunaporn@gmail.com>
Signed-off-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

🤩 Can't wait for this!

@alexfauquette alexfauquette merged commit 87d3b48 into mui:next Nov 23, 2022
@alexfauquette alexfauquette deleted the css-vars branch November 23, 2022 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: css Design CSS customizability
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pickers] Support CSS theme variables
5 participants