Skip to content

Instantly share code, notes, and snippets.

@spilth
Created February 25, 2022 02:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spilth/c4a9e1f030200bd8f4ceb4b6efb3ce74 to your computer and use it in GitHub Desktop.
Save spilth/c4a9e1f030200bd8f4ceb4b6efb3ce74 to your computer and use it in GitHub Desktop.
Tabbed Navigation with Turbo Frames
<p style="color: green"><%= notice %></p>
<h1>Albums</h1>
<%= turbo_frame_tag :items do %>
<% @albums.each do |album| %>
<%= render album %>
<p>
<%= link_to "Show this album", album %>
</p>
<% end %>
<% end %>
<%= link_to "New album", new_album_path %>
a {
text-decoration: none;
padding: 4px;
border-radius: 4px;
background: #fff;
color: #000;
}
a.active {
background: #000;
color: #fff;
}
<p style="color: green"><%= notice %></p>
<h1>Books</h1>
<%= turbo_frame_tag :items do %>
<% @books.each do |book| %>
<%= render book %>
<p>
<%= link_to "Show this book", book %>
</p>
<% end %>
<% end %>
<%= link_to "New book", new_book_path %>
<h1>Tabs</h1>
<div data-controller="tabs">
<%= link_to "Albums", albums_path, class: "active",
data: { turbo_frame: :items, tabs_target: "tab", action: "click->tabs#toggle" } %>
<%= link_to "Books", books_path,
data: { turbo_frame: :items, tabs_target: "tab", action: "click->tabs#toggle" } %>
</div>
<%= turbo_frame_tag :items, src: albums_path, target: :_top, loading: :lazy do %>
<p>Loading...</p>
<% end %>
import {Controller} from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["tab"];
toggle(event) {
this.tabTargets.forEach(tab => {
if (tab === event.target) {
tab.classList.add("active");
} else {
tab.classList.remove("active");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment