react-theme-provider

--- [![Build Status][build-badge]][build] [![Version][version-badge]][package] [![MIT License][license-badge]][license] ## About `@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. ## Features - Works in **React** and **React Native** - `createTheming(defaultTheme)` - factory returns: - `ThemeProvider` - component - `withTheme` - Higher Order Component - `useTheme` - React Hook ## Examples - built-in example for web react - ['/examples/web'](/examples/web) - [![Edit v6o562k6l7](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/v6o562k6l7) ## Getting started ### Installation ```sh npm install --save @callstack/react-theme-provider ``` or using yarn ```sh yarn add @callstack/react-theme-provider ``` ### Usage Import `createTheming` from the library to create a theming object. ```js 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. ```js ``` You can access the theme data in your components by wrapping it in `withTheme` HOC: ```js class App extends React.Component { render() { return
Hello
; } } export default withTheme(App); ``` Another usage for functional component: ```js const App = ({ theme }) => (
Hello
); export withTheme(App); ``` You can also use the [hooks](https://reactjs.org/docs/hooks-intro.html) based API: ```js function App() { const theme = useTheme(); return
Hello
; } ``` ### Injected props 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. ### Injecting theme by a direct prop You can also override `theme` provided by `ThemeProvider` by setting `theme` prop on the component wrapped in `withTheme` HOC. Just like this: ```js const Button = withTheme(({ theme }) => (
Click me
)); const App = () => (