You are designing a pop up modal for a frontend form. The pop up modal will appear once a click to edit an entries country is made. The modal should display all of the countries that are available to select with countries that are already selected shown as already selected. To see which countries are selected, the button element will have attribute "data-codes" which will list all countries separated by a comma. Once the user selects or deselects any country in the modal, any new data shall swap the datavalues of the data-codes and the accompanying input field under id country-input accordingly with the appropriate newly made list of countries separated by a comma. This input field will also be the nearest one to the button. Heres some code to get you started with the modal.
Make html, css and js that suits best this description and refrain using any non dark greyish pallete colours.
Certainly! Below is a complete implementation of the pop-up modal for editing country selections using HTML, CSS, and JavaScript. The design adheres to a dark greyish palette and ensures accessibility and responsiveness.
Overview
HTML:
Contains a button to trigger the modal and an input field to display selected countries.
Includes the modal structure with a header, a scrollable country selection area, and action buttons.
CSS:
Styles the modal with a dark grey palette.
Ensures the modal is centered and has appropriate spacing.
Highlights selected countries.
JavaScript:
Handles opening and closing of the modal.
Dynamically populates the country list.
Manages the selection and updating of the data-codes attribute and the input field.
Complete Code
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Country Selection Modal</title>
<style>
/* General Styles */
body {
font-family: Arial, sans-serif;
background-color: #2E2E2E;
color: #E0E0E0;
margin: 0;
padding: 0;
}
/* Button Styles */
.edit-button {
padding: 10px 20px;
background-color: #4A4A4A;
color: #E0E0E0;
border: none;
border-radius: 4px;
cursor: pointer;
margin: 20px;
}
.edit-button:hover {
background-color: #6A6A6A;
}
/* Input Field Styles */
.country-input {
padding: 10px;
border: 1px solid #5A5A5A;
border-radius: 4px;
background-color: #3A3A3A;
color: #E0E0E0;
width: 300px;
margin: 20px;
}
/* Modal Styles */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1000; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgba(0, 0, 0, 0.7); /* Black w/ opacity */
}
.modal-content {
background-color: #3A3A3A;
margin: 10% auto; /* 10% from the top and centered */
padding: 20px;
border: 1px solid #555;
width: 80%; /* Could be more or less, depending on screen size */
border-radius: 8px;
color: #E0E0E0;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.modal-header h2 {
margin: 0;
}
.close-button {
background-color: #5A5A5A;
border: none;
color: #E0E0E0;
padding: 5px 10px;
cursor: pointer;
border-radius: 4px;
}
.close-button:hover {
background-color: #7A7A7A;
}
.country-selection {
display: flex;
flex-wrap: wrap;
max-height: 400px;
overflow-y: auto;
margin-top: 20px;
}
.country-item {
display: flex;
align-items: center;
padding: 10px;
margin: 5px;
background-color: #4A4A4A;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
.country-item.selected {
background-color: #616161;
}
.country-item img {
width: 24px;
height: 16px;
margin-right: 10px;
}
.modal-actions {
display: flex;
justify-content: flex-end;
margin-top: 20px;
}
.modal-actions button {
padding: 10px 20px;
margin-left: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
background-color: #4A4A4A;
color: #E0E0E0;
}
.modal-actions button:hover {
background-color: #6A6A6A;
}
/* Scrollbar Styles */
.country-selection::-webkit-scrollbar {
width: 8px;
}
.country-selection::-webkit-scrollbar-track {
background: #3A3A3A;
}
.country-selection::-webkit-scrollbar-thumb {
background-color: #5A5A5A;
border-radius: 4px;
}
</style>
</head>
<body>
<!-- Edit Button and Input Field -->
<div style="display: flex; align-items: center;">
<button class="edit-button" id="edit-country-button" data-codes="US,CA">Edit Countries</button>
<input type="text" id="country-input" class="country-input" value="US,CA" readonly>
</div>
<!-- The Modal -->
<div id="country-modal" class="modal">
<div class="modal-content">
<div class="modal-header">
<h2>Select Countries</h2>
<button class="close-button" id="close-modal">Close</button>
</div>
<div id="country-selection" class="country-selection">
<!-- Countries will be dynamically populated here -->
</div>
<div class="modal-actions">
<button id="save-selection">Save</button>
<button id="cancel-selection">Cancel</button>
</div>
</div>
</div>
<!-- Country List Script (Mock) -->
<script>
// Mock CountryList.getAll() function
const CountryList = {
getAll: function() {
return [
{ data: { code: 'US', name: 'United States' } },
{ data: { code: 'CA', name: 'Canada' } },
{ data: { code: 'GB', name: 'United Kingdom' } },
{ data: { code: 'FR', name: 'France' } },
{ data: { code: 'DE', name: 'Germany' } },
{ data: { code: 'JP', name: 'Japan' } },
{ data: { code: 'CN', name: 'China' } },
{ data: { code: 'IN', name: 'India' } },
{ data: { code: 'BR', name: 'Brazil' } },
{ data: { code: 'AU', name: 'Australia' } },
// Add more countries as needed
];
}
};
</script>
<!-- Main JavaScript -->
<script>
document.addEventListener("DOMContentLoaded", () => {
const editButton = document.getElementById("edit-country-button");
const modal = document.getElementById("country-modal");
const closeModalButton = document.getElementById("close-modal");
const saveButton = document.getElementById("save-selection");
const cancelButton = document.getElementById("cancel-selection");
const countrySelection = document.getElementById("country-selection");
const countryInput = document.getElementById("country-input");
// Function to open modal and initialize selections
editButton.addEventListener("click", () => {
// Reset the country selection area
countrySelection.innerHTML = "";
// Get the currently selected country codes
const selectedCodes = editButton.getAttribute("data-codes").split(",").map(code => code.trim());
// Populate the country list
CountryList.getAll().forEach(country_entry => {
const countryElement = document.createElement("div");
countryElement.className = 'country-item';
countryElement.setAttribute('data-code', country_entry.data.code);
let html_text = `
<img
alt="${country_entry.data.name}"
src="https://purecatamphetamine.github.io/country-flag-icons/3x2/${country_entry.data.code}.svg" />
<p>${country_entry.data.code}</p>
`;
countryElement.innerHTML = html_text;
// If the country is already selected, add the 'selected' class
if (selectedCodes.includes(country_entry.data.code)) {
countryElement.classList.add('selected');
}
// Add click event to toggle selection
countryElement.addEventListener('click', () => {
countryElement.classList.toggle('selected');
});
countrySelection.appendChild(countryElement);
});
// Show the modal
modal.style.display = "block";
});
// Function to close modal
const closeModal = () => {
modal.style.display = "none";
};
closeModalButton.addEventListener("click", closeModal);
cancelButton.addEventListener("click", closeModal);
// Save the selection
saveButton.addEventListener("click", () => {
const selectedElements = countrySelection.querySelectorAll('.country-item.selected');
const selectedCodes = Array.from(selectedElements).map(elem => elem.getAttribute('data-code'));
// Update the data-codes attribute
editButton.setAttribute('data-codes', selectedCodes.join(','));
// Update the input field
countryInput.value = selectedCodes.join(',');
closeModal();
});
// Close modal when clicking outside the modal content
window.addEventListener("click", (event) => {
if (event.target == modal) {
closeModal();
}
});
});
</script>
</body>
</html>
Explanation of the Implementation
HTML Structure:
Edit Button and Input Field:
The button with the class edit-button has the data-codes attribute which holds the currently selected country codes (e.g., US,CA).
The adjacent input field (country-input) displays the selected countries and is set to readonly to prevent manual editing.
Modal:
The modal is hidden by default (display: none) and is displayed when the edit button is clicked.
It contains a header with a title and a close button.
The country-selection div is where all country items will be dynamically inserted.
The modal actions include Save and Cancel buttons to handle user actions.
CSS Styling:
A dark grey palette is used throughout with shades like #2E2E2E, #3A3A3A, #4A4A4A, etc.
The modal backdrop is semi-transparent to focus attention on the modal content.
Country items change background color when selected to provide visual feedback.
Scrollbars within the country selection area are styled to match the dark theme.
JavaScript Functionality:
Mock CountryList.getAll():
A mock function is provided to simulate fetching a list of countries. Replace this with your actual data source as needed.
Event Listeners:
Edit Button (edit-country-button):
Opens the modal.
Populates the country list, marking those already selected based on the data-codes attribute.
Country Items:
Toggle the selected class on click to mark/unmark selection.
Save Button:
Gathers all selected country codes.
Updates the data-codes attribute of the edit button.
Updates the adjacent input field with the new list.
Closes the modal.
Cancel and Close Buttons:
Customization Tips
Adding More Countries:
Extend the CountryList.getAll() function to include all the required countries with their respective codes and names.
Flag Images:
The country flag images are sourced from the country-flag-icons repository. Ensure you have internet access or host the flags locally if needed.
Styling Adjustments:
Feel free to tweak the CSS to better fit the overall design of your application or to enhance responsiveness on different screen sizes.
Enhanced Functionality:
Implement search functionality within the modal to allow users to quickly find and select countries.
Add pagination or grouping (e.g., by continent) if the country list becomes extensive.
This implementation provides a solid foundation for a user-friendly and visually consistent country selection modal in your frontend application. Adjust and expand upon it as per your specific requirements!
Not yet included in a published release.
Simply close the modal without saving changes.
Outside Click:
Closes the modal if the user clicks outside the modal content area.
Dynamic Data Handling:
The data-codes attribute and the country-input field are kept in sync. When a user makes changes in the modal and saves, both are updated to reflect the current selection.
Accessibility & UX:
The modal can be closed by clicking the Close button, the Cancel button, or by clicking outside the modal content.
The Save button ensures that only the selections made within the modal are applied.