@ehfuse/alerts is a React alert and dialog component library with Material Design styling.
Install the package using npm:
npm install @ehfuse/alerts
The library requires no initial setup. You can import and use it immediately.
import {
SuccessAlert,
InfoAlert,
WarningAlert,
ErrorAlert,
} from "@ehfuse/alerts";
Letβs create the simplest form of alert:
import { SuccessAlert } from "@ehfuse/alerts";
function App() {
const handleClick = () => {
SuccessAlert("Task completed successfully!");
};
return <button onClick={handleClick}>Show Success Alert</button>;
}
import {
SuccessAlert,
InfoAlert,
WarningAlert,
ErrorAlert,
} from "@ehfuse/alerts";
function AlertExample() {
return (
<div>
<button onClick={() => SuccessAlert("Success message")}>
Success Alert
</button>
<button onClick={() => InfoAlert("Info message")}>
Info Alert
</button>
<button onClick={() => WarningAlert("Warning message")}>
Warning Alert
</button>
<button onClick={() => ErrorAlert("Error message")}>
Error Alert
</button>
</div>
);
}
SuccessAlert("Success", "File uploaded successfully.");
InfoAlert("Notice", "New update available.");
WarningAlert("Warning", "Disk space is low.");
ErrorAlert("Error", "Please check your network connection.");
β οΈ Important: Before using dialogs, you must first wrap your application with AlertProvider.
To use dialog functionality, you need to set up AlertProvider at the top level of your application:
import React from "react";
import { AlertProvider } from "@ehfuse/alerts";
function App() {
return (
<AlertProvider>
{/* Rest of your app components */}
<YourAppComponents />
</AlertProvider>
);
}
export default App;
import { AlertDialog } from "@ehfuse/alerts";
function DialogExample() {
const showDialog = () => {
AlertDialog({
title: "Alert",
message: "Task has been completed.",
onConfirm: () => console.log("OK clicked"),
});
};
return <button onClick={showDialog}>Show Dialog</button>;
}
ConfirmDialog can be used in two ways:
1. Promise approach (recommended):
import { ConfirmDialog } from "@ehfuse/alerts";
function ConfirmExample() {
const showConfirm = async () => {
const result = await ConfirmDialog({
title: "Confirm Delete",
message: "Are you sure you want to delete this item?",
confirmText: "Delete",
cancelText: "Cancel",
});
if (result) {
console.log("User confirmed deletion.");
} else {
console.log("User cancelled.");
}
};
return <button onClick={showConfirm}>Confirm Delete</button>;
}
2. Callback approach (same as AlertDialog):
function ConfirmExample() {
const showConfirm = () => {
ConfirmDialog({
title: "Confirm Delete",
message: "Are you sure you want to delete this item?",
confirmText: "Delete",
cancelText: "Cancel",
onConfirm: () => {
console.log("User confirmed deletion.");
},
onCancel: () => {
console.log("User cancelled.");
},
});
};
return <button onClick={showConfirm}>Confirm Delete</button>;
}
You can update alert content in real-time after displaying it:
import { InfoAlert, updateAlert } from "@ehfuse/alerts";
function SimpleProgressExample() {
const handleProgress = async () => {
// Create alert and get ID
const alertId = InfoAlert({
message: "Starting task...",
autoHide: false,
});
// Simple update
await new Promise((resolve) => setTimeout(resolve, 1000));
updateAlert(alertId, {
message: "Task in progress...",
});
await new Promise((resolve) => setTimeout(resolve, 1000));
updateAlert(alertId, {
message: "β
Task completed!",
autoHide: true,
delay: 2000,
});
};
return <button onClick={handleProgress}>Simple Progress</button>;
}
The updateAlert function allows you to dynamically change these properties:
message: Alert messagetitle: Alert titleautoHide: Auto-hide enableddelay: Delay timeshowIcon: Show iconNow that youβve learned the basics, explore these advanced topics:
These advanced features are covered in detail in the Examples documentation.
Documentation is also available in the node_modules/@ehfuse/alerts/docs/ folder after package installation.