Helpers / Remote Search Component

Remote Search Component

A standalone command menu that queries your server as you type, streaming results back with Turbo.

Preview

Try typing to filter (this demo uses static results; in your app they come from the server):

<%# Basic usage: %>
<%= basecoat_remote_search_tag("/posts/search") %>

<%# With custom turbo frame name: %>
<%= basecoat_remote_search_tag("/posts/search", turbo_frame: "custom_frame") %>

<%# With custom placeholder: %>
<%= basecoat_remote_search_tag("/posts/search", placeholder: "Search posts...") %>

The server side

Your controller responds with a Turbo Stream:

# posts_controller.rb
def search
  @posts = Post.where("title LIKE ?", "%#{params[:query]}%")

  respond_to do |format|
    format.turbo_stream
  end
end

And the stream template updates the frame (posts/search.turbo_stream.erb):

<%= turbo_stream.update "_posts_search" do %>
  <% @posts.each do |post| %>
    <%= link_to post.title, post_path(post), role: "menuitem" %>
  <% end %>
<% end %>

Options

OptionDescriptionDefault
url (required)The URL to fetch search results from
turbo_frameCustom frame nameunderscored URL, e.g. _posts_search
placeholderPlaceholder text"Type a command or search..."
data_emptyMessage when nothing matches"No results found."
classesExtra CSS classes on the component

Requires Stimulus

The install task copies a small search_controller.js that debounces input and renders the Turbo Stream response.