1+ // ===========================================================================
2+ // RESPONSIVE TAB BAR LOGIC WITH GRACEFUL DEGRADATION
3+ // Source: https://css-tricks.com/container-adapting-tabs-with-more-button/
4+ // ===========================================================================
5+
6+ // Store references to relevant selectors
17const container = document . querySelector ( '#tabs' )
28const primary = container . querySelector ( '.primary' )
39const primaryItems = container . querySelectorAll ( '.primary > li:not(.-more)' )
4- container . classList . add ( 'has-js' )
510
6- // insert "more" button and duplicate the list
11+ // Add a class to turn off graceful degradation style
12+ container . classList . add ( 'has-js' )
713
14+ // insert "more" button and duplicate the original tab bar items
815primary . insertAdjacentHTML ( 'beforeend' , `
916 <li class="-more">
1017 <button type="button" aria-haspopup="true" aria-expanded="false" aria-controls="more-options">
@@ -20,21 +27,23 @@ const secondaryItems = secondary.querySelectorAll('li')
2027const allItems = container . querySelectorAll ( 'li' )
2128const moreLi = primary . querySelector ( '.-more' )
2229const moreBtn = moreLi . querySelector ( 'button' )
30+
31+ // When the more button is clicked, toggle classes to indicate the secondary menu is open
2332moreBtn . addEventListener ( 'click' , ( e ) => {
2433 e . preventDefault ( )
2534 container . classList . toggle ( '--show-secondary' )
2635 moreBtn . setAttribute ( 'aria-expanded' , container . classList . contains ( '--show-secondary' ) )
2736} )
2837
2938// adapt tabs
30-
3139const doAdapt = ( ) => {
40+
3241 // reveal all items for the calculation
3342 allItems . forEach ( ( item ) => {
3443 item . classList . remove ( '--hidden' )
3544 } )
3645
37- // hide items that won't fit in the Primary
46+ // hide items that won't fit in the Primary tab bar
3847 let stopWidth = moreBtn . offsetWidth
3948 let hiddenItems = [ ]
4049 const primaryWidth = primary . offsetWidth
@@ -47,7 +56,7 @@ const doAdapt = () => {
4756 }
4857 } )
4958
50- // toggle the visibility of More button and items in Secondary
59+ // toggle the visibility of More button and items in Secondary menu
5160 if ( ! hiddenItems . length ) {
5261 moreLi . classList . add ( '--hidden' )
5362 container . classList . remove ( '--show-secondary' )
@@ -62,10 +71,11 @@ const doAdapt = () => {
6271 }
6372}
6473
65- doAdapt ( ) // adapt immediately on load
66- window . addEventListener ( 'resize' , doAdapt ) // adapt on window resize
74+ // Adapt the tabs to fit the viewport
75+ doAdapt ( ) // immediately on load
76+ window . addEventListener ( 'resize' , doAdapt ) // on window resize
6777
68- // hide Secondary on the outside click
78+ // hide Secondary menu on the outside click
6979document . addEventListener ( 'click' , ( e ) => {
7080 let el = e . target
7181 while ( el ) {
0 commit comments