- π free-alerts
- π Toast Methods
- π’ Alert Method
- β Confirm Method
Open Demo View on NPM GitHub Repository
Note
free-alerts is a lightweight JavaScript library designed to display modern notifications, alerts, and confirmation dialogs with a simple and intuitive API.
The library was created to provide a clean user experience without external dependencies, making it easy to integrate into any JavaScript application or framework.
- π Toast notifications
- β Success notifications
- β Error notifications
β οΈ Warning notifications- βΉοΈ Info notifications
- π’ Alert dialogs
- β Confirmation dialogs
- π Zero dependencies
- π¨ Customizable through CSS
- π¦ Framework agnostic
- β‘ Lightweight and fast
- π§ͺ Tested with Vitest
- π Browser compatible
- π§ Easy integration
Before using the library, make sure you have:
- π Modern browser
- π¦ Node.js (for package installation)
- π¦ npm
npm install free-alertsimport FreeAlerts from 'free-alerts';
import 'free-alerts/style';
FreeAlerts.success('Operation completed successfully');<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/free-alerts@1.0.0/dist/free-alerts.css" /><script src="https://cdn.jsdelivr.net/npm/free-alerts@1.0.0/dist/free-alerts.umd.js"></script><button id="btn">Show Toast</button>
<script>
document.getElementById('btn').addEventListener('click', () => {
FreeAlerts.success('Hello World!');
});
</script>| Method | Description |
|---|---|
success(message, options) |
π’ Shows a success notification |
error(message, options) |
π΄ Shows an error notification |
warning(message, options) |
π‘ Shows a warning notification |
info(message, options) |
π΅ Shows an info notification |
alert(options) |
πͺ Opens an informational modal |
confirm(options) |
β Opens a confirmation modal |
The following methods display temporary notifications:
FreeAlerts.success();
FreeAlerts.error();
FreeAlerts.warning();
FreeAlerts.info();message: string
options?: {
duration?: number
}FreeAlerts.success('User created successfully');FreeAlerts.error('Unexpected error occurred');FreeAlerts.warning('Please complete all fields');FreeAlerts.info('New version available');FreeAlerts.success('Saved correctly', {
duration: 5000,
});Displays an informational modal dialog.
{
title?: string;
message?: string;
}FreeAlerts.alert({
title: 'Attention',
message: 'This action cannot be undone.',
});Displays a confirmation dialog and returns a Promise.
{
title?: string;
message?: string;
}Promise<boolean>;const confirmed = await FreeAlerts.confirm({
title: 'Delete User',
message: 'Are you sure you want to continue?',
});
if (confirmed) {
console.log('Confirmed');
}document.getElementById('form').addEventListener('submit', () => {
FreeAlerts.success('Form submitted successfully');
});try {
await saveData();
} catch {
FreeAlerts.error('Failed to save data');
}const confirmed = await FreeAlerts.confirm({
title: 'Delete Record',
message: 'This action cannot be undone.',
});
if (confirmed) {
deleteRecord();
}import FreeAlerts from 'free-alerts';
import 'free-alerts/style';
function App() {
const handleClick = () => {
FreeAlerts.success('Saved from React');
};
return <button onClick={handleClick}>Save</button>;
}
export default App;There are two ways to use FreeAlerts in Angular depending on TypeScript configuration.
import { Component } from '@angular/core';
// @ts-ignore
import FreeAlerts from 'free-alerts';
// @ts-ignore
import 'free-alerts/style';
@Component({
selector: 'app-root',
template: '<button (click)="showAlert()">Show alert</button>',
})
export class AppComponent {
showAlert(): void {
FreeAlerts.success('test');
}
}Create the TypeScript declaration file:
src/types/free-alerts.d.ts
// This is for the FreeAlerts β
declare module 'free-alerts' {
const FreeAlerts: {
success(message: string, options?: Record<string, any>): void;
error(message: string, options?: Record<string, any>): void;
warning(message: string, options?: Record<string, any>): void;
info(message: string, options?: Record<string, any>): void;
alert(options: { title?: string; message?: string }): void;
confirm(options: { title?: string; message?: string }): Promise<boolean>;
};
export default FreeAlerts;
}
// This is for the styles β
declare module 'free-alerts/style';import { Component } from '@angular/core';
// Imports here β
import FreeAlerts from 'free-alerts';
import 'free-alerts/style';
@Component({
selector: 'app-root',
template: '<button (click)="showAlert()">Show alert</button>',
})
export class AppComponent {
showAlert(): void {
FreeAlerts.success('test');
}
}- No
@ts-ignoreneeded when using.d.tsfiles - Keeps Angular clean and fully typed
<template>
<button @click="notify">Notify</button>
</template>
<script setup>
import FreeAlerts from 'free-alerts';
import 'free-alerts/style';
function notify() {
FreeAlerts.success('Saved from Vue');
}
</script>The library includes unit tests using Vitest.
npm run testimport { describe, it, expect } from 'vitest';
import FreeAlerts from '../src/index.js';
describe('FreeAlerts.alert', () => {
it('should create modal', () => {
FreeAlerts.alert({
title: 'Error',
message: 'Something happened',
});
const modal = document.querySelector('.free-alerts-modal');
expect(modal).not.toBeNull();
});
});free-alerts/
βββ demo/
β βββ dev/
β β βββ app.js
β β βββ index.html
β β βββ styles.css
β βββ prod/
β βββ app.js
β βββ index.html
β βββ styles.css
β
βββ dist/
β βββ free-alerts.css
β βββ free-alerts.mjs
β βββ free-alerts.umd.js
β
βββ src/
β βββ assets/
β β βββ preview.png
β β
β βββ core/
β β βββ alert.js
β β βββ confirm.js
β β βββ createElement.js
β β βββ toast.js
β β
β βββ styles/
β β βββ free-alerts.css
β β
β βββ utils/
β β βββ constants.js
β β βββ icons.js
β β
β βββ index.js
β
βββ tests/
β βββ alert.test.js
β βββ confirm.test.js
β βββ setup.js
β
βββ package.json
βββ package-lock.json
βββ vite.config.js
βββ vitest.config.js
βββ LICENSE
βββ README.md
- Use success notifications for successful operations
- Use error notifications for failures
- Use confirm dialogs before destructive actions
- Keep messages concise and meaningful
- Avoid excessive notifications
- Maintain consistent user feedback
- Provide clear action outcomes
Contributions are welcome.
git clone git@github.com:alobuuls/free-alerts.gitnpm installnpm run devnpm run testnpm run buildIf you find bugs, have ideas for improvements, or want to contribute new features, feel free to open an issue or submit a pull request.
This project is intended for educational and portfolio purposes.
Created by Alondra Francisco.
