No description, website, or topics provided.
JavaScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.circleci
__mocks__
assets Readme (#3) Feb 5, 2018
examples/web Fixed error with getWrappedInstance (#19) Jun 13, 2018
src Fixed error with getWrappedInstance (#19) Jun 13, 2018
.babelrc Fixed build scripts Feb 2, 2018
.babelrc.js Fixed build scripts Feb 2, 2018
.eslintrc Fixed eslint Feb 2, 2018
.flowconfig
.gitignore Fixed build scripts Feb 2, 2018
CODE_OF_CONDUCT.md Fixes after CR Feb 13, 2018
CONTRIBUTING.md Fixes after CR Feb 13, 2018
LICENSE Initial commit Jan 31, 2018
README.md Docs + version bump (#18) May 29, 2018
package.json Fixed error with getWrappedInstance (#19) Jun 13, 2018
rollup.config.js Fixed rollup config (#16) Apr 12, 2018
yarn.lock Fixed error with getWrappedInstance (#19) Jun 13, 2018

README.md

react-theme-provider


Build Status Version MIT 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
  • ThemeProvider - component
  • withTheme - Higher Order Component
  • createTheming(defaultTheme) - factory returns ThemeProvider component and withTheme HOC with default theme injected.

Examples

Getting started

Instalation

npm install --save @callstack/react-theme-provider

or using yarn

yarn add @callstack/react-theme-provider

Usage

To use, simply wrap your code into ThemeProvider component and pass your theme as a theme prop.

<ThemeProvider theme={{ primaryColor: 'red', background: 'gray'}}>
  <App />
</ThemeProvider>

You could access theme data inside every component by wraping it into withTheme HOC. Just like this:

class App extends React.Component {
  render() {
    return (
      <div style={{ color: props.theme.primaryColor }}>
        Hello
      </div>
    );
  }
}

export default withTheme(App);

ThemeProvider

type:

type ThemeProviderType<T> = React.ComponentType<{
  children?: any,
  theme: T,
}>

Component you have to use to provide the theme to any component wrapped in withTheme HOC.

props

-theme - your theme object

withTheme

type:

type WithThemeType<T, S> = <C: React.ComponentType<*>>(
  Comp: C
) => C &
  React.ComponentType<
    $Diff<React.ElementConfig<C>, { theme: T }> & { theme?: S }
  >;

Classic Higher Order Component which takes your component as an argument and injects theme prop into it.

Example of usage

const App = ({ theme }) => (
  <div style={{ color: theme.primaryColor }}>
    Hello
  </div>
);

export withTheme(App);

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:

const Button = withTheme(({ theme }) => (
  <div style={{ color: theme.primaryColor }}>
    Click me
  </div>
));

const App = () => (
  <ThemeProvider theme={{ primaryColor: 'red' }}>
    <Button theme= {{ primaryColor: 'green' }}/>
  </ThemeProvider>
)

In this example Button will have green text.

createTheming

type:

<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: ThemeProvider and withTheme generated by createTheming always will use different context so make sure you are using matching withTheme!
If you acidentially import withTheme from @callstack/react-theme-provider instead of your theming instance it won't work.

Arguments

  • defaultTheme - default theme object

Benefits

  • Possibility to define flow types for your theme
  • Possibility to pass default theme
  • You can use multiple ThemeProviders in your app without any conflicts.

Example of usage

// 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';

Applying a custom theme to a component

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>
  );
}

Gotchas

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.