1.0.2 • Published 6 years ago

react-toast-bs v1.0.2

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

React Toast Notifications

A configurable, composable, toast notification system for react.

https://jossmac.github.io/react-toast-notifications

Install

yarn add react-toast-notifications

Use

Wrap your app in the ToastProvider, which provides context for the Toast descendants.

import { ToastConsumer, ToastProvider, withToastManager } from 'react-toast-notifications';
import validate from 'some-validation-lib';

class FormComponent extends React.Component {
  onSubmit = (value) => {
    const { toastManager } = this.props;
    const { error, success } = validate(value);

    if (error) {
      toastManager.add(`Something went wrong: "${error.message}"`, {
        appearance: 'error',
      });
    } else if (success) {
      toastManager.add('Saved Successfully', { appearance: 'success' });
    }
  };
  render() {
    return <form onSubmit={this.onSubmit}>...</form>;
  }
}

// wrap your component to pass in the `toastManager` prop
const FormWithToasts = withToastManager(FormComponent);

const App = () => (
  <ToastProvider>
    <FormWithToasts />

    {/* or if render props are more your speed */}
    <ToastConsumer>{({ addToast }) => (
      <button onClick={(e) => addToast(`Notified by ${e.target}`)}>
        Toasty
      </button>
    )}</ToastConsumer>
  </ToastProvider>
);

ToastProvider Props

For brevity:

  • PlacementType is equal to 'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right'.
  • TransitionState is equal to 'entering' | 'entered' | 'exiting' | 'exited'.
PropertyDescription
autoDismissTimeout numberDefault 5000. The time until a toast will be dismissed automatically, in milliseconds.
children NodeRequired. Your app content.
components { ToastContainer, Toast }Replace the underlying components.
placement PlacementTypeDefault top-right. Where, in relation to the viewport, to place the toasts.
transitionDuration numberDefault 220. The duration of the CSS transition on the Toast component.

Toast Props

PropertyDescription
childrenRequired. The content of the toast notification.
autoDismiss booleanWhether or not to dismiss the toast automatically after a timeout.
autoDismissTimeout numberInherited from ToastProvider.
onDismiss: Event => anyPassed in dynamically.
placement PlacementTypeInherited from ToastProvider.
transitionDuration numberInherited from ToastProvider.
transitionState: TransitionStatePassed in dynamically.

Add & Remove

The add and remove methods on ToastManager have three arguments.

  1. The first is the content of the toast, which can be any renderable Node.
  2. The second is the Options object, which can take any shape you like. When departing from the default shape, you must provide an alternative, compliant Toast component.
  3. The third is a callback, which is passed the added or removed toast ID.

Replaceable Components

To bring each toast notification inline with your app, you can provide alternative components to the ToastProvider:

import { ToastProvider } from 'react-toast-notifications';

const MyCustomToast = ({ appearance, children }) => (
  <div style={{ background: appearance === 'error' ? 'red' : 'green'}}>
    {children}
  </div>
);

const App = () => (
  <ToastProvider components={{ Toast: MyCustomToast }}>
    ...
  </ToastProvider>
);

Alternatives

This library may not meet your needs. Here are some alternative I came across whilst searching for this solution:

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago