New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-native-snackbar

Package Overview
Dependencies
Maintainers
0
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-snackbar

Material Design "Snackbar" component for Android and iOS.

  • 2.8.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14K
decreased by-6.03%
Maintainers
0
Weekly downloads
 
Created
Source

React Native Snackbar

Build status npm downloads npm version Latest GitHub tag

Material Design "Snackbar" component for Android and iOS. Supports custom colors, fonts, and languages.

Snackbar screenshot

Snackbars are used for displaying a brief message to the user, along with an optional action. They animate up from the bottom of the screen and then disappear shortly afterward.

See Google's Material Design guidelines for more info on Snackbars and when to use them.

How it works

Snackbar.show({
  text: 'Hello world',
  duration: Snackbar.LENGTH_SHORT,
});

Or, to include an action button:

Snackbar.show({
  text: 'Hello world',
  duration: Snackbar.LENGTH_INDEFINITE,
  action: {
    text: 'UNDO',
    textColor: 'green',
    onPress: () => { /* Do something. */ },
  },
});

Installation

  1. Install:

    • Using npm: npm install react-native-snackbar --save
    • Using Yarn: yarn add react-native-snackbar
  2. Link:

    • RN >= 0.60 supports autolinking: first cd ios && pod install && cd ..
    • RN < 0.60: react-native link react-native-snackbar
    • Or if that fails, link manually using these steps
    • Note that because this is a native module, Expo does not support it -- to use with Expo you need to eject to ExpoKit
  3. Import it in your JS:

    import Snackbar from 'react-native-snackbar';
    

Usage

Snackbar.show(options)

Shows a Snackbar, dismissing any existing Snackbar first. Accepts an object with the following options:

KeyData typeDefault value?Description
textstringRequired.The message to show.
durationSee belowSnackbar.LENGTH_SHORTHow long to display the Snackbar.
numberOfLinesnumber2The max number of text lines to allow before ellipsizing.
marginBottomnumber0Margin from bottom.
textColorstring or style'white'The color of the message text.
backgroundColorstring or styleundefined (dark gray)The background color for the whole Snackbar.
fontFamilystringundefined[Android only] The basename of a .ttf font from assets/fonts/ (see setup guide and example app, remember to react-native link after).
rtlbooleanfalse[Android only, API 17+] Whether the Snackbar should render right-to-left (requires android:supportsRtl="true", see setup guide and example app).
actionobject (described below)undefined (no button)Optional config for the action button (described below).

Where duration can be one of the following (timing may vary based on device):

  • Snackbar.LENGTH_SHORT (just over a second)
  • Snackbar.LENGTH_LONG (about three seconds)
  • Snackbar.LENGTH_INDEFINITE (stays on screen until dismissed, replaced, or action button is tapped)

The optional action object can contain the following options:

KeyData typeDefault value?Description
textstringRequired.The button text.
textColorstring or style'white'The color of the button text.
onPressfunctionundefined (Snackbar is simply dismissed)A callback for when the user taps the button.

Deprecation note: The old keys title and color have been replaced by text and textColor for consistency. The old keys will continue to work for now but are deprecated and may be removed at any time.

Snackbar.dismiss()

Dismisses any existing Snackbars.

Advanced usage

Snackbar events

You can have information on snackbar visibility.

  componentDidMount() {
    const SnackbarEventEmitter = new NativeEventEmitter(
      NativeModules.RNSnackbar,
    );
    this.eventListener = SnackbarEventEmitter.addListener('onSnackbarVisibility', (event) => {
      console.log(event.event);
    });
  }

  componentWillUnmount() {
    this.eventListener.remove();
  }

Or, with functional components:

  useEffect(() => {
    const subscription = new NativeEventEmitter(
      NativeModules.RNSnackbar,
    ).addListener('onSnackbarVisibility', event => {
      console.log(event.event);
    });
    return () => {
      subscription.remove();
    };
  }, []);

Where event is one of the following options :

KeyData typeValueDescription
Snackbar.DISMISS_EVENT_SWIPEnumber0Indicates that the Snackbar was dismissed via a swipe.
Snackbar.DISMISS_EVENT_ACTIONnumber1Indicates that the Snackbar was dismissed via an action click.
Snackbar.DISMISS_EVENT_TIMEOUTnumber2Indicates that the Snackbar was dismissed via a timeout.
Snackbar.DISMISS_EVENT_MANUALnumber3Indicates that the Snackbar was dismissed via Snackbar.dismiss() call.
Snackbar.DISMISS_EVENT_CONSECUTIVEnumber4Indicates that the Snackbar was dismissed from a new Snackbar being shown.
Snackbar.SHOW_EVENTnumber5Indicates that Snackbar appears

Mocking via jest

This package uses NativeModules, which jest does not have access to.

You can use the provided mocks in your jest.setup.js:

import mockSnackbar from 'react-native-snackbar/jest/snackbar-mock.js'

jest.mock('react-native-snackbar', () => mockSnackbar);

Troubleshooting

Snackbar not appearing [Android]

The Snackbar is designed to attach to whatever view is on top of your screen when show is called. If that view happens to be a temporary alert modal or some other view that goes away, you'll never see the Snackbar.

A workaround in some cases is to use setTimeout to show the Snackbar a few seconds later after the modal is gone. See issue #28 for further discussion. If you want to submit a PR to improve the view-finding logic, feel free.

Undefined import

If you see errors similar to Cannot read property 'LENGTH_LONG' of undefined or Undefined not an object (NativeModules.RNSnackbar), please refer to issue #43 for help.

Compiling [Android]

If you have issues compiling for Android after linking this library, please try updating your Gradle and Android configs to the latest versions. For example:

In your android/build.gradle:

  • com.android.tools.build:gradle:3.4.1 (or higher)

In your android/app/build.gradle:

  • compileSdkVersion 28 (or higher)
  • buildToolsVersion "28.0.3" (or higher)
Compiling [iOS]

Make sure your Deployment Target is iOS 9.0 or above.

Software development

If you want to help contribute to this library, here are local setup steps:

  1. Clone this git repo
  2. Install main dependencies: yarn install
  3. Set up the example app too: cd example && yarn install
  4. Within the example directory, react-native run-android to run it

The example app will update automatically when changing JS code. To see your changes in the example app after updating native library code, reinstall it via:

  1. yarn add file:.. && react-native run-android in the example directory
  2. Type "rr" in the app to trigger a reload

Keywords

FAQs

Package last updated on 26 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc