Skip to content

Commit

Permalink
Issue scientific-python#607: proof of concept for site search with pa…
Browse files Browse the repository at this point in the history
…gefind
  • Loading branch information
Brian Hawthorne committed May 10, 2024
1 parent 6c111e2 commit b484a52
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ resources
# Auto-generated
doc/content/shortcodes.md
public

# vim droppings
.*.swp
2 changes: 2 additions & 0 deletions layouts/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{ define "main" }}
{{ partial "search.html" . }}

{{ if .Site.Params.hero }}
{{ partial "hero.html" . }}
{{ end }}
Expand Down
4 changes: 4 additions & 0 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
</script>
{{ end }}
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Fira+Sans">

<!-- Support site search -->
<link href="/pagefind/pagefind-ui.css" rel="stylesheet">
<script src="/pagefind/pagefind-ui.js"></script>
5 changes: 5 additions & 0 deletions layouts/partials/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<div id="navbar-menu" class="navbar-menu">

<div class="navbar-end">
<button class="search-button">
<i class="fa-solid fa-magnifying-glass fa-lg" title="Search (Ctrl+k)"></i>
</button>

{{- range $navbar }}
{{- if .sublinks }}
<div class="navbar-item has-dropdown">
Expand All @@ -50,6 +54,7 @@
{{- if .IsTranslated }}
{{ partial "translation.html" . }}
{{- end }}

</div>
</div>
</div>
Expand Down
56 changes: 56 additions & 0 deletions layouts/partials/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<style>
.search-button {
border-radius: 20px;
padding: 8px;
margin-right: 15px;
cursor: pointer;
}
.search-container {
padding: 10px 15px;
}
</style>
<div class="search-container" style="display: none"></div>
<script>
let getElem = (className) => document.getElementsByClassName(className)[0];

window.addEventListener('DOMContentLoaded', (evt) => {
new PagefindUI({
element: ".search-container",
autofocus: true,
showSubResults: true,
});

let searchContainer = getElem('search-container');
let searchInput = getElem('pagefind-ui__search-input');

let showSearch = () => {
searchContainer.style.display ='block';
searchInput.focus();
}

let hideSearch = () => {
searchContainer.style.display ='none';
searchInput.blur();
}

let toggleSearch = (evt) => {
if (searchContainer.style.display === 'none') {
showSearch();
} else {
hideSearch();
}
};

window.addEventListener('keydown', (evt) => {
if (evt.key === 'Escape') {
hideSearch();
}
if (evt.key === 'k' && evt.ctrlKey) {
toggleSearch();
}
});

getElem('search-button').addEventListener('click', toggleSearch);
});
</script>

0 comments on commit b484a52

Please sign in to comment.