-
Notifications
You must be signed in to change notification settings - Fork 108
/
expandable-mobile.js
45 lines (37 loc) · 1.08 KB
/
expandable-mobile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const OPEN_DEFAULT = 'o-expandable_content__onload-open';
const MOBILE_COLLAPSED_CLASS = 'o-expandable__mobile-collapsed';
const MOBILE_WIDTH = 900;
let innerWidth;
/**
* Get the viewport width
*
* @returns {number} The viewport width
*/
function getInnerWidth() {
return innerWidth || window.innerWidth;
}
/**
* Fix the output of getInnerWidth() for test purposes.
*
* @param {number} width - Width.
*/
function setInnerWidth(width) {
innerWidth = width;
}
/**
* Alter DOM as necessary before cfpb-expandables ExpandableTransition.init()
* so some elements won't be expanded by default.
*/
function beforeExpandableTransitionInit() {
const nodeList = document.querySelectorAll('.' + MOBILE_COLLAPSED_CLASS);
// IE11 lacks NodeList.forEach
[].forEach.call(nodeList, (el) => {
if (getInnerWidth() <= MOBILE_WIDTH) {
el.classList.remove(OPEN_DEFAULT);
}
// Always clean up this class, just used at init time
el.classList.remove(MOBILE_COLLAPSED_CLASS);
});
}
export { setInnerWidth, MOBILE_COLLAPSED_CLASS };
export default beforeExpandableTransitionInit;