Coder Social home page Coder Social logo

vishalchaturvediencoresky / react-native-custom-theme Goto Github PK

View Code? Open in Web Editor NEW

This project forked from encoresky/react-native-custom-theme

0.0 0.0 0.0 811 KB

Custom theme support system for react native

License: MIT License

JavaScript 4.66% Ruby 3.55% C++ 17.08% Objective-C 5.87% Java 37.57% TypeScript 17.11% Objective-C++ 8.94% Makefile 3.75% Starlark 1.47%

react-native-custom-theme's Introduction

React Native Custom Theme

Custom theme system for react native

Build Status

React native custom theme is the library which enable your app to support multiple themes.

Supported platforms

  • iOS
  • Android

Installation

npm install react-native-custom-theme

The solution is implemented in JavaScript so no native module linking is required

How to use

Create theme object for light and dark theme Colors.js

import {Theme} from 'react-native-custom-theme';
export const light: Theme = {
  mode: 'light',
  primary: '#4e9cdb',
  secondary: '#4e9cdb',
  background: '#ffffff',
};

export const dark: Theme = {
  mode: 'dark',
  primary: '#0a5897',
  secondary: '#0a5897',
  background: '#000000',
};

Wrap your app within ThemeProvider. ThemeProvider require light and dark theme objects as props

import React from 'react';
import ThemeProvider from 'react-native-custom-theme';
import {dark, light} from './Colors';
import Home from './Home';

const App = () => {
  return (
    <ThemeProvider darkTheme={dark} lightTheme={light}>
      <Home />
    </ThemeProvider>
  );
};

export default App;

Import useTheme hook and use theme and isDarkTheme variables to set your colors.

-- other imports ---
import {useTheme} from 'react-native-custom-theme';

const Home = () => {
    const {theme, isDarkTheme} = useTheme();

    const backgroundStyle = {
        backgroundColor: theme.background,
        flex: 1,
    };

  const textStyle = {
    color: isDarkTheme ? '#fff' : '#000',
  };
  
  return (
      <View style={backgroundStyle}>
        <Text style={textStyle}>React Native Custom Theme</Text>
      </View>
}

To change theme use setTheme method from useTheme hook

-- other imports ---
import {ThemeModes, useTheme} from 'react-native-custom-theme';

const Settings () => {
    const {themeMode, setTheme} = useTheme();
    
    const onPress = () => {
        setTheme(ThemeModes.DARK);
    }
    
    return (
        <View>
            <TouchableOpacity onPress={onPress}>
                <Text>Change to dark theme</Text>
            </TouchableOpacity>
        </View>
}

API

ThemeProvider

Property Type Description
darkTheme Theme This object contain colors values for dark theme
lightTheme Theme This object contain colors values for light theme

useTheme

const {theme, isDarkTheme, themeMode, setTheme} = useTheme();
Property Type Description
theme Theme This object contain colors values for current selected theme
isDarkTheme boolean true if selected theme is dark else false
themeMode ThemeModes.LIGHT
ThemeModes.DARK
ThemeModes.DEVICE_THEME
Mode to apply theme
setTheme function set new theme, it takes new theme mode as parameter

Theme

  • mode - 'light' or 'dark',
  • [colorName]: [colorValue]

ThemeModes

  • LIGHT
  • DARK
  • DEVICE_THEME

Authors

Contributing

Pull requests are most welcome! Don't forget to add a title and a description that explain the issue you're trying to solve and your suggested solution. Screenshots and gifs are VERY helpful. Please do NOT format the files as we are trying to keep a unified syntax and the reviewing process fast and simple.

License

MIT

react-native-custom-theme's People

Contributors

estbalveer avatar

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.