Skip to content

Add controller for search focus for slash keypress #10395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Controller } from "stimulus";

export default class extends Controller {
static targets = ["searchField"];

focusSearchField(event) {
// When we receive a keydown event, check if it's `/` and that we're not
// already focused on the search field, or any other input field.
if (event.code === "Slash" && event.target.tagName !== "INPUT") {
// Prevent the key from being handled as an actual input
event.preventDefault();
this.searchFieldTarget.focus();
}
}
}
2 changes: 1 addition & 1 deletion warehouse/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@

<form class="search-form search-form--primary" action="{{ request.route_path('search') }}" role="search">
<label for="search" class="sr-only">{% trans %}Search PyPI{% endtrans %}</label>
<input id="search" class="search-form__search" type="text" name="q" placeholder="{% trans %}Search projects{% endtrans %}" value="{{ term }}" autocomplete="off" autocapitalize="off" spellcheck="false">
<input id="search" class="search-form__search" type="text" name="q" placeholder="{% trans %}Search projects{% endtrans %}" value="{{ term }}" autocomplete="off" autocapitalize="off" spellcheck="false" data-controller="search-focus" data-action="keydown@window->search-focus#focusSearchField" data-target="search-focus.searchField">
{% block search_form_extra_inputs %}{% endblock %}
<button type="submit" class="search-form__button">
<i class="fa fa-search" aria-hidden="true"></i>
Expand Down
2 changes: 1 addition & 1 deletion warehouse/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h1 class="homepage-banner__title">{{ banner }}</h1>
<label for="search" class="sr-only">{% trans %}Search PyPI{% endtrans %}</label>

<!-- The following input is intentionally not autofocused, see https://github.com/pypa/warehouse/issues/6088 for more details -->
<input id="search" class="search-form__search large-input" type="text" name="q" placeholder="{% trans %}Search projects{% endtrans %}" autocomplete="off" autocapitalize="off" spellcheck="false">
<input id="search" class="search-form__search large-input" type="text" name="q" placeholder="{% trans %}Search projects{% endtrans %}" autocomplete="off" autocapitalize="off" spellcheck="false" data-controller="search-focus" data-action="keydown@window->search-focus#focusSearchField" data-target="search-focus.searchField">

<button type="submit" class="search-form__button">
<i class="fa fa-search" aria-hidden="true"></i>
Expand Down