<link href="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.css" rel="stylesheet">
<script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
<div class="container">
<div class="header">
<h1>Autocomplete Search</h1>
<p>Type to search programming languages</p>
</div>
<div class="autocomplete-wrapper">
<div class="search-container">
<ion-icon name="search-outline" class="search-icon"></ion-icon>
<input
type="text"
id="autocompleteInput"
placeholder="Search programming languages..."
autocomplete="off"
>
<ion-icon name="close-circle-outline" class="clear-icon" id="clearIcon"></ion-icon>
</div>
<div class="suggestions-list" id="suggestionsList"></div>
</div>
<div class="selected-items" id="selectedItems"></div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
color: #ffffff;
}
.container {
max-width: 600px;
width: 100%;
}
.header {
text-align: center;
margin-bottom: 50px;
}
.header h1 {
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 10px;
background: linear-gradient(135deg, #00FFFF 0%, #00cccc 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.header p {
color: #a0a0a0;
font-size: 1rem;
}
.autocomplete-wrapper {
position: relative;
width: 100%;
}
.search-container {
position: relative;
width: 100%;
background: rgba(255, 255, 255, 0.05);
border: 2px solid rgba(0, 255, 255, 0.2);
border-radius: 12px;
transition: all 0.3s ease;
backdrop-filter: blur(10px);
}
.search-container:focus-within {
border-color: #00FFFF;
box-shadow: 0 0 30px rgba(0, 255, 255, 0.3);
transform: translateY(-2px);
}
.search-icon {
position: absolute;
left: 20px;
top: 50%;
transform: translateY(-50%);
color: #00FFFF;
font-size: 24px;
pointer-events: none;
z-index: 2;
}
#autocompleteInput {
width: 100%;
padding: 20px 60px 20px 60px;
font-size: 1.1rem;
background: transparent;
border: none;
outline: none;
color: #ffffff;
font-family: inherit;
}
#autocompleteInput::placeholder {
color: rgba(255, 255, 255, 0.4);
}
.clear-icon {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
color: #00FFFF;
font-size: 24px;
cursor: pointer;
opacity: 0;
transition: all 0.3s ease;
z-index: 2;
}
.clear-icon.visible {
opacity: 1;
}
.clear-icon:hover {
color: #00cccc;
transform: translateY(-50%) scale(1.1);
}
.suggestions-list {
position: absolute;
top: calc(100% + 10px);
left: 0;
width: 100%;
background: rgba(26, 26, 46, 0.95);
border: 2px solid rgba(0, 255, 255, 0.2);
border-radius: 12px;
max-height: 400px;
overflow-y: auto;
opacity: 0;
visibility: hidden;
transform: translateY(-10px);
transition: all 0.3s ease;
backdrop-filter: blur(20px);
z-index: 1000;
}
.suggestions-list.show {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.suggestions-list::-webkit-scrollbar {
width: 8px;
}
.suggestions-list::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.05);
border-radius: 10px;
}
.suggestions-list::-webkit-scrollbar-thumb {
background: #00FFFF;
border-radius: 10px;
}
.suggestion-item {
padding: 15px 20px;
cursor: pointer;
transition: all 0.2s ease;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
display: flex;
align-items: center;
gap: 15px;
}
.suggestion-item:last-child {
border-bottom: none;
}
.suggestion-item:hover {
background: rgba(0, 255, 255, 0.1);
padding-left: 25px;
}
.suggestion-icon {
color: #00FFFF;
font-size: 20px;
flex-shrink: 0;
}
.suggestion-text {
color: #ffffff;
font-size: 1rem;
}
.suggestion-item.highlighted {
background: rgba(0, 255, 255, 0.15);
}
.no-results {
padding: 30px;
text-align: center;
color: rgba(255, 255, 255, 0.5);
}
.selected-items {
margin-top: 30px;
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.selected-item {
background: rgba(0, 255, 255, 0.1);
border: 1px solid #00FFFF;
padding: 8px 16px;
border-radius: 25px;
display: inline-flex;
align-items: center;
gap: 10px;
animation: fadeIn 0.3s ease;
}
.selected-item-text {
color: #00FFFF;
}
.remove-item {
cursor: pointer;
color: #00FFFF;
font-size: 18px;
transition: transform 0.2s ease;
}
.remove-item:hover {
transform: scale(1.2) rotate(90deg);
}
@keyframes fadeIn {
from {
opacity: 0;
transform: scale(0.8);
}
to {
opacity: 1;
transform: scale(1);
}
}
@media (max-width: 768px) {
.header h1 {
font-size: 2rem;
}
#autocompleteInput {
padding: 18px 50px 18px 50px;
font-size: 1rem;
}
.suggestions-list {
max-height: 300px;
}
}
// Data source for autocomplete
const programmingLanguages = [
{ name: 'JavaScript', icon: 'logo-javascript' },
{ name: 'Python', icon: 'logo-python' },
{ name: 'Java', icon: 'logo-java' },
{ name: 'TypeScript', icon: 'code-outline' },
{ name: 'C++', icon: 'code-slash-outline' },
{ name: 'C#', icon: 'code-slash-outline' },
{ name: 'Ruby', icon: 'code-outline' },
{ name: 'PHP', icon: 'code-outline' },
{ name: 'Swift', icon: 'logo-apple' },
{ name: 'Kotlin', icon: 'code-outline' },
{ name: 'Go', icon: 'code-outline' },
{ name: 'Rust', icon: 'code-outline' },
{ name: 'SQL', icon: 'server-outline' },
{ name: 'HTML', icon: 'logo-html5' },
{ name: 'CSS', icon: 'logo-css3' },
{ name: 'R', icon: 'stats-chart-outline' },
{ name: 'Scala', icon: 'code-outline' },
{ name: 'Perl', icon: 'code-outline' },
{ name: 'Dart', icon: 'code-outline' },
{ name: 'Objective-C', icon: 'logo-apple' }
];
const input = document.getElementById('autocompleteInput');
const suggestionsList = document.getElementById('suggestionsList');
const clearIcon = document.getElementById('clearIcon');
const selectedItemsContainer = document.getElementById('selectedItems');
let selectedItems = [];
let currentFocus = -1;
// Event listener for input
input.addEventListener('input', function() {
const value = this.value.trim();
// Show/hide clear icon
if (value) {
clearIcon.classList.add('visible');
} else {
clearIcon.classList.remove('visible');
}
if (value.length > 0) {
const filtered = programmingLanguages.filter(lang =>
lang.name.toLowerCase().includes(value.toLowerCase())
);
displaySuggestions(filtered);
} else {
closeSuggestions();
}
currentFocus = -1;
});
// Clear input
clearIcon.addEventListener('click', function() {
input.value = '';
clearIcon.classList.remove('visible');
closeSuggestions();
input.focus();
});
// Display suggestions
function displaySuggestions(suggestions) {
suggestionsList.innerHTML = '';
if (suggestions.length === 0) {
suggestionsList.innerHTML = '<div class="no-results">No results found</div>';
suggestionsList.classList.add('show');
return;
}
suggestions.forEach((item, index) => {
const div = document.createElement('div');
div.className = 'suggestion-item';
div.innerHTML = `
<ion-icon name="${item.icon}" class="suggestion-icon"></ion-icon>
<span class="suggestion-text">${item.name}</span>
`;
div.addEventListener('click', function() {
selectItem(item);
});
suggestionsList.appendChild(div);
});
suggestionsList.classList.add('show');
}
// Close suggestions
function closeSuggestions() {
suggestionsList.classList.remove('show');
currentFocus = -1;
}
// Select item
function selectItem(item) {
if (!selectedItems.some(selected => selected.name === item.name)) {
selectedItems.push(item);
renderSelectedItems();
}
input.value = '';
clearIcon.classList.remove('visible');
closeSuggestions();
input.focus();
}
// Render selected items
function renderSelectedItems() {
selectedItemsContainer.innerHTML = '';
selectedItems.forEach((item, index) => {
const div = document.createElement('div');
div.className = 'selected-item';
div.innerHTML = `
<ion-icon name="${item.icon}"></ion-icon>
<span class="selected-item-text">${item.name}</span>
<ion-icon name="close-outline" class="remove-item" onclick="removeItem(${index})"></ion-icon>
`;
selectedItemsContainer.appendChild(div);
});
}
// Remove selected item
function removeItem(index) {
selectedItems.splice(index, 1);
renderSelectedItems();
}
// Keyboard navigation
input.addEventListener('keydown', function(e) {
const items = suggestionsList.querySelectorAll('.suggestion-item');
if (e.key === 'ArrowDown') {
e.preventDefault();
currentFocus++;
addActive(items);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
currentFocus--;
addActive(items);
} else if (e.key === 'Enter') {
e.preventDefault();
if (currentFocus > -1 && items[currentFocus]) {
items[currentFocus].click();
}
} else if (e.key === 'Escape') {
closeSuggestions();
}
});
function addActive(items) {
if (!items || items.length === 0) return;
removeActive(items);
if (currentFocus >= items.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = items.length - 1;
items[currentFocus].classList.add('highlighted');
items[currentFocus].scrollIntoView({ block: 'nearest' });
}
function removeActive(items) {
items.forEach(item => item.classList.remove('highlighted'));
}
// Close suggestions when clicking outside
document.addEventListener('click', function(e) {
if (!e.target.closest('.autocomplete-wrapper')) {
closeSuggestions();
}
});
No comments yet. Be the first!