material-ui: [theme] Fails when combining @chakra-ui/react and @material-ui/core
TypeError: undefined is not a function
It fails here: https://github.com/mui-org/material-ui/blob/next/packages/material-ui/src/Autocomplete/Autocomplete.js#L340
- The issue is present in the latest release aka alpha
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior šÆ
<Autocomplete /> component canāt be opened.
Expected Behavior š¤
<Autocomplete /> should work with chakra and material ui š
Steps to Reproduce š¹
https://codesandbox.io/s/frosty-browser-694e6?file=/src/App.js
Steps:
- Try to open
Autocompletecomponent
Context š¦
I am using @material-ui/core and @chakra-ui/react in the same application (which might not be the best but that is a different topic). Looks like with the release of version 5 and some changes to the theme/styling the Autocomplete component broke.
Your Environment š
`npx @material-ui/envinfo`
System:
OS: macOS 10.15.7
Binaries:
Node: 15.12.0 - ~/.nvm/versions/node/v15.12.0/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 7.6.3 - ~/.nvm/versions/node/v15.12.0/bin/npm
Browsers:
Chrome: 89.0.4389.128
Edge: Not Found
Firefox: 86.0
Safari: 14.0.2
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 27 (15 by maintainers)
Commits related to this issue
- feat: install ChakraUI and remove MUI they conflicted, see: https://github.com/mui/material-ui/issues/25852 — committed to WouterrV/marketplace by deleted user 2 years ago
This is like cake. Move inside the chakra user interface. The problem will be solved. š§
Sandbox link: https://codesandbox.io/s/eloquent-hopper-e2upeh?file=/src/App.js:0-1026
This issue is still present for latest mui and Chakra-ui version. If someone has found a solution, Iām interested.
Hey everyone, I have a POC for the solution.
All you have to do is put the theme of Material UI inside a special id that we provide from the library. Here are some examples:
Chakra + Material UI
Whenever you access the theme via
styled,useTheme, orsxprop from@mui/material/styles, you will get the Material UIās theme as if the themes are independent.Iāve come up with a solution, and itās pretty simple:
deepMergefunction.The end code will look something like this:
This works because MaterialUI and ChakraUI both use ThemeUI-based themes, so they are technically compatible. Some things are not used in the same way, but they donāt break each other where it matters, so for the transition between MaterialUI and ChakraUI, it will be more than enough IMO.
Theme scoping is introduced in v5.12.0, here is the doc on how to use it with Theme UI š¾.
cc @castrosuellenx
I had a look at the theme conflicts, I couldnāt see any, so a deep merge between the two seems enough. https://codesandbox.io/s/musing-nightingale-7hnd9?file=/src/App.js
These are all descriptive facts about the current state. This isnāt helpful unless you think the current implementation is perfect.
What weāre interested in is better behavior. And the current behavior is not ok and goes against one of the initial goals that we were explicitly concerned with styled-components.
To be clear: We need to investigate how we can use a separate context. Affecting existing context is quite problematic.
Thank you for your fast responses, but looks like the solution from @mnajdova only works in my skeleton example, as soon as you integrate an component from chakra-ui it also fails probably for the reasons that @oliviertassinari pointed out.
@oliviertassinari Regarding your question why I need to use both libraries I can easily tell you that the project Iām working on started using chakra-ui but at some point we needed the look and feel of the material ui form elements so I hooked that one in. But when I browse through the codebase I can see that we mostly use the
<Box />and<Flex />components for layout things. I guess Iāll just replicate those and will kick out chakra for now. šIāve switched the providers, and added second the
ThemeProvidercoming from Material-UI and it works - https://codesandbox.io/s/focused-meninsky-uj9js?file=/src/App.js:663-664 Both libraries are using emotionāsThemeProvider, but expect and create different theme structure. If you would like to use both libraries, Iād suppose you need to take care of making sure that the theme that you use in the end is compatible with both libraries.Hey @siriwatknp this solution can be used on v5.11.15 ?
We should definitely update the docs on this. It works differently than how it did in v4. I will create a PR for the docs next week.
Interesting issue. Itās a case we discussed a long time ago when we talked about the migration to emotion/styled components. Basically, itās about how our theme structure might conflict with the one existing apps have. So I donāt think that itās specific to Chakra-UI.
As @mnajdova said, itās unlikely we can easily solve this. The only alternative I can think of is to remove the styled engine theme provider from our ThemeProvider and forward the
themewith a prop. I guess this would bring back to the question @eps1lon asked not too long ago on https://github.com/mui-org/material-ui/pull/25776/files@fel1xw I would love to learn more about the incentive for using both libraries. Is there something that Material-UI could do, in its long-term roadmap to remove the requirement?