Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zoontek/react-native-bootsplash
π Show a splash screen during app startup. Hide it when you are ready.
https://github.com/zoontek/react-native-bootsplash
bootsplash react react-native splash-screen splashscreen
Last synced: 5 days ago
JSON representation
π Show a splash screen during app startup. Hide it when you are ready.
- Host: GitHub
- URL: https://github.com/zoontek/react-native-bootsplash
- Owner: zoontek
- License: mit
- Created: 2019-07-02T10:45:52.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-28T18:44:57.000Z (about 2 months ago)
- Last Synced: 2024-10-29T11:24:41.598Z (about 2 months ago)
- Topics: bootsplash, react, react-native, splash-screen, splashscreen
- Language: TypeScript
- Homepage:
- Size: 23.3 MB
- Stars: 3,708
- Watchers: 18
- Forks: 255
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
- awesome - react-native-bootsplash - Show a splash screen during app startup. Hide it when you are ready. [Tutorial π](https://www.freecodecamp.org/news/react-native-splash-screen/) (Uncategorized / Uncategorized)
README
# π react-native-bootsplash
Show a splash screen during app startup. Hide it when you are ready.
**For migration from the v5, check the [`MIGRATION.md` guide](./MIGRATION.md).**[![mit licence](https://img.shields.io/dub/l/vibe-d.svg?style=for-the-badge)](https://github.com/zoontek/react-native-bootsplash/blob/main/LICENSE)
[![npm version](https://img.shields.io/npm/v/react-native-bootsplash?style=for-the-badge)](https://www.npmjs.org/package/react-native-bootsplash)
[![npm downloads](https://img.shields.io/npm/dt/react-native-bootsplash.svg?label=downloads&style=for-the-badge)](https://www.npmjs.org/package/react-native-bootsplash)
[![platform - android](https://img.shields.io/badge/platform-Android-3ddc84.svg?logo=android&style=for-the-badge)](https://www.android.com)
[![platform - ios](https://img.shields.io/badge/platform-iOS-000.svg?logo=apple&style=for-the-badge)](https://developer.apple.com/ios)
## Support
This library follows the React Native [releases support policy](https://github.com/reactwg/react-native-releases/blob/main/docs/support.md).
It is supporting the **latest version**, and the **two previous minor series**.## Installation
```bash
$ 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`._
## 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 β¨.
```bash
$ npx react-native-bootsplash generate --help
# --- or ---
$ yarn react-native-bootsplash generate --help
```The command can take multiple arguments:
```bash
Usage: react-native-bootsplash generate [options]Generate a launch screen using a logo file path (PNG or SVG)
Options:
--project-type Project type ("detect", "bare" or "expo") (default: "detect")
--platforms Platforms to generate for, separated by a comma (default: "android,ios,web")
--background Background color (in hexadecimal format) (default: "#fff")
--logo-width Logo width at @1x (in dp - we recommend approximately ~100) (default: 100)
--assets-output Assets output directory path (default: "assets/bootsplash")
--flavor Android flavor build variant (where your resource directory is) (default: "main")
--html HTML template file path (your web app entry point) (default: "public/index.html")
--license-key License key to enable brand and dark mode assets generation
--brand Brand file path (PNG or SVG)
--brand-width Brand width at @1x (in dp - we recommend approximately ~80) (default: 80)
--dark-background [dark mode] Background color (in hexadecimal format)
--dark-logo [dark mode] Logo file path (PNG or SVG)
--dark-brand [dark mode] Brand file path (PNG or SVG)
-h, --help display help for command
```#### πͺ Unlock the CLI full potential
In order to use the `--brand`, `--brand-width` and `--dark-*` options, you must specify a `--license-key`.
With it, the generator is able to output over **50 files** (logo and brand images generated in all pixel densities, dark mode versions, etc.), saving you (and your company!) a massive amount of time not only at creation, but also at each adjustment β±οΈ
_π This license key grants unlimited and unrestricted usage of the generator for the buyer's purposes (meaning you can execute the assets generation as much as you want)._
#### Full command usage example
```bash
# Without license key
yarn react-native-bootsplash generate svgs/light-logo.svg \
--platforms=android,ios,web \
--background=F5FCFF \
--logo-width=100 \
--assets-output=assets/bootsplash \
--flavor=main \
--html=public/index.html# With license key π
yarn react-native-bootsplash generate svgs/light-logo.svg \
--platforms=android,ios,web \
--background=F5FCFF \
--logo-width=100 \
--assets-output=assets/bootsplash \
--flavor=main \
--html=public/index.html \
--license-key=xxxxx \
--brand=svgs/light-brand.svg \
--brand-width=80 \
--dark-background=00090A \
--dark-logo=svgs/dark-logo.svg \
--dark-brand=svgs/dark-brand.svg
```This tool relies on the naming conventions that are used in the `/example` project and will therefore create the following files:
```bash
# Without license key
android/app/src/main/res/drawable-mdpi/bootsplash_logo.png
android/app/src/main/res/drawable-hdpi/bootsplash_logo.png
android/app/src/main/res/drawable-xhdpi/bootsplash_logo.png
android/app/src/main/res/drawable-xxhdpi/bootsplash_logo.png
android/app/src/main/res/drawable-xxxhdpi/bootsplash_logo.png
android/app/src/main/AndroidManifest.xml
android/app/src/main/res/values/colors.xml
android/app/src/main/res/values/styles.xmlios/YourApp/BootSplash.storyboard
ios/YourApp/Colors.xcassets/BootSplashBackground-.colorset/Contents.json
ios/YourApp/Images.xcassets/BootSplashLogo-.imageset/Contents.json
ios/YourApp/Images.xcassets/BootSplashLogo-.imageset/logo-.png
ios/YourApp/Images.xcassets/BootSplashLogo-.imageset/[email protected]
ios/YourApp/Images.xcassets/BootSplashLogo-.imageset/[email protected]
ios/YourApp/Info.plist
ios/YourApp.xcodeproj/project.pbxprojpublic/index.html
assets/bootsplash/manifest.json
assets/bootsplash/logo.png
assets/bootsplash/logo@1,5x.png
assets/bootsplash/[email protected]
assets/bootsplash/[email protected]
assets/bootsplash/[email protected]# + Over 40 files with license key π (brand images, dark mode versionsβ¦)
```![](./docs/cli_generator.png)
### With Expo
1. Disable `expo-splash-screen` autolinking (in your `package.json`):
```json
"expo": {
"autolinking": {
"exclude": [
"expo-splash-screen"
]
}
}
```2. Add the plugin in your `app.json`:
```diff
{
"expo": {
- "splash": {
- "image": "./assets/splash.png",
- "resizeMode": "contain",
- "backgroundColor": "#ffffff"
- },
+ "plugins": [
+ ["react-native-bootsplash", { "assetsDir": "assets/bootsplash" }]
+ ]
}
}
```_π The available plugins options are:_
```ts
type PluginOptions = {
assetsDir?: string; // optional, default is "assets/bootsplash"
android?: {
parentTheme?: "TransparentStatus" | "EdgeToEdge"; // optional, default is `undefined` (`Theme.BootSplash`)
darkContentBarsStyle?: boolean; // optional, default is `undefined`
};
};
```### With bare React Native
#### iOS
Edit the `ios/YourApp/AppDelegate.mm` file:
```obj-c
#import "AppDelegate.h"
#import "RNBootSplash.h" // β¬ οΈ add the header import// β¦
@implementation AppDelegate
// β¦
// β¬οΈ Add this before file @end (for react-native 0.74+)
- (void)customizeRootView:(RCTRootView *)rootView {
[super customizeRootView:rootView];
[RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; // β¬ οΈ initialize the splash screen
}// OR
// β¬οΈ Add this before file @end (for react-native < 0.74)
- (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
```#### Android
Edit your `android/app/src/main/java/com/yourapp/MainActivity.{java,kt}` file:
```java
// Java (react-native < 0.73)
// β¦// 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, R.style.BootTheme); // β¬ οΈ initialize the splash screen
super.onCreate(savedInstanceState); // super.onCreate(null) with react-native-screens
}
}
``````kotlin
// Kotlin (react-native >= 0.73)
// β¦// add these required imports:
import android.os.Bundle
import com.zoontek.rnbootsplash.RNBootSplashclass MainActivity : ReactActivity() {
// β¦
override fun onCreate(savedInstanceState: Bundle?) {
RNBootSplash.init(this, R.style.BootTheme) // β¬ οΈ initialize the splash screen
super.onCreate(savedInstanceState) // super.onCreate(null) with react-native-screens
}
}
```## API
### hide()
Hide the splash screen (immediately, or with a fade out).
#### Method type
```ts
type hide = (config?: { fade?: boolean }) => Promise;
```#### Usage
```tsx
import { useEffect } from "react";
import { Text } from "react-native";
import BootSplash from "react-native-bootsplash";const App = () => {
useEffect(() => {
const init = async () => {
// β¦do multiple sync or async tasks
};init().finally(async () => {
await BootSplash.hide({ fade: true });
console.log("BootSplash has been hidden successfully");
});
}, []);return My awesome app;
};
```### isVisible()
Return the current visibility status of the native splash screen.
#### Method type
```ts
type isVisible = () => Promise;
```#### Usage
```ts
import BootSplash from "react-native-bootsplash";BootSplash.isVisible().then((value) => console.log(value));
```### useHideAnimation()
A hook to easily create a custom hide animation by animating all splash screen elements using `Animated`, `react-native-reanimated` or else (similar as the video on top of this documentation).
#### Method type
```ts
type useHideAnimation = (config: {
ready?: boolean; // a boolean flag to delay the animate execution (default: true)// the required generated assets
manifest: Manifest; // the manifest file is generated in your assets directory
logo?: ImageRequireSource;
darkLogo?: ImageRequireSource;
brand?: ImageRequireSource;
darkBrand?: ImageRequireSource;// specify if you are using translucent status / navigation bars
// in order to avoid a shift between the native and JS splash screen
statusBarTranslucent?: boolean;
navigationBarTranslucent?: boolean;animate: () => void;
}) => {
container: ContainerProps;
logo: LogoProps;
brand: BrandProps;
};
```#### Usage
```tsx
import { useState } from "react";
import { Animated, Image } from "react-native";
import BootSplash from "react-native-bootsplash";type Props = {
onAnimationEnd: () => void;
};const AnimatedBootSplash = ({ onAnimationEnd }: Props) => {
const [opacity] = useState(() => new Animated.Value(1));const { container, logo /*, brand */ } = BootSplash.useHideAnimation({
manifest: require("../assets/bootsplash/manifest.json"),logo: require("../assets/bootsplash/logo.png"),
// darkLogo: require("../assets/bootsplash/dark-logo.png"),
// brand: require("../assets/bootsplash/brand.png"),
// darkBrand: require("../assets/bootsplash/dark-brand.png"),statusBarTranslucent: true,
navigationBarTranslucent: false,animate: () => {
// Perform animations and call onAnimationEnd
Animated.timing(opacity, {
useNativeDriver: true,
toValue: 0,
duration: 500,
}).start(() => {
onAnimationEnd();
});
},
});return (
{/* */}
);
};const App = () => {
const [visible, setVisible] = useState(true);return (
{/* content */}{visible && (
{
setVisible(false);
}}
/>
)}
);
};
```**This example is simple for documentation purpose (we only animate the container).**
**π€ A more complex example is available in the [`/example` folder](./example/src/AnimatedBootSplash.tsx).**## FAQ
### How can I have a transparent status bar, or [edge-to-edge layout](https://developer.android.com/develop/ui/views/layout/edge-to-edge)?
Edit your `values/styles.xml` file to inherit from `Theme.BootSplash.TransparentStatus` / `Theme.BootSplash.EdgeToEdge` instead of `Theme.BootSplash`:
```xml
<!-- β¦ --><!-- optional, used to enforce the initial bars styles -->
<!-- default is true in light mode, false in dark mode -->
<item name="darkContentBarsStyle">true</item>
```
### Why are both light and dark assets inlined in my index.html?
For the sake of simplicity. Since the light and dark versions of your assets are likely identical (except for the colors), if your `index.html` file is compressed with **gzip**, the size difference will be negligible.
### How should I use it 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.
```tsx
import { NavigationContainer } from "@react-navigation/native";
import BootSplash from "react-native-bootsplash";const App = () => (
{
BootSplash.hide();
}}
>
{/* content */}
);
```### How can I mock the module in my tests?
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:
```ts
jest.mock("react-native-bootsplash", () => {
return {
hide: jest.fn().mockResolvedValue(),
isVisible: jest.fn().mockResolvedValue(false),
useHideAnimation: jest.fn().mockReturnValue({
container: {},
logo: { source: 0 },
brand: { source: 0 },
}),
};
});
```After that, we need to add the setup file in the jest config. You can add it under [setupFiles](https://jestjs.io/docs/en/configuration.html#setupfiles-array) option in your jest config file:
```json
{
"setupFiles": ["/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.