html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modal Dialog Example</title>
<style>
.custom-modal-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.custom-modal-container {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
width: 90%;
max-width: 500px;
padding: 20px;
font-family: system-ui, -apple-system, sans-serif;
}
.custom-modal-header {
margin-top: 0;
margin-bottom: 16px;
font-size: 1.5em;
font-weight: bold;
}
.custom-modal-content {
margin-bottom: 20px;
}
.custom-modal-buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
}
.custom-modal-button {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.custom-modal-button-primary {
background-color: #4a86e8;
color: white;
}
.custom-modal-button-secondary {
background-color: #f1f3f4;
color: #202124;
}
.custom-modal-button-danger {
background-color: #ea4335;
color: white;
}
</style>
</head>
<body>
<button onclick="showExampleModal()">Show Modal</button>
<script>
/**
* Creates and displays a modal dialog
* @param {Object} options - Configuration options
* @param {string} options.title - Modal title
* @param {string} options.content - Modal content/description
* @param {Array} options.buttons - Array of button objects with text and value properties
* @param {boolean} options.cancelable - Whether the modal can be cancelled by clicking outside
* @param {string|null} options.defaultValue - Value to return if modal is cancelled
* @returns {Promise<string>} - The value of the selected button
*/
async function showModal(options) {
const {
title = 'Modal Title',
content = 'Modal content goes here',
buttons = [{ text: 'OK', value: 'ok', primary: true }],
cancelable = true,
defaultValue = null
} = options;
return new Promise((resolve) => {
// Create modal elements
const overlay = document.createElement('div');
overlay.className = 'custom-modal-overlay';
const modalContainer = document.createElement('div');
modalContainer.className = 'custom-modal-container';
const modalHeader = document.createElement('h3');
modalHeader.className = 'custom-modal-header';
modalHeader.textContent = title;
const modalContent = document.createElement('div');
modalContent.className = 'custom-modal-content';
modalContent.textContent = content;
const modalButtons = document.createElement('div');
modalButtons.className = 'custom-modal-buttons';
// Add buttons
buttons.forEach(button => {
const buttonElement = document.createElement('button');
buttonElement.className = 'custom-modal-button';
if (button.primary) {
buttonElement.classList.add('custom-modal-button-primary');
} else if (button.danger) {
buttonElement.classList.add('custom-modal-button-danger');
} else {
buttonElement.classList.add('custom-modal-button-secondary');
}
buttonElement.textContent = button.text;
buttonElement.addEventListener('click', () => {
document.body.removeChild(overlay);
resolve(button.value);
});
modalButtons.appendChild(buttonElement);
});
// Assemble modal
modalContainer.appendChild(modalHeader);
modalContainer.appendChild(modalContent);
modalContainer.appendChild(modalButtons);
overlay.appendChild(modalContainer);
// Handle clicking outside the modal
if (cancelable) {
overlay.addEventListener('click', (event) => {
if (event.target === overlay) {
document.body.removeChild(overlay);
resolve(defaultValue);
}
});
}
// Add modal to the DOM
document.body.appendChild(overlay);
});
}
// Example usage
async function showExampleModal() {
// Example with 3 buttons
const result = await showModal({
title: "Confirm Action",
content: "Are you sure you want to proceed with this action?",
buttons: [
{ text: "Cancel", value: "cancel" },
{ text: "No", value: "no" },
{ text: "Yes", value: "yes", primary: true }
],
cancelable: true,
defaultValue: "cancel"
});
console.log("Modal result:", result);
// Example with 2 buttons
const result2 = await showModal({
title: "Delete Item",
content: "This action cannot be undone. Are you sure?",
buttons: [
{ text: "Cancel", value: "cancel" },
{ text: "Delete", value: "delete", danger: true }
],
cancelable: false
});
console.log("Second modal result:", result2);
}
</script>
</body>
</html>