@@ -63,17 +63,32 @@ function setupCloseModalButtons() {
63
63
function setupTabs ( ) {
64
64
const $tabLinks = getAll ( '.tabs a' ) ;
65
65
66
+ function activateTab ( $tabLink ) {
67
+ const activeLink = document . querySelector ( '.tabs .is-active' ) ;
68
+ const activeTabContent = document . querySelector ( '.tab-content.is-active' ) ;
69
+ const targetId = $tabLink . dataset . target ;
70
+ const targetTabContent = document . getElementById ( targetId ) ;
71
+
72
+ activeLink . classList . remove ( 'is-active' ) ;
73
+ $tabLink . parentNode . classList . add ( 'is-active' ) ;
74
+ if ( activeTabContent ) activeTabContent . classList . remove ( 'is-active' ) ;
75
+ if ( targetTabContent ) targetTabContent . classList . add ( 'is-active' ) ;
76
+
77
+ // Set the active tab in the URL hash. The "tab-" prefix is removed to avoid
78
+ // un-wanted scrolling to the related "id" element
79
+ document . location . hash = targetId . replace ( 'tab-' , '' ) ;
80
+ }
81
+
82
+ // Activate the related tab if hash is present in URL
83
+ if ( document . location . hash !== "" ) {
84
+ let tabName = document . location . hash . slice ( 1 ) ;
85
+ let tabLink = document . querySelector ( `a[data-target="tab-${ tabName } "]` ) ;
86
+ if ( tabLink ) activateTab ( tabLink ) ;
87
+ }
88
+
66
89
$tabLinks . forEach ( function ( $el ) {
67
- $el . addEventListener ( 'click' , function ( event ) {
68
- const activeLink = document . querySelector ( '.tabs .is-active' ) ;
69
- const activeTabContent = document . querySelector ( '.tab-content.is-active' ) ;
70
- const target_id = $el . dataset . target ;
71
- const targetTabContent = document . getElementById ( target_id ) ;
72
-
73
- activeLink . classList . remove ( 'is-active' ) ;
74
- $el . parentNode . classList . add ( 'is-active' ) ;
75
- if ( activeTabContent ) activeTabContent . classList . remove ( 'is-active' ) ;
76
- if ( targetTabContent ) targetTabContent . classList . add ( 'is-active' ) ;
90
+ $el . addEventListener ( 'click' , function ( ) {
91
+ activateTab ( $el )
77
92
} ) ;
78
93
} ) ;
79
94
}
0 commit comments