react-alerts-lite
Synopsis
A minimal library for rendering alert / toasts / popups / notifications in react apps.
Storybook
https://reactiness.github.io/react-alerts-lite/
Code Example
Importing AlertProvider
Import the AlertProvider and render before you need to use an Alert. You must only render the AlertProvider once.
import React Component from "react";import AlertProvider from "react-alerts-lite"; { return <div> <AlertProvider/> <Root/> </div> ; }
Importing Alerts
Import Alerts where you need to display an alert.
import React from "react";import Alerts from "react-alerts-lite"; Component { return <button = > Click me </button> }
Importing CSS
;
Installation
react-alerts-lite requires react and react-dom to run. It has a single dependency on react-transition-group.
$ npm install react-alerts-lite
or
$ yarn add react-alerts-lite
API Reference
AlertProvider
Themes:
<AlertProvider ="simple" | "rounded" | "shadowed" | "flat" | "bordered" =""/>
Custom transitions: Create your own
The transitions prop accepts an array of transitions with the :name and :transitions keys. Refer to react-transition-group documentation on creating transitions See bottom of page for simple template Note: maxHeight and duration are passed into transitions as props - use them where needed.
import ScaleSlideRight from "../path/to/my/react-transition-group-custom-transition";import ScaleSlideRight from "../path/to/my/react-transition-group-custom-transition"; const customTransitions = name: "custom-transition_one" transition: ScaleSlideRight name: "custom-transition-two" transition: ScaleSlideUp ;
pass your transitions array into AlertProvider
<AlertProvider =[]/>
reference your transition with the :name you provided.
<button => Click me</button>
DefaultProps:
you can pass default props into the AlertProvider to apply to all alerts. Props passed directly to an Alert take precedence so you can overwrite these where necessary.
<AlertProvider = />
Now when you render an alert with no props defaultProps will take precedence over stock props.
<button => Click me</button>
Note: the alert created here would now have type "error", position: "bottom", and transition: "slide-up"
Alert
An alert accepts the following arguments
Alerts
- timeout: How long before your alert unmounts
- duration: How long the transition animation will take
- closeButton: Whether to display the close button or not
- onClose: callback for when the alert unmounts
- maxHeight: Used in transitions to determine positioning of alerts in stack
Custom Transitions Example
Example of a simple fade transition.
import Transition from "react-transition-group/Transition";import React from "react"; { const defaultStyle = transition: `ms ease-in` transitionProperty: "opacity, max-height" ; const transitionStyles = entering: opacity: 0 maxHeight: "0px" entered: opacity: 1 maxHeight exiting: opacity: 0 maxHeight: "0px" ; return <Transition = = > { if status === "exited" return null; const currentStyles = transitionStylesstatus; return React; } </Transition> ;}
Contributors
I'll post issues and enhancements as i find them - feel free to contribute :)
License
MIT
Tech
peer-dependencies: react, react-dom dependencies: react-transition-group