| 1 |
(function () { |
| 2 |
function toArray(nl) { |
| 3 |
return Array.prototype.slice.call(nl || []); |
| 4 |
} |
| 5 |
|
| 6 |
function initToggle(wrap) { |
| 7 |
var trigger = wrap.querySelector('.bkbg-tgl-trigger'); |
| 8 |
var content = wrap.querySelector('.bkbg-tgl-content'); |
| 9 |
if (!trigger || !content) return; |
| 10 |
|
| 11 |
var speed = parseInt(wrap.getAttribute('data-speed'), 10) || 280; |
| 12 |
|
| 13 |
// Set initial max-height for open state |
| 14 |
if (wrap.classList.contains('is-open')) { |
| 15 |
content.style.maxHeight = content.scrollHeight + 'px'; |
| 16 |
} |
| 17 |
|
| 18 |
trigger.addEventListener('click', function () { |
| 19 |
var isOpen = wrap.classList.contains('is-open'); |
| 20 |
|
| 21 |
if (isOpen) { |
| 22 |
// Closing: set explicit height first, then animate to 0 |
| 23 |
content.style.maxHeight = content.scrollHeight + 'px'; |
| 24 |
requestAnimationFrame(function () { |
| 25 |
requestAnimationFrame(function () { |
| 26 |
content.style.maxHeight = '0'; |
| 27 |
wrap.classList.remove('is-open'); |
| 28 |
trigger.setAttribute('aria-expanded', 'false'); |
| 29 |
content.setAttribute('aria-hidden', 'true'); |
| 30 |
}); |
| 31 |
}); |
| 32 |
} else { |
| 33 |
// Opening |
| 34 |
wrap.classList.add('is-open'); |
| 35 |
trigger.setAttribute('aria-expanded', 'true'); |
| 36 |
content.setAttribute('aria-hidden', 'false'); |
| 37 |
content.style.maxHeight = content.scrollHeight + 'px'; |
| 38 |
|
| 39 |
content.addEventListener('transitionend', function onEnd() { |
| 40 |
if (wrap.classList.contains('is-open')) { |
| 41 |
content.style.maxHeight = 'none'; |
| 42 |
} |
| 43 |
content.removeEventListener('transitionend', onEnd); |
| 44 |
}); |
| 45 |
} |
| 46 |
}); |
| 47 |
|
| 48 |
// Keyboard support |
| 49 |
trigger.addEventListener('keydown', function (e) { |
| 50 |
if (e.key === 'Enter' || e.key === ' ') { |
| 51 |
e.preventDefault(); |
| 52 |
trigger.click(); |
| 53 |
} |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
function init() { |
| 58 |
toArray(document.querySelectorAll('.bkbg-tgl-wrap')).forEach(initToggle); |
| 59 |
} |
| 60 |
|
| 61 |
if (document.readyState === 'loading') { |
| 62 |
document.addEventListener('DOMContentLoaded', init); |
| 63 |
} else { |
| 64 |
init(); |
| 65 |
} |
| 66 |
})(); |
| 67 |
|