Highly configurable and easy to use React Component to notify your users

Highly configurable and easy to use React Component to notify your users

Highly configurable and easy to use React Component to notify your users!

npm version npm Build Status Coverage Status Dependency Status Minified & Gzipped size License

react-notifications-component

Highly configurable and easy to use React component to notify your users!

Demo

https://teodosii.github.io/react-notifications-component/

alt text

Features

Touch support :white_check_mark:
Responsive notifications :white_check_mark:
Standard notification types
(default, success, info, danger, warning)
:white_check_mark:
Custom notification types :white_check_mark:
Custom notification content
(images, icons etc)
:white_check_mark:
Dismiss after timeout :white_check_mark:
Dismissable by swiping :white_check_mark:
Dismissable by clicking :white_check_mark:
Dismissable by custom × icon :white_check_mark:
Custom animations on show :white_check_mark:
Custom animations on exit :white_check_mark:
Custom transitions on sliding :white_check_mark:
Custom transitions on swiping :white_check_mark:
Notification insertion at both ends of container :white_check_mark:

Install

npm install react-notifications-component

Usage

You must place ReactNotificationsComponent component at the root level of the application in order to work properly, otherwise it might conflict with other DOM elements due to the positioning.

Use ref syntax when declaring ReactNotificationsComponent in order to have access to internal method addNotification. All API methods provided must be called like this.

For further information on supported options, check documentation.

import React from "react";
import ReactNotification from "react-notifications-component";
import "react-notifications-component/dist/theme.css";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.addNotification = this.addNotification.bind(this);
    this.notificationDOMRef = React.createRef();
  }

  addNotification() {
    this.notificationDOMRef.current.addNotification({
      title: "Awesomeness",
      message: "Awesome Notifications!",
      type: "success",
      insert: "top",
      container: "top-right",
      animationIn: ["animated", "fadeIn"],
      animationOut: ["animated", "fadeOut"],
      dismiss: { duration: 2000 },
      dismissable: { click: true }
    });
  }

  render() {
    return (
      <div className="app-content">
        <ReactNotification ref={this.notificationDOMRef} />
        <button onClick={this.addNotification} className="btn btn-primary">
          Add Awesome Notification
        </button>
      </div>
    );
  }
}

Note: It is important to import react-notifications-component CSS theme, which is located in dist/theme.css

Development

First build the library

npm run build:library:dev

then run the webpack server to see the app running

npm run start

Test

npm run test

API

addNotification(options)

Render a new notification. Method returns a unique ID representing the rendered notification. Supplied options are internally validated and an exception will be thrown if validation fails.

removeNotification(id)

Manually remove a notification by ID. Nothing will happen if notification does not exist.

Examples

View examples here and here

Options

There are 2 types of options. In order to configure the root component - <ReactNotificationComponent /> - you need to set props to be used as options

Name Type Description
isMobile Boolean Set whether you want component to be responsive or not
breakpoint Number Breakpoint for showing mobile notifications, it defaults to 768 (px). If window width is smaller than set number, then the responsive containers will be shown - top and bottom
types Object User defined types - see examples on GitHub pages
onNotificationRemoval Function Callback function to be called when notification has been removed. Function is called with id and removedBy as parameters. removedBy parameter takes one of the following values
  • 1 - timeout
  • 2 - click
  • 3 - touch
  • 4 - API call

In order to configure the notification itself you need to use the following properties when calling addNotification

Name Type Description
id String Id of the notification. Option is not required and should be used only if you want to have custom id over random id that is generated internally
title String Title of the notification. Option is ignored if content is set
message String Message of the notification. Option is ignored if content is set, otherwise it is required
content React.Component Custom notification content, must be a valid React component
type String Type of the notification (success, danger, default, info, warning or custom). Option is ignored if content is set, otherwise it is required
container String Container in which the notification will be displayed (top-left, top-right, bottom-left, bottom-right). Option is required
insert String Insert notification at the top or at the bottom of the container. Option defaults to top
userDefinedTypes Array Define allowed types when rendering custom types
  • htmlClasses - Array - CSS classes to be applied to the notification element
  • name - String - name of the custom type
dismissable Object Specify how a notification should be manually dismissed
  • click - Boolean - dismiss by clicking (option defaults to true)
  • touch - Boolean - dismiss by swiping on mobile devices (option defaults to true)
dismissIcon Object Custom X icon
  • className - Array - CSS classes to be applied to icon's parent
  • content - React.Component - must be a valid React component
animationIn Array CSS classes used to animate notification on show
animationOut Array CSS classes used to animate notification on removal
slidingEnter Object Transition to be used when sliding to show a notification
  • duration - Number (ms)
  • cubicBezier - String
  • delay - Number (ms)
slidingExit Object Transition to be used when sliding to hide a notification
  • duration - Number (ms)
  • cubicBezier - String
  • delay - Number (ms)
touchSlidingBack Object Transition to be used when sliding back after an incomplete swipe
  • duration - Number (ms)
  • cubicBezier - String
  • delay - Number (ms)
touchSlidingExit Object Transition to be used when sliding on swipe
  • duration - Number (ms)
  • cubicBezier - String
  • delay - Number (ms)
dismiss Object Automatically dismiss a notification after specified timeout
  • duration - Number (ms - 0 means Infinite)
width Number Overwrite notification's width defined by stylesheets

Github Repository