1.0.0 • Published 6 years ago

@freecodecamp/react-notification v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

react-notification

npm version Dependency Status Build Status npm downloads

Overview

npm.io npm.io

This is a component designed to provide "snackbar" notification messages and notification stacks (similar to how notifications stack on OS X). I would suggest reading the usage guidelines for snackbars.

Getting Started

Install the component via npm: npm install react-notification.

If you are using the React 0.13.x or lower, you can install the previously compatible version of this component with npm i react-notification@2.3.0 -S. The current version only works with React 0.14.x.

Please read the contributing guide if you are interested in contributing. If you are coming from version 1.0.0, there is an upgrade guide to help you make the switch. If you have questions, please feel free to create an issue on GitHub.

Note the component uses Object.assign. If you are compiling with Babel, you should include the Babel Polyfill in your build to ensure the method works.

Usage

Single notification:

import { Notification } from 'react-notification';

<Notification
  isActive={boolean}
  message={string}
  action={string}
  onClick={myClickHander}
/>

Notification stack:

import { NotificationStack } from 'react-notification';
import { OrderedSet } from 'immutable';

constructor () {
  super();
  this.state = {
    notifications: OrderedSet()
  };
}

addNotification () {
  const newCount = count + 1;
  return this.setState({
    notifications: this.state.notifications.add({
      message: `Notification ipsum...`,
      key: 'some UID',
      action: 'Dismiss',
      onClick: () => this.removeNotification('some UID'),
    })
  });
}

removeNotification (count) {
  this.setState({
    notifications: this.state.notifications.filter(n => n.key !== count)
  })
}

render () {
  return (
    <NotificationStack
      notifications={this.state.notifications.toArray()}
      onDismiss={notification => this.setState({
        notifications: this.state.notifications.delete(notification)
      })}
    />
  );
}

See the examples for more context on how to use a notification stack.

Props

For Notification component:

NameTypeDescriptionRequiredDefault
isActivebooleanIf true, the notification is visibletruefalse
messagestring or React elementThe message or component for the notificationtrue
titlestringThe title for the notification
actionstringThe name of the action, e.g., "close" or "undo"
stylebooleanSetting this prop to false will disable all inline styles
barStyleobjectCustom snackbar styles
activeBarStyleobjectCustom snackbar styles when the bar is active
actionStyleobjectCustom action styles
classNamestringCustom class to apply to the top-level component
activeClassNamestringCustom class to apply to the top-level component when active'notification-bar-active'
dismissAfternumberTimeout for onDismiss event2000

The style prop useful if you are not using React inline styles and would like to use CSS instead. See styles for more.

For NotificationStack component:

NameTypeDescriptionRequiredDefault
notificationsarrayArray of notifications to rendertrue
barStylefunccreate the style of the notificationfalsefn
activeBarStylefunccreate the style of the active notificationfalsefn

Update v5.0.3: Now notifications used in a stack can have all properties included in the regular notification component.

Events

For Notification component:

EventDescription
onClickCallback function to run when the action is clicked
onDismissCallback function to run when dismissAfter timer runs out

For NotificationStack component:

EventDescriptionArguments
onDismissCallback function to run when dismissAfter timer runs out for a notificationThe object for the notification currently being dismissed

Styles

This component does use basic inline CSS to style the position and visibility of the notification. You have two options for adding additional styles:

  1. Remove all inline styles and use only CSS.
  2. Add additional inline styles via the style prop.

The DOM tree of the component for reference:

<div class="notification-bar">
  <div class="notification-bar-wrapper" onClick={this.props.onClick}>
    <span class="notification-bar-message">{this.props.message}</span>
    <span class="notification-bar-action">{this.props.action}</span>
  </div>
</div>

To use additional inline styles, return two objects. The bar object applies styles to the entire notification "snackbar" and the action object applies styles to the action message. Under the hood, this uses Object.assign to handle properly combining styles.

I would highly suggest using this method since the styles included in the component by default handle the visibility of the notification. If you remove these styles, the component won't actually show or hide itself.

barStyle and activeBarStyle NotificationStack props

These two function have the following signature:

(index: Number, style: Object|Void) => Object

Where index is the index of the notifiction in the notifictions array and style is the style property of the individual notification.

This function is used to dynamically set the style of each notification in the stack. The default function adds the bottom style property to correctly position of the notification in a stack.

function defaultStyleFactory(index, style) {
  return Object.assign(
    {},
    style,
    { bottom: `${2 + index * 4}rem` }
  );
}

Built with care in New Orleans by Patrick Burtchaell.

Copyright 2016 Patrick Burtchaell. Licensed MIT. Gratipay.