_dialog.html•5.93 kB
<!-- Dialog component -->
<div id="dialog" class="fixed inset-0 z-50 hidden" role="dialog" aria-modal="true">
<!-- Backdrop -->
<div class="fixed inset-0 bg-black bg-opacity-50 transition-opacity"></div>
<!-- Dialog panel -->
<div class="fixed inset-0 z-10 overflow-y-auto">
<div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
<div class="relative transform overflow-hidden rounded-lg dark:bg-tokyo-bg-secondary text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg">
<div class="px-4 pb-4 pt-5 sm:p-6 sm:pb-4">
<div class="sm:flex sm:items-start">
<div id="dialog-icon" class="mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full sm:mx-0 sm:h-10 sm:w-10">
<!-- Icon will be injected here -->
</div>
<div class="mt-3 text-center sm:ml-4 sm:mt-0 sm:text-left">
<h3 id="dialog-title" class="text-base font-semibold leading-6 dark:text-tokyo-text"></h3>
<div class="mt-2">
<p id="dialog-message" class="text-sm dark:text-tokyo-text-secondary"></p>
</div>
</div>
</div>
</div>
<div class="px-4 py-3 sm:flex sm:flex-row-reverse sm:px-6">
<button id="dialog-confirm" type="button" class="inline-flex w-full justify-center rounded-md px-3 py-2 text-sm font-semibold shadow-sm sm:ml-3 sm:w-auto">
</button>
<button id="dialog-cancel" type="button" class="mt-3 inline-flex w-full justify-center rounded-md dark:bg-tokyo-bg-accent px-3 py-2 text-sm font-semibold dark:text-tokyo-text shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-tokyo-bg-message sm:mt-0 sm:w-auto">
Cancel
</button>
</div>
</div>
</div>
</div>
</div>
<script>
class Dialog {
constructor() {
this.dialog = document.getElementById('dialog');
this.title = document.getElementById('dialog-title');
this.message = document.getElementById('dialog-message');
this.confirmBtn = document.getElementById('dialog-confirm');
this.cancelBtn = document.getElementById('dialog-cancel');
this.iconContainer = document.getElementById('dialog-icon');
// Close on cancel
this.cancelBtn.addEventListener('click', () => this.hide());
// Close on backdrop click
this.dialog.addEventListener('click', (e) => {
if (e.target === this.dialog) this.hide();
});
// Close on Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && !this.dialog.classList.contains('hidden')) {
this.hide();
}
});
}
show({ title, message, confirmText, confirmStyle, onConfirm, showCancel = true, icon }) {
this.title.textContent = title;
this.message.textContent = message;
this.confirmBtn.textContent = confirmText;
// Set confirm button style
this.confirmBtn.className = `inline-flex w-full justify-center rounded-md px-3 py-2 text-sm font-semibold shadow-sm sm:ml-3 sm:w-auto ${confirmStyle}`;
// Set icon
this.iconContainer.innerHTML = icon;
this.iconContainer.className = `mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full sm:mx-0 sm:h-10 sm:w-10 ${icon ? '' : 'hidden'}`;
// Show/hide cancel button
this.cancelBtn.style.display = showCancel ? 'inline-flex' : 'none';
// Set confirm action
this.confirmBtn.onclick = () => {
if (onConfirm) onConfirm();
this.hide();
};
this.dialog.classList.remove('hidden');
}
hide() {
this.dialog.classList.add('hidden');
}
// Predefined dialog types
confirm({ title, message, onConfirm }) {
this.show({
title,
message,
confirmText: 'Confirm',
confirmStyle: 'dark:bg-tokyo-accent-red dark:text-white dark:hover:bg-red-600',
onConfirm,
showCancel: true,
icon: `<svg class="h-6 w-6 dark:text-tokyo-accent-red" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
</svg>`
});
}
error({ title, message }) {
this.show({
title,
message,
confirmText: 'OK',
confirmStyle: 'dark:bg-tokyo-accent-red dark:text-white dark:hover:bg-red-600',
showCancel: false,
icon: `<svg class="h-6 w-6 dark:text-tokyo-accent-red" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
</svg>`
});
}
}
// Initialize dialog
window.dialog = new Dialog();
</script>