react-theme-provider

A set of utilities that help you create your own React theming system in few easy steps

  • 所有者: callstack/react-theme-provider
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖


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
  • createTheming(defaultTheme) - factory returns:
    • ThemeProvider - component
    • withTheme - Higher Order Component
    • useTheme - React Hook

Examples

Getting started

Installation

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

or using yarn

yarn add @callstack/react-theme-provider

Usage

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 <div style={{ color: props.theme.primaryColor }}>Hello</div>;
  }
}

export default withTheme(App);

You can also use the hooks based API:

function App() {
  const theme = useTheme();

  return <div style={{ color: theme.primaryColor }}>Hello</div>;
}

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.

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

Helpers

ThemeProvider

type:

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.

Props

-theme - your theme object

withTheme

type:

type WithThemeType<Theme> = React.ComponentType<{ theme: Theme }>

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

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

useTheme

type:

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 <div style={{ color: theme.primaryColor }}>Hello</div>;
}

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.

主要指標

概覽
名稱與所有者callstack/react-theme-provider
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2018-01-31 12:38:13
推送於2023-08-01 12:10:13
最后一次提交2023-08-01 12:10:12
發布數12
最新版本名稱v3.0.9 (發布於 2023-08-01 12:10:12)
第一版名稱1.1.0 (發布於 )
用户参与
星數466
關注者數11
派生數54
提交數155
已啟用問題?
問題數29
打開的問題數16
拉請求數64
打開的拉請求數38
關閉的拉請求數24
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?