-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle varied search field configurations (#807)
Co-authored-by: Chris Holdgraf <choldgraf@gmail.com>
- Loading branch information
Showing
6 changed files
with
114 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Test of persistent search field | ||
|
||
There should also be a persistent (non-hidden) search field in the left sidebar of this page, and if you use the keyboard shortcut it should focus the persistent field, not overlay the hidden one. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 2 additions & 36 deletions
38
src/pydata_sphinx_theme/theme/pydata_sphinx_theme/components/search-button.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,14 @@ | ||
{# Search page has its own UI / UX for search #} | ||
<!-- A button that will trigger a search field to be displayed on click. --> | ||
{% set containerClass="search-button__search-container" %} | ||
<script> | ||
var toggleSearchField = () => { | ||
// Class to make the search field appear and expand the clickable div behind it | ||
// Note that `.show` will only have an effect on pages that aren't `search.html` | ||
let button = document.getElementById("bd-search-button"); | ||
button.classList.toggle('show'); | ||
|
||
// We'll grab the elements we need to modify for the search field | ||
let form = document.querySelector("form.bd-search"); | ||
let input = form.querySelector("input"); | ||
|
||
// Change the symbol to `meta key` if we are a Mac | ||
var isMac = window.navigator.platform.toUpperCase().indexOf('MAC')>=0; | ||
if (isMac) { | ||
let kbd = form.querySelector("kbd.kbd-shortcut__modifier"); | ||
kbd.innerText = "⌘"; | ||
}; | ||
|
||
// Select the search input field, and focus the page on it | ||
input.focus(); | ||
input.select(); | ||
input.scrollIntoView({block: "center"}); | ||
}; | ||
|
||
// Add an event listener for this function for Ctrl/Cmd + K | ||
window.addEventListener("keydown", (event) => { | ||
if ((event.ctrlKey || event.metaKey) && event.code == "KeyK") { | ||
event.preventDefault(); | ||
toggleSearchField(); | ||
}}, true); | ||
</script> | ||
|
||
<!-- An overlay that will expand in the background and cause the search to disappear when clicked --> | ||
<button class="btn btn-sm navbar-btn search-button" id="bd-search-button" onclick="toggleSearchField()" title="{{ _('Toggle search field') }}"> | ||
<button class="btn btn-sm navbar-btn search-button" id="bd-search-button" title="{{ _('Search') }}"> | ||
<i class="fas fa-search"></i> | ||
</button> | ||
|
||
{#- Only on pages not `search.html`. This is because we only want the in-page search on this page -#} | ||
{%- if pagename !="search" -%} | ||
{#- This will be hidden by default until click -#} | ||
<div class="search-button__overlay" onclick="toggleSearchField()"></div> | ||
<div class="search-button__overlay"></div> | ||
<div class="{{ containerClass }}"> | ||
{% include "../components/search-field.html" %} | ||
</div> | ||
{% endif %} |