Make a Blur Background in React Native

React Native Blur Background

This is an Example to Make a Blur Background in React Native. To Make a Blur Background in React Native we will use the BlurView component from @react-native-community/blur provided by react-native-community. This is very useful if you want to make a splash screen.

In this example, we will make a simple screen with a Switch to switch between simple and blurred screen and three buttons to switch different type of blur effects. So let’s get started.

Make BlurView using

<BlurView
  viewRef={this.state.viewRef}
  style={styles.blurView}
  blurRadius={1}
  blurType={blurType} //xlight, light, dark
  // The following props are also available on Android:
  // blurRadius={20}
  // downsampleFactor={10}
  // overlayColor={'rgba(0, 0, 255, .6)'}   // set a blue overlay
/>

Different Type of Blur Effects

  1. xlight: Extra Light Blur Type
  2. light: Light Blur Type
  3. dark: Dark Blur Type
  4. extraDark: Extra Dark Blur Type(tvOS only)
  5. regular: Regular Blur Type (iOS 10+ and tvOS only)
  6. prominent: Prominent Blur Type (iOS 10+ and tvOS only)

Adjust Blur Intensity

0-100 – Adjusts Blur Intensity

Note: The maximum blur amount on Android is 32, so higher values will be clamped to 32.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use BlurView we need to install @react-native-community/blur dependency.

To install this open the terminal and jump into your project using

cd ProjectName

Run the following command to install

npm install @react-native-community/blur --save

This command will copy all the dependency into your node_module directory. –save is optional, it is just to update the @react-native-community/blur dependency in your package.json file.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use

cd ios && pod install && cd ..

Code

Now Open App.js in any code editor and replace the code with the following code

App.js

// Make a Blur Background in React Native
// https://aboutreact.com/blur-background/

// import React in our code
import React, {useState, createRef} from 'react';

// import all the components we are going to use
import {
  Image,
  InteractionManager,
  StyleSheet,
  Switch,
  Text,
  View,
  TouchableOpacity,
  findNodeHandle,
} from 'react-native';

//import BlurView to make the Blur Background
import {BlurView} from '@react-native-community/blur';

const App = () => {
  const [showBlur, setShowBlur] = useState(true);
  const [viewRef, setViewRef] = useState(null);
  const [blurType, setBlurType] = useState('light');
  const backgroundImageRef = createRef();

  const tintColor = ['#ffffff', '#000000'];
  if (blurType === 'xlight') {
    tintColor.reverse();
  }

  const renderBlurView = () => {
    //Function for the blur background
    return (
      <View
        style={{
          flexDirection: 'column',
          justifyContent: 'flex-end'
        }}>
        {viewRef && (
          <BlurView
            viewRef={viewRef}
            style={styles.blurViewStyle}
            blurRadius={1}
            blurType={blurType}
            // Additional available on Android
            // blurRadius={20}
            // downsampleFactor={10}
            // overlayColor={'rgba(0, 0, 255, .6)'}
          />
        )}
        <View
          style={{
            flex: 1,
            flexDirection: 'column',
            justifyContent: 'flex-end',
            paddingBottom: 32,
          }}>
          <Text
            style={[
              styles.textStyle,
              {color: tintColor[0]
            }]}>
              Blur component
          </Text>
          <TouchableOpacity
            style={styles.buttonStyle}
            onPress={() => {
              //To make extra light background
              setBlurType('xlight');
            }}>
            <Text
              style={[
                styles.textStyle,
                {color: tintColor[0]}
              ]}>
                xlight
            </Text>
          </TouchableOpacity>
          <TouchableOpacity
            style={styles.buttonStyle}
            onPress={() => {
              //To make light background
              setBlurType('light');
            }}>
            <Text
              style={[
                styles.textStyle,
               {color: tintColor[0]}
              ]}>
                light
            </Text>
          </TouchableOpacity>
          <TouchableOpacity
            style={styles.buttonStyle}
            onPress={() => {
              //To make dark light background
              setBlurType('dark');
            }}>
            <Text
              style={[
                styles.textStyle,
                {color: tintColor[0]}
              ]}>
                dark
            </Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  };

  return (
    <View style={styles.container}>
      <Image
        source={{
          uri:
            'https://raw.githubusercontent.com/AboutReact/sampleresource/master/site_banner_vertical.png',
        }}
        //source={require('./bgimage.jpg')}
        style={styles.imageStyle}
        ref={backgroundImageRef}
        onLoadEnd={() => {
          // Workaround for a tricky race condition on initial load
          InteractionManager.runAfterInteractions(() => {
            setTimeout(() => {
              setViewRef(
                findNodeHandle(backgroundImageRef.current)
              );
            }, 500);
          });
        }}
      />
      {showBlur ? renderBlurView() : null}
      <View style={styles.blurToggleStyle}>
        <Text
          style={[
            styles.textStyle,
            {color: tintColor[0]}
          ]}>
            Show Blur Background
        </Text>
        <Text
          style={[
            styles.textStyle,
            {color: tintColor[0]}
          ]}>
            {showBlur ? 'Yes' : 'No'}
        </Text>
        <Switch
          onValueChange={
            (value) => setShowBlur(value)
          }
          value={showBlur}
        />
      </View>
    </View>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  imageStyle: {
    position: 'absolute',
    left: 0,
    top: 0,
    bottom: 0,
    right: 0,
    resizeMode: 'cover',
    width: null,
    height: null,
  },
  blurViewStyle: {
    position: 'absolute',
    left: 0,
    top: 0,
    bottom: 0,
    right: 0,
  },
  textStyle: {
    fontSize: 22,
    fontWeight: 'bold',
    textAlign: 'center',
    margin: 10,
    color: '#d0d0d0',
  },
  blurToggleStyle: {
    position: 'absolute',
    top: 30,
    alignItems: 'center',
  },
  buttonStyle: {
    alignItems: 'center',
    backgroundColor: 'lightgreen',
    width: 300,
    marginLeft: 100,
    marginRight: 100,
    marginTop: 16,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshots

IOS

         

Android

         

This is how you can Make a Blur Background in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.