react-snackbar-alert
Simple snackbar-style notifications for React.
Installation
npm install --save react-snackbar-alert
Usage
The basic usage is as follows:
- Import the
SnackbarManager
component - Import the CSS
- Add the
SnackbarManager
component to your application'srender
method and save it to a ref - Call
create
on the ref to create snackbar alerts
Example
import React from 'react';
import { SnackbarManager } from 'react-snackbar-alert';
import 'react-snackbar-alert/styles/react-snackbar-alert.css';
export default class App extends React.Component {
constructor(props) {
super(props);
this.snackbarManager = React.createRef();
this.showAlert = this.showAlert.bind(this);
}
showAlert() {
this.snackbarManager.current.create({
message: 'Hello World!'
});
}
render() {
return (
<main>
<SnackbarManager ref={this.snackbarManager} />
<button onClick={this.showAlert}>Show Alert</button>
</main>
)
}
}
Options
SnackbarManager
Props
The SnackbarManager
component accepts the following props:
animationTimeout
- The duration of the show and hide animations, in milliseconds (default: 500)component
- A custom component to use instead of the built-inSnackbar
componentdismissable
- Whether or not the created snackbars can be manually dismissed by the userpauseOnHover
- Whether or not a snackbar's timeout should be paused when it is hovered over (default: false)progressBar
- Whether or not to show an animated progress bar showing the time before a snackbar is removed (default: true)timeout
- The time before a snackbar is removed, in milliseconds (default: 3000)
SnackbarManager.create
Options
The create
method of SnackbarManager
accepts an options object, which can have the following properties:
animationTimeout
- The duration of the show and hide animations (overrides the timeout inSnackbarManager
)data
- Custom data to use when rendering a custom snackbardismissable
- Whether or not this snackbar can be manually dismissed by the usermessage
- The message to displaypauseOnHover
- Whether or not to pause this snackbar on hoverprogressBar
- Whether or not to show the progress bar for this snackbarsticky
- Whether or not this snackbar should be sticky. Sticky snackbars are not automatically removed.timeout
- The time before this snackbar is removed (overrides the timeout inSnackbarManager
)