
@callstack/react-theme-provider is a set of utilities that help you create your own theming system in few easy steps.
You can use it to customize colors, fonts, etc.
createTheming(defaultTheme) - factory returns:ThemeProvider - componentwithTheme - Higher Order ComponentuseTheme - React Hooknpm install --save @callstack/react-theme-provider
or using yarn
yarn add @callstack/react-theme-provider
Import createTheming from the library to create a theming object.
import { createTheming } from '@callstack/react-theme-provider';
const { ThemeProvider, withTheme, useTheme } = createTheming(defaultTheme);
Then wrap your code in ThemeProvider component to make it available to all components.
<ThemeProvider>
<App />
</ThemeProvider>
You can access the theme data in your components by wrapping it in withTheme HOC:
class App extends React.Component {
render() {
return
Hello
;
}
}
export default withTheme(App);
Another usage for functional component:
const App = ({ theme }) => (
Hello
);
export withTheme(App);
You can also use the hooks based API:
function App() {
const theme = useTheme();
return
Hello
;
}
It will inject the following props to the component:
theme - our theme object.getWrappedInstance - exposed by some HOCs like react-redux's connect.
Use it to get the ref of the underlying element.You can also override theme provided by ThemeProvider by setting theme prop on the component wrapped in withTheme HOC.
Just like this:
const Button = withTheme(({ theme }) => (
Click me
));
const App = () => (
<ThemeProvider theme={{ primaryColor: 'red' }}>
<Button theme={{ primaryColor: 'green' }} />
</ThemeProvider>
);
In this example Button will have green text.
createThemingtype:
<T, S>(defaultTheme: T) => {
ThemeProvider: ThemeProviderType<T>,
withTheme: WithThemeType<T, S>,
}
This is more advanced replacement to classic importing ThemeProvider and withTheme directly from the library.
Thanks to it you can create your own ThemeProvider with any default theme.
Returns instance of ThemeProvider component and withTheme HOC.
You can use this factory to create a singleton with your instances of ThemeProvider and withTheme.
Note:
ThemeProviderandwithThemegenerated bycreateThemingalways will use different context so make sure you are using matchingwithTheme! If you acidentially importwithThemefrom@callstack/react-theme-providerinstead of your theming instance it won't work.
defaultTheme - default theme objectflow types for your themeThemeProviders in your app without any conflicts.// theming.js
import { createTheming } from '@callstack/react-theme-provider';
const { ThemeProvider, withTheme } = createTheming({
primaryColor: 'red',
secondaryColor: 'green',
});
export { ThemeProvider, withTheme };
//App.js
import { ThemeProvider, withTheme } from './theming';
ThemeProvidertype:
type ThemeProviderType<Theme> = React.ComponentType<{
children: React.Node,
theme?: Theme,
}>;
Component you have to use to provide the theme to any component wrapped in withTheme HOC.
-theme - your theme object
withThemetype:
type WithThemeType<Theme> = React.ComponentType<{ theme: Theme }>
Higher Order Component which takes your component as an argument and injects theme prop into it.
useThemetype:
type UseTheme = (overrides?: PartialTheme) => Theme;
Hook which takes theme overrides and returns a theme object.
Example:
function App(props) {
const theme = useTheme(props.theme);
return
Hello
;
}
If you want to change the theme for a certain component, you can directly pass the theme prop to the component. The theme passed as the prop is merged with the theme from the Provider.
import * as React from 'react';
import MyButton from './MyButton';
export default function ButtonExample() {
return <MyButton theme={{ roundness: 3 }}>Press me</MyButton>;
}
The ThemeProvider exposes the theme to the components via React's context API,
which means that the component must be in the same tree as the ThemeProvider. Some React Native components will render a
different tree such as a Modal, in which case the components inside the Modal won't be able to access the theme. The work
around is to get the theme using the withTheme HOC and pass it down to the components as props, or expose it again with the
exported ThemeProvider component.
$ claude mcp add react-theme-provider \
-- python -m otcore.mcp_server <graph>