JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog Search</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="search-container">
<h1>Blog Search</h1>
<div class="search-box">
<input type="text" id="search-input" placeholder="Search blog posts...">
<button id="search-button">Search</button>
</div>
<div id="search-suggestions" class="suggestions-container"></div>
</div>
<div id="content" class="content-container">
<!-- Search results will appear here -->
<div class="initial-state">
<h2>Welcome to Blog Search</h2>
<p>Enter a search term above to find relevant blog posts.</p>
</div>
</div>
<script src="search.js"></script>
</body>
</html>
CSS
/* Content Container */
.content-container {
max-width: 800px;
margin: 20px auto 50px;
background-color: white;
padding: 20px;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.initial-state {
text-align: center;
padding: 40px 20px;
color: #7f8c8d;
}
.suggestion-item {
border-radius:5px;
border:1px solid black;
min-width:300px;
max-width:fit-content;
padding:5px;
}
.suggestion-item:hover {
cursor: pointer;
background-color:gray;
color:white;
}
/* Search Results */
.result-item {
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #eee;
}
.result-item:last-child {
border-bottom: none;
margin-bottom: 0;
padding-bottom: 0;
}
.result-item h3 {
margin-top: 0;
color: #2c3e50;
}
.result-item p {
margin-bottom: 0;
color: #7f8c8d;
}
/* Loading indicator */
.loading {
text-align: center;
padding: 20px;
color: #7f8c8d;
font-style: italic;
}
JavaScript
// Global variables
let searchTerm = '';
let suggestionTimeoutId = null;
// Initialize after DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
// Get DOM elements
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
const suggestionsDiv = document.getElementById('search-suggestions');
// Add event listeners
searchInput.addEventListener('keyup', handleSearchInput);
searchInput.addEventListener('keypress', handleKeyPress);
searchButton.addEventListener('click', () => performFullSearch(searchInput.value));
// Close suggestions when clicking outside
document.addEventListener('click', (event) => {
if (event.target !== suggestionsDiv &&
event.target !== searchInput &&
!suggestionsDiv.contains(event.target)) {
hideSuggestions();
}
});
});
// Handle each keystroke in the search input
function handleSearchInput(event) {
// Get current search term
searchTerm = event.target.value.trim();
// Debounce suggestions search
if (suggestionTimeoutId) {
clearTimeout(suggestionTimeoutId);
}
// Only show suggestions if we have at least 2 characters
if (searchTerm.length >= 2) {
suggestionTimeoutId = setTimeout(() => {
fetchAndShowSuggestions(searchTerm);
}, 300); // 300ms delay
} else {
hideSuggestions();
}
}
// Handle special keys like Enter
function handleKeyPress(event) {
if (event.key === 'Enter') {
// Hide suggestions
hideSuggestions();
// Perform full search
performFullSearch(searchTerm);
event.preventDefault();
}
}
// Fetch blog title suggestions based on current search term
function fetchAndShowSuggestions(term) {
// This function should call your API and get matching blog titles
getBlogTitleSuggestions(term).then(titles => {
...