sirnails / BloomQuote

Quote management app for a florist
2 stars 0 forks source link

Text entry on mobile is hard #14

Closed sirnails closed 4 months ago

sirnails commented 4 months ago

Implement modal as per ChatGPT suggestion:

A modal text entry will make form inputs easier on mobile devices.

This can be achieved using HTML, CSS, and JavaScript. Below are the steps to create a modal text entry field.

Step-by-Step Guide

1. Create HTML Structure

First, you need an HTML structure for your form and the modal. Here is an example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modal Text Entry</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

<!-- Form -->
<form id="mainForm">
    <label for="name">Name</label>
    <input type="text" id="name" name="name" onfocus="openModal(this)" readonly>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" onfocus="openModal(this)" readonly>
    <button type="submit">Submit</button>
</form>

<!-- Modal -->
<div id="modal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="closeModal()">&times;</span>
        <label for="modalInput">Enter Text</label>
        <input type="text" id="modalInput">
        <button onclick="saveText()">Save</button>
    </div>
</div>

<script src="scripts.js"></script>
</body>
</html>

2. Style the Modal with CSS

Next, add some CSS to style the modal.

/* styles.css */

/* The Modal (background) */
.modal {
    display: none; 
    position: fixed; 
    z-index: 1; 
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%; 
    overflow: auto; 
    background-color: rgb(0,0,0); 
    background-color: rgba(0,0,0,0.4); 
    padding-top: 60px;
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 5% auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%; 
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

3. Add JavaScript for Modal Functionality

Now, add JavaScript to handle the modal's opening, closing, and saving the input text.

// scripts.js

let currentInput = null;

function openModal(inputElement) {
    currentInput = inputElement;
    document.getElementById('modalInput').value = inputElement.value;
    document.getElementById('modal').style.display = 'block';
}

function closeModal() {
    document.getElementById('modal').style.display = 'none';
}

function saveText() {
    currentInput.value = document.getElementById('modalInput').value;
    closeModal();
}

// Close the modal when clicking outside of it
window.onclick = function(event) {
    if (event.target == document.getElementById('modal')) {
        closeModal();
    }
}

How It Works

  1. HTML: The form contains input fields that trigger the modal when focused. The modal itself is hidden by default.
  2. CSS: The modal is styled to cover the screen and center its content. It includes a close button.
  3. JavaScript:
    • openModal(): Opens the modal and sets the current input element.
    • closeModal(): Closes the modal.
    • saveText(): Saves the text from the modal back to the input field.

Integration Tips

By following these steps, you can create a more user-friendly text entry experience on mobile devices.