npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-dark

v0.1.3

Published

Tiny wrapper around React Native's StyleSheet.create API to easily support dark mode.

Downloads

92

Readme

React Native Dark — Formidable, We build the modern web

react-native-dark is a minor augmentation of StyleSheet.create to support dynamic dark-mode styling with little hassle, made possible by babel.

A little, illustrative example:

import { StyleSheet, Text, View } from "react-native";

export default function App() {
  return (
    <View style={styles.container}>
      <Text style={styles.title}>Hello, world!</Text>
    </View>
  );
}

const styles = createStyleSheet({
  container: {
    flex: 1,
    backgroundColor: "white",
    // 🎉 dark mode 🎉
    $dark: {
      backgroundColor: "black",
    },
  },

  title: {
    color: "black",
    // 🎉 dark mode 🎉
    $dark: {
      color: "white",
    },
  },
});

Setup

Setup involves three steps.

Step 1: Installation

From a React Native (or Expo) project, install react-native-dark from npm:

npm install react-native-dark # npm
yarn add react-native-dark # yarn
pnpm add react-native-dark # pnpm

Step 2: Add the babel plugin

In your babel configuration (in e.g. babel.config.js), add the react-native-dark babel plugin:

module.exports = function() {
  return {
    // ...
    plugins: ["react-native-dark/plugin"], // 👈 add this
  };
};

Step 3: Add the TypeScript shim for StyleSheet.create

We'll also "shim" the type for StyleSheet.create to enhance the developer experience. Add a declaration file to your project, such as shim.d.ts and add the following line:

/// <reference types="react-native-dark/shim" />

Step 4: Go to town!

You're ready to start adding dark-mode styles to your app! See below for more details on usage.

Usage

The babel plugin and TS shim were built to make adding dark-mode support to your app as easy as just declaring dark-mode styles in your stylesheets. In a standard style declaration, just add a $dark field with the styles to be applied in dark mode! These styles will be applied on top of the baseline styles.

import { StyleSheet } from "react-native";

// ...

const styles = StyleSheet.create({
  card: {
    padding: 8,
    borderRadius: 8,
    backgroundColor: "lightblue",
    
    // 🪄 magic happens here 🪄
    $dark: {
      backgroundColor: "blue"
    }
  }
});

Now when you call styles.card within your function components, the value will be automagically dynamic based on color scheme preference.

Manually setting color mode

By default, $dark styles will be applied when the user's device color scheme preference is set to dark. However, you can manually override this behavior by wrapping a component tree in DarkModeProvider from react-native-dark.

import { DarkModeProvider } from "react-native-dark";

// Example of forcing dark mode and ignore user's color scheme preference
const App = () => {
  return (
    <DarkModeProvider colorMode="dark">
      <Body />
    </DarkModeProvider>
  )
}

The DarkModeProvider has a single colorMode prop that can accept:

  • "auto" (default) to respect user's color scheme preference;
  • "light" to force light mode;
  • "dark" to force dark mode.

🦄 Magical, but not magic

The babel plugin does the heavy lifting here and will turn code like the following:

import { StyleSheet, View } from "react-native";

export const App = () => {
  return <View style={styles.container} />;
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "white",
    
    $dark: {
      backgroundColor: "black"
    }
  }
});

into something like this:

import { StyleSheet, View } from "react-native";
import { useDarkMode } from "react-native-dark";

export const App = () => {
  const isDark = useDarkMode();
  
  return <View style={isDark ? __styles__container__$dark : styles.container} />;
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "white",
  },
  __container__$dark: {
    backgroundColor: "black"
  }
});

const __styles__container__$dark = StyleSheet.compose(styles.container, styles.__container__$dark);

This is a reasonable and performant approach that you might take by hand if you were implementing dark mode by hand. react-native-dark just cuts out the extra code for you. This, however, comes with a limitation or two...

Limitations

  1. Styles should be defined in the same file that they are referenced. E.g., don't import/export your styles object – define them in the same file that they're used.
  2. The dynamic support is handled by the useColorScheme hook from React Native, therefore this library only currently supports function components.
  3. Who knows, we'll probably find more limitations as we go 🤷‍