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

@gsoftware/react-native-appearance

v0.3.5

Published

Polyfill for `Appearance` API which will be available in `[email protected]`.

Downloads

11

Readme

react-native-appearance

Access operating system appearance information on iOS, Android, and web. Currently supports detecting preferred color scheme (light/dark).

⚠️ Appearance in React Native core is recommended unless you have a good reason to use the library (eg: you're on an older React Native version.) This project is archived now that it will not be needed going forward.

Installation

Installation instructions vary depending on whether you're using a managed Expo project or a bare React Native project.

Managed Expo project

This library is supported in Expo SDK 35+ (SDK 35 includes iOS support, SDK 36 and higher includes support for all platforms).

expo install react-native-appearance

Then, in app.json, include "userInterfaceStyle" to listen to the device's appearance settings:

{
  "expo": {
    /*
        Supported user interface styles. If left blank, "light" will be used. Use "automatic" if you would like to support either "light" or "dark" depending on device settings.
      */
    "userInterfaceStyle": "automatic" | "light" | "dark"
  }
}

Android support and web support are available on SDK36+.

Bare React Native project

yarn add react-native-appearance

Linking

If you are not using AndroidX on your project already (this is the default on React Native 0.60+, but not on lower versions) you will want to use the jetifier npm package. Install the package with yarn add -D jetifier and then under scripts add "postinstall": "jetify -r". Next run yarn jetify.

After installing the package you need to link the native parts of the library for the platforms you are using. The easiest way to link the library is using the CLI tool by running this command from the root of your project:

react-native link react-native-appearance

If you can't or don't want to use the CLI tool, you can also manually link the library using the instructions below (click on the arrow to show them):

Either follow the instructions in the React Native documentation to manually link the framework or link using Cocoapods by adding this to your Podfile:

pod 'react-native-appearance', :path => '../node_modules/react-native-appearance'
  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import io.expo.appearance.RNCAppearancePackage; to the imports at the top of the file
  • Add new RNCAppearancePackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
include ':react-native-appearance'
project(':react-native-appearance').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-appearance/android')
  1. Insert the following lines inside the dependencies block in android/app/build.gradle:
implementation project(':react-native-appearance')

Configuration

In Expo managed projects, add ios.userInterfaceStyle to your app.json:

{
  "expo": {
    "ios": {
      "userInterfaceStyle": "automatic"
    }
  }
}

For bare React Native apps, run npx pod-install. You can configure supported styles with the UIUserInterfaceStyle key in your app Info.plist.

Add the uiMode flag in AndroidManifest.xml:

<activity
...
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode">

Implement the onConfigurationChanged method in MainActivity.java:

import android.content.Intent; // <--- import
import android.content.res.Configuration; // <--- import

public class MainActivity extends ReactActivity {
  ......

  // copy these lines
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Intent intent = new Intent("onConfigurationChanged");
    intent.putExtra("newConfig", newConfig);
    sendBroadcast(intent);
  }

  ......
}

Usage

First, you need to wrap your app in the AppearanceProvider. At the root of your app, do the following:

import { AppearanceProvider } from 'react-native-appearance';

export default () => (
  <AppearanceProvider>
    <App />
  </AppearanceProvider>
);

Now you can use Appearance and useColorScheme anywhere in your app.

import { Appearance, useColorScheme } from 'react-native-appearance';

/**
 * Get the current color scheme
 */
Appearance.getColorScheme();

/**
 * Subscribe to color scheme changes with a hook
 */
function MyComponent() {
  const colorScheme = useColorScheme();
  if (colorScheme === 'dark') {
    // render some dark thing
  } else {
    // render some light thing
  }
}

/**
 * Subscribe to color scheme without a hook
 */
const subscription = Appearance.addChangeListener(({ colorScheme }) => {
  // do something with color scheme
});

// Remove the subscription at some point
subscription.remove();

Attribution

This was mostly written by Facebook for inclusion in React Native core.