Skip to content

Commit cb2f0ee

Browse files
committed
rework search behavior with anchor
Signed-off-by: Kai Wagner <kai.wagner@percona.com>
1 parent 8d7ea33 commit cb2f0ee

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

app/controllers/topics_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ def search
9797

9898
if @search_query.blank?
9999
respond_to do |format|
100-
format.html { redirect_to topics_path(focus: 1) }
101-
format.turbo_stream { redirect_to topics_path(focus: 1) }
100+
format.html { redirect_to topics_path(anchor: "search") }
101+
format.turbo_stream { redirect_to topics_path(anchor: "search") }
102102
end
103103
return
104104
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
static targets = ["input", "section"]
5+
6+
connect() {
7+
this._onHashChange = this.focusIfHash.bind(this)
8+
this._onTurboLoad = this.focusIfHash.bind(this)
9+
this._onLinkClick = this.handleLinkClick.bind(this)
10+
window.addEventListener("hashchange", this._onHashChange)
11+
document.addEventListener("turbo:load", this._onTurboLoad)
12+
document.addEventListener("click", this._onLinkClick)
13+
this.focusIfHash()
14+
}
15+
16+
disconnect() {
17+
window.removeEventListener("hashchange", this._onHashChange)
18+
document.removeEventListener("turbo:load", this._onTurboLoad)
19+
document.removeEventListener("click", this._onLinkClick)
20+
}
21+
22+
focusIfHash() {
23+
const isSearch = window.location.hash === "#search"
24+
if (this.hasSectionTarget) {
25+
this.sectionTarget.classList.toggle("is-search-focus", isSearch)
26+
}
27+
if (isSearch && this.hasInputTarget) {
28+
this.inputTarget.focus()
29+
}
30+
}
31+
32+
handleLinkClick(event) {
33+
if (event.defaultPrevented) return
34+
const link = event.target.closest('a[href="#search"]')
35+
if (!link) return
36+
window.location.hash = "search"
37+
setTimeout(() => this.focusIfHash(), 0)
38+
}
39+
}

app/views/layouts/application.html.slim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ html data-theme="light"
4242
span.tagline PostgreSQL Hackers Archive
4343
.nav-links
4444
= link_to "Topics", topics_path, class: "nav-link"
45-
= link_to "Search", topics_path(focus: 1), class: "nav-link"
45+
- search_link = content_for?(:search_sidebar) ? "#search" : topics_path(anchor: "search")
46+
= link_to "Search", search_link, class: "nav-link"
4647
= link_to "Statistics", stats_path, class: "nav-link"
4748
.nav-right
4849
button.nav-link.theme-toggle type="button" aria-label="Toggle theme" data-controller="theme" data-action="click->theme#toggle"

app/views/topics/_sidebar.html.slim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- content_for :sidebar do
2+
- content_for :search_sidebar, true
23
.sidebar
34

45
- if user_signed_in?
@@ -8,11 +9,10 @@
89
button.mark-aware-button data-action="click->topics-aware#markVisibleAware" Mark displayed threads aware
910
button.mark-aware-button.secondary data-action="click->topics-aware#markAllAware" Mark everything up to now aware
1011

11-
.sidebar-section class=(params[:focus].present? ? "is-search-focus" : nil)
12-
h3.sidebar-heading Search
12+
.sidebar-section#search data-controller="search-focus" data-search-focus-target="section"
1313
.sidebar-search.sidebar-content
1414
= form_with url: search_topics_path, method: :get, local: true do |f|
15-
= f.text_field :q, placeholder: "Search topics and messages...", class: "search-input", value: search_query, autofocus: params[:focus].present?
15+
= f.text_field :q, placeholder: "Search topics and messages...", class: "search-input", value: search_query, data: { "search-focus-target": "input" }
1616
= f.submit "Search", class: "search-button"
1717

1818
.sidebar-section

0 commit comments

Comments
 (0)