Skip to content

Commit ba19d82

Browse files
committed
Comments in JS file
1 parent b5c87f5 commit ba19d82

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

app/javascript/source_tabs.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
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
17
const container = document.querySelector('#tabs')
28
const primary = container.querySelector('.primary')
39
const 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
815
primary.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')
2027
const allItems = container.querySelectorAll('li')
2128
const moreLi = primary.querySelector('.-more')
2229
const moreBtn = moreLi.querySelector('button')
30+
31+
// When the more button is clicked, toggle classes to indicate the secondary menu is open
2332
moreBtn.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-
3139
const 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
6979
document.addEventListener('click', (e) => {
7080
let el = e.target
7181
while(el) {

0 commit comments

Comments
 (0)