Coder Social home page Coder Social logo

htdangkhoa / react-native-appearance Goto Github PK

View Code? Open in Web Editor NEW

This project forked from expo/react-native-appearance

0.0 2.0 0.0 314 KB

Access operating system appearance information (currently only light/dark mode) on iOS, Android, and web

License: MIT License

JavaScript 5.81% TypeScript 28.54% Java 28.20% Objective-C 26.48% Ruby 8.88% Starlark 2.10%

react-native-appearance's Introduction

react-native-appearance

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

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

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):

Manually link the library on iOS

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'
Manually link the library on Android
  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

iOS configuration

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

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

In bare React Native apps, you can configure supported styles with the UIUserInterfaceStyle key in your app Info.plist.

Android configuration

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.

react-native-appearance's People

Contributors

brentvatne avatar evanbacon avatar bbarthec avatar bycedric avatar ericp3reira avatar mikehardy avatar tsapeta avatar anhtuank7c avatar movibe avatar

Watchers

James Cloos avatar  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.