Skip to content

Commit

Permalink
Merge pull request #153 from hugovk/search-focus
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Oct 4, 2023
2 parents 3f48c0c + c33e6ae commit 77918d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python_docs_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h3>{{ _('Navigation') }}</h3>
{%- if builder != "htmlhelp" %}
<div class="inline-search" role="search">
<form class="inline-search" action="{{ pathto('search') }}" method="get">
<input placeholder="{{ _('Quick search') }}" aria-label="{{ _('Quick search') }}" type="search" name="q" />
<input placeholder="{{ _('Quick search') }}" aria-label="{{ _('Quick search') }}" type="search" name="q" id="search-box" />
<input type="submit" value="{{ _('Go') }}" />
</form>
</div>
Expand Down Expand Up @@ -76,6 +76,7 @@ <h3>{{ _('Navigation') }}</h3>
{%- if not embedded %}
<script type="text/javascript" src="{{ pathto('_static/copybutton.js', 1) }}"></script>
<script type="text/javascript" src="{{ pathto('_static/menu.js', 1) }}"></script>
<script type="text/javascript" src="{{ pathto('_static/search-focus.js', 1) }}"></script>
<script type="text/javascript" src="{{ pathto('_static/themetoggle.js', 1) }}"></script>
{%- endif -%}
{%- endif -%}
Expand Down
21 changes: 21 additions & 0 deletions python_docs_theme/static/search-focus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function isInputFocused() {
const activeElement = document.activeElement;
return (
activeElement.tagName === 'INPUT' ||
activeElement.tagName === 'TEXTAREA' ||
activeElement.isContentEditable
);
}

document.addEventListener('keydown', function(event) {
if (event.key === '/') {
if (!isInputFocused()) {
// Prevent "/" from being entered in the search box
event.preventDefault();

// Set the focus on the search box
const searchBox = document.getElementById('search-box');
searchBox.focus();
}
}
});

0 comments on commit 77918d1

Please sign in to comment.