Reapop

A simple and customizable React notifications system

README

Reapop


npm version npm download/month coveralls status

A simple and customizable React notifications system

Summary



Compatibility


Supported browsers


[[[[[
---------------------------------------------
IE10,lastlastlastlast

Demo


Check out the demo.

Installation


  1. ```
  2. npm install reapop --save
  3. ```

Integration & usage


With React and Redux


1 - Add the notifications reducer to your Redux store.

  1. ``` js
  2. import {combineReducers, createStore} from 'redux'
  3. import {reducer as notificationsReducer} from 'reapop'

  4. const rootReducer = combineReducers({
  5.     notifications: notificationsReducer(),
  6.     ... your other reducers
  7. })
  8. const store = createStore(rootReducer)
  9. ```


2 - Add the NotificationsSystem component to your app. Place this component at the root of your application to avoid position conflicts.

  1. ``` jsx
  2. import React from 'react'
  3. import {useDispatch, useSelector} from 'react-redux'
  4. import NotificationsSystem, {atalhoTheme, dismissNotification} from 'reapop'

  5. const ATopLevelComponent = () => {
  6.     const dispatch = useDispatch();
  7.     // 1. Retrieve the notifications to display.
  8.     const notifications = useSelector((state) => state.notifications)
  9.     
  10.     return (
  11.         <div>
  12.             <NotificationsSystem
  13.                 // 2. Pass the notifications you want Reapop to display.
  14.                 notifications={notifications}
  15.                 // 3. Pass the function used to dismiss a notification.
  16.                 dismissNotification={(id) => dispatch(dismissNotification(id))}
  17.                 // 4. Pass a builtIn theme or a custom theme.
  18.                 theme={atalhoTheme}
  19.             />
  20.         </div>
  21.     )
  22. }
  23. ```

3 - Set default notifications attributes

  1. ``` js
  2. import {setUpNotifications} from 'reapop'

  3. // run this function when your application starts before creating any notifications
  4. setUpNotifications({
  5.     defaultProps: {
  6.         position: 'top-right',
  7.         dismissible: true
  8.     }
  9. })
  10. ```

4 - Upsert or dismiss notification from any React components.

  1. ``` jsx
  2. import React from 'react'
  3. import {useDispatch} from 'react-redux'
  4. // 1. Retrieve the action to create/update a notification, or any other actions.
  5. import {notify} from 'reapop'

  6. const AComponent = () => {
  7.     // 2. Retrieve the function to dispatch an action.
  8.     const dispatch = useDispatch()
  9.     useEffect(() => {
  10.         // 3. Create a notification.
  11.         dispatch(notify('Welcome to the documentation', 'info'))
  12.     }, [])

  13.     return (
  14.         ...
  15.     )
  16. }
  17. ```

5 - Upsert or dismiss notification from Redux actions.

  1. ``` js
  2. // 1. Retrieve the action to create/update a notification.
  3. import {notify} from 'reapop'

  4. const sendResetPasswordLink = () => (dispatch) => {
  5.     axios.post('https://api.example.com/users/ask-reset-password')
  6.         // 2. Create a notification.
  7.         .then((resp) => dispatch(notify(resp.data.detail, 'success'))
  8.         .catch((resp) => dispatch(notify(resp.data.detail, 'error'))
  9.     }
  10. }
  11. ```

With React alone (react >= 16.8.0)


1 - Add the NotificationsProvider at the root of your application.
It is important that this component wraps all the components
where you want to access the notifications and the actions to manipule notifications.

  1. ``` jsx
  2. import React from 'react'
  3. import {NotificationsProvider} from 'reapop'

  4. const ARootComponent = () => {
  5.     return (
  6.         <NotificationsProvider>
  7.             // ... components
  8.         </NotificationsProvider>
  9.     )
  10. }
  11. ```


2 - Add the NotificationsSystem component to your app. Place this component at the root of your application to avoid position conflicts.

  1. ``` jsx
  2. import React from 'react'
  3. import NotificationsSystem, {atalhoTheme, useNotifications} from 'reapop'

  4. const ATopLevelComponent = () => {
  5.     // 1. Retrieve the notifications to display, and the function used to dismiss a notification.
  6.     const {notifications, dismissNotification} = useNotifications()
  7.     return (
  8.         <div>
  9.             <NotificationsSystem
  10.                 // 2. Pass the notifications you want Reapop to display.
  11.                 notifications={notifications}
  12.                 // 3. Pass the function used to dismiss a notification.
  13.                 dismissNotification={(id) => dismissNotification(id)}
  14.                 // 4. Pass a builtIn theme or a custom theme.
  15.                 theme={atalhoTheme}
  16.             />
  17.         </div>
  18.     )
  19. }
  20. ```

3 - Set default notifications attributes

  1. ``` js
  2. import {setUpNotifications} from 'reapop'

  3. // run this function when your application starts before creating any notifications
  4. setUpNotifications({
  5.     defaultProps: {
  6.         position: 'top-right',
  7.         dismissible: true
  8.     }
  9. })
  10. ```

4 - Upsert or dismiss notification from any React components.

  1. ``` jsx
  2. import React from 'react'
  3. import {useNotifications} from 'reapop'

  4. const AComponent = () => {
  5.     // 1. Retrieve the action to create/update a notification.
  6.     const {notify} = useNotifications()
  7.     
  8.     useEffect(() => {
  9.         // 2. Create a notification.
  10.         notify('Welcome to the documentation', 'info')
  11.     }, [])

  12.     return (
  13.         ...
  14.     )
  15. }
  16. ```

Documentation


Read the documentation to learn more and see what you can with it.

License


Reapop is under MIT License