Snackbar
warning
Snackbars display important information that informs the user about the consequence of an action – e.g. "Chart deleted". They are semantically categorized and colored to reflect the status.
Snackbars are superimposed on the UI, positioned in the top-right corner of the viewport. They self-dismiss after a certain delay.
Example
Usage
To set up the Snackbar component you need to wrap your component tree with the SnackbarProvider
.
// App.js
import { SnackbarProvider } from '@adjust/components';
export default function() {
return (
<SnackbarProvider>
{...}
</SnackbarProvider>
);
}
Through the SnackbarProvider
your components will have access to a helper function for showing Snackbar items.
Access this function via SnackbarContext
like this:
// MyComponent.js
import { useContext } from 'react';
import { SnackbarContext, Button } from '@adjust/components';
export default function YourComponent() {
const openSnackbar = useContext(SnackbarContext);
return (
<Button
label="Open Snackbar Item"
onClick={() => {
openSnackbar({
title: 'Item deleted',
action: { label: 'Undo', onClick: () => {} }
});
}}
/>
);
}
The openSnackbar
function accepts a title, an optional action, an optional isClosable and an optional severity (kind
).
Variants
Kind
The kind
prop lets you set the severity of snackbar messages and give it a semantic meaning.
Four different options are available: neutral
, positive
, warning
and negative
.
Props
Properties of the SnackbarProvider
:
Name | Type | Default |
---|---|---|
offsetTop Adjust the distance to the top of the screen |
| 16 |
offsetRight Adjust the distance to the right of the screen |
| 16 |
children Children of the Snackbar Provider |
| — |
data-{foo} Data attributes can be used by testing libraries to retrieve components or assert their existence |
| — |
* - the prop is required. |
The object structure for individual snackbar items passed into openSnackbar
:
Name | Type | Default |
---|---|---|
title A short headline |
| — |
kind Type of the Snackbar |
| "neutral" |
action Optional action button to be shown in the Snackbar |
| — |
isClosable Optional prop to show close button in the Snackbar |
| false |
dataTestId A test id to be used by testing libraries. It will be added as a "data-testid" attribute on the container of the snackbar item |
| — |
* - the prop is required. |