Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/stylesheets/partials/_loading_spinner.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}

// Pagination overlay when loading
[busy]:not([no-spinner]) {
.spinner {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the manual addition/removal adds complexity without improving the outcome in a meaningful way. Would you mind either clarifying why this is better or changing it back?

position: relative;
min-height: 400px;
display: block;
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ def link_to_result(result)
end
end

def link_to_tab(target)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner in the view now and easier to test. ❤️

if @active_tab == target.downcase
link_to target, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: target.downcase)),
aria: { current: "page" },
class: "active tab-link",
data: { turbo_frame: "search-results", turbo_action: "advance" }
else
link_to target, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: target.downcase)),
class: "tab-link",
data: { turbo_frame: "search-results", turbo_action: "advance" }
end
end

def view_online(result)
return unless result[:source_link].present?

Expand Down
38 changes: 28 additions & 10 deletions app/javascript/loading_spinner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Update the tab UI to reflect the newly-requested state. This function is called
// by a click event handler in the tab UI. It follows a two-step process:
function swapTabs(new_target) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call consolidating this logic into a function

// 1. Reset all tabs to base condition
document.querySelectorAll('.tab-link').forEach((tab) => {
tab.classList.remove('active');
tab.removeAttribute('aria-current');
});
// 2. Add "active" class and aria-current attribute to the newly-active tab link
const currentTabLink = document.querySelector(`.tab-link[href*="tab=${new_target}"]`);
if (currentTabLink) {
currentTabLink.classList.add('active');
currentTabLink.setAttribute('aria-current', 'page');
}
}

// Loading spinner behavior for pagination (Turbo Frame updates)
document.addEventListener('turbo:frame-render', function(event) {
if (window.pendingFocusAction === 'pagination') {
Expand All @@ -23,16 +39,8 @@ document.addEventListener('turbo:frame-render', function(event) {
// console.log(`Updated tab input value to: ${queryParam}`);
}

// update active tab styling
// remove active class from all tabs
document.querySelectorAll('.tab-link').forEach((tab) => {
tab.classList.remove('active');
});
// add active class to current tab
const currentTabLink = document.querySelector(`.tab-link[href*="tab=${queryParam}"]`);
if (currentTabLink) {
currentTabLink.classList.add('active');
}
// Remove the spinner now that things are ready
document.getElementById('search-results').classList.remove('spinner');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the manual addition/removal adds complexity without improving the outcome in a meaningful way. Would you mind either clarifying why this is better or changing it back?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test without this change - my thinking initially was the spinner wasn't appearing early enough - which seemed to be the case when I simulated low-bandwidth connections working locally.

I'll re-check now that everything is in place.

Copy link
Member Author

@matt-bernhardt matt-bernhardt Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've set up a comparison to show the behavior I was noticing. This applies to two review apps:

  1. The review app for this PR, which has had the spinner CSS class reverted back to the prior state, and keys off commit b261749, can be seen at https://timdex-ui-pi-use-180-tee54nbsx.herokuapp.com/results?booleanType=AND&q=archives&tab=primo
  2. A comparison review app, which still has the spinner defined in a CSS class and added/removed manually. This app keys off commit f35d4ca, which just prior to the tip of this PR. This can be seen at https://timdex-ui-pi-use-180-or-ixjq7j.herokuapp.com/results?booleanType=AND&q=archives&tab=primo

Both apps use the "simulate search latency" feature, but there's another component to this - simulating slow network connections via the browser. The test sequence is:

  1. In your browser, set a network throttle via dev tools (Firefox allows me to go all the way down to 50 Kbps, although the behavior I can see emerges as a flicker at 4G speeds. It is quite pronounced at 2G speeds).
  2. Load a page of resuts, similar to the URL above.
  3. Flip back and forth between tabs - I think it might become more pronounced after doing this a few times, but I can't say that for sure. What you're looking for is that the spinner gets removed just before the tab content actually changes. When the browser throttle is particularly pronounced (i.e. the user has very slow internet connection), there can be a seconds-long gap between the spinner disappearing and the new tab content becoming visible.

I've found that once I've noticed this stutter, I find that I can notice it even on faster connections - but the stutter is minimal / non-existent if we remove the spinner manually as in the other review app.

I may be overthinking this - I like the approach of letting Turbo Frames control the spinner - but having seen that there are cases where the spinner doesn't get removed at the right moment, I worry about inducing confusion when a user starts to scroll after the spinner disappears, only to have a new batch of results appear after a moment.


// Clear the pending action
window.pendingFocusAction = null;
Expand All @@ -51,7 +59,17 @@ document.addEventListener('click', function(event) {

// Handle tab clicks
if (clickedElement.closest('.tab-navigation')) {
const clickedParams = new URLSearchParams(clickedElement.search);
const newTab = clickedParams.get('tab');

// Throw the spinner on the search results immediately
document.getElementById('search-results').classList.add('spinner');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the manual addition/removal adds complexity without improving the outcome in a meaningful way. Would you mind either clarifying why this is better or changing it back?


// Position the window at the top of the results
window.scrollTo({ top: 0, behavior: 'smooth' });

swapTabs(newTab);

window.pendingFocusAction = 'tab';
}
});
Expand Down
10 changes: 2 additions & 8 deletions app/views/search/_source_tabs.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
<!-- Tab Navigation -->
<nav id="tabs" class="tab-navigation" aria-label="Result type navigation">
<ul>
<li>
<%= link_to "Primo", results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: 'primo')),
class: "tab-link #{'active' if @active_tab == 'primo'}",
data: { turbo_frame: "search-results", turbo_action: "advance" } %></li>
<li>
<%= link_to "TIMDEX", results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: 'timdex')),
class: "tab-link #{'active' if @active_tab == 'timdex'}",
data: { turbo_frame: "search-results", turbo_action: "advance" } %></li>
<li><%= link_to_tab("Primo") %></li>
<li><%= link_to_tab("TIMDEX") %></li>
</ul>
</nav>

21 changes: 21 additions & 0 deletions test/helpers/search_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ class SearchHelperTest < ActionView::TestCase
assert_equal 'Sample Document Title', link_to_result(result)
end

test 'link_to_tab builds a link without an aria attribute when that tab is not active' do
@active_tab = 'bar'
actual = link_to_tab('Foo')

assert_select Nokogiri::HTML::Document.parse( actual ), 'a' do |link|
assert_select '[class*=?]', 'active', count: 0
assert_select '[class*=?]', 'tab-link'
assert_select '[aria-current=?]', 'page', count: 0
end
end

test 'link_to_tab builds a link that includes an aria attribute when that tab is active' do
@active_tab = 'foo'
actual = link_to_tab('Foo')

assert_select Nokogiri::HTML::Document.parse( actual ), 'a' do |link|
assert_select link, '[class*=?]', 'active'
assert_select link, '[aria-current=?]', 'page', count: 1
end
end

test 'primo_search_url generates correct Primo URL' do
query = 'machine learning'
expected_url = 'https://mit.primo.exlibrisgroup.com/discovery/search?query=any%2Ccontains%2Cmachine+learning&vid=01MIT_INST%3AMIT'
Expand Down