SnapAlert is a lightweight Javascript plugin that provides customizable and user-friendly alert notifications , Easily display alerts, confirmations, and prompts in your applications with simple methods and flexible options.
npm install snap-alert-js tag.
html
`
Instead of installing you may use the remote version.
`html
href="https://cdn.jsdelivr.net/npm/snap-alert-js@latest/dist/snapAlert.css">
href="https://unpkg.com/snap-alert-js@latest/dist/snapAlert.min.css">
`
$3
Include the Plugin on your document's before tag.
`html
`
`
Instead of installing you may use the remote version.
html
`
`
Usage
You can now use SnapAlert to display different types of alerts. Here are some examples:
#### Basic Alert
javascript
`
SnapAlert().alert('Alert Title', 'This is a basic alert.');
`
#### Success Alert
javascript
`
SnapAlert().success('Success', 'Your operation was successful!');
`
#### Info Alert
javascript
`
SnapAlert().info('Info', 'Be Notice!');
`
#### Error Alert
javascript
`
SnapAlert().error('Error', 'Something went wrong.');
`
#### Confirmation Alert with Actions
javascript
`
SnapAlert().warning('Are you sure?', 'This action cannot be undone.', {
enableConfirm: true,
enableCancel: true,
onConfirm: () => {
console.log('Confirmed!');
},
onCancel: () => {
console.log('Cancelled!');
}
});
`
$3
You can customize the options for each alert. For example:
javascript
`
SnapAlert().info('Information', 'This is an info alert.', {
position: 'top right',
duration: 5000,
icon: 'custom-icon-class', // Replace with a Boxicons class name (https://boxicons.com)
isDark: true
});
`
$3
You can display various types of alerts using the provided methods. Below is an example of how to show a custom HTML alert:
javascript
SnapAlert().html(, {
`
position: 'top right',
duration: 5000,
}
);
SnapOptions
$3
You can use the method to set global default options for all alerts. This is particularly useful if you want to maintain consistency across multiple alerts.
`
javascript
`
SnapAlert().SnapOptions({
duration: 5000,
isDark: true,
position: 'bottom right'
});
`
#### Example of Using Global Options
After setting global options, all subsequent alerts will use the specified default settings unless overridden by individual alert options:
javascript
`
// Set global options
SnapAlert().SnapOptions({
duration: 4000,
isDark: false,
});
// Now all alerts will have these default settings
SnapAlert().success('Global Success', 'This alert will use global options.');
SnapAlert().error('Global Error', 'This alert will also use global options.');
clearAll
$3
The method is used to remove all active alerts from the screen. This can be useful in scenarios where you want to clear multiple notifications at once, such as when navigating away from a page, or after performing a certain action where notifications are no longer relevant.
`
javascript
`
SnapAlert().clearAll();
false
This will instantly clear all alerts, including success, error, warning, info, and custom HTML alerts, if any are currently visible.
$3
- rtl: (Enable/disable right-to-left support)
'info'
- type: , 'success', 'error', 'warning'
'top left'
- title: The title of the alert
- message: The message of the alert
- position: , 'top right', 'bottom left', 'bottom right', 'top center', 'bottom center'.
true
- duration: Time before auto-close (in milliseconds, default is 3000)
- autoClose: Automatically close the alert (default is )
false
- enableConfirm: Show confirm button (default is )
false
- onConfirm: Function to execute when confirmed
- enableCancel: Show cancel button (default is )
false`)
- onCancel: Function to execute when cancelled
- isDark: Dark mode for the alert (default is
- icon: Custom icon (default icons are provided)
This project is licensed under the MIT License - see the LICENSE file for details.