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

automatically focus in the textbox when you start a search #1426

Merged
merged 10 commits into from
Oct 16, 2024
14 changes: 14 additions & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
<% mobile_logo_path = SiteSetting['MobileLogoPath'] %>
<% sticky_header = user_preference('sticky_header', community: false) == 'true' %>

<script>
$(document).ready(function () {
$('a[data-header-slide="#search-slide"]').on('click', focusSearch);
});

var search = false;
function focusSearch() {
Copy link
Member

@Oaphi Oaphi Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of notes:

  • avoid using var at all costs, const for unchangeable cariables and let for reassignable is a must:
  • the focusSearch function has a "scope bleed" - it should be called something like toggleSearchFocus and accept a single boolean parameter, which you then pass in the handler function. I'll provide example implementation a bit later;
  • avoid using function style of delaring pure functions, arrow syntax has better semantics in this context;

search = !search;
if (search) {
$('#search').focus();
}
}
</script>

<header class="header is-small has-margin-0<%=' is-dark' if SiteSetting['SiteHeaderIsDark'] %><%= ' sticky' if sticky_header %>">
<div class="container header--container">
<div class="header--brand">
Expand Down
Loading