1+ // Update the tab UI to reflect the newly-requested state. This function is called
2+ // by a click event handler in the tab UI. It follows a two-step process:
3+ function swapTabs ( new_target ) {
4+ // 1. Reset all tabs to base condition
5+ document . querySelectorAll ( '.tab-link' ) . forEach ( ( tab ) => {
6+ tab . classList . remove ( 'active' ) ;
7+ tab . removeAttribute ( 'aria-current' ) ;
8+ } ) ;
9+ // 2. Add "active" class and aria-current attribute to the newly-active tab link
10+ const currentTabLink = document . querySelector ( `.tab-link[href*="tab=${ new_target } "]` ) ;
11+ if ( currentTabLink ) {
12+ currentTabLink . classList . add ( 'active' ) ;
13+ currentTabLink . setAttribute ( 'aria-current' , 'page' ) ;
14+ }
15+ }
16+
117// Loading spinner behavior for pagination (Turbo Frame updates)
218document . addEventListener ( 'turbo:frame-render' , function ( event ) {
319 if ( window . pendingFocusAction === 'pagination' ) {
@@ -23,16 +39,8 @@ document.addEventListener('turbo:frame-render', function(event) {
2339 // console.log(`Updated tab input value to: ${queryParam}`);
2440 }
2541
26- // update active tab styling
27- // remove active class from all tabs
28- document . querySelectorAll ( '.tab-link' ) . forEach ( ( tab ) => {
29- tab . classList . remove ( 'active' ) ;
30- } ) ;
31- // add active class to current tab
32- const currentTabLink = document . querySelector ( `.tab-link[href*="tab=${ queryParam } "]` ) ;
33- if ( currentTabLink ) {
34- currentTabLink . classList . add ( 'active' ) ;
35- }
42+ // Remove the spinner now that things are ready
43+ document . getElementById ( 'search-results' ) . classList . remove ( 'spinner' ) ;
3644
3745 // Clear the pending action
3846 window . pendingFocusAction = null ;
@@ -51,7 +59,17 @@ document.addEventListener('click', function(event) {
5159
5260 // Handle tab clicks
5361 if ( clickedElement . closest ( '.tab-navigation' ) ) {
62+ const clickedParams = new URLSearchParams ( clickedElement . search ) ;
63+ const newTab = clickedParams . get ( 'tab' ) ;
64+
65+ // Throw the spinner on the search results immediately
66+ document . getElementById ( 'search-results' ) . classList . add ( 'spinner' ) ;
67+
68+ // Position the window at the top of the results
5469 window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
70+
71+ swapTabs ( newTab ) ;
72+
5573 window . pendingFocusAction = 'tab' ;
5674 }
5775} ) ;
0 commit comments