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

Fix keydown event for Search engines dropdown #483

Merged
merged 9 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifest(firefox).json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Material You NewTab",
"version": "3.0",
"version": "3.0.19",
"description": "A Simple New Tab (browser's home page) inspired by Google's 'Material You' design.",
"permissions": [
"bookmarks",
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Material You NewTab",
"version": "3.0",
"version": "3.0.19",
"description": "A Simple New Tab (browser's home page) inspired by Google's 'Material You' design.",
"optional_permissions": ["bookmarks"],
"host_permissions": [
Expand Down
42 changes: 30 additions & 12 deletions scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,12 @@ document.addEventListener("click", function (event) {
// Search function
document.addEventListener("DOMContentLoaded", () => {
const dropdown = document.querySelector(".dropdown-content");

dropdown.addEventListener("click", (event) => {
if (dropdown.style.display === "block") {
event.stopPropagation();
dropdown.style.display = "none";
searchInput.focus();
}
})

Expand All @@ -581,6 +583,11 @@ document.addEventListener("DOMContentLoaded", () => {
document.querySelector(".dropdown-btn").addEventListener("click", function (event) {
const resultBox = document.getElementById("resultBox");
if (resultBox.classList.toString().includes("show")) return;

// Clear selected state and reset index when dropdown opens
dropdownItems.forEach(item => item.classList.remove("selected"));
selectedIndex = -1;

dropdown.style.display = dropdown.style.display === "block" ? "none" : "block";
});

Expand All @@ -605,7 +612,7 @@ document.addEventListener("DOMContentLoaded", () => {
// get the parent
const parent = sortedDropdowns[0]?.parentNode;

// Append the items. if parent exists.
// Append the items if parent exists.
if (parent) {
sortedDropdowns.forEach(item => parent.appendChild(item));
}
Expand All @@ -618,11 +625,9 @@ document.addEventListener("DOMContentLoaded", () => {
const radioButton = document.querySelector(`input[type="radio"][value="engine${engine}"]`);
const selector = `*[data-engine-name=${element.getAttribute("data-engine-name")}]`;

// console.log(element, selector);

radioButton.checked = true;

// Swap The dropdown. and sort them
// Swap the dropdown and sort them
swapDropdown(selector);
sortDropdown()

Expand All @@ -632,7 +637,8 @@ document.addEventListener("DOMContentLoaded", () => {

// Make entire search-engine div clickable
document.querySelectorAll(".search-engine").forEach((engineDiv) => {
engineDiv.addEventListener("click", () => {
engineDiv.addEventListener("click", (event) => {
event.stopPropagation();
const radioButton = engineDiv.querySelector('input[type="radio"]');

radioButton.checked = true;
Expand All @@ -641,11 +647,15 @@ document.addEventListener("DOMContentLoaded", () => {

const selector = `[data-engine="${radioButtonValue}"]`;

// Swap The dropdown.
// Swap the dropdown
swapDropdown(selector);
sortDropdown()
sortDropdown();

localStorage.setItem("selectedSearchEngine", radioButton.value);

const searchBar = document.querySelector(".searchbar");
searchInput.focus();
searchBar.classList.add("active");
});
});

Expand Down Expand Up @@ -748,19 +758,27 @@ document.addEventListener("DOMContentLoaded", () => {
document.querySelector(".dropdown").addEventListener("keydown", function (event) {
if (dropdown.style.display === "block") {
if (event.key === "ArrowDown") {
event.preventDefault(); // Prevent the page from scrolling
selectedIndex = (selectedIndex + 1) % dropdownItems.length; // Move down, loop around
} else if (event.key === "ArrowUp") {
event.preventDefault(); // Prevent the page from scrolling
selectedIndex = (selectedIndex - 1 + dropdownItems.length) % dropdownItems.length; // Move up, loop around
} else if (event.key === "Enter") {
const selector = ".dropdown-content .selected";
const engine = element.getAttribute("data-engine");
const selectedItem = document.querySelector(".dropdown-content .selected");
const engine = selectedItem.getAttribute("data-engine");
const radioButton = document.querySelector(`input[type="radio"][value="engine${engine}"]`);

radioButton.checked = true;

// Swap The dropdown. and sort them
swapDropdown(selector);
sortDropdown()
// Swap the dropdown and sort them
swapDropdown(`*[data-engine="${engine}"]`);
sortDropdown();

localStorage.setItem("selectedSearchEngine", radioButton.value);

// Close the dropdown after selection
dropdown.style.display = "none";
searchInput.focus();
}
updateSelection();
}
Expand Down