This document provides various usage examples for the @ehfuse/alerts library.
import {
SuccessAlert,
InfoAlert,
WarningAlert,
ErrorAlert,
} from "@ehfuse/alerts";
// Basic alerts
SuccessAlert("Task completed successfully!");
InfoAlert("New information is available.");
WarningAlert("This requires attention.");
ErrorAlert("An error has occurred.");
// Display title and message together
SuccessAlert("Task Complete", "All tasks have been completed successfully.");
InfoAlert("System Notice", "New update available.");
InfoAlert({
message: "Alert displayed in the top right",
title: "Positioned Alert",
position: "topRight",
duration: 4000,
showIcon: true,
autoHide: true,
});
The updateAlert function allows you to change the content of already displayed alerts in real-time.
import { InfoAlert, updateAlert, type AlertCommonProps } from "@ehfuse/alerts";
// Create alert and get ID
const alertId = InfoAlert({
message: "Processing...",
title: "Task Progress",
autoHide: false,
});
// Update alert content
updateAlert(alertId, {
message: "New message",
title: "Updated title",
showIcon: false,
autoHide: true,
delay: 3000,
});
Properties that can be dynamically updated:
interface AlertCommonProps {
message?: string; // Alert message
title?: string; // Alert title
autoHide?: boolean; // Auto-hide enabled
delay?: number; // Delay time (milliseconds)
showIcon?: boolean; // Show icon
}
Example of an alert that displays real-time progress:
const handleProgressAlert = async () => {
// Create initial alert
const alertId = InfoAlert({
message: "Preparing task...",
title: "Progress Tracking",
autoHide: false,
showIcon: true,
});
// Preparation phase
await new Promise((resolve) => setTimeout(resolve, 500));
updateAlert(alertId, {
message: "Task in progress... 0%",
title: "Progress Tracking",
});
// Progress updates (0~100%)
for (let progress = 0; progress <= 100; progress += 10) {
await new Promise((resolve) => setTimeout(resolve, 300));
const emoji = progress < 50 ? "β³" : progress < 90 ? "π" : "π―";
updateAlert(alertId, {
message: `${emoji} Task in progress... ${progress}%`,
title: `Progress Tracking (${progress}%)`,
});
}
// Completion phase - hide icon and show completion message
updateAlert(alertId, {
message: "β
All tasks completed!",
title: "Task Complete",
showIcon: false,
});
// Enable auto-close after 3 seconds
setTimeout(() => {
updateAlert(alertId, {
autoHide: true,
delay: 2000,
});
}, 1000);
};
// Top positions
InfoAlert({ message: "Top left alert", position: "topLeft" });
InfoAlert({ message: "Top center alert", position: "top" });
InfoAlert({ message: "Top right alert", position: "topRight" });
// Center positions
InfoAlert({ message: "Left center alert", position: "left" });
InfoAlert({ message: "Center alert", position: "center" });
InfoAlert({ message: "Right center alert", position: "right" });
// Bottom positions
InfoAlert({ message: "Bottom left alert", position: "bottomLeft" });
InfoAlert({ message: "Bottom center alert", position: "bottom" });
InfoAlert({ message: "Bottom right alert", position: "bottomRight" });
// Display at mouse position
InfoAlert({
message: "Alert displayed at mouse position",
position: "mouse",
});
// Display at specific coordinates
WarningAlert({
message: "Alert displayed at specific position",
x: 300,
y: 200,
});
const handleAnchorAlert = (event: React.MouseEvent) => {
const anchorElement = event.currentTarget as HTMLElement;
InfoAlert({
message: "Alert positioned relative to button",
position: "top",
anchorEl: anchorElement,
duration: 4000,
});
};
// JSX
<button onClick={handleAnchorAlert}>Show Anchor Alert</button>;
// Alert that appears after 2 seconds
WarningAlert({
message: "Alert that appears after 2 seconds",
position: "bottom",
duration: 3000,
delay: 2000,
showIcon: true,
});
// Alert that doesn't auto-close
InfoAlert({
message: "Alert that must be manually closed",
autoHide: false,
showIcon: true,
});
// Alert with icon
SuccessAlert({
message: "Alert with icon",
showIcon: true,
});
// Clean alert without icon
InfoAlert({
message: "Clean alert without icon",
showIcon: false,
});
import { AlertDialog } from "@ehfuse/alerts";
const showAlert = () => {
AlertDialog({
title: "Alert Dialog",
message: "This is an AlertDialog. A dialog with only an OK button.",
confirmText: "Got it",
onConfirm: () => {
SuccessAlert("AlertDialog confirmed!");
},
});
};
import { ConfirmDialog } from "@ehfuse/alerts";
const showConfirm = async () => {
const result = await ConfirmDialog({
title: "Confirmation Dialog",
message: "Do you want to continue?",
confirmText: "Continue",
cancelText: "Cancel",
onConfirm: () => {
SuccessAlert("User chose to confirm!");
},
onCancel: () => {
InfoAlert("User chose to cancel!");
},
});
if (result) {
console.log("User confirmed");
} else {
console.log("User cancelled");
}
};
const showYesNo = async () => {
const result = await ConfirmDialog({
title: "Yes/No Dialog",
message: "Do you agree?",
confirmText: "Yes",
cancelText: "No",
customStyles: {
buttonType: "yes-no",
},
});
};
import { closeAllAlerts } from "@ehfuse/alerts";
const handleCloseAll = () => {
closeAllAlerts();
};
import { configureAlerts, resetAlertConfig } from "@ehfuse/alerts";
// Apply global configuration
const applyGlobalConfig = () => {
const config = {
success: {
backgroundColor: "#4caf50",
textColor: "#ffffff",
borderRadius: "12px",
fontSize: "16px",
},
default: {
duration: 4000,
position: "topRight",
showIcon: true,
autoHide: true,
topOffset: 72,
},
};
configureAlerts(config);
};
// Reset configuration
const resetConfig = () => {
resetAlertConfig();
};
const validateForm = (formData: any) => {
if (!formData.email) {
ErrorAlert("Please enter your email.");
return false;
}
if (!formData.password) {
ErrorAlert("Please enter your password.");
return false;
}
SuccessAlert("Form validation completed!");
return true;
};
const collectFeedback = async () => {
const result = await ConfirmDialog({
title: "Feedback",
message: "Was this feature helpful?",
confirmText: "Helpful",
cancelText: "Needs Improvement",
});
if (result) {
SuccessAlert("Thank you for your feedback! π");
} else {
InfoAlert("Thank you for your valuable input. We'll improve! π§");
}
};
autoHide: false for progress indicatorsUse these examples as reference to effectively utilize @ehfuse/alerts in various situations!
Documentation is also available in the node_modules/@ehfuse/alerts/docs/ folder after package installation.