create select with avatars and search bar, only black and white aesthetic theme
- User
create select with avatars and search bar, only black and white aesthetic theme
Assistant
create select with avatars and search bar, only black and white aesthetic theme
Okay, here's a simple implementation of a black and white avatar select component with a search bar using HTML, CSS, and basic JavaScript.
This example uses simple SVG data URIs for the avatars to maintain the B&W aesthetic easily.
HTML (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>B&W Avatar Select</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="avatar-select-container">
<!-- Trigger element showing the currently selected option -->
<div id="selectTrigger" class="select-trigger">
<div class="selected-avatar">
<!-- Default placeholder or first item's avatar -->
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" class="avatar-icon">
<defs>
<filter id="bwFilter">
<feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0, 0 0 0 1 0"/>
</filter>
</defs>
<circle cx="50" cy="50" r="45" fill="none" stroke="#333" stroke-width="5" filter="url(#bwFilter)"/>
<circle cx="50" cy="40" r="15" fill="none" stroke="#333" stroke-width="4" filter="url(#bwFilter)"/>
<path d="M30 65 Q 50 85, 70 65" fill="none" stroke="#333" stroke-width="4" filter="url(#bwFilter)"/>
</svg>
</div>
<span id="selectedName" class="selected-name">Choose...</span>
<span class="dropdown-arrow">▼</span>
</div>
<!-- Dropdown menu -->
<div id="avatarDropdown" class="avatar-select-dropdown">
<input type="text" id="searchBar" class="search-bar" placeholder="Search avatars...">
<div id="optionsList" class="options-list">
<!-- Options will be populated here by JavaScript -->
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (style.css):
/* Basic Reset & Body Styling */
body {
font-family: Arial, Helvetica, sans-serif;
background-color: #f8f8f8; /* Very light gray background */
display: flex;
justify-content: center;
align-items: flex-start; /* Align to top */
min-height: 100vh;
margin: 20px;
color: #333; /* Default text color */
}
/* SVG Filter for Black & White */
/* Apply this class to any SVG needing the effect */
.bw-svg {
filter: url(#bwFilter); /* Using the filter defined in HTML */
/* Fallback if filter doesn't work as expected or for simpler SVGs */
/* filter: grayscale(100%) brightness(0%); */ /* This makes it black */
/* filter: grayscale(100%) brightness(1); */ /* This makes it white */
/* For outlines, it's often better to design the SVG B&W */
}
/* Container for the whole component */
.avatar-select-container {
position: relative;
width: 250px; /* Adjust as needed */
border: 1px solid #ccc; /* Subtle border */
border-radius: 5px;
background-color: #fff; /* White background */
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
}
/* Trigger element (the visible part) */
.select-trigger {
display: flex;
align-items: center;
padding: 10px 15px;
cursor: pointer;
border-radius: 5px; /* Match container */
transition: background-color 0.2s ease;
background-color: #fff;
border: none; /* Remove default button border if it were a button */
width: 100%;
box-sizing: border-box;
}
.select-trigger:hover {
background-color: #f0f0f0; /* Light gray hover */
}
/* Selected item display */
.selected-avatar {
margin-right: 10px;
display: inline-block; /* Needed for sizing */
}
.avatar-icon {
width: 30px;
height: 30px;
border-radius: 50%; /* Circular avatars */
object-fit: cover; /* For image backgrounds */
vertical-align: middle; /* Align with text */
border: 1px solid #ccc; /* Subtle border */
background-color: #eee; /* Placeholder background */
}
.selected-name {
flex-grow: 1; /* Take available space */
color: #333;
text-align: left;
font-weight: bold;
}
.dropdown-arrow {
margin-left: 10px;
font-size: 0.8em;
color: #888; /* Gray arrow */
}
/* Dropdown Menu */
.avatar-select-dropdown {
position: absolute;
top: calc(100% + 5px); /* Position below trigger */
left: 0;
right: 0;
background-color: #fff;
border: 1px solid #ccc;
border-top: none; /* Avoid double border */
border-radius: 0 0 5px 5px; /* Rounded bottom corners */
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
z-index: 10;
max-height: 250px; /* Limit height */
overflow-y: auto; /* Add scroll if needed */
display: none; /* Hidden by default */
}
.avatar-select-dropdown.is-open {
display: block; /* Show when open */
}
/* Search Bar */
.search-bar {
width: calc(100% - 30px); /* Padding included */
padding: 10px 15px;
border: none;
border-bottom: 1px solid #eee; /* Separator */
outline: none; /* Remove focus outline */
background-color: #fdfdfd; /* Very light background */
color: #333;
font-size: 14px;
box-sizing: border-box; /* Include padding in width */
border-radius: 0; /* Ensure no rounding here */
}
.search-bar::placeholder {
color: #aaa; /* Lighter placeholder text */
}
.search-bar:focus {
background-color: #f5f5f5; /* Slightly darker on focus */
}
/* Options List */
.options-list {
/* Container styling handled by dropdown */
}
/* Individual Option Item */
.option-item {
display: flex;
align-items: center;
padding: 12px 15px;
cursor: pointer;
border-bottom: 1px solid #eee; /* Item separator */
transition: background-color 0.2s ease;
background-color: #fff;
}
.option-item:last-child {
border-bottom: none; /* No border on the last item */
}
.option-item:hover {
background-color: #f0f0f0; /* Light gray hover */
}
.option-item .avatar-icon {
margin-right: 12px;
border: 1px solid #ddd; /* Slightly darker border for avatars in list */
}
.option-item .selected-name {
color: #333;
font-weight: normal; /* Default weight */
}
JavaScript (script.js):
const userData = [
{ id: 1, name: "Alice", svg: 'data:image/svg+xml,<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><defs><filter id="bwFilter"><feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0, 0 0 0 1 0"/></filter></defs><circle cx="50" cy="50" r="45" fill="none" stroke="%23333" stroke-width="5" filter="url(%23bwFilter)"/><circle cx="50" cy="40" r="15" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/><path d="M30 65 Q 50 85, 70 65" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/></svg>' },
{ id: 2, name: "Bob", svg: 'data:image/svg+xml,<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><defs><filter id="bwFilter"><feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0, 0 0 0 1 0"/></defs><circle cx="50" cy="50" r="45" fill="none" stroke="%23333" stroke-width="5" filter="url(%23bwFilter)"/><circle cx="50" cy="35" r="20" fill="%23eee" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/><path d="M25 70 h50" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/></svg>' },
{ id: 3, name: "Charlie", svg: 'data:image/svg+xml,<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><defs><filter id="bwFilter"><feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0, 0 0 0 1 0"/></defs><circle cx="50" cy="50" r="45" fill="none" stroke="%23333" stroke-width="5" filter="url(%23bwFilter)"/><path d="M70 60 L 50 80 L 30 60" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/><circle cx="50" cy="45" r="12" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/></svg>' },
{ id: 4, name: "Diana", svg: 'data:image/svg+xml,<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><defs><filter id="bwFilter"><feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0, 0 0 0 1 0"/></defs><circle cx="50" cy="50" r="45" fill="none" stroke="%23333" stroke-width="5" filter="url(%23bwFilter)"/><path d="M50 30 V 60" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/><path d="M30 50 Q 50 70 70 50" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/></svg>' },
{ id: 5, name: "Ethan", svg: 'data:image/svg+xml,<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><defs><filter id="bwFilter"><feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0, 0 0 0 1 0"/></defs><circle cx="50" cy="50" r="45" fill="none" stroke="%23333" stroke-width="5" filter="url(%23bwFilter)"/><path d="M50 30 L 30 55 L 50 75 L 70 55 Z" fill="%23eee" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/></svg>' },
{ id: 6, name: "Fiona", svg: 'data:image/svg+xml,<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><defs><filter id="bwFilter"><feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0, 0 0 0 1 0"/></defs><circle cx="50" cy="50" r="45" fill="none" stroke="%23333" stroke-width="5" filter="url(%23bwFilter)"/><circle cx="50" cy="45" r="20" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/><path d="M35 70 Q 50 85 65 70" fill="none" stroke="%23333" stroke-width="4" filter="url(%23bwFilter)"/></svg>' },
];
const selectTrigger = document.getElementById('selectTrigger');
const selectedName = document.getElementById('selectedName');
const avatarDropdown = document.getElementById('avatarDropdown');
const searchBar = document.getElementById('searchBar');
const optionsList = document.getElementById('optionsList');
const triggerAvatar = selectTrigger.querySelector('.selected-avatar .avatar-icon');
let currentSelectedValue = null;
// Function to create an SVG element from data URI
function createSvgAvatar(svgDataUri, altText = 'Avatar') {
const img = document.createElement('img');
img.src = svgDataUri;
img.alt = altText;
img.classList.add('avatar-icon');
// Apply the BW filter via CSS class if needed, or assume SVG is designed B&W
// img.classList.add('bw-svg'); // Add if using filter approach
return img;
}
// Function to render all options
function renderOptions(options) {
optionsList.innerHTML = ''; // Clear existing options
options.forEach(user => {
const optionItem = document.createElement('div');
optionItem.classList.add('option-item');
optionItem.dataset.value = user.id; // Store value for selection
optionItem.dataset.name = user.name;
optionItem.dataset.svg = user.svg;
const avatarElement = createSvgAvatar(user.svg, user.name);
const nameSpan = document.createElement('span');
nameSpan.classList.add('selected-name'); // Reuse class for styling consistency
nameSpan.textContent = user.name;
optionItem.appendChild(avatarElement);
optionItem.appendChild(nameSpan);
optionsList.appendChild(optionItem);
});
}
// Function to filter options based on search input
function handleSearch() {
const searchTerm = searchBar.value.toLowerCase();
const options = optionsList.querySelectorAll('.option-item');
options.forEach(option => {
const name = option.dataset.name.toLowerCase();
if (name.includes(searchTerm)) {
option.style.display = 'flex'; // Show matching option
} else {
option.style.display = 'none'; // Hide non-matching option
}
});
}
// Function to handle option selection
function handleSelect(event) {
// Ensure the click originated from an option item, not its children directly if structure changes
const optionItem = event.target.closest('.option-item');
if (!optionItem) return;
currentSelectedValue = optionItem.dataset.value;
selectedName.textContent = optionItem.dataset.name;
// Update the trigger avatar visually
triggerAvatar.remove(); // Remove previous avatar if any
const newAvatar = createSvgAvatar(optionItem.dataset.svg, optionItem.dataset.name);
// Insert the new avatar at the beginning of the trigger's first child div
selectTrigger.querySelector('.selected-avatar').prepend(newAvatar);
avatarDropdown.classList.remove('is-open'); // Close dropdown
}
// --- Event Listeners ---
// Toggle dropdown visibility
selectTrigger.addEventListener('click', (event) => {
event.stopPropagation(); // Prevent closing when clicking the trigger itself
avatarDropdown.classList.toggle('is-open');
});
// Close dropdown when clicking outside
document.addEventListener('click', (event) => {
if (!selectTrigger.contains(event.target) && !avatarDropdown.contains(event.target)) {
avatarDropdown.classList.remove('is-open');
}
});
// Filter options on search input
searchBar.addEventListener('input', handleSearch);
// Handle selection clicks using event delegation on the options list
optionsList.addEventListener('click', handleSelect);
// --- Initial Setup ---
renderOptions(userData);
Explanation:
HTML Structure:
.avatar-select-container: The main wrapper, sets position: relative..select-trigger: Displays the currently selected item (avatar and name) and the dropdown arrow. It acts as the button to open the menu..avatar-select-dropdown: The menu container, hidden by default (display: none). It contains the search bar and the list of options..search-bar: Standard text input for filtering..options-list: A container where JavaScript will dynamically add the option items..option-item: Represents a single selectable option, containing an avatar (<img> with SVG data) and a name (<span>). Includes data-* attributes to store the value and display info.#bwFilter): Defined in the <defs> section. This filter matrix effectively removes all color, resulting in black shapes on a transparent background (or whatever the SVG background is). It's applied to the SVG elements via the filter="url(#bwFilter)" attribute.CSS Styling:
#fff, #f8f8f8, #f0f0f0, #ccc, #eee, #888, #333..select-trigger is styled to look clean and clickable..avatar-icon is styled to be circular with a subtle border..avatar-select-dropdown appears below the trigger when the .is-open class is added.JavaScript Interactivity:
userData: An array holding sample data, including SVG data URIs for the avatars. Remember to URL-encode the SVG content when putting it into the data URI (e.g., # becomes %23).renderOptions: Populates the #optionsList with divs based on the userData.handleSearch: Filters the displayed options based on the input value in the search bar. It shows/hides .option-item elements.handleSelect: Updates the .select-trigger with the chosen avatar and name, then closes the dropdown. It uses to ensure the click event is handled correctly even if the user clicks on the avatar or name span within an option item.This setup provides a functional and visually consistent black and white avatar select component.
closest().is-open class on the dropdown.input event on the search bar triggers filtering.handleSelect via event delegation.