Last modification | View Log | RSS feed
A simple package to make the usage of various components within Bootstrap 5 easier to use.
If this package has helped you, and you're feeling particularly generous:
Note: The package is also available on npmjs: https://www.npmjs.com/package/bs5-utils
Contents
-
Configuration
-
There are several defaults which you can customize:
Bs5Utils.defaults.toasts.position = 'top-right';
Bs5Utils.defaults.toasts.container = 'toast-container';
Bs5Utils.defaults.toasts.stacking = false;
As bs5Utils.Snack
is a subset of bs5Utils.Toast
, the configuration for toasts will also apply to bs5Utils.Sanck
.
Theming
-
You can register your own custom styles by passing classes to specific components by using the static
method Bs5Utils.registerStyle
. The components you can customise are:
btnClose
- The dismiss buttonmain
- The area of the toast, snack, or modal which will display the type
colorborder
- The border of the componentThese components have been clearly illustrated below. For the time being, the border
style for bs5Utils.Snack
cannot
be overridden.
Note: All of these keys must be passed in the styles
parameter object.
Method Overview
/**
*/
Bs5Utils.registerStyle(key, styles)
Usage
You first define your CSS classes:
.bg-pink {
background-color: pink;
}
.text-purple {
color: purple;
}
.border-pink {
border-color: pink !important;
}
Then you register the style:
Bs5Utils.registerStyle('pink', {
btnClose: ['btn-close-white'],
main: ['bg-pink', 'text-purple'],
border: ['border-pink']
});
Pass empty arrays if you wish to leave the default styles e.g.
Bs5Utils.registerStyle('pink', {
btnClose: [],
main: ['bg-pink', 'text-purple'],
border: ['border-pink']
});
Now, pink
can be used as a type
when displaying snacks, toasts, or modals e.g.
Snack
Toast
Modal
API
-
This package is based around the Bs5Utils
class, so first things first, construct the object:
const bs5Utils = new Bs5Utils();
Thereafter you'll be able to use the methods outlined below.
Method Overview
/**
*/
bs5Utils.Snack.show(
type,
title,
delay = 0,
dismissible = true
);
Usage
bs5Utils.Snack.show('secondary', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('light', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('white', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('dark', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('info', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('primary', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('success', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('warning', 'Hello World!', delay = 0, dismissible = true);
bs5Utils.Snack.show('danger', 'Hello World!', delay = 0, dismissible = true);
Example
Method Overview
/**
*/
bs5Utils.Toast.show({
type,
icon = '',
title,
subtitle = '',
content = '',
buttons = [],
delay = 0,
dismissible = true,
});
Usage
bs5Utils.Toast.show({
type: 'primary',
icon: `<i class="far fa-check-circle fa-lg me-2"></i>`,
title: 'Notification!',
subtitle: '23 secs ago',
content: 'Hello World!',
buttons: [
{
text: 'Click Me!',
class: 'btn btn-sm btn-primary',
handler: () => {
alert(`Button #1 has been clicked!`);
}
},
{
text: 'Click Me Too!',
class: 'btn btn-sm btn-warning',
handler: () => {
alert(`You clicked me too!`);
}
},
{
type: 'dismiss',
text: 'Hide',
class: 'btn btn-sm btn-secondary'
}
],
delay: 0,
dismissible: true
});
Example
Method Overview
/**
*/
bs5Utils.Modal.show({
type,
title = '',
content = '',
buttons = [],
centered = false,
dismissible = true,
backdrop = dismissible ? true : 'static',
keyboard = dismissible,
focus = true,
fullscreen = false,
size = ''
})
Usage
bs5Utils.Modal.show({
type: 'primary',
title: `Hello World!`,
content: `<p class="text-center fw-bold">Hello World!</p>`,
buttons: [
{
text: 'Click Me!',
class: 'btn btn-sm btn-primary',
handler: () => {
alert(`Button #1 has been clicked!`);
}
},
{
text: 'Click Me Too!',
class: 'btn btn-sm btn-warning',
handler: () => {
alert(`You clicked me too!`);
}
},
{
type: 'dismiss',
text: 'Hide',
class: 'btn btn-sm btn-secondary'
}
],
centered: true,
dismissible: true,
backdrop: 'static',
keyboard: false,
focus: false
});
Example
Support & Contribute
-
app to transpile and minify your changes