• Stars
    star
    2,172
  • Rank 21,231 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍞 A toast notification system for react

🚨 Not Maintained

This was a great project to learn from and fulfilled the requirements it set out to. Unfortunately, I can no-longer give this project the time it needs. Consider react-hot-toast as an alternative, or look at the source and make your own 🎉 (there really isn't much to it).

No Maintenance Intended


react-toast-notifications

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 { ToastProvider, useToasts } from 'react-toast-notifications';

const FormWithToasts = () => {
  const { addToast } = useToasts();

  const onSubmit = async value => {
    const { error } = await dataPersistenceLayer(value);

    if (error) {
      addToast(error.message, { appearance: 'error' });
    } else {
      addToast('Saved Successfully', { appearance: 'success' });
    }
  };

  return <form onSubmit={onSubmit}>...</form>;
};

const App = () => (
  <ToastProvider>
    <FormWithToasts />
  </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'.
Property Description
autoDismissTimeout number Default 5000. The time until a toast will be dismissed automatically, in milliseconds.
autoDismiss boolean Default: false. Whether or not to dismiss the toast automatically after a timeout.
children Node Required. Your app content.
components { ToastContainer, Toast } Replace the underlying components.
newestOnTop boolean Default false. When true, insert new toasts at the top of the stack.
placement PlacementType Default top-right. Where, in relation to the viewport, to place the toasts.
portalTargetSelector string Which element to attach the container's portal to. Uses document.body when not provided.
transitionDuration number Default 220. The duration of the CSS transition on the Toast component.

Toast Props

Property Description
appearance Used by the default toast. One of success, error, warning, info.
children Required. The content of the toast notification.
autoDismiss boolean Inherited from ToastProvider if not provided.
autoDismissTimeout number Inherited from ToastProvider.
onDismiss: Id => void Passed in dynamically. Can be called in a custom toast to dismiss it.
placement PlacementType Inherited from ToastProvider.
transitionDuration number Inherited from ToastProvider.
transitionState: TransitionState Passed in dynamically.

Hook

The useToast hook has the following signature:

const {
  addToast,
  removeToast,
  removeAllToasts,
  updateToast,
  toastStack,
} = useToasts();

The addToast method has 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. Options.appearance is required when using the DefaultToast. When departing from the default shape, you must provide an alternative, compliant Toast component.
  3. The third is an optional callback, which is passed the added toast ID.

The removeToast method has two arguments:

  1. The first is the ID of the toast to remove.
  2. The second is an optional callback.

The removeAllToasts method has no arguments.

The updateToast method has three arguments:

  1. The first is the ID of the toast to update.
  2. The second is the Options object, which differs slightly from the add method because it accepts a content property.
  3. The third is an optional callback, which is passed the updated toast ID.

The toastStack is an array of objects representing the current toasts, e.g.

[
  {
    content: 'Something went wrong',
    id: 'generated-string',
    appearance: 'error',
  },
  { content: 'Item saved', id: 'generated-string', appearance: 'success' },
];

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>
);

To customize the existing component instead of creating a new one, you may import DefaultToast:

import { DefaultToast } from 'react-toast-notifications';
export const MyCustomToast = ({ children, ...props }) => (
  <DefaultToast {...props}>
    <SomethingSpecial>{children}</SomethingSpecial>
  </DefaultToast>
);

Alternatives

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

More Repositories

1

react-images

🌄 A mobile-friendly, highly customizable, carousel component for displaying media in ReactJS
JavaScript
2,348
star
2

react-scrolllock

🔒 Prevent scroll on the <body />
JavaScript
451
star
3

react-focus-marshal

👮🏻 Traps focus within a DOM node
JavaScript
82
star
4

react-blink

👁 React replacement for the <blink> element
JavaScript
70
star
5

react-radios

🔘 Proper handling of HTML radios in react
JavaScript
60
star
6

react-height-transition

📏 Animate height when children mount/unmount
JavaScript
57
star
7

jade-field-mixins

A collection of Jade mixins for defining form fields. The style and structure follow Bootstrap's form definition. Initially crafted for recreating dashboards using KeystoneJS
CSS
38
star
8

react-scroll-captor

📜 Restrict scroll events to the immediate child
JavaScript
27
star
9

interpolate-range

💯 Super simple numeric range interpolation
JavaScript
15
star
10

react-icons

Simple component for icons in your React app
JavaScript
14
star
11

react-deprecate

⚠️ Rename props with deprecation warning
JavaScript
13
star
12

react-select-simple-value

A simple value wrapper for react-select
JavaScript
12
star
13

react-node-resolver

🔍 Resolve the DOM node of any react component
JavaScript
12
star
14

react-prop-toggle

🕹 Influence nodes outside your app's render tree
JavaScript
7
star
15

react-view-transition

🎟 Animate views as they mount/unmout
JavaScript
6
star
16

react-pseudo-state

🎛 Stateful pseudo-classes in React
JavaScript
5
star
17

less-entypo-icons

A single LESS file for including entypo icons
CSS
4
star
18

react-focuslock

Restrict focus scope to a single element, typically a modal dialog
JavaScript
3
star
19

asser-ts

Assertion functions for TS
TypeScript
3
star
20

adjustable

Adjustable helper utilities for CSS-in-JS
JavaScript
2
star
21

simple-grid

A simple, responsive grid
CSS
1
star
22

react-sronly

🗣 Provide accessible text to Screen Readers
JavaScript
1
star
23

swipe

experiment with touch events
JavaScript
1
star