react-notifications-component

Highly customisable React component to show UI notifications

Github星跟踪图

npm version npm Minified & Gzipped size

react-notifications-component

A delightful, easy to use and highly configurable component to help you notify your users out of the box. No messy setup, just beautiful notifications!

Demo

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

alt text

Features

  • Touch support
  • Responsive notifications
  • Standard notification types
  • Custom notification types
  • Custom notification content
  • Dismissable (touch, click, timeout)
  • Customizable transitions
  • Small library

Getting started

npm install react-notifications-component

Development

First build the library

npm run build:library:dev

then run the webpack server to see the app running

npm run start

Usage

Import react-notifications-component

import ReactNotification from 'react-notifications-component'

Import the CSS theme

import 'react-notifications-component/dist/theme.css'
SASS

SASS files are located in react-notifications-component/dist/scss

Render react-notifications-component at the top of your application so that it does not conflict with other absolutely positioned DOM elements.

const App = () => {
  return (
    <div className="app-container">
      <ReactNotification />
      <Application />
    </div>
  )
};

Import store where needed - will be used to access addNotification and removeNotification API methods

import { store } from 'react-notifications-component';

Then call addNotification and watch the magic happens

store.addNotification({
  title: "Wonderful!",
  message: "teodosii@react-notifications-component",
  type: "success",
  insert: "top",
  container: "top-right",
  animationIn: ["animated", "fadeIn"],
  animationOut: ["animated", "fadeOut"],
  dismiss: {
    duration: 5000,
    onScreen: true
  }
});

Voila!

Note: We rely on animate.css in this example as fadeIn and fadeOut are part of animate.css. We recommend using it as it's an excellent animation library, but you're not forced to. If you prefer you may also use your custom animations as long as they're valid CSS animations.

API

store.addNotification(options)

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

store.removeNotification(id)

Manually remove a notification by id.

Examples

In the following examples for brevity some options will not be mentioned. Strictly focusing on the needed options to present each example. For reference, we will be using Object spread operator on a notification object to have non relevant fields included as well.

notification = {
  title: "Wonderful!",
  message: "Configurable",
  type: "success",
  insert: "top",
  container: "top-right",
  animationIn: ["animated", "fadeIn"],
  animationOut: ["animated", "fadeOut"]
};

Notification container

You have in total 6 containers for desktop and 2 for mobile, if component is set to be responsive. List of containers:

  • top-left
  • top-right
  • top-center
  • bottom-left
  • bottom-right
  • bottom-center
store.addNotification({
  ...notification,
  container: 'top-right'
})

Will position the notification in top right of the screen.

Notification type

List of types:

  • success
  • danger
  • info
  • default
  • warning
store.addNotification({
  ...notification,
  type: 'danger'
})

Will trigger a danger notification.

Animating

store.addNotification({
  ...notification,
  animationIn: ['animated', 'fadeIn'],
  animationOut: ['animated', 'fadeOut']
})

animationIn and animationOut rely on CSS classes that toggle animations. On github pages we rely on animate.css, we suggest you to import that package and use their animations as they have plenty.

Note: Failing to have animations set properly will lead to bugs in some causes, as react-notifications-component relies on onAnimationEnd event to know when an animation has finished.

Dismiss notification automatically after timeout expires

store.addNotification({
  ...notification,
  dismiss: {
    duration: 2000
  }
})

Dismiss notification automatically with the time left shown on UI

store.addNotification({
  ...notification,
  dismiss: {
    duration: 2000,
    onScreen: true
  }
})

Subscribe to notification's removal

Easily subscribe to onRemoval by supplying callback as option to the notification object. Callback will get called after the removal animation finishes.

store.addNotification({
  ...notification,
  onRemoval: (id, removedBy) => {
    ...
  }
})

Pause notification's timeout by hovering

store.addNotification({
  ...notification,
  dismiss: {
    duration: 2000,
    pauseOnHover: true
  }
})

Change transition

store.addNotification({
  ...notification,
  slidingExit: {
    duration: 800,
    timingFunction: 'ease-out',
    delay: 0
  }
})

slidingEnter, touchRevert and touchSlidingExit can all be configured in the same way, with the mention that touchSlidingExit has 2 transitions nested.

store.addNotification({
  ...notification,
  touchSlidingExit: {
    swipe: {
      duration: 400,
      timingFunction: 'ease-out',
      delay: 0,
    },
    fade: {
      duration: 400,
      timingFunction: 'ease-out',
      delay: 0
    }
  }
})

Props

Options

Transition

Transition is used each time you define a transition.

Dismiss

Dismiss is used to describe how a notification should be dismissed.

Migration from v1

  • Ref usage has been deprecated. Import store from library and use it for adding and removing notifications
  • touchSlidingBack has been renamed to touchRevert
  • Default values for transitions have been slightly changed
  • dismissIcon has been removed. Use showIcon instead. If you relly on customized close icon, then stick to custom content.
  • dismiss supports now more options
  • cubicBezier has been renamed to timingFunction
  • Validators are now no longer included in the prod build, they are only included in the dev build. If you inspect the npm package you will see that the component has 2 builds - dev and prod - and relies on ENV variable when importing.

License

MIT

主要指标

概览
名称与所有者teodosii/react-notifications-component
主编程语言TypeScript
编程语言JavaScript (语言数: 3)
平台
许可证MIT License
所有者活动
创建于2018-06-24 18:43:40
推送于2023-08-28 15:14:57
最后一次提交2022-06-21 10:44:52
发布数8
最新版本名称3.3.3 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数1.3k
关注者数11
派生数77
提交数349
已启用问题?
问题数125
打开的问题数12
拉请求数19
打开的拉请求数6
关闭的拉请求数23
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?