Git Product home page Git Product logo

react-native-theme-provider's Introduction

react-theme-provider

Build Status npm version

Theme provider for react-native

Example

Preview

https://github.com/WhatAKitty/react-native-theme-provider-example

Usage

If you want to use this module, you should first install a react native babel dependence:

yarn add babel-plugin-transform-decorators-legacy
# or npm
# npm install babel-plugin-transform-decorators-legacy --save

And then, create a file named .babelrc contains:

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    "transform-decorators-legacy"
  ]
}

All right, then you can use this module in an easy way:

  • install react-native-theme-provider
yarn add react-native-theme-provider
# or npm
# npm install react-native-theme-provider --save
  • import package
import ThemeProvider, { applyTheme } from 'react-native-theme-provider';
  • wrap app under ThemeProvider and put themes in it
<ThemeProvider
  themes={{
    'default': {
      color: '#333333',
    },
    'red': {
      color: 'red',
    },
  }}   {/* you can use require('/path/to/your/themefile') also, import is not support current. */}

  onChange={(name, before, after) => {
    console.log(before)
    console.log(after)
    // persist new theme into redux or something else.
    // this module will persist theme key into localstorage defaultly that key named `@App:theme`
  }}   {/* listen changes while theme changed */}
>
  <App />
</ThemeProvider>
  • use theme styles from props by @applyTheme()
@applyTheme()
class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit App.js
        </Text>
        {/* this.props.theme.styles is the theme styles object passed by yourself in ThemeProvider Component */}
        <Text style={[styles.instructions, this.props.theme.styles]}>
          {instructions}
        </Text>
        <Button
          title="Toggle Theme"
          onPress={() => {
            this.props.activeTheme(this.props.theme.current === 'default' ? 'red' : 'default');
          }}
        />
      </View>
    );
  }
}

Properties

Property Name Description
themes object of all themes or require function
onChange listen for theme changed

LICENSE

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.