Skip to content

Commit

Permalink
Turbo frame refinements (#502)
Browse files Browse the repository at this point in the history
* Separate .app-main and #main turbo-frame (reverting #497) but move turbo-frame into layout partial
* Remove turbo stream layout in order to apply more granular update behavior
* Introduce #content turbo-frame in layout partial and deprecate index_turbo_frame
  • Loading branch information
spohlenz authored Sep 26, 2024
1 parent c8bf895 commit 4da5426
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 64 deletions.
38 changes: 10 additions & 28 deletions app/helpers/trestle/turbo/frame_helper.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
module Trestle
module Turbo
module FrameHelper
# Renders a <turbo-frame> container for an index view. An index turbo frame
# is by default reloadable (it will be refreshed by the `reload` turbo stream
# action), and has the Turbo visit behavior always set to "advance".
#
# attributes - Additional HTML attributes to add to the <turbo-frame> tag
#
# Examples
#
# <%= index_turbo_frame do %> ...
#
# <%= index_turbo_frame id: "articles-index",
# data: {
# reloadable_url_value: admin.path(:articles)
# } do %> ...
#
# Returns a HTML-safe String.
def index_turbo_frame(**attributes, &block)
defaults = {
id: "index",
data: {
controller: "reloadable",
turbo_action: "advance"
}
}

tag.turbo_frame(**defaults.merge(attributes), &block)
end

# Renders a <turbo-frame> container for an instance/resource view. A resource
# turbo frame sets its DOM id from the given instance and has a default target of
# "_top" (except for modal requests).
Expand All @@ -53,6 +25,16 @@ def resource_turbo_frame(instance, **attributes, &block)

tag.turbo_frame(**defaults.merge(attributes), &block)
end

# [DEPRECATED]
#
# The #content turbo-frame found in app/views/trestle/application/_layout.html.erb
# is now used as a common top-level hook for the 'reloadable' Stimulus controller.
def index_turbo_frame(**attributes, &block)
Trestle.deprecator.warn("The index_turbo_frame helper is deprecated and will be removed in future versions of Trestle.")
yield
nil
end
end
end
end
4 changes: 2 additions & 2 deletions app/views/layouts/trestle/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
<div class="app-container">
<%= render "trestle/shared/header" %>

<turbo-frame class="app-main" id="main" data-turbo-action="advance">
<div class="app-main">
<%= yield %>
</turbo-frame>
</div>

<%= render "trestle/shared/footer" %>
</div>
Expand Down
4 changes: 0 additions & 4 deletions app/views/layouts/trestle/admin.turbo_stream.erb

This file was deleted.

40 changes: 22 additions & 18 deletions app/views/trestle/application/_layout.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<%= render "header", hide_breadcrumbs: local_assigns.fetch(:hide_breadcrumbs, false) if local_assigns.fetch(:header, true) %>
<turbo-frame id="main" data-turbo-action="advance">
<%= render "header", hide_breadcrumbs: local_assigns.fetch(:hide_breadcrumbs, false) if local_assigns.fetch(:header, true) %>

<div class="main-content-area" data-scroll-target>
<turbo-frame id="flash">
<%= render "trestle/flash/flash" %>
</turbo-frame>
<div class="main-content-area" data-scroll-target>
<turbo-frame id="flash">
<%= render "trestle/flash/flash" %>
</turbo-frame>

<%= render "utilities" %>
<%= render "tabs", data: { controller: "tabs tab-errors" } %>
<turbo-frame id="content" data-controller="reloadable" data-turbo-action="advance">
<%= render "utilities" %>
<%= render "tabs", data: { controller: "tabs tab-errors" } %>

<% if local_assigns.fetch(:wrapper, true) %>
<%= container do |c| %>
<%= yield %>
<% if local_assigns.fetch(:wrapper, true) %>
<%= container do |c| %>
<%= yield %>

<% c.sidebar do %>
<%= content_for(:sidebar) %>
<% end if content_for?(:sidebar) %>
<% end %>
<% else %>
<%= yield %>
<% end %>
</div>
<% c.sidebar do %>
<%= content_for(:sidebar) %>
<% end if content_for?(:sidebar) %>
<% end %>
<% else %>
<%= yield %>
<% end %>
</turbo-frame>
</div>
</turbo-frame>
1 change: 1 addition & 0 deletions app/views/trestle/resource/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<%= turbo_stream.replace admin.build_instance({}, params), template: "trestle/resource/#{instance.persisted? ? "show" : "new"}" %>
<%= turbo_stream.reload if modal_request? && instance.persisted? %>
2 changes: 2 additions & 0 deletions app/views/trestle/resource/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<%= turbo_stream.close_modal instance %>
<%= turbo_stream.flash %>
<%= turbo_stream.reload %>
22 changes: 10 additions & 12 deletions app/views/trestle/resource/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@
<% end if admin.scopes.any? %>

<%= render layout: "layout" do %>
<%= index_turbo_frame do %>
<% if hook?("resource.index.header") %>
<header class="main-content-header">
<%= hook("resource.index.header") %>
</header>
<% end %>
<% if hook?("resource.index.header") %>
<header class="main-content-header">
<%= hook("resource.index.header") %>
</header>
<% end %>

<%= render "trestle/table/table", table: admin.table, collection: collection %>
<%= render "trestle/table/table", table: admin.table, collection: collection %>

<footer class="main-content-footer">
<%= hook("resource.index.footer") %>
<footer class="main-content-footer">
<%= hook("resource.index.footer") %>

<%= pagination collection: collection, entry_name: admin.model_name %>
</footer>
<% end %>
<%= pagination collection: collection, entry_name: admin.model_name %>
</footer>
<% end %>
1 change: 1 addition & 0 deletions app/views/trestle/resource/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<%= turbo_stream.replace instance, template: "trestle/resource/show" %>
<%= turbo_stream.reload if modal_request? %>

0 comments on commit 4da5426

Please sign in to comment.