Created
February 25, 2022 02:46
-
-
Save spilth/c4a9e1f030200bd8f4ceb4b6efb3ce74 to your computer and use it in GitHub Desktop.
Tabbed Navigation with Turbo Frames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a { | |
text-decoration: none; | |
padding: 4px; | |
border-radius: 4px; | |
background: #fff; | |
color: #000; | |
} | |
a.active { | |
background: #000; | |
color: #fff; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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