<%= turbo_frame_tag 'modal' %>
import { Controller } from "@hotwired/stimulus"
import { Turbo } from "@hotwired/turbo-rails"
export default class extends Controller {
connect() {
this.onKeyDown = (e) => {
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
if (document.activeElement?.tagName === "INPUT" || document.activeElement?.tagName === "TEXTAREA") return
e.preventDefault()
Turbo.visit("/palette", { frame: "modal" })
}
}
document.addEventListener("keydown", this.onKeyDown)
}
disconnect() {
document.removeEventListener("keydown", this.onKeyDown)
}
}
<%= turbo_frame_tag 'modal' do %>
<div class="fixed inset-0 bg-black/40"></div>
<div class="fixed inset-0 flex items-start justify-center p-6">
<div class="w-full max-w-xl rounded bg-white p-4 shadow">
<%= form_with url: palette_path, method: :post do |f| %>
<%= f.search_field :q, autofocus: true, placeholder: 'Search…', class: 'w-full rounded border p-2' %>
<% end %>
</div>
</div>
<% end %>
A command palette feels like a SPA feature, but you can do it Hotwire-first: place a turbo_frame_tag 'modal' in the layout and load the palette HTML into it. A small Stimulus controller listens for meta+k and navigates the modal frame to /palette. The palette is just a server-rendered form; submitting it can redirect to the selected URL. This keeps the UI accessible and doesn’t require client-side routing. It also reduces build complexity: no React just for a command palette. The key is to keep the controller careful about focus and not intercept keystrokes in text inputs. I also support Esc to close by navigating the modal frame back to blank.