added search functionality

This commit is contained in:
gurusabarish
2022-11-27 21:39:55 +05:30
parent 5a872cf17a
commit 80e9a7971b
7 changed files with 154 additions and 23 deletions
+46
View File
@@ -56,4 +56,50 @@ li > .dropdown-toggle:focus{
.dropdown-item:focus, .dropdown-item:active {
background-color: var(--secondary-color) !important;
}
#search {
border-radius: 1rem !important;
background-color: var(--secondary-color);
color: var(--text-color);
border-color: var(--background-color) !important;
transition: none;
}
#search:focus {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
#search-content {
display: none;
border-radius: 1rem;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
z-index: 9999;
width: 10%;
max-height: 350px;
background-color: var(--secondary-color) !important;
position: absolute;
overflow: scroll;
}
#search-results > .card {
background-color: transparent !important;
border: none;
margin-bottom: 10px;
}
#search-results > .card :hover {
border-radius: 1rem !important;
background-color: var(--background-color) !important;
transition: .2s;
}
#search-results .card a {
opacity: 0.9;
display: inline-block;
text-decoration: none;
color: var(--text-color) !important;
}
#search-results .card a:hover {
color: var(--primary-color) !important;
}
+52
View File
@@ -0,0 +1,52 @@
async function searchOnChange(evt) {
let searchQuery = evt.target.value;
if (searchQuery !== "") {
const searchButtonEle = document.querySelectorAll("#search");
let searchButtonPosition;
if (window.innerWidth > 768) {
searchButtonPosition = searchButtonEle[0].getBoundingClientRect();
document.getElementById("search-content").style.width = "500px";
} else {
searchButtonPosition = searchButtonEle[1].getBoundingClientRect();
document.getElementById("search-content").style.width = "430px";
}
console.log(searchButtonPosition);
document.getElementById("search-content").style.display = "block";
document.getElementById("search-content").style.top =
searchButtonPosition.top + 50 + "px";
document.getElementById("search-content").style.left =
searchButtonPosition.left + "px";
let searchJson = await fetch("/index.json").then((res) => res.json());
let searchResults = searchJson.filter((item) => {
return (
item.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
item.description.toLowerCase().includes(searchQuery.toLowerCase()) ||
item.content.toLowerCase().includes(searchQuery.toLowerCase())
);
});
if (searchResults.length > 0) {
let searchResultsHtml = "";
searchResults.map((item) => {
searchResultsHtml += `<div class="card">
<a href="${item.permalink}">
<div class="p-3">
<h5>${item.title}</h5>
<div>${item.description}</div>
</div>
</a>
</div>`;
});
document.getElementById("search-results").innerHTML = searchResultsHtml;
} else {
let searchResultsHtml = `<p class="text-center py-3">No results found for "${searchQuery}"</p>`;
document.getElementById("search-results").innerHTML = searchResultsHtml;
}
console.log(searchResults);
} else {
document.getElementById("search-content").style.display = "none";
document.getElementById("search-results").innerHTML = "";
}
}