A small, very easy to use and highly customisable snackbar component to use on any project.
đ Try it out on the Ackbar Snackbar Sandbox
npm i ackbar-snackbar
<script src="node_modules/ackbar-snackbar/dist/index.js"></script>
import 'ackbar-bar.js';
</body>
)
<ackbar-snackbar></ackbar-snackbar>
ackbar-snackbar-add
event from any element on your page
const snackbarOptions = {
duration: 4000,
message: `This is an awesome snackbar`,
variant: 'success'
}
window.dispatchEvent(new CustomEvent('ackbar-snackbar-add', {
bubbles: true,
composed: true,
detail: snackbarOptions
}));
There are several options that you can customize when you create a snackbar.
To set any option, just pass it into the snackbar options object in the ackbar-snackbar-add
event. For instance to set a custom animation name:
window.dispatchEvent(new CustomEvent('ackbar-snackbar-add', {
bubbles: true,
composed: true,
detail: {
message: `This is an awesome snackbar`,
animationName: 'slide-in',
}
}));
If an option isnât passed in the options object, the default value will be used.
Set the duration of the show and hide animations (in milliseconds).
Option Name | Default | Type | Required |
---|---|---|---|
animationDuration |
500 | Number | no |
Example:
// Set custom animation duration
const snackbarOptions = {
message: 'This is the snackbar message',
animationDuration: 1000
}
Choose which animation you want to use to show / hide the snackbar
Option Name | Possible values | Default | Type | Required |
---|---|---|---|---|
animationName |
âdefaultâ, âslide-inâ, âzoomâ | âdefaultâ | String | no |
Example:
// Set custom animation name
const snackbarOptions = {
message: 'This is the snackbar message',
animationName: 'slide-in'
}
You can specify a callback function that will be called when the snackbar button is clicked
Option Name | Default | Type | Required |
---|---|---|---|
buttonCallback |
null | Function | no |
Example:
// Set a custom callback function
const snackbarOptions = {
message: 'This is a snackbar message',
buttonCallback() { console.log('Hello World') }
}
Set the text to be displayed on the snackbar button.
Option Name | Default | Type | Required |
---|---|---|---|
buttonText |
null | String | no |
Example:
// Set custom button text
const snackbarOptions = {
message: 'This is a snackbar message',
buttonText: 'OK'
}
Add custom classes to the snackbar element
Option Name | Default | Type | Required |
---|---|---|---|
customClasses |
null | String | no |
Example:
// Set custom classes
const snackbarOptions = {
message: 'This is a super cool snackbar message',
customClasses: 'my-class my-other-class'
}
Set how long the auto snackbar (refer to type) will be displayed (in milliseconds).
Option Name | Default | Type | Required |
---|---|---|---|
duration |
4000 | Number | no |
Example:
// Set custom duration
const snackbarOptions = {
message: 'This is a snackbar message',
duration: 8000
}
You can specify a callback function that will be called when the snackbar hide animation finishes
Option Name | Default | Type | Required |
---|---|---|---|
hideCallback |
null | Function | no |
Example:
// Set a custom callback function
const snackbarOptions = {
message: 'This is a snackbar message',
hideCallback() { console.log('Snackbar closed') }
}
Set the message that will be shown on the snackbar.
Option Name | Default | Type | Required |
---|---|---|---|
message |
âAckbar snackbar: Itâs a snackbarâ | String | yes |
Example:
// Set custom duration
const snackbarOptions = {
message: 'This is a super cool snackbar message',
}
You can specify a callback function that will be called when the snackbar show animation finishes
Option Name | Default | Type | Required |
---|---|---|---|
showCallback |
null | Function | no |
Example:
// Set a custom callback function
const snackbarOptions = {
message: 'This is a snackbar message',
showCallback() { console.log('Snackbar opened') }
}
Set the type to choose between a snackbar that is automatically dismissed or a snackbar that stays visible until the user clicks the dismiss button.
Option Name | Possible values | Default | Type | Required |
---|---|---|---|---|
type |
âautoâ, âdismissâ | âautoâ | String | no |
Example:
// Set custom duration
const snackbarOptions = {
message: 'This is a super cool snackbar message',
type: 'dismiss'
}
Choose one of the different snackbar variants to set the snackbar âstyleâ (background-color, âŚ).
Option Name | Possible values | Default | Type | Required |
---|---|---|---|---|
variant |
âdefaultâ, âsuccessâ, âwarningâ, âerrorâ, âinfoâ | âdefaultâ | String | no |
Example:
// Set custom duration
const snackbarOptions = {
message: 'This is a super cool snackbar message',
type: 'dismiss'
}
Set the size of the snackbar
Option Name | Possible values | Default | Type | Required |
---|---|---|---|---|
size |
âsmallâ, ânormalâ, âlargeâ | ânormalâ | String | no |
Example:
// Set custom duration
const snackbarOptions = {
message: 'This is a super cool snackbar message',
size: 'small'
}
You can also set some attributes directly on the element.
Set the position of the snackbars inside the browser window
Attribute Name | Possible values | Default | Type | Required |
---|---|---|---|---|
position |
âtop leftâ, âtop centerâ, âtop rightâ, âmiddle leftâ, âmiddle centerâ, âmiddle rightâ, âbottom leftâ, âbottom centerâ, âbottom rightâ | âbottom leftâ | String | no |
Example:
<!-- set snackbar container position -->
<ackbar-snackbar position="top right"></ackbar-snackbar>
You can further customize the snackbars by adding your custom styles using the available custom CSS Variables
Example:
/* Add to your css */
acbar-snackbar {
--ackbar-color-success: green;
}
CSS Variable Name | Description | Default |
---|---|---|
--ackbar-color-bg |
Background color of the default snackbar | rgb(53, 53, 53) |
--ackbar-color-success |
Background color of the success variant snackbar | rgb(105, 199, 109) |
--ackbar-color-error |
Background color of the error variant snackbar | rgb(234, 94, 94) |
--ackbar-color-warning |
Background color of the warning variant snackbar | rgb(241, 153, 78) |
--ackbar-color-info |
Background color of the info variant snackbar | rgb(47, 162, 255) |
--ackbar-color-text |
Message text color on success, error, warning and info variants | rgb(255, 255, 255) |
--ackbar-color-button-default |
Button text color | rgb(149, 104, 228) |
--ackbar-color-button-background |
Button background color | transparent |
--ackbar-color-button-background-hover |
Button background color on hover | rgba(0, 0, 0, .1) |
--ackbar-padding-normal |
Snackbar padding | 1rem |
--ackbar-button-padding |
Padding on the snackbar button | 0.125rem 0.5rem |
--ackbar-spacing-normal |
Spacing between snackbars | .25rem |
--ackbar-margins |
Containerâs distance to page border | 1rem |
--ackbar-width |
Snackbar width | 25rem |
--ackbar-max-width |
Snackbar maximal width | calc(100vw - calc(var(âackbar-margins) * 4)) |
--ackbar-line-height |
Line height | 1.2 |
--ackbar-button-opacity |
Set the buttonâs opacity | .75 |
--ackbar-button-opacity-hover |
Set the buttonâs opacity on hover | 1 |
--ackbar-base-font-size |
Base font size for the snackbar elements | 1rem |
--ackbar-radius-normal |
Border-radius (on the snackbar and the button) | .25rem |
--ackbar-opacity |
Opacity of the component | 1 |
--ackbar-grid-gap |
Set gap between the message and the button | 1rem |
--ackbar-box-shadow |
Set the box-shadow on each snackbar | none |