This repository has been archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
react.js
489 lines (402 loc) 路 12.7 KB
/
react.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*! [replace-name] v[replace-version] */
/***************************************************************************************************************************************************************
*
* mainNav function
*
* Horizontal list of links to key areas on the website. Usually located in the header.
*
**************************************************************************************************************************************************************/
import React from 'react';
import PropTypes from 'prop-types';
import AU from '@gov.au/animate'; // interdependency with our animate lib
// ES5 dependency: import AUlinkList from '@gov.au/link-list';
// ES6 dependency: import AUlinkList from './link-list';
// The following line will be replaced automatically with generic imports for the ES5 pipeline.
// You can safely ignore this bit if you use this module with pancake
//
// [replace-imports]
/**
* A menu inside the AUmainNav
*
* @param {array} items - The links in an array containing text, location and active status
* @param {string} linkComponent - The component used for the link
*/
const AUmainNavMenu = ({ items, linkComponent }) => {
// Generate the menu
const GenerateMenu = ( items ) => {
const menu = items.map( item => {
const link = item.active ? {
link: item.link,
text: item.text,
'aria-current': 'page',
li: {
className: 'active',
},
} :
{
link: item.link,
text: item.text,
};
// return the link, maybe with children
return link;
});
return menu;
}
// Create the menu with children
return (
<AUlinkList items={ GenerateMenu( items ) } linkComponent={ linkComponent } />
);
}
AUmainNavMenu.propTypes = {
items: PropTypes.arrayOf(
PropTypes.shape({
link: PropTypes.string,
text: PropTypes.node.isRequired,
children: PropTypes.array,
})
).isRequired,
linkComponent: PropTypes.oneOfType([ PropTypes.string, PropTypes.elementType ]),
};
AUmainNavMenu.defaultProps = {
linkComponent: 'a',
};
// This value gets increased for each navigation component
let IDvalue = 0;
export class AUmainNavContent extends React.PureComponent {
constructor( props ) {
super( props );
const {
items,
className = '',
children,
linkComponent,
onOpen,
afterOpen,
onClose,
afterClose,
...attributeOptions
} = props;
// Functions
this.toggleClasses = this.toggleClasses.bind( this );
this.removeClass = this.removeClass.bind( this );
this.addClass = this.addClass.bind( this );
this.mainNavToggle = this.mainNavToggle.bind( this );
this.toggle = this.toggle.bind( this );
this.toggleAnimationState = function() {
this.setState({ animating: !this.state.animating } )
}.bind( this );
// Event listeners
this.auFocusTrapListenerTop = this.auFocusTrapListenerTop.bind( this );
this.auFocusTrapListenerBottom = this.auFocusTrapListenerBottom.bind( this );
this.auKeyListener = this.auKeyListener.bind( this );
// Variables
this.state = {
closed: true,
animating: false,
expanded: false
};
// Increase the ID so it's unique for each instance
IDvalue += 1;
this.id = `au-main-nav-${ IDvalue }`;
}
/**
* Toggle an main navigation on click
*
* @param {event object} event - The event object of the click
*/
toggle( event ) {
event.preventDefault();
// If the main menu is animating don't change toggle the menu
if( this.state.animating ){
return;
}
// Toggle the menu's current state
this.setState({
closed: !this.state.closed,
expanded: !this.state.expanded,
animating: true,
});
}
/**
* componentDidUpdate - Update the component when it changes
*
* @param {*} prevProps - The previous property values
* @param {*} prevState - The previous state values
*/
componentDidUpdate( prevProps, prevState ){
if( prevState !== undefined && this.state !== undefined && this.state.closed !== prevState.closed ) {
this.mainNavToggle( this.mainNavContent, this.props.speed, {
onOpen: this.props.onOpen,
afterOpen: this.props.afterOpen,
onClose: this.props.onClose,
afterClose: this.props.afterClose,
});
}
}
/**
* IE8 compatible function for replacing classes on a DOM node
*
* @param {object} element - The DOM element we want to toggle classes on
* @param {string} openingClass - The firstClass you want to toggle on the DOM node
* @param {string} closingClass - The secondClass you want to toggle on the DOM node
*/
toggleClasses( element, state, openingClass, closingClass ) {
if( state === 'opening' || state === 'open' ) {
var oldClass = openingClass || 'au-main-nav__content--closed';
var newClass = closingClass || 'au-main-nav__content--open';
}
else {
var oldClass = closingClass || 'au-main-nav__content--open';
var newClass = openingClass || 'au-main-nav__content--closed';
}
this.removeClass( element, oldClass );
this.addClass( element, newClass );
}
/**
* IE8 compatible function for removing a class
*
* @param {object} element - The DOM element we want to manipulate
* @param {object} className - The name of the class to be removed
*/
removeClass( element, className ) {
if( element.classList ) {
element.classList.remove( className );
}
else {
element.className = element.className.replace( new RegExp("(^|\\b)" + className.split(" ").join("|") + "(\\b|$)", "gi"), " " );
}
}
/**
* IE8 compatible function for adding a class
*
* @param {object} element - The DOM element we want to manipulate
* @param {object} className - The name of the class to be added
*/
addClass( element, className ) {
if( element.classList ) {
element.classList.add( className );
}
else {
element.className = element.className + " " + className;
}
}
/**
* auFocusTrapListenerTop - What happens when the focus trap top gets focus
*
* @param {event} event - the event
*/
auFocusTrapListenerTop( event ) {
var focusContent = this.mainNavMenu.querySelectorAll( 'a, .au-main-nav__toggle' );
focusContent[ focusContent.length - 1 ].focus();
}
/**
* auFocusTrapListenerBottom - What happens when the focus trap bottom gets focus
*
* @param {event} event - the event
*/
auFocusTrapListenerBottom( event ) {
var focusContent = this.mainNavMenu.querySelectorAll( 'a, .au-main-nav__toggle' );
focusContent[ 0 ].focus();
}
/**
* auKeyListener - What happens when a key gets pressed and the menu is open
*
* @param {event} event - the event
*/
auKeyListener( event ) {
// This should only close the menu when the menu is visibly open
var overlayOpen = window.getComputedStyle( this.mainNavOverlay ).getPropertyValue( 'display' );
// Check the menu is open and visible and the escape key is pressed
if( event.keyCode === 27 && overlayOpen === 'block' ) {
this.toggle( event );
}
}
/**
* Toggle a mainNav element
*
* @param {string} element - The toggle for the main nav
* @param {integer} speed - The speed in ms for the animation
*
*/
mainNavToggle( element, speed, callbacks ) {
// stop event propagation
try {
window.event.cancelBubble = true;
event.stopPropagation();
}
catch( error ) {}
// check this once
if( typeof callbacks != 'object' ) {
callbacks = {};
}
// Elements we modify
var menu = this.mainNavMenu;
var overlay = this.mainNavOverlay;
var closeButton = element.querySelector( '.au-main-nav__toggle--close' );
var openButton = element.querySelector( '.au-main-nav__toggle--open' );
var focustrapTop = element.querySelector( '.au-main-nav__focus-trap-top' );
var focustrapBottom = element.querySelector( '.au-main-nav__focus-trap-bottom' );
var closed = !this.state.closed;
var state = closed ? 'opening' : '';
var menuWidth = menu.offsetWidth;
// Functions
var ToggleClasses = this.toggleClasses;
var AUkeyListener = this.auKeyListener;
var AUfocusTrapListenerTop = this.auFocusTrapListenerTop;
var AUfocusTrapListenerBottom = this.auFocusTrapListenerBottom;
var ToggleAnimationState = this.toggleAnimationState;
overlay.style.display = 'block';
(function( menu ) {
AU.animate.Toggle({
element: menu,
property: 'left',
openSize: 0,
closeSize: -1 * menuWidth,
speed: speed || 250,
prefunction: function( ) {
// Set these value immediately for transitions
if( state === 'opening' ) {
menu.style.display = 'block';
overlay.style.left = 0;
overlay.style.opacity = 0.5;
// run when opening
if( typeof callbacks.onOpen === 'function' ) {
callbacks.onOpen();
}
}
else {
overlay.style.opacity = '0';
// run when closing
if( typeof callbacks.onClose === 'function' ) {
callbacks.onClose();
}
}
},
postfunction: function(){
if ( state === 'opening' ){
// Move the focus to the close button
closeButton.focus();
// Focus trap enabled
focustrapTop.setAttribute( "tabindex", 0 );
focustrapBottom.setAttribute( "tabindex", 0 );
focustrapTop.addEventListener( 'focus', AUfocusTrapListenerTop );
focustrapBottom.addEventListener( 'focus', AUfocusTrapListenerBottom );
// Add key listener
document.addEventListener( 'keyup', AUkeyListener );
if( typeof callbacks.afterOpen === 'function' ) {
callbacks.afterOpen();
}
}
else {
// Move the focus back to the menu button
openButton.focus();
// Remove the focus trap
focustrapTop.removeAttribute( "tabindex" );
focustrapBottom.removeAttribute( "tabindex" );
// Remove the event listeners
focustrapTop.removeEventListener( 'focus', AUfocusTrapListenerTop );
focustrapBottom.removeEventListener( 'focus', AUfocusTrapListenerBottom );
// Remove the event listener for the keypress
document.removeEventListener( 'keyup', AUkeyListener );
if( typeof callbacks.afterClose === 'function' ) {
callbacks.afterClose();
}
}
// Toggle classes
ToggleClasses( element, state );
ToggleClasses(
document.body,
state,
'au-main-nav__scroll--unlocked',
'au-main-nav__scroll--locked',
);
// Reset inline styles
menu.style.display = '';
menu.style.left = '';
overlay.style.display = '';
overlay.style.left = '';
overlay.style.opacity = '';
// Toggle the animating state to false so that we can allow toggling again
ToggleAnimationState();
},
});
})( menu );
}
render(){
return (
<div
id={ this.id }
className="au-main-nav__content"
ref={ mainNavContent => { this.mainNavContent = mainNavContent }}>
<button
aria-controls={ this.id }
aria-expanded={this.state.expanded}
onClick={ ( event ) => this.toggle( event ) }
className="au-main-nav__toggle au-main-nav__toggle--open">
Menu
</button>
<div
className="au-main-nav__menu"
ref={ mainNavMenu => { this.mainNavMenu = mainNavMenu }}
>
<div className="au-main-nav__menu-inner">
<div className="au-main-nav__focus-trap-top"></div>
<button
aria-controls={ this.id }
aria-expanded={this.state.expanded}
onClick={ ( event ) => this.toggle( event ) }
className="au-main-nav__toggle au-main-nav__toggle--close">
Close
</button>
<AUmainNavMenu
linkComponent={ this.props.linkComponent }
items={ this.props.items }
/>
{ this.props.children }
<div className="au-main-nav__focus-trap-bottom"></div>
</div>
</div>
<div
aria-controls={ this.id }
ref={ mainNavOverlay => { this.mainNavOverlay = mainNavOverlay }}
onClick={ ( event ) => this.toggle( event ) }
className="au-main-nav__overlay"></div>
</div>
);
}
};
AUmainNavContent.propTypes = {
dark: PropTypes.bool,
alt: PropTypes.bool,
children: PropTypes.node,
speed: PropTypes.number,
onOpen: PropTypes.func,
afterOpen: PropTypes.func,
onClose: PropTypes.func,
afterClose: PropTypes.func,
className: PropTypes.string,
};
const AUmainNav = ({ dark, alt, className, ariaLabel, children, ...attributeOptions }) => (
<nav
className={
'au-main-nav ' +
`${ dark ? ' au-main-nav--dark' : '' }` +
`${ alt ? ' au-main-nav--alt' : '' }` +
`${ className ? ' ' + className : '' }`
}
aria-label = { ariaLabel }
{ ...attributeOptions }
>
{ children }
</nav>
);
AUmainNav.propTypes = {
dark: PropTypes.bool,
alt: PropTypes.bool,
ariaLabel: PropTypes.string,
children: PropTypes.node.isRequired,
};
AUmainNav.defaultProps = {
ariaLabel: 'main',
};
export default AUmainNav;