Using the main app's theme in my own custom library #4534
Replies: 7 comments 8 replies
-
it works if you export your own Provider from your package. but you can try something like: import { ChakraProvider, ColorModeProvider } from '@chakra-ui/react';
import { DesignProvider, theme } from 'your-package';
<ChakraProvider resetCSS theme={theme}>
<ColorModeProvider options={{ initialColorMode: theme.config.initialColorMode }}>
<DesignProvider>
<App />
</DesignProvider>
</ColorModeProvider>
</ChakraProvider> I don't know if that would be the best approach. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. I git cloned this Chakra datepicker library, made some changes locally and ran |
Beta Was this translation helpful? Give feedback.
-
@davidbarker Did you ever find a solution for this? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I finally figure out how to apply project theme on imported monorepo packages. If you are using webpack as bundler, change
https://webpack.js.org/configuration/resolve/#resolvemodules |
Beta Was this translation helpful? Give feedback.
-
Just add in |
Beta Was this translation helpful? Give feedback.
-
we solved this by making the theme a function ==> some hooks in react make a snapshot of the functions dependencies (arguments) before everything has been resolved.
|
Beta Was this translation helpful? Give feedback.
-
I'm going to be reusing my custom theme and some custom components across multiple apps, so I created an NPM package that I can import into multiple apps. This package exports a theme and also some custom components. I ran
npm link
to use the library while I'm working on it locally.I can import the theme from my library into my app, and all standard Chakra UI components are themed correctly within my app. However, all of the custom components I'm using within my app have no theming at all. If I attempt to access the theme within the custom components inside the library, the theme object is empty.
To illustrate it more concretely, here's a custom control example from my library that uses a standard Chakra UI component inside.
I then use it in my app.
It seems the theme from my app's ChakraProvider isn't being passed down into my library components.
For example, if I use the
useTheme
hook, and console.log the resulting theme in my app, I get the correct theme.However, if I use the
useTheme
hook within one of my custom components, the resulting theme is completely empty.How do I ensure my library can access the ChakraProvider/theme from the parent app? Thanks!
Beta Was this translation helpful? Give feedback.
All reactions