← Template Browser

Modal Dialog

Accessible overlay dialog. Closes on Esc, backdrop click, or the × button. Focus is moved to the close button on open.

Live Demo

Template Code

Copy templates/modal.stpl into your app.stpl. Move the <style> block into your stylesheet.

{#
================================================================
 Modal Dialog  (ScribeFramework template prefab)

 How to use:
   1. Copy this route into your app.stpl
   2. Move the <style> block into your stylesheet (or keep it inline)
   3. Rename the route path, modal ID, and text to suit your needs

 No external dependencies — vanilla JS only.
================================================================
#}

@route('/example-modal')
@no_layout
{$
page_title = "Modal Example"
$}

<style>
/* ── Modal ── move this block to your stylesheet ───────────── */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}
.modal-overlay.is-open {
    display: flex;
}
.modal {
    background: var(--surface-color, #fff);
    border-radius: var(--radius-lg, 0.75rem);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    width: min(90vw, 520px);
    max-height: 90vh;
    overflow-y: auto;
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
}
.modal-title {
    margin: 0;
    font-size: 1.125rem;
}
.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    color: var(--text-muted, #64748b);
    padding: 0;
}
.modal-close:hover {
    color: var(--text-color, #1e293b);
}
.modal-body {
    padding: 1.5rem;
}
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    padding: 1.25rem 1.5rem;
    border-top: 1px solid var(--border-color, #e2e8f0);
}
/* ──────────────────────────────────────────────────────────── */
</style>

<div class="container">
    <h1>Modal Example</h1>
    <p>Click the button below to open a modal dialog.</p>

    <button class="btn btn-primary" onclick="openModal('demo-modal')">
        Open Modal
    </button>

    <div id="demo-modal"
         class="modal-overlay"
         role="dialog"
         aria-modal="true"
         aria-labelledby="demo-modal-title"
         onclick="closeModalOnBackdrop(event, 'demo-modal')">
        <div class="modal">
            <div class="modal-header">
                <h3 class="modal-title" id="demo-modal-title">Confirm Action</h3>
                <button class="modal-close"
                        onclick="closeModal('demo-modal')"
                        aria-label="Close">&times;</button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to do this? This action cannot be undone.</p>
            </div>
            <div class="modal-footer">
                <button class="btn btn-secondary" onclick="closeModal('demo-modal')">Cancel</button>
                <button class="btn btn-primary">Confirm</button>
            </div>
        </div>
    </div>
</div>

<script>
function openModal(id) {
    const el = document.getElementById(id);
    el.classList.add('is-open');
    document.body.style.overflow = 'hidden';
    el.querySelector('.modal-close').focus();
}
function closeModal(id) {
    document.getElementById(id).classList.remove('is-open');
    document.body.style.overflow = '';
}
function closeModalOnBackdrop(e, id) {
    if (e.target === e.currentTarget) closeModal(id);
}
document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape') {
        document.querySelectorAll('.modal-overlay.is-open')
            .forEach(el => closeModal(el.id));
    }
});
</script>