html Copy code
<!DOCTYPE html>
<html>
<head>
<style>
/* Isolated modal styles with specificity */
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal-container {
background: white;
border-radius: 8px;
padding: 20px;
width: 400px;
max-width: 90%;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.modal-title {
margin-top: 0;
margin-bottom: 15px;
font-size: 1.2em;
}
.modal-description {
margin-bottom: 20px;
}
.modal-buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
}
.modal-buttons button {
padding: 8px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.modal-buttons .btn-primary {
background-color: #007bff;
color: white;
}
.modal-buttons .btn-secondary {
background-color: #6c757d;
color: white;
}
.modal-buttons .btn-cancel {
background-color: #dc3545;
color: white;
}
</style>
</head>
<body>
<script>
/**
* Creates a configurable modal dialog
* @param {Object} options - Configuration options
* @param {string} options.title - Modal title
* @param {string} options.description - Modal description
* @param {string[]} options.buttons - Button labels
* @param {boolean} [options.cancellable=true] - Whether modal can be cancelled
* @param {number} [options.defaultChoice=0] - Default selected button index
* @returns {Promise<number>} Selected button index
*/
async function showModal({
title,
description,
buttons = ['OK', 'Cancel'],
cancellable = true,
defaultChoice = 0
} = {}) {
return new Promise((resolve, reject) => {
// Create modal elements
const overlay = document.createElement('div');
overlay.className = 'modal-overlay';
const container = document.createElement('div');
container.className = 'modal-container';
const titleEl = document.createElement('h2');
titleEl.className = 'modal-title';
titleEl.textContent = title;
const descriptionEl = document.createElement('p');
descriptionEl.className = 'modal-description';
descriptionEl.textContent = description;
const buttonContainer = document.createElement('div');
buttonContainer.className = 'modal-buttons';
// Create buttons
buttons.forEach((buttonText, index) => {
const button = document.createElement('button');
button.textContent = buttonText;
button.className = index === defaultChoice
? 'btn-primary'
: 'btn-secondary';
button.addEventListener('click', () => {
document.body.removeChild(overlay);
resolve(index);
});
buttonContainer.appendChild(button);
});
// Optional close on overlay click
if (cancellable) {
overlay.addEventListener('click', (e) => {
if (e.target === overlay) {
document.body.removeChild(overlay);
resolve(-1);
}
});
}
// Assemble modal
container.appendChild(titleEl);
container.appendChild(descriptionEl);
container.appendChild(buttonContainer);
overlay.appendChild(container);
// Prevent event propagation within modal
container.addEventListener('click', (e) => e.stopPropagation());
// Add to document
document.body.appendChild(overlay);
});
}
// Example usage
async function demonstrateModal() {
try {
// 3-button modal
const result3 = await showModal({
title: 'Confirm Action',
description: 'Choose your preferred option carefully.',
buttons: ['Save', 'Discard', 'Cancel'],
defaultChoice: 0
});
console.log('3-button modal result:', result3);
// 2-button modal
const result2 = await showModal({
title: 'Simple Confirmation',
description: 'Do you want to proceed?',
buttons: ['Yes', 'No'],
cancellable: false
});
console.log('2-button modal result:', result2);
} catch (error) {
console.error('Modal error:', error);
}
}
// Trigger on page load for demonstration
document.addEventListener('DOMContentLoaded', demonstrateModal);
</script>
</body>
</html>
You can easily customize the styling, add more buttons, or modify the behavior as needed.