• Stars
    star
    3,620
  • Rank 12,236 (Top 0.3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿš€ Show a splash screen during app startup. Hide it when you are ready.

๐Ÿš€ react-native-bootsplash

Show a splash screen during app startup. Hide it when you are ready.
For migration from the v3, check the MIGRATION.md guide.

mit licence npm version npm downloads
platform - android platform - ios

iOS demo android demo

Support

This library follows the React Native releases support policy.
It is supporting the latest version, and the two previous minor series.

Installation

$ npm install --save react-native-bootsplash
# --- or ---
$ yarn add react-native-bootsplash

โš ๏ธย ย Don't forget going into the ios directory to execute a pod install.

๐Ÿ†˜ Manual linking

Because this package targets recent React Native versions, you probably don't need to link it manually. But if you have a special case, follow these additional instructions:

๐Ÿ‘€ See manual linking instructions

iOS

Add this line to your ios/Podfile file, then run pod install.

target 'YourAwesomeProject' do
  # โ€ฆ
  pod 'RNBootSplash', :path => '../node_modules/react-native-bootsplash'
end

Android

  1. Add the following lines to android/settings.gradle:
include ':react-native-bootsplash'
project(':react-native-bootsplash').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-bootsplash/android')
  1. Add the implementation line to the dependencies in android/app/build.gradle:
dependencies {
  // ...
  implementation project(':react-native-bootsplash')
}
  1. Add the import and link the package in MainApplication.java:
import com.zoontek.rnbootsplash.RNBootSplashPackage; // โฌ…๏ธ add the RNBootSplashPackage import

public class MainApplication extends Application implements ReactApplication {

  // โ€ฆ

  @Override
  protected List<ReactPackage> getPackages() {
    @SuppressWarnings("UnnecessaryLocalVariable")
    List<ReactPackage> packages = new PackageList(this).getPackages();
    // โ€ฆ
    packages.add(new RNBootSplashPackage());
    return packages;
  }

  // โ€ฆ
}

Setup

Assets generation

In order to speed up the setup, we provide a CLI to generate assets, create the Android Drawable XML file and the iOS Storyboard file automatically โœจ.

$ npx react-native generate-bootsplash --help
# --- or ---
$ yarn react-native generate-bootsplash --help

The command can take multiple arguments:

yarn react-native generate-bootsplash <logoPath>

Generate a launch screen using an original logo file (PNG or SVG)

Options:
  --background-color <color>  color used as launch screen background (in hexadecimal format) (default: "#fff")
  --logo-width <width>        logo width at @1x (in dp - we recommend approximately ~100) (default: 100)
  --assets-path [path]        path to your static assets directory (useful to require the logo file in JS)
  --flavor <flavor>           [android only] flavor build variant (outputs in an android resource directory other than "main")
  --platforms <platforms>     platforms to generate assets for (comma separated) (default: "android,ios")
  -h, --help                  output usage information

Full command usage example

yarn react-native generate-bootsplash assets/bootsplash_logo_original.png \
  --background-color=F5FCFF \
  --logo-width=100 \
  --assets-path=assets \
  --flavor=main \
  --platforms=android,ios

This tool relies on the naming conventions that are used in the /example project and will therefore create the following files:

# Only if --assets-path was specified
assets/bootsplash_logo.png
assets/bootsplash_logo@1,5x.png
assets/[email protected]
assets/[email protected]
assets/[email protected]

android/app/src/main/res/values/colors.xml (creation and edition)
android/app/src/main/res/mipmap-hdpi/bootsplash_logo.png
android/app/src/main/res/mipmap-mdpi/bootsplash_logo.png
android/app/src/main/res/mipmap-xhdpi/bootsplash_logo.png
android/app/src/main/res/mipmap-xxhdpi/bootsplash_logo.png
android/app/src/main/res/mipmap-xxxhdpi/bootsplash_logo.png

ios/YourProjectName/BootSplash.storyboard
ios/YourProjectName/Images.xcassets/BootSplashLogo.imageset/bootsplash_logo.png
ios/YourProjectName/Images.xcassets/BootSplashLogo.imageset/[email protected]
ios/YourProjectName/Images.xcassets/BootSplashLogo.imageset/[email protected]

iOS

โ„น๏ธ For react-native < 0.71 setup, follow the v4.4.0 README.md.


Edit the ios/YourProjectName/AppDelegate.mm file:

#import "AppDelegate.h"
#import "RNBootSplash.h" // โฌ…๏ธ add the header import

// โ€ฆ

@implementation AppDelegate

// โ€ฆ

// โฌ‡๏ธ Add this before file @end
- (UIView *)createRootViewWithBridge:(RCTBridge *)bridge
                          moduleName:(NSString *)moduleName
                           initProps:(NSDictionary *)initProps {
  UIView *rootView = [super createRootViewWithBridge:bridge
                                          moduleName:moduleName
                                           initProps:initProps];

  [RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; // โฌ…๏ธ initialize the splash screen

  return rootView;
}

@end

Set the BootSplash.storyboard as launch screen file:

Drag and drop the file Create folder reference Set as Launch Screen File

Android

โš ๏ธย ย On Android >= 12, the splash screen will not appear if you start your app from the terminal / Android Studio. To see it, kill your app and restart it in normal conditions (tap on your app icon in the app launcher).


  1. Edit your android/app/build.gradle file:
dependencies {
  // The version of react-native is set by the React Native Gradle Plugin
  implementation("com.facebook.react:react-android")
  implementation("androidx.core:core-splashscreen:1.0.0") // add this line

  // โ€ฆ

โš ๏ธย ย Don't forget going into the android directory to execute a ./gradlew clean && ./gradlew build (or perform a Gradle sync in Android Studio).

  1. Edit your android/app/src/main/res/values/styles.xml file:
<resources>

  <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
      <!-- Your base theme customization -->
  </style>

  <!-- BootTheme should inherit from Theme.SplashScreen -->
  <style name="BootTheme" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/bootsplash_background</item>
    <item name="windowSplashScreenAnimatedIcon">@mipmap/bootsplash_logo</item>
    <item name="postSplashScreenTheme">@style/AppTheme</item>
  </style>

</resources>
  1. Edit your android/app/src/main/AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.rnbootsplashexample">

  <!-- โ€ฆ -->

  <application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:allowBackup="false"
    android:theme="@style/BootTheme"> <!-- Replace @style/AppTheme with @style/BootTheme -->
    <!-- โ€ฆ -->
  </application>
</manifest>
  1. Finally edit your android/app/src/main/java/com/yourprojectname/MainActivity.java file:
// โ€ฆ

// add these required imports:
import android.os.Bundle;
import com.zoontek.rnbootsplash.RNBootSplash;

public class MainActivity extends ReactActivity {

  // โ€ฆ

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    RNBootSplash.init(this); // โฌ…๏ธ initialize the splash screen
    super.onCreate(savedInstanceState); // or super.onCreate(null) with react-native-screens
  }
}

API

hide()

Method type

type hide = (config?: { fade?: boolean; duration?: number }) => Promise<void>;

Note: Only durations above 220ms are visually noticeable. Smaller values will be ignored and the default duration will be used.

Usage

import RNBootSplash from "react-native-bootsplash";

RNBootSplash.hide(); // immediate
RNBootSplash.hide({ fade: true }); // fade with 220ms default duration
RNBootSplash.hide({ fade: true, duration: 500 }); // fade with custom duration

getVisibilityStatus()

Method type

type VisibilityStatus = "visible" | "hidden" | "transitioning";
type getVisibilityStatus = () => Promise<VisibilityStatus>;

Usage

import RNBootSplash from "react-native-bootsplash";

RNBootSplash.getVisibilityStatus().then((status) => console.log(status));

Real world example

import React, { useEffect } from "react";
import { Text } from "react-native";
import RNBootSplash from "react-native-bootsplash";

function App() {
  useEffect(() => {
    const init = async () => {
      // โ€ฆdo multiple sync or async tasks
    };

    init().finally(async () => {
      await RNBootSplash.hide({ fade: true, duration: 500 });
      console.log("BootSplash has been hidden successfully");
    });
  }, []);

  return <Text>My awesome app</Text>;
}

๐Ÿค™ A more complex example is available in the /example folder.

With React Navigation

If you are using React Navigation, you can hide the splash screen once the navigation container and all children have finished mounting by using the onReady function.

import React from "react";
import { NavigationContainer } from "@react-navigation/native";
import RNBootSplash from "react-native-bootsplash";

function App() {
  return (
    <NavigationContainer onReady={() => RNBootSplash.hide()}>
      {/* content */}
    </NavigationContainer>
  );
}

With react-native-bars

In order to keep fully transparent status and navigation bars on Android once the splash screen is hidden (and control them), this library play nicely with react-native-bars. Check its README for more informations.

Testing with Jest

Testing code which uses this library requires some setup since we need to mock the native methods.

To add the mocks, create a file jest/setup.js (or any other file name) containing the following code:

jest.mock("react-native-bootsplash", () => {
  return {
    hide: jest.fn().mockResolvedValueOnce(),
    getVisibilityStatus: jest.fn().mockResolvedValue("hidden"),
  };
});

After that, we need to add the setup file in the jest config. You can add it under setupFiles option in your jest config file:

{
  "setupFiles": ["<rootDir>/jest/setup.js"]
}

Sponsors

This module is provided as is, I work on it in my free time.

If you or your company uses it in a production app, consider sponsoring this project ๐Ÿ’ฐ. You also can contact me for premium enterprise support: help with issues, prioritize bugfixes, feature requests, etc.

Sponsors list

More Repositories

1

react-native-permissions

An unified permissions API for React Native on iOS, Android and Windows.
Objective-C++
4,010
star
2

react-native-localize

๐ŸŒ A toolbox for your React Native app localization
TypeScript
2,259
star
3

react-native-bars

Components to control your app status and navigation bars.
TypeScript
288
star
4

react-native-dev-menu

Add custom items to the React Native dev menu.
Java
204
star
5

react-native-bootsplash-lottie-example

Java
53
star
6

react-atomic-state

Dead simple React global state management based on use-sync-external-store.
TypeScript
47
star
7

valienv

A simple environment variables validator for Node.js, web browsers and React Native โœ“
TypeScript
32
star
8

prisma-cursor-pagination

Relay cursor pagination helpers for prisma
TypeScript
15
star
9

purestead

Use Laravel Homestead the Vagrant way.
Shell
14
star
10

vagrant-rethinkdb

Try RethinkDB with Vagrant
JavaScript
6
star
11

react-router-with-chicane

4
star
12

react-native-module-template

Java
3
star
13

sound-mind.css

Default HTML elements style for modern daysย ย ๐Ÿ‘€
CSS
3
star
14

lillefp15-demo

A reason-react-native app to check Paris piscines pools opening hours
Reason
3
star
15

types-testing-library-vitest-dom

TypeScript definitions for @testing-library/jest-dom (with vitest)
3
star
16

PSD-Browser-Preview

Sync Photoshop and your browser
JavaScript
3
star
17

next-type-routes

An experiment to make next.js routes usage safer.
TypeScript
2
star
18

twitter-eraser

A simple script to purge old tweets
JavaScript
2
star
19

personal-website

Reason
1
star
20

ScrollViewIssue

JavaScript
1
star
21

punk-api-exercise

HTML
1
star
22

sign-machine-poc

TypeScript
1
star
23

rn-location-permission-example

Java
1
star
24

wff

An extend based semantic grid for Sass
CSS
1
star
25

bump-app-package-version

A GitHub action to bump your app package.json version
TypeScript
1
star