Skip to content

Commit

Permalink
Sidebar Search Filter
Browse files Browse the repository at this point in the history
allows users to filter content in the sidebar
  • Loading branch information
denisahearn authored Feb 9, 2025
1 parent 64cf1aa commit 482f9cd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contribuing Guide
# Contributing Guide

Contributions to this project are welcome. If you have an idea for a bigger change, [open an issue first](https://github.com/brettchalupa/graphql-docs/issues/new/choose) and we can discuss it.

Expand Down
32 changes: 32 additions & 0 deletions lib/graphql-docs/layouts/assets/_sass/_sidebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@
}
}
}
#search {
display: flex;
position: relative;
align-items: center;
border: 1px solid #ddd;
border-radius: 5px;
padding: 0.01em 16px;
margin-bottom: 20px;

img {
position: absolute;
left: 10px;
height: 16px;
width: 16px;
}

input {
height: 24px;
line-height: 1.5;
width: 100%;
padding-left: 15px;
background-color: transparent;
color: #444;
border: none;
font-size: 14px;
font-family: 'ProximaNova-Semibold';
}

input:focus {
outline: none;
}
}
}

#sidebar-mobile {
Expand Down
3 changes: 3 additions & 0 deletions lib/graphql-docs/layouts/assets/images/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 56 additions & 1 deletion lib/graphql-docs/layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,71 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/3.2.2/anchor.min.js"></script>
<script>

// Add anchors on DOMContentLoaded
document.addEventListener("DOMContentLoaded", function(event) {
// Add anchors
anchors.options = {
placement: 'left',
visible: 'hover',
icon: '¶'
};
anchors.add('h2, h3, h4, h5, h6, .anchored');

// Add listener for search filter input
const sidebarDiv = document.getElementById('sidebar');
const searchDiv = sidebarDiv.querySelector('#search')

if (searchDiv) {
const searchInput = searchDiv.querySelector('input');
const menuElements = sidebarDiv.querySelectorAll('ul.menu-root');

const listener = debounce((event) => {
const searchValue = event.target.value;
applySearchFilter(searchValue, menuElements)
}, 500);

searchInput.addEventListener('input', listener);
}
});

function debounce(func, wait) {
let timeout;

return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};

clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}

function applySearchFilter(searchValue, menuElements) {
menuElements.forEach(menuElement => {
const listElements = menuElement.getElementsByTagName('li');
let hasVisibleElements = false;
Array.from(listElements).forEach(listElement => {
let contains = true
if (searchValue.length > 0) {
const anchorElement = listElement.querySelector('a');
const textContent = anchorElement.innerText;
contains = textContent.toLowerCase().includes(searchValue.toLowerCase());
}

// Hide the list element if its text does not contain the search value
listElement.style.display = contains ? '' : 'none';

if (contains) {
hasVisibleElements = true;
}
});

// Hide the entire menu if none of the list items are visible
const menuParentElement = menuElement.closest('li')
menuParentElement.style.display = hasVisibleElements ? '' : 'none';
});
}
</script>
</head>
<body>
Expand Down
4 changes: 4 additions & 0 deletions lib/graphql-docs/layouts/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<div id="search">
<img src="<%= base_url %>/assets/images/search.svg">
<input autocomplete="off" placeholder="Search" />
</div>
<ul class="categories">
<li>
<ul class="menu-root">
Expand Down

0 comments on commit 482f9cd

Please sign in to comment.