Simple snackbar-style notifications for React
React Snackbar Alert requires React 16.8.0 or newer and styled-components 4 as peer dependencies:
npm install --save react react-dom styled-components
npm install --save react-snackbar-alert
https://joeattardi.github.io/react-snackbar-alert/
Snackbar notifications in 3 easy steps:
- Import the
SnackbarProvider
component and thewrapComponent
helper - Wrap your application in the
SnackbarProvider
and pass any desired configuration props - Wrap any components you want to create snackbars from by passing them to the
wrapComponent
helper. This will pass acreateSnackbar
prop to the passed component which can be used to show a snackbar notification.
import React from 'react';
import { SnackbarProvider, wrapComponent } from 'react-snackbar-alert';
export default function App() {
return (
<SnackbarProvider>
<Container />
</SnackbarProvider>
);
}
const Container = wrapComponent(function({ createSnackbar }) {
function showSnackbar() {
createSnackbar({
message: 'Helo Snackbar!'
});
}
return (
<div>
<button onClick={showSnackbar}>Show Snackbar</button>
</div>
)
});
The SnackbarProvider
accepts the following props:
Unless otherwise noted, any of these props can be overridden for a specific snackbar instance by adding that same property to the object passed to createSnackbar
.
animationTimeout
- The duration of the show and hide animations, in milliseconds (default:500
)component
- A custom component to use instead of the built-inSnackbar
component. Cannot be overridden for a specific snackbar.dismissable
- 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)position
- The position on screen to show the snackbars. One oftop
,top-left
,top-right
,bottom
,bottom-left
,bottom-right
(default:bottom
). Cannot be overridden for a specific snackbar.progressBar
- Whether or not to show an animated progress bar showing the time before a snackbar is removed (default:true
)sticky
- Whether or not snackbars should be sticky (not automatically removed) (default:false
)timeout
- The time before a snackbar is removed, in milliseconds (default:3000
)
The createSnackbar
function accepts an options object, which can have the following properties:
animationTimeout
- The duration of the show and hide animationsdata
- 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.theme
- The theme to use for this snackbar. One ofdefault
,info
,success
,warning
,error
(default:default
)timeout
- The time before this snackbar is removed