2.5.1 • Published 3 years ago

react-toast-notifications v2.5.1

Weekly downloads
64,716
License
MIT
Repository
github
Last release
3 years ago

🚨 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

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'.
PropertyDescription
autoDismissTimeout numberDefault 5000. The time until a toast will be dismissed automatically, in milliseconds.
autoDismiss booleanDefault: false. Whether or not to dismiss the toast automatically after a timeout.
children NodeRequired. Your app content.
components { ToastContainer, Toast }Replace the underlying components.
newestOnTop booleanDefault false. When true, insert new toasts at the top of the stack.
placement PlacementTypeDefault top-right. Where, in relation to the viewport, to place the toasts.
portalTargetSelector stringWhich element to attach the container's portal to. Uses document.body when not provided.
transitionDuration numberDefault 220. The duration of the CSS transition on the Toast component.

Toast Props

PropertyDescription
appearanceUsed by the default toast. One of success, error, warning, info.
childrenRequired. The content of the toast notification.
autoDismiss booleanInherited from ToastProvider if not provided.
autoDismissTimeout numberInherited from ToastProvider.
onDismiss: Id => voidPassed in dynamically. Can be called in a custom toast to dismiss it.
placement PlacementTypeInherited from ToastProvider.
transitionDuration numberInherited from ToastProvider.
transitionState: TransitionStatePassed 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:

react-interactive-payment-formannotesttannotweannotateimagetestannotatestimgeswarlibtestabbtestiaageneric-image-annotationleihuo-cloud-game-reactquantic-uimymoria-ui@equipo.nutrix/nutrix-componentsintegrador-hgfpayfacit-litecope-spareact-webpack-babel-sass-boilerplate@infinitebrahmanuniverse/nolb-react-to@pixiebrix/extensionsimplest-analytics@everything-registry/sub-chunk-2618@itoa/app-admin-ui@itoa/fields@itoa/fields-location-googleweb-search-gatewayemotion-packagereal-estate-uireact-transaction-toasts@castcloudfm/playerproptoken@contentpi/ui-kit@builtbypixel/nucleus-core@builtbypixel/plasma@britishlibrary/component-librarynickhsine-k5-fieldsnucleus-core@asany/startp@davidsemakula/keystone-app-admin-ui@davidsemakula/keystone-fields@drivekyte/ui-components@drivekyte/web-components@alecortega/design-systemetherface@defencedigital/react-component-library@folk-org/folk-components@equipo.nutrix/components@falsy/ui@recidiviz/case-triage-components@recidiviz/design-system@signallabs/admin@overflow-ui/react@ocopjs/app-admin-ui@ocopjs/fields@stubee2/stubee2-rolling-toastify@wiplant/lib-front@workhard/react-core@workhard/react-sdk@workhard/react-utils@sentecacommerce-theme/clean-theme@sentecacommerce-theme/hippo-theme@sentecacommerce-theme/madame-coco-theme@sentecacommerce-theme/office-one-theme@sentecacommerce-theme/packlion-theme@sentecacommerce-theme/samsung-theme@sentecacommerce-theme/balevbio-theme@sentecacommerce-theme/byfar-mobile-theme@sentecacommerce-theme/byfar-showroom-theme@sentecacommerce-theme/byfar-theme@sentecacommerce-theme/christian-of-roma-theme@seragam-ui/toast@tithon/app-admin-ui@tithon/keystonefields@voussoir/admin-uianeop-componentsaneop-management@types/react-toast-notifications@unifiprotocol/shell@tssbpchannel/bpmsutilsteam20keystone-admin-ui-translationtommy-testgatsby-theme-medusa-adminuseform-formbuilderhand_action_detectwao-react-shared-componentsit-custom-admin-ui-appweb-builder-jobuit@invisible/componentsme.easymove.web@hojicha/frontendm7kvqbe1-react-component-librarynascent-hummingbird@lightspeed/flame@liyibass/admin-ui@liyibass/app-admin-ui@liyibass/keystonefields@lambdacurry/gatsby-theme-medusa-admin@keystonejs/fields-location-google@keystonejs/fields@keystonejs/app-admin-ui
2.5.1

3 years ago

2.5.0

3 years ago

2.4.4

3 years ago

2.4.1

3 years ago

2.4.3

3 years ago

2.4.2

3 years ago

2.3.0

4 years ago

2.4.0

4 years ago

2.2.6

4 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago