Skip to content

Add beautiful alerts to your React app. Built with Hooks and Context API, animated with react-spring.

Notifications You must be signed in to change notification settings

GfxKai/react-spring-alerts

Repository files navigation

react-spring alerts

The easiest way to add beautiful alerts to your React app. Built with Hooks and Context API, animated with react-spring.

Demo

Quickstart

  1. npm i react-spring-alerts / yarn add react-spring-alerts
  2. Wrap your root component with the AlertsWrapper component:
import { AlertsWrapper } from 'react-spring-alerts';
import { App } from './App';

const AppWithAlerts = () => (
    <AlertsWrapper>
        <App />
    </AlertsWrapper>
);
  1. Access the Alerts context from any component within your app
import { useAlerts } from 'react-spring-alerts';

const AlertButton = () => {
    const Alerts = useAlerts();
    ...
};
  1. Trigger an alert
Alerts.showAlert({
    type: 'error',
    title: 'Example Error',
    message: 'This is an error message',
    duration: 5000,
});

AlertWrapper Configuration

You can override the style of the alerts by passing an optional styleConfig prop to AlertWrapper:

const styleConfigPropType = PropTypes.shape({
    container: PropTypes.object,
    header: PropTypes.object,
    body: PropTypes.object,
    error: PropTypes.object,
    info: PropTypes.object,
    success: PropTypes.object,
});

You can also customise the transition animation by passing an optional transitionConfig prop to AlertWrapper. For more information, please see documentation for react-spring's useTransition.

Alert Configuration

Property Type Required Default value Description
title string - Header for the alert
message string - Message content for the alert
type string 'error' Type of the alert. One of 'success', 'info' or 'error'
duration number 8000 Duration of the alert in ms. 0 disables auto-dismiss

About

Add beautiful alerts to your React app. Built with Hooks and Context API, animated with react-spring.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published