/**
* Shows a custom modal dialog using the native <dialog> element.
*
* @param {object} options - Configuration for the dialog.
* @param {string} options.title - The title text for the dialog.
* @param {string} options.description - The description text for the dialog.
* @param {Array<{label: string, value: string}>} options.buttons - An array of button configurations.
* Each object should have a `label` (button text) and a `value` (the action returned when clicked).
* @returns {Promise<string|null>} A promise that resolves with the `value` of the clicked button,
* the `data-cancel-action` value if the dialog is cancelled (e.g., via Escape key),
* or `null` if no `data-cancel-action` is set and it's cancelled.
*/
async function showCustomDialog({ title, description, buttons }) {
const dialog = document.getElementById('customAlertDialog');
if (!dialog) {
console.error("Dialog element with ID 'customAlertDialog' not found.");
return null; // Cannot show dialog if element doesn't exist
}
// --- Populate Dialog Content ---
dialog.querySelector('.dialog-title').textContent = title;
dialog.querySelector('.dialog-description').textContent = description;
const buttonContainer = dialog.querySelector('.dialog-buttons');
buttonContainer.innerHTML = ''; // Clear previous buttons
// Add new buttons based on the configuration
buttons.forEach(btnConfig => {
const buttonElement = document.createElement('button');
buttonElement.textContent = btnConfig.label;
buttonElement.value = btnConfig.value; // Set the value for dialog.close()
// Add a click listener to each button
// When a button is clicked, close the dialog and pass the button's value
buttonElement.addEventListener('click', () => {
dialog.close(btnConfig.value); // This triggers the 'close' event with returnValue set
});
buttonContainer.appendChild(buttonElement);
});
// --- Show Dialog and Wait for Close ---
return new Promise((resolve) => {
// Define the handler for the native 'close' event
const handleClose = () => {
// Get the value passed to dialog.close()
const result = dialog.returnValue;
// Get the configured cancel action from the data attribute
const cancelAction = dialog.dataset.cancelAction || null;
// Determine the final action:
// If result is empty string, it was likely cancelled via Escape key/backdrop.
// In that case, return the data-cancel-action value.
// Otherwise, return the button's value.
const finalAction = result === "" ? cancelAction : result;
// Clean up the event listener
dialog.removeEventListener('close', handleClose);
// Resolve the promise with the final action
resolve(finalAction);
};
// Add the 'close' event listener
dialog.addEventListener('close', handleClose);
// Show the dialog modally
dialog.showModal();
});
}
// --- Example Usage ---
const openDialogBtn = document.getElementById('openDialogBtn');
const dialogResultEl = document.getElementById('dialogResult');
openDialogBtn.addEventListener('click', async () => {
dialogResultEl.textContent = 'Dialog shown, waiting for result...';
// Define the dialog content and buttons
const dialogConfig = {
title: 'Choose an Action',
description: 'Please select one of the options below:',
buttons: [
{ label: 'Option A', value: 'action-a' },
{ label: 'Option B', value: 'action-b' },
{ label: 'Cancel', value: 'cancelled-by-button' } // Note: This 'Cancel' button explicitly sets a value
]
};
// Show the dialog and wait for the result
const result = await showCustomDialog(dialogConfig);
// Display the result
if (result !== null) {
dialogResultEl.textContent = `Dialog closed. Action: ${result}`;
} else {
// This case might happen if data-cancel-action is NOT set and user cancels via Escape.
// In our HTML, data-cancel-action *is* set, so null shouldn't happen for Escape key cancel.
dialogResultEl.textContent = `Dialog closed without a specific action.`;
}
console.log('Dialog Result:', result);
});