-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflyout-menu.js
140 lines (114 loc) · 3.84 KB
/
flyout-menu.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
window.addEventListener("load", (event) => {
const flyoutMenu = document.querySelector( '#flyout-menu' );
if ( ! flyoutMenu ) {
return;
}
const summaryTags = flyoutMenu.querySelectorAll( 'summary' );
const detailsTags = flyoutMenu.querySelectorAll( 'details' );
const backButtons = flyoutMenu.querySelectorAll( 'details .menu-back' );
const openers = document.querySelectorAll( '.flyout-menu-opener a' );
const closers = document.querySelectorAll( '.flyout-menu-closer' );
const inertEls = getSiblings( flyoutMenu.parentNode );
openers.forEach( ( opener ) => {
opener.setAttribute( 'aria-controls', 'flyout-menu' );
opener.setAttribute( 'aria-expanded', 'false' );
opener.addEventListener( 'click', function ( event ) {
event.preventDefault();
openMenuDrawer();
} );
} );
closers.forEach( ( closer ) => {
closer.addEventListener( 'click', function ( event ) {
event.preventDefault();
closeMenuDrawer();
} );
} );
function openMenuDrawer() {
setTimeout( () => {
document.body.classList.add( 'flyout-menu-open' );
});
openers.forEach( ( opener ) => {
opener.setAttribute( 'aria-expanded', 'true' );
} );
trapFocus( inertEls, document.querySelector( '.flyout-menu-closer.menu-back' ) );
}
function closeMenuDrawer() {
detailsTags.forEach( ( detailsTag ) => {
detailsTag.classList.remove( 'submenu-open' );
setTimeout( () => {
detailsTag.removeAttribute( 'open' );
}, 50 );
} );
releaseFocus( inertEls );
document.body.classList.remove( 'flyout-menu-open' );
flyoutMenu.querySelectorAll( '.has-open-submenu' ).forEach( ( item ) => {
item.classList.remove( 'has-open-submenu' );
} );
openers.forEach( ( opener ) => {
opener.setAttribute( 'aria-expanded', 'false' );
} );
}
summaryTags.forEach( ( summaryTag ) => {
summaryTag.addEventListener('click', () => {
let detailsElement = summaryTag.closest( 'details' );
let parentMenu = detailsElement.closest( '.menu-container' );
setTimeout( () => {
detailsElement.classList.add( 'submenu-open' );
parentMenu.classList.add( 'has-open-submenu' );
focusAfterAnimation( detailsElement.querySelector( '.menu-back' ) );
}, 50 );
} );
} );
backButtons.forEach( ( backButton ) => {
backButton.addEventListener( 'click', () => {
let detailsElement = backButton.closest( 'details' );
detailsElement.classList.remove( 'submenu-open' );
let parentMenu = detailsElement.closest( '.menu-container' );
parentMenu.classList.remove( 'has-open-submenu' );
detailsCloseDelay( detailsElement );
focusAfterAnimation( detailsElement.querySelector( 'summary' ) );
} );
} );
function detailsCloseDelay( detailsElement ) {
let animationStart;
const handleAnimation = ( time ) => {
if ( animationStart === undefined ) {
animationStart = time;
}
const elapsedTime = time - animationStart;
if ( elapsedTime < 300 ) {
window.requestAnimationFrame( handleAnimation );
} else {
detailsElement.removeAttribute( 'open' );
}
};
window.requestAnimationFrame( handleAnimation );
}
function focusAfterAnimation ( elToFocus ) {
flyoutMenu.addEventListener( 'transitionend', function faa() {
elToFocus.focus();
this.removeEventListener( 'transitionend', faa );
} );
}
function trapFocus( inertEls, elToFocus = false ) {
inertEls.forEach( ( el ) => {
el.inert = true;
} );
if ( elToFocus ) {
focusAfterAnimation( elToFocus );
}
}
function releaseFocus( inertEls, elToFocus = false ) {
inertEls.forEach( ( el ) => {
el.inert = false;
} );
if ( elToFocus ) {
elToFocus.focus();
}
}
});
function getSiblings( el ) {
return Array.prototype.filter.call(el.parentNode.children, function (sibling) {
return sibling !== el;
} );
};