Code Monkey home page Code Monkey logo

rn-dark-mode's Introduction

A tiny and simple helper set to make it easy to switch your styles in React Native when switching between "light" and "dark" mode. ๐ŸŒŸ

rn-dark-mode


Maintains typesafety and code completion on par with the normal StyleSheet.create.

Creating a dark mode stylesheet simply generates two static react native stylesheets and switches between the two with the complimentary hook.

Useage

Create a stylesheet

import { createStyleSheet, DarkModeValue } from 'rn-dark-mode'

export default createStyleSheet({
  appContainer: {
    flex: 1,
    backgroundColor: new DarkModeValue(
      '#ffffff', // light
      '#2a2a2a', // dark
    ),
  },
})

Use a stylesheet

import React from 'react'
import { Text, SafeAreaView } from 'react-native'
import { useDarkModeStyleSheet } from 'rn-dark-mode'

import stylesheet from './App.styles'

function App(): React.ReactElement {
  const styles = useDarkModeStyleSheet(stylesheet)

  return (
    <SafeAreaView style={styles.appContainer}>
      <Text>Hello</Text>
    </SafeAreaView>
  )
}

export default App

Check if dark mode or not

import React from 'react'
import { Text, SafeAreaView } from 'react-native'
import { useIsDarkMode } from 'rn-dark-mode'

function App(): React.ReactElement {
  const isDarkMode = useIsDarkMode()

  return (
    <SafeAreaView>
      <Text>{isDarkMode ? 'It sure is dark in here' : 'Rise and shine'}</Text>
    </SafeAreaView>
  )
}

export default App

Switch between two arbitrary values

import React from 'react'
import { Text, SafeAreaView } from 'react-native'
import { useDarkModeSwitch } from 'rn-dark-mode'

function App(): React.ReactElement {
  const text = useDarkModeSwitch(
    'Rise and shine', // light
    'It sure is dark in here', // dark
  )

  return (
    <SafeAreaView>
      <Text>{text}</Text>
    </SafeAreaView>
  )
}

export default App

Use a dark mode value to switch values

import React from 'react'
import { Text, SafeAreaView } from 'react-native'
import { useDarkModeValue, DarkModeValue } from 'rn-dark-mode'

const BACKGROUND_COLOR = new DarkModeValue(
  '#ffffff', // light
  '#000000', // dark
)

function App(): React.ReactElement {
  const backgroundColor = useDarkModeValue(BACKGROUND_COLOR)

  return (
    <SafeAreaView style={{ backgroundColor }}>
      <Text>Hello</Text>
    </SafeAreaView>
  )
}

export default App

Requirements


React Native >= 0.62.0

rn-dark-mode's People

Contributors

lewisyearsley avatar vladimir-vovk avatar

Stargazers

 avatar Sam Tutton avatar Peter avatar Anton Kiselev-Minaev avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

vladimir-vovk

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.