Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Arrow Key Selection of Search Suggestion #202

Merged
merged 12 commits into from
Nov 22, 2024
78 changes: 69 additions & 9 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,10 @@ const applySelectedTheme = (colorValue) => {
.dark-theme #sujhaw {
fill: #b1b1b1;
}

.resultItem.active {
background-color: var(--darkColor-dark);;
}
`;
document.head.appendChild(darkThemeStyleTag);

Expand Down Expand Up @@ -1140,7 +1144,7 @@ document.getElementById("0NIHK").onclick = () => {
}
}

// ------------search suggestions ---------------
// ------------Search Suggestions---------------

// Show the result box
function showResultBox() {
Expand All @@ -1153,8 +1157,10 @@ function hideResultBox() {
resultBox.classList.remove('show');
//resultBox.style.display = "none";
}
showResultBox()
hideResultBox()

showResultBox();
hideResultBox();

document.getElementById("searchQ").addEventListener("input", async function () {
const searchsuggestionscheckbox = document.getElementById("searchsuggestionscheckbox");
if (searchsuggestionscheckbox.checked) {
Expand All @@ -1174,27 +1180,80 @@ document.getElementById("searchQ").addEventListener("input", async function () {
const suggestions = await getAutocompleteSuggestions(query);

if (suggestions == "") {
hideResultBox()
hideResultBox();
} else {
// Clear the result box
resultBox.innerHTML = '';

// Add suggestions to the result box
suggestions.forEach((suggestion) => {
suggestions.forEach((suggestion, index) => {
const resultItem = document.createElement("div");
resultItem.classList.add("resultItem");
resultItem.textContent = suggestion;
resultItem.setAttribute("data-index", index);
resultItem.onclick = () => {
var resultlink = searchEngines[selectedOption] + encodeURIComponent(suggestion);
window.location.href = resultlink;
};
resultBox.appendChild(resultItem);
});
showResultBox()
showResultBox();
}

} else {
hideResultBox()
hideResultBox();
}
}
});

let isMouseOverResultBox = false;
// Track mouse entry and exit within the resultBox
resultBox.addEventListener("mouseenter", () => {
isMouseOverResultBox = true;
// Remove keyboard highlight
const activeItem = resultBox.querySelector(".active");
if (activeItem) {
activeItem.classList.remove("active");
}
});

resultBox.addEventListener("mouseleave", () => {
isMouseOverResultBox = false;
});

document.getElementById("searchQ").addEventListener("keydown", function (e) {
if (isMouseOverResultBox) {
return; // Ignore keyboard events if the mouse is in the resultBox
}
const activeItem = resultBox.querySelector(".active");
let currentIndex = activeItem ? parseInt(activeItem.getAttribute("data-index")) : -1;

if (resultBox.children.length > 0) {
if (e.key === "ArrowDown") {
e.preventDefault();
if (activeItem) {
activeItem.classList.remove("active");
}
currentIndex = (currentIndex + 1) % resultBox.children.length;
resultBox.children[currentIndex].classList.add("active");

// Ensure the active item is visible within the result box
const activeElement = resultBox.children[currentIndex];
activeElement.scrollIntoView({ block: "nearest" });
} else if (e.key === "ArrowUp") {
e.preventDefault();
if (activeItem) {
activeItem.classList.remove("active");
}
currentIndex = (currentIndex - 1 + resultBox.children.length) % resultBox.children.length;
resultBox.children[currentIndex].classList.add("active");

// Ensure the active item is visible within the result box
const activeElement = resultBox.children[currentIndex];
activeElement.scrollIntoView({ block: "nearest" });
} else if (e.key === "Enter" && activeItem) {
e.preventDefault();
activeItem.click();
}
}
});
Expand Down Expand Up @@ -1257,16 +1316,17 @@ async function getAutocompleteSuggestions(query) {
}
}


// Hide results when clicking outside
document.addEventListener("click", function (event) {
const searchbar = document.getElementById("searchbar");
const resultBox = document.getElementById("resultBox");

if (!searchbar.contains(event.target)) {
hideResultBox()
hideResultBox();
}
});
// ------------End of Search Suggestions---------------

// ------------Showing & Hiding Menu-bar ---------------
const menuButton = document.getElementById("menuButton");
const menuBar = document.getElementById("menuBar");
Expand Down
88 changes: 45 additions & 43 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -107,51 +107,53 @@
--always-show-dock-background: 0;
}
.resultBox {
max-height: 250px;
overflow-y: auto;

position: absolute;
top: 1;
left: 0;
width: 100%;
border-radius: var(--round);

padding: 10px;
scrollbar-width: none; /* For Firefox */

/* Initially hidden */
opacity: 0;
z-index: -1000;
transform: translateY(-70px);
transition: opacity 0.5s ease, transform 0.5s ease, z-index 0.5s ease;
pointer-events: none;
}

.resultBox::-webkit-scrollbar {
display: none; /* For Chrome, Safari, and Edge */
}

/* Visible state: when you want the result box to appear */
.resultBox.show {
opacity: 1;
visibility: visible;
transform: translateY(0);
z-index: 1000;
pointer-events: all;
}

.resultItem {
padding: 10px;
cursor: pointer;
font-size: 1.2rem;
color: var(--textColorDark-blue);
}
max-height: 222px;
overflow-y: auto;
position: absolute;
top: 1;
left: 0;
width: 100%;
border-radius: var(--round);
padding: 5px 10px;
scrollbar-width: none; /* For Firefox */
/* Initially hidden */
opacity: 0;
z-index: -1000;
transform: translateY(-70px);
transition: opacity 0.5s ease, transform 0.5s ease, z-index 0.5s ease;
pointer-events: none;
}
.resultBox::-webkit-scrollbar {
display: none; /* For Chrome, Safari, and Edge */
}
/* Visible state: when you want the result box to appear */
.resultBox.show {
opacity: 1;
visibility: visible;
transform: translateY(0);
z-index: 1000;
pointer-events: all;
}
.resultItem {
padding: 8px;
cursor: pointer;
font-size: 1.1rem;
color: var(--textColorDark-blue);
}

.resultItem.active,
.resultItem:hover {
background-color: var(--darkColor-blue);
border-radius: 15px;
color: #fff;
}
background-color: var(--darkColor-blue);
border-radius: 15px;
itz-rj-here marked this conversation as resolved.
Show resolved Hide resolved
color: #fff;
}

.spacer {
height: 65px; /* Adjust this value to create space */
}
Expand Down