Skip to content
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

Add controller for search focus for slash keypress #10395

Merged
merged 4 commits into from
Nov 30, 2021

Conversation

miketheman
Copy link
Member

@miketheman miketheman commented Nov 22, 2021

While having the search key autofocus is forbidden, having a keypress
focus the search field is a desirable feature.

Add a new Stimulus Controller that listens to the / keypress and
focuses the input on the search field.

Note: Currently applies only to the home page.

Fixes #6099

Signed-off-by: Mike Fiedler miketheman@gmail.com

While having the search key autofocus is forbidden, having a keypress
focus the search field is a desirable feature.

Add a new Stumulus Controller that listens to the `/` keypress and
focuses the input on the search field.

Note: Currently applies only to the home page.

Refs: pypi#6099

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@di
Copy link
Member

di commented Nov 22, 2021

Thanks! I think if we add a check that the cursor is not currently in an input field, we could add this to the base template, so this is usable everywhere the search box is currently present.

@miketheman
Copy link
Member Author

Happy to look at that change as well - but then I'm not sure how the base template property would be inherited to the homepage's search box?

@di
Copy link
Member

di commented Nov 22, 2021

It wouldn't, we'd need to add it to both the form on the homepage (which you've already done here) and to this form: https://github.com/pypa/warehouse/blob/597938cff04261fd55f59dd538982b4bfcad4621/warehouse/templates/base.html#L225-L233

Instead of registering a listener, use an action to inform the
controller to take an action.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
In order to prevent the feature from taking over other input fields,
only activate when NOT in an input field, so the `/` key can be used in
any input field and not interfere with inputs.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@miketheman
Copy link
Member Author

@di I think I'm ready for another review! I'm happier with this implementation - it's more tightly scoped, albeit increasing the html line length.

@di di merged commit f351a20 into pypi:main Nov 30, 2021
@di
Copy link
Member

di commented Nov 30, 2021

Thanks, this is great!

@miketheman miketheman deleted the slash_focus_search branch December 1, 2021 01:29
domdfcoding pushed a commit to domdfcoding/warehouse that referenced this pull request Jun 7, 2022
* Add controller for search focus for slash keypress

While having the search key autofocus is forbidden, having a keypress
focus the search field is a desirable feature.

Add a new Stumulus Controller that listens to the `/` keypress and
focuses the input on the search field.

Note: Currently applies only to the home page.

Refs: pypi#6099

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* refactor: use stimulus action instead of a listener

Instead of registering a listener, use an action to inform the
controller to take an action.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* feat: add search focus to base template

In order to prevent the feature from taking over other input fields,
only activate when NOT in an input field, so the `/` key can be used in
any input field and not interfere with inputs.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create keyboard shortcut to focus search bar
2 participants