@getalby/lightning-messageboard
Version:
A web component for a lightning messageboard powered by NWC
40 lines (39 loc) • 1.22 kB
JavaScript
/**
* Utility functions for the Lightning Messageboard
*/
// Escape HTML special characters to prevent XSS
export function escapeHTML(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// Format numbers with commas for thousands
export function formatNumber(num) {
return new Intl.NumberFormat().format(num);
}
// Create and show a toast notification
export function showToast(parent, title, message, type = "info", duration = 3000) {
// Remove any existing toast
const existingToast = parent.querySelector(".toast");
if (existingToast) {
existingToast.remove();
}
// Create toast container
const toast = document.createElement("div");
toast.className = `toast${type === "error" ? " toast-error" : ""}`;
// Add toast content
toast.innerHTML = `
<div class="toast-title">${escapeHTML(title)}</div>
<div>${escapeHTML(message)}</div>
`;
// Add to shadow DOM
parent.appendChild(toast);
// Remove after duration
const timeout = window.setTimeout(() => {
toast.remove();
}, duration);
return timeout;
}