HTML5 Dialog Example

by b_g_v

HTML

<button id="openDialog">Open Dialog</button>

    <dialog id="myDialog">
        <div class="dialog-header">
            <h2 class="dialog-title">Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, Dialog Title, </h2>
            <button class="close-button" id="closeButton" aria-label="Close">&times;</button>
        </div>
        
        <div class="dialog-content">
            <p>This is the content area of the dialog. You can put any content here.</p>
            
            <!-- Example form content -->
            <form id="dialogForm">
                <div class="form-group">
                    <label for="nameInput">Name:</label>
                    <input type="text" id="nameInput" name="name">
                </div>
                
                <div class="form-group">
                    <label for="messageInput">Message:</label>
                    <textarea id="messageInput" name="message" rows="4"></textarea>
                </div>
                
                <div class="action-buttons">
                    <button type="button" id="cancelButton">Cancel</button>
                    <button type="submit" class="primary">Submit</button>
                </div>
            </form>
        </div>
        
        <div class="dialog-footer" id="statusLine">
            Ready
        </div>
    </dialog>

CSS

/* Dialog Styles */
        #myDialog {
            padding: 0;
            border: 1px solid #ccc;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            width: 500px;
            max-width: 90vw;
        }

        #myDialog::backdrop {
            background-color: rgba(0, 0, 0, 0.5);
        }

        /* Title Bar */
        .dialog-header {
            background-color: #f0f0f0;
            padding: 12px 16px;
            border-bottom: 1px solid #ddd;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-radius: 8px 8px 0 0;
        }

        .dialog-title {
            margin: 0;
            font-size: 18px;
            font-weight: 600;
            color: #333;
        }

        /* Close Button */
        .close-button {
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: #666;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 4px;
            transition: background-color 0.2s;
        }

        .close-button:hover {
            background-color: #e0e0e0;
            color: #333;
        }

        /* Content Area */
        .dialog-content {
            padding: 20px;
            min-height: 200px;
        }

        /* Status Line */
        .dialog-footer {
            background-color: #f8f8f8;
            padding: 10px 16px;
            border-top: 1px solid #ddd;
            font-size: 14px;
            color: #666;
            border-radius: 0 0 8px 8px;
        }

        /* Demo Button */
        #openDialog {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #007bff;
            color: white;
            border:...

JavaScript

// Get elements
        const dialog = document.getElementById('myDialog');
        const openButton = document.getElementById('openDialog');
        const closeButton = document.getElementById('closeButton');
        const cancelButton = document.getElementById('cancelButton');
        const statusLine = document.getElementById('statusLine');
        const dialogForm = document.getElementById('dialogForm');
        const nameInput = document.getElementById('nameInput');

        // Open dialog
        openButton.addEventListener('click', function() {
            dialog.showModal();
            updateStatus('Dialog opened');
            // Focus on first input
            nameInput.focus();
        });

        // Close dialog with X button
        closeButton.addEventListener('click', function() {
            closeDialog();
        });

        // Close dialog with Cancel button
        cancelButton.addEventListener('click', function() {
            closeDialog();
        });

        // Handle form submission
        dialogForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(dialogForm);
            const name = formData.get('name');
            const message = formData.get('message');
            
            updateStatus('Processing...');
            
            // Simulate async operation
            setTimeout(() => {
                console.log('Form submitted:', { name, message });
                updateStatus('Form submitted successfully!');
                
                // Close dialog after a short delay
                setTimeout(() => {
                    closeDialog();
                    dialogForm.reset();
                }, 1000);
            }, 1000);
        });

        // Close dialog on backdrop click
        dialog.addEventListener('click', function(e) {
            const rect =...