diff --git a/assets/css/style.css b/assets/css/style.css index f12b129..76a51a1 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -102,14 +102,11 @@ body { background: #00660f; } -/*placeholder for search and login section*/ + .search-and-login { display: flex; align-items: center; position: relative; - /* width: 277px; - height: 49px; - border: #000 solid 1px; */ } .search-input { @@ -126,6 +123,33 @@ body { } + +.search-results { + position: absolute; + top: 110%; + left: 0; + background-color: var(--panel-background); + border: 1px solid #A8C9AA; + border-radius: 10px; + box-shadow: 0 4px 8px rgba(0,0,0,0.1); + width: 220px; + z-index: 1000; + overflow: hidden; +} + +.search-result-item { + padding: 10px 16px; + font-size: 14px; + cursor: pointer; + color: var(--common-text); + font-family: var(--common-font); +} + +.search-result-item a{ + text-decoration: none; + color: var(--title-color); +} + .login-link { background-color: #C7E2C9; height: 36px; diff --git a/assets/js/main.js b/assets/js/main.js index b97a6ba..f10fbf5 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -64,3 +64,66 @@ document.addEventListener('DOMContentLoaded', function () { } }); }); + +const searchInput = document.getElementById('search-input'); +const searchResults = document.getElementById('search-results'); + +let searchTimeout; + +searchInput.addEventListener('input', function () { + clearTimeout(searchTimeout); + + searchTimeout = setTimeout(async () => { + let searchValue = this.value.trim(); + + + if (searchValue.length < 3) { + searchResults.innerHTML = ''; + searchResults.hidden = true; + return; + } + + const formData = new FormData(); + formData.append('action', 'search'); + formData.append('query', searchValue); + + const response = await fetch('/ajax', { + method: 'POST', + body: formData + }); + + const json = await response.json(); + + if (!response.ok) { + const message = json.error || 'Something went wrong.'; + showToastify(message, 'error'); + return; + } + + const results = json.result; + + searchResults.innerHTML = ''; + + if (results.length > 0) { + results.forEach(result => { + searchResults.innerHTML += ` +
+ `; + }); + searchResults.hidden = false; + } else { + searchResults.innerHTML = `