Skip to content

Commit 482f9cd

Browse files
authored
Sidebar Search Filter
allows users to filter content in the sidebar
1 parent 64cf1aa commit 482f9cd

5 files changed

Lines changed: 96 additions & 2 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Contribuing Guide
1+
# Contributing Guide
22

33
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.
44

lib/graphql-docs/layouts/assets/_sass/_sidebar.scss

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,38 @@
6969
}
7070
}
7171
}
72+
#search {
73+
display: flex;
74+
position: relative;
75+
align-items: center;
76+
border: 1px solid #ddd;
77+
border-radius: 5px;
78+
padding: 0.01em 16px;
79+
margin-bottom: 20px;
80+
81+
img {
82+
position: absolute;
83+
left: 10px;
84+
height: 16px;
85+
width: 16px;
86+
}
87+
88+
input {
89+
height: 24px;
90+
line-height: 1.5;
91+
width: 100%;
92+
padding-left: 15px;
93+
background-color: transparent;
94+
color: #444;
95+
border: none;
96+
font-size: 14px;
97+
font-family: 'ProximaNova-Semibold';
98+
}
99+
100+
input:focus {
101+
outline: none;
102+
}
103+
}
72104
}
73105

74106
#sidebar-mobile {
Lines changed: 3 additions & 0 deletions
Loading

lib/graphql-docs/layouts/default.html

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,71 @@
99
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/3.2.2/anchor.min.js"></script>
1010
<script>
1111

12-
// Add anchors on DOMContentLoaded
1312
document.addEventListener("DOMContentLoaded", function(event) {
13+
// Add anchors
1414
anchors.options = {
1515
placement: 'left',
1616
visible: 'hover',
1717
icon: '¶'
1818
};
1919
anchors.add('h2, h3, h4, h5, h6, .anchored');
20+
21+
// Add listener for search filter input
22+
const sidebarDiv = document.getElementById('sidebar');
23+
const searchDiv = sidebarDiv.querySelector('#search')
24+
25+
if (searchDiv) {
26+
const searchInput = searchDiv.querySelector('input');
27+
const menuElements = sidebarDiv.querySelectorAll('ul.menu-root');
28+
29+
const listener = debounce((event) => {
30+
const searchValue = event.target.value;
31+
applySearchFilter(searchValue, menuElements)
32+
}, 500);
33+
34+
searchInput.addEventListener('input', listener);
35+
}
2036
});
2137

38+
function debounce(func, wait) {
39+
let timeout;
40+
41+
return function executedFunction(...args) {
42+
const later = () => {
43+
clearTimeout(timeout);
44+
func(...args);
45+
};
46+
47+
clearTimeout(timeout);
48+
timeout = setTimeout(later, wait);
49+
};
50+
}
51+
52+
function applySearchFilter(searchValue, menuElements) {
53+
menuElements.forEach(menuElement => {
54+
const listElements = menuElement.getElementsByTagName('li');
55+
let hasVisibleElements = false;
56+
Array.from(listElements).forEach(listElement => {
57+
let contains = true
58+
if (searchValue.length > 0) {
59+
const anchorElement = listElement.querySelector('a');
60+
const textContent = anchorElement.innerText;
61+
contains = textContent.toLowerCase().includes(searchValue.toLowerCase());
62+
}
63+
64+
// Hide the list element if its text does not contain the search value
65+
listElement.style.display = contains ? '' : 'none';
66+
67+
if (contains) {
68+
hasVisibleElements = true;
69+
}
70+
});
71+
72+
// Hide the entire menu if none of the list items are visible
73+
const menuParentElement = menuElement.closest('li')
74+
menuParentElement.style.display = hasVisibleElements ? '' : 'none';
75+
});
76+
}
2277
</script>
2378
</head>
2479
<body>

lib/graphql-docs/layouts/includes/sidebar.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<div id="search">
2+
<img src="<%= base_url %>/assets/images/search.svg">
3+
<input autocomplete="off" placeholder="Search" />
4+
</div>
15
<ul class="categories">
26
<li>
37
<ul class="menu-root">

0 commit comments

Comments
 (0)