Skip to content

Commit 58eedd6

Browse files
committed
Add loading spinner for Turbo Frame updates
Why these changes are being introduced: Users experience long delays when switching between search tabs due to slow Primo API responses, with no visual feedback indicating that the system is processing their request. Relevant ticket(s): - [USE-134](https://mitlibraries.atlassian.net/browse/USE-134) How this addresses that need: This adds a CSS spinner animation that appears during Turbo Frame updates. It uses Turbo's built-in `busy` attribute for reliable state detection. Turbo also adds `aria-busy` for screen reader users. Side effects of this change: - Adds data-turbo-action="advance" to search results frame. - Spinner may briefly show during fast API responses. - Deprecated Turbo API syntax has been updated to current syntax. - This is different the from built-in Turbo progress bar that we use for page reloads, as that feature is not available for Turbo Frame updates. It's fairly straightforward to rebuild in JS if we decide we want consistent loading progress indicators.
1 parent b4195f1 commit 58eedd6

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

app/assets/stylesheets/partials/_results.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,36 @@
119119
line-height: 1.1;
120120
}
121121
}
122+
123+
// Loading indicator for turbo frame updates
124+
// https://discuss.hotwired.dev/t/show-spinner-everytime-async-frame-reloads/3483/3
125+
@keyframes spinner {
126+
to {
127+
transform: rotate(360deg);
128+
}
129+
}
130+
131+
[busy]:not([no-spinner]) {
132+
position: relative;
133+
134+
> * {
135+
opacity: 0.5;
136+
}
137+
138+
&::after {
139+
content: '';
140+
box-sizing: border-box;
141+
position: fixed;
142+
top: 50%;
143+
left: 50%;
144+
width: 2.5rem;
145+
height: 2.5rem;
146+
margin-top: -1.25rem;
147+
margin-left: -1.25rem;
148+
border-radius: 50%;
149+
border: 0.275rem solid rgba($color-red-500, 0.3);
150+
border-top-color: $color-red-500;
151+
animation: spinner 0.6s linear infinite;
152+
z-index: 9999;
153+
}
154+
}

app/javascript/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import "@hotwired/turbo-rails"
33
import "controllers"
44

55
// Show the progress bar after 200 milliseconds, not the default 500
6-
window.Turbo.setProgressBarDelay(200);
6+
Turbo.config.drive.progressBarDelay = 200;

app/views/search/results.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<%= render(partial: 'trigger_tacos') if tacos_enabled? %>
1616

17-
<%= turbo_frame_tag "search-results" do %>
17+
<%= turbo_frame_tag "search-results", data: { turbo_action: "advance" } do %>
1818
<div class="layout-3q1q layout-band top-space">
1919
<main id="results" class="col3q wrap-results">
2020
<% if @show_primo_continuation %>

0 commit comments

Comments
 (0)