Home

Awesome

React-awesome-notifications

npm GitHub license npm Storybook style: styled-components

A beautiful fully customizable React + Redux notification System built with styled-components

Table of contents

Demo

Check out the demo

Installation

using npm

npm install react-awesome-notifications --save

using yarn

yarn add react-awesome-notifications

Integration

Follow this 4 steps to integrate react-awesome-notifications to your application.

Integrate NotificationsSystem React component

Render this component at the root of your web application to avoid position conflicts.

import React, {Component} from 'react';
import NotificationsSystem from 'react-awesome-notifications';

class ATopLevelComponent extends Component {
  render() {
    return (
      <div>
        <NotificationsSystem />
      </div>
    );
  }
}

Apply thunk middleware and add notifications reducer to Redux store

  1. Since react-awesome-notifications use thunk async actions creator, you must apply thunk middleware from redux-thunk to your Redux store. Install it with npm install --save redux-thunk.
  2. Add notifications reducer as notifications to your root reducer.
import {createStore, combineReducers, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import {Provider} from 'react-redux';

import {
  reducer as notificationsReducer,
} from 'react-awesome-notifications';

const reducers = combineReducers({
  // notifications reducer must be 'notifications'
  notifications: notificationsReducer,
  // your reducers
  {...}
});

const store = createStore(reducers, applyMiddleware(thunk));

notice: reducer must be mounted as notifications

Usage

In a React component

If you are not familiar with react-redux library or the way to connect a React component with a Redux store, I recommend you to read Redux documentation - Usage with React to understand this example.

import React, {Component} from 'react';
import {connect} from 'react-redux';
// 1. we import `addNotification` (thunk action creator)
import { addNotification } from 'react-awesome-notifications';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    // 4. don't forget to bind method
    this._onClick = this._onClick.bind(this);
  }

  _onClick() {
    const {addNotification} = this.props;
    // 3. we use `addNotification` to create a notification
    addNotification({
      title: 'Welcome',
      message: 'you clicked on the button',
      level: 'success',
      dismissible: true,
      dismissAfter: 3000
    });
  }

  render() {
    return (
      <div>
        // 5. we notify user when he click on the button
        <button onClick={this._onClick}>Add a notification</button>
      </div>
    );
  }
}
// 2. we map dispatch to props `addNotification` async action creator
//    here we use a shortcut instead of passing a `mapDispathToProps` function
export default connect(null, {addNotification})(MyComponent);

In a Redux async action creator

If you are not familiar with async actions creator, I recommend you to read Redux documentation - Async actions to understand this example.

// 1. we import `notify` (thunk action creator)
import {notify} from 'react-awesome-notifications';

// we add a notification to inform user about
// state of his request (success or failure)
const sendResetPasswordLink = (props) => (dispatch) => {
    axios.post('https://api.example.com/users/ask-reset-password', props)
      .then((res) => {
        // 2. we use `dispatch` to notify user.
        dispatch(notify({message: res.data.detail, level: 'success'}));
      })
      .catch((res) => {
       // 3. same thing here
        dispatch(notify({message: res.data.detail, level: 'error'}));
      });
    };
};

API Documentation

Objects

Notification

PropertyTypeDefaultDescription
idString or NumberID of the notification. If not provided during creation, will be generated automatically using the universal id.
titleStringTitle of the notification
messageStringMessage of the notification
levelStringprimarylevel of the notification, available options : primary, info, success, warning, error.
positionStringtrPosition of the notification, available options : tc, tl, tr, bc, br, bl.
dismissibleBooleantrueDefine if a notification is dismissible by clicking on it
dismissAfterNumber5000Time before the notification disappear (ms). Paused when mouse is hovering the notification. 0: infinite.
closeButtonBooleanfalseDisplay a close button if it is dismissible
buttonsArrayArray of button objects.
onMountedFunctionFunction executed at component lifecycle : componentDidMount
onUnmountedFunctionFunction executed at component lifecycle : componentWillUnmount
allowHTMLBooleanfalseAllow HTML in title and message of the notification
extendStylesObjectadd custom styles to the notification see. ExtendStyles.

Notification button

PropertyTypeDefaultDescription
labelStringTitle of the button
actionFunctionFunction executed when user click on it

ExtendStyles

PropertyTypeDefaultDescription
notificationWrapperStringAdd custom styles for the notification wrapper
notificationTitleStringAdd custom styles for the notification title
notificationMessageStringAdd custom styles for the notification message
notificationButtonsContainerStringAdd custom styles for the notification buttons container
notificationButtonStringAdd custom styles for the notification button

Action creators

Update or create a notification

Updates a notification if it does exist or creates it. It returns the notification just updated or created. You basically want to use this function all the time to update and create notifications.

Syntax
notify(notification);
Parameters
ParameterTypeDescription
notificationObjectA notification object
Example
// add a notification
let notif = notify({
  title: 'Upload status',
  message: 'Your file is uploading...',
  level: 'info',
  dismissible: false,
  dismissAfter: 0
});

// simulate file upload
setTimeout(function() {
  notif.level = 'success';
  notif.message = 'Your file has been successfully uploaded';
  notif.dismissible = true;
  notif.dismissAfter = 5000;
  // update the notification
  notify(notif);
}, 10000);

Create a notification

Creates a notification and returns it.

Syntax
addNotification(notification);
Parameters
ParameterTypeDescription
notificationObjectA notification object
Example
const notif = addNotification({
  title: 'Welcome on demo!',
  message: 'Hey buddy, here you can see what you can do with it.',
  position: 'br',
  level: 'info',
  dismissAfter: 10000,
  dismissible: false,
  onMounted: function() {
    console.log('hey buddy');
  },
  onUnmounted: function() {
      console.log('cya buddy');
  },
  buttons:[{
    label: 'OK',
    action: () => {
      console.log('i\'m OK too');
    }
  }] 
});
console.log(JSON.stringify(notif));
/*
{
  "id":1463345312016,
  "title":"Welcome on demo!",
  "message":"Hey buddy, here you can see what you can do with it.",
  "position":"br",
  "level":"info",
  "dismissAfter":10000,
  "dismissible":false,
  "buttons":[{
    "label":"OK",
  }]
}
*/

Update a notification

Updates a notification and returns it. If the notification has been removed, it will not display it again.

Syntax
updateNotification(notification);
Parameters
ParameterTypeDescription
notificationObjectA notification object
Example
let notif = addNotification({
  title: 'Upload status',
  message: 'Your file is uploading...',
  level: 'info',
  dismissible: false,
  dismissAfter: 0
});

// simulate file upload
setTimeout(function() {
  notif.level = 'success';
  notif.message = 'Your file has been successfully uploaded';
  notif.dismissible = true;
  notif.dismissAfter = 5000;
  updateNotification(notif);
}, 10000);

Remove a notification

Removes a notification.

Syntax
removeNotification(id);
Parameters
ParameterTypeDescription
idString or NumberID of the notification

Remove all notifications

Removes all notifications.

Syntax
removeAllNotifications();

Notifications system component

PropertyTypeDefaultDescription
filterFunctionnullFilter notifications to display. Can be useful to display some notifications with another component, like modal notifications or banner notifications.

Example

<NotificationsSystem 
  filter={notif => notif.style !== 'banner'} 
/>

Customize styling

you can add custom styles using ExtendStyles property . because this package uses styled components for apply styles to the final ui so you can use the power of styled components.

Example usage of the ExtendStyles property

import { notify } from 'react-awesome-notifications';

let not = {
  id: 123,
  title: 'my title',
  message: 'my message',
  buttons: [
    {
      label: 'submit'
    }
  ],
  // custom styles
  extendStyles: {
    // customize styles for the notification wrapper
    // features: & parent placeholder, nesting, nesting classes..., any feature of styled-components
    notificationWrapper: `
      background-color: pink;
      padding: 20px;
      
      .notification-close-btn{
        .close-btn{
          padding: 10px;
          
          &:hover{
            color: red;
          }
        }
      }
    `,
    notificationTitle: ``;
    notificationMessage: ``;
    notificationButtonsContainer: ``;
    notificationButton: ``;
  }
};

Final rendred markup

<div class="notification-wrapper">
        <div className='container'>
          <div className="notification-content">
              <div>
                 {title}
              </div>
              <div>
                 {message}
               </div>
          </div>
          <div className='notification-close-btn'>
             <span className="close-btn">&times;</span>
          </div>
          <div class='notification-button'>
            <span className="btn-text">{button.label}</span>
          </div>
        </div>
</div>

Contributing guide

All kinds of contributions (enhancements, new features, stories, documentation & bugs reporting) are welcome.

Setting up your environment

# clone repository
https://github.com/zakariaharti/react-awesome-notifications.git

cd react-awesome-notifications

# install dependencies

** yarn **
yarn install

** npm **
npm install

# Launch demo at http://localhost:6006
npm start

Here is the list of the available npm scripts :

npm run <script>Description
startLaunch demo at http://localhost:6006
webpackCompile and lint code using the development env
lintLint javascript files of source folder (src)
testRun tests with jest
test:coverageRun tests with jest and generate coverage report (HTML and LCOV)
buildrun build for production env
storybookstart storybook server at http://localhost:6006
build-storybookbuild stories and generate a static files ready for deployment

Example :

npm run compile

Pull requests

All pull requests must be done on the master branch.

Before a pull request :

License

React-awesome-notifications is under MIT License