-
Notifications
You must be signed in to change notification settings - Fork 42
/
navigation.element.ts
443 lines (381 loc) · 13.1 KB
/
navigation.element.ts
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
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { html, LitElement, PropertyValues } from 'lit';
import {
animate,
baseStyles,
event,
EventEmitter,
i18n,
I18nService,
state,
isVisible,
onKey,
property,
querySlot,
querySlotAll,
reverseAnimation,
setAttributes,
syncProps,
syncPropsForAllItems,
} from '@cds/core/internal';
import styles from './navigation.element.scss';
import {
FocusableElement,
getNextFocusElement,
getPreviousFocusElement,
removeFocus,
setFocus,
visibleElement,
} from './utils/utils.js';
import { CdsNavigationGroup } from './navigation-group.element.js';
import { CdsNavigationStart } from './navigation-start.element.js';
import { CdsNavigationItem } from './navigation-item.element.js';
import { CdsDivider } from '@cds/core/divider/index.js';
import { AnimationNavigationOpenName } from '../internal/motion/animations/cds-navigation-open.js';
export const CdsNavigationTagName = 'cds-navigation';
/**
* ```typescript
* import '@cds/core/navigation/register.js';
* ```
*
* ```html
* <cds-navigation>
* <cds-navigation-item><a href="/home">Home</cds-navigation-item>
* <cds-navigation-item><a href="/account">Account</cds-navigation-item>
* </cds-navigation>
* ```
*
* @beta
* @element cds-navigation
* @event expandedChange - notify when the user has clicked the navigation expand/collapse button
* @cssprop --animation-duration
* @cssprop --animation-easing
* @cssprop --background
* @cssprop --collapsed-width
* @cssprop --expanded-width
* @cssprop --font-size
* @cssprop --font-weight
* @cssprop --letter-spacing
* @cssprop --line-height
* @cssprop --nested-padding
* @cssprop --padding
* @slot
* @slot cds-navigation-substart - project content below the navigation toggle button
* @slot cds-navigation-end - project content below the scrollable section
*/
@animate({
expanded: {
true: AnimationNavigationOpenName,
false: reverseAnimation(AnimationNavigationOpenName),
},
})
export class CdsNavigation extends LitElement {
expandedRoot = false;
@property({ type: String })
cdsMotion = 'on';
@event()
protected expandedChange: EventEmitter<boolean>;
@event()
cdsMotionChange: EventEmitter<string>;
/**
* This is used to sync down the information to this.navigationGroupItems
*
* @type { boolean }
* @protected
*/
@state({ type: Boolean })
protected groupItem = true;
/**
*
* Vertical navigation elements can be either wide or narrow. Expanded indicates it should be wide.
* When navigation is wide cds-navigation-start button elements and cds-navigation-item a elements display
* text. When it is narrow they do not (consumer should provide an icon that stays visible).
* @type {boolean}
*/
@property({ type: Boolean })
expanded = false;
@i18n() i18n = I18nService.keys.navigation;
/**
* The end slot that items can be projected into with slot="cds-navigation-end"
*/
@querySlot('[slot="cds-navigation-end"]', { assign: 'cds-navigation-end' })
protected navigationEnd: HTMLElement;
/**
* This slot query is used to identify and manage all focusable elements needed for arrow key navigation
* TODO: How to add in forms selector attribute and other things that are not FocusableElements like I use here
* tbd - I don;'t have an answer yet.
*/
@querySlotAll('cds-navigation-group > cds-navigation-start, cds-navigation-item:not([disabled])')
protected allNavigationElements: NodeListOf<FocusableElement>;
/**
* Get references to all of the start elements so they can be passed state when updates are made.
*/
@querySlotAll('cds-navigation-start')
protected navigationStartRefs: NodeListOf<CdsNavigationStart>;
/** make navigation-body default and eliminate extra assigns **?
/**
* query for cds-divider and project into navigation-body slot.
*/
@querySlotAll(':scope > cds-divider')
protected rootDividers: NodeListOf<CdsDivider>;
/**
* query for root level groups and project them into the navigation-body slot.
*/
@querySlotAll(':scope > cds-navigation-group')
protected rootNavigationGroups: NodeListOf<CdsNavigationGroup>;
/**
* query for root level items and project them into the navigation-body slot.
*/
@querySlotAll(':scope > cds-navigation-item')
protected rootNavigationItems: NodeListOf<CdsNavigationItem>;
/**
* query for the root level start items and project them into the navigation-start slot.
*/
@querySlot(':scope > cds-navigation-start', { assign: 'navigation-start' })
protected rootNavigationStart: CdsNavigationStart;
/**
* query for items inside a cds-navigation-group, used to pass state down
*/
@querySlotAll(':scope > cds-navigation-group > cds-navigation-item')
protected navigationGroupItems: NodeListOf<CdsNavigationGroup>;
/**
* query for all cds-navigation elements, used to pass state down
*/
@querySlotAll('cds-navigation-item')
protected navigationItemRefs: NodeListOf<CdsNavigationItem>;
/**
* query for all groups (including any nested groups), used ot pass state down
*/
@querySlotAll('cds-navigation-group')
protected navigationGroupRefs: NodeListOf<CdsNavigationGroup>;
role = 'list';
private toggle() {
this.expandedChange.emit(!this.expanded);
}
private get currentActiveItem() {
return this.visibleChildren.find(c => c.id === this.ariaActiveDescendant);
}
protected get endTemplate() {
return this.navigationEnd
? html`
<div class="navigation-end" cds-layout="vertical align:horizontal-stretch">
<slot name="cds-navigation-end"></slot>
</div>
`
: '';
}
protected get startTemplate() {
let returnHTML;
this.rootNavigationStart
? (returnHTML = html`
<div class="navigation-start" cds-layout="vertical align:horizontal-stretch">
<slot @slotchange="${() => this.addStartEventListener()}" name="navigation-start"></slot>
<cds-divider class="start-divider"></cds-divider>
</div>
`)
: (returnHTML = '');
return returnHTML;
}
protected get visibleChildren(): FocusableElement[] {
return Array.from(this.allNavigationElements).filter(n => isVisible(n));
}
addStartEventListener() {
// This is controlled by the slotchange event on the navigation-start slot
// Make sure we reattach the click handler for toggle if consumer changes the start element (e.g *ngIf)
if (this.rootNavigationStart) {
this.rootNavigationStart.addEventListener('click', this.toggle.bind(this));
}
}
firstUpdated(props: PropertyValues) {
super.firstUpdated(props);
// set all visible navigation elements to tabindex = -1
this.allNavigationElements.forEach(item => {
setAttributes(item, ['tabindex', '-1']);
});
const itemWrapper = this.shadowRoot?.querySelector('#item-container');
itemWrapper?.addEventListener('focus', this.initItemKeys.bind(this));
itemWrapper?.addEventListener('keydown', this.handleItemKeys.bind(this));
itemWrapper?.addEventListener('blur', this.blurItemKeys.bind(this));
}
private blurItemKeys() {
if (this.currentActiveItem) {
removeFocus(this.currentActiveItem);
}
}
private focusRootStart() {
if (this.rootNavigationStart) {
setFocus(this.rootNavigationStart);
}
}
private blurRootStart() {
if (this.rootNavigationStart) {
removeFocus(this.rootNavigationStart);
}
}
private initItemKeys() {
if (!this.currentActiveItem) {
setFocus(this.focusableElements[0]);
this.ariaActiveDescendant = this.focusableElements[0].id;
} else {
const focusElement = this.currentActiveItem ? this.currentActiveItem : this.allNavigationElements[0];
setFocus(focusElement);
this.ariaActiveDescendant = focusElement.id;
}
}
private get focusableElements() {
return Array.from(this.allNavigationElements).filter(visibleElement);
}
private handleItemKeys(event: KeyboardEvent) {
onKey('arrow-down', event, () => {
if (this.currentActiveItem) {
removeFocus(this.currentActiveItem);
const next = getNextFocusElement(this.currentActiveItem, this.focusableElements);
this.ariaActiveDescendant = next.id;
setFocus(next);
event.preventDefault();
}
});
onKey('arrow-up', event, () => {
if (this.currentActiveItem) {
removeFocus(this.currentActiveItem);
const previous = getPreviousFocusElement(this.currentActiveItem, this.focusableElements);
this.ariaActiveDescendant = previous.id;
setFocus(previous);
event.preventDefault();
}
});
onKey('arrow-right', event, () => {
const groupParent = this.currentActiveItem?.closest('cds-navigation-group');
if (groupParent && !groupParent.expanded) {
groupParent.expandedChange.emit(!groupParent.expanded);
return;
}
});
onKey('arrow-left', event, () => {
const groupParent = this.currentActiveItem?.closest('cds-navigation-group');
if (this.currentActiveItem?.tagName === 'CDS-NAVIGATION-ITEM' && !!groupParent) {
const groupStartElement = groupParent?.querySelector('cds-navigation-start');
removeFocus(this.currentActiveItem as FocusableElement);
this.ariaActiveDescendant = groupStartElement?.id ?? null;
setFocus(groupStartElement as FocusableElement);
return;
}
if (groupParent && groupParent.expanded) {
groupParent.expandedChange.emit(!groupParent.expanded);
return;
}
});
onKey('home', event, () => {
if (this.currentActiveItem) {
removeFocus(this.currentActiveItem);
const home = this.focusableElements[0];
this.ariaActiveDescendant = home.id;
setFocus(home);
}
});
onKey('end', event, () => {
if (this.currentActiveItem) {
removeFocus(this.currentActiveItem);
const end = this.focusableElements[this.focusableElements.length - 1];
this.ariaActiveDescendant = end.id;
setFocus(end);
}
});
onKey('enter', event, () => {
// focusElement is either an Anchor or a button and we want to click it here
if (this.currentActiveItem?.focusElement) {
this.currentActiveItem?.focusElement.click();
}
});
onKey('space', event, () => {
// focusElement is either an Anchor or a button and we want to click it here
if (this.currentActiveItem?.focusElement) {
this.currentActiveItem?.focusElement.click();
}
});
}
private handleRootStartKeys(event: KeyboardEvent) {
onKey('arrow-right', event, () => {
if (!this.expanded) {
this.toggle();
return;
}
});
onKey('arrow-left', event, () => {
if (this.expanded) {
this.toggle();
return;
}
});
}
connectedCallback() {
super.connectedCallback();
this.rootNavigationStart?.addEventListener('focus', this.focusRootStart.bind(this));
this.rootNavigationStart?.addEventListener('blur', this.blurRootStart.bind(this));
this.rootNavigationStart?.addEventListener('keydown', this.handleRootStartKeys.bind(this));
}
disconnectedCallback() {
super.disconnectedCallback();
if (this.rootNavigationStart) {
this.rootNavigationStart.removeEventListener('click', this.toggle.bind(this));
}
}
render() {
return html`<div class="private-host" aria-label="${this.i18n.navigationLabel}" cds-layout="vertical wrap:none">
${this.startTemplate}
<slot name="cds-navigation-substart"></slot>
<nav class="navigation-body-wrapper">
<div .ariaActiveDescendant=${this.ariaActiveDescendant} tabindex="0" id="item-container">
<div class="navigation-body" cds-layout="vertical wrap:none align:horizontal-stretch">
<slot></slot>
</div>
</div>
</nav>
${this.endTemplate}
</div>`;
}
updated(props: PropertyValues<this>) {
super.updated(props);
if (props.has('expanded')) {
this.expandedRoot = this.expanded;
}
this.updateChildrenProps();
}
updateChildrenProps() {
if (this.navigationGroupItems) {
syncPropsForAllItems(Array.from(this.navigationGroupItems), this, { groupItem: true });
}
if (this.navigationItemRefs) {
syncPropsForAllItems(Array.from(this.navigationItemRefs), this, {
expanded: true,
});
}
if (this.navigationStartRefs) {
syncPropsForAllItems(Array.from(this.navigationStartRefs), this, {
expandedRoot: true,
});
}
if (this.rootNavigationStart) {
syncProps(this.rootNavigationStart, this, {
expanded: this.expanded,
});
}
if (this.rootNavigationItems.length > 0) {
syncPropsForAllItems(Array.from(this.rootNavigationItems), this, {
expanded: this.expanded,
});
}
if (this.navigationGroupRefs.length > 0) {
syncPropsForAllItems(Array.from(this.navigationGroupRefs), this, {
layout: true,
});
}
}
static get styles() {
return [baseStyles, styles];
}
}