-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtabs.ts
More file actions
459 lines (379 loc) · 12.2 KB
/
tabs.ts
File metadata and controls
459 lines (379 loc) · 12.2 KB
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
import { LitElement, html, nothing } from 'lit';
import {
eventOptions,
property,
queryAssignedElements,
state,
} from 'lit/decorators.js';
import { cache } from 'lit/directives/cache.js';
import { createRef, ref } from 'lit/directives/ref.js';
import { styleMap } from 'lit/directives/style-map.js';
import { themes } from '../../theming/theming-decorator.js';
import IgcIconButtonComponent from '../button/icon-button.js';
import {
addKeybindings,
arrowLeft,
arrowRight,
endKey,
homeKey,
} from '../common/controllers/key-bindings.js';
import {
type MutationControllerParams,
createMutationController,
} from '../common/controllers/mutation-observer.js';
import { createResizeObserverController } from '../common/controllers/resize-observer.js';
import { watch } from '../common/decorators/watch.js';
import { registerComponent } from '../common/definitions/register.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import {
findElementFromEventPath,
first,
getRoot,
isEmpty,
isLTR,
isString,
last,
partNameMap,
scrollIntoView,
wrap,
} from '../common/util.js';
import type { TabsActivation, TabsAlignment } from '../types.js';
import { createTabHelpers, getTabHeader } from './tab-dom.js';
import IgcTabComponent from './tab.js';
import { styles as shared } from './themes/shared/tabs/tabs.common.css.js';
import { all } from './themes/tabs-themes.js';
import { styles } from './themes/tabs.base.css.js';
export interface IgcTabsComponentEventMap {
igcChange: CustomEvent<IgcTabComponent>;
}
/* blazorAdditionalDependency: IgcTabComponent */
/**
* Tabs organize and allow navigation between groups of content that are related and at the same level of hierarchy.
*
* The `<igc-tabs>` component allows the user to navigate between multiple `<igc-tab>` elements.
* It supports keyboard navigation and provides API methods to control the selected tab.
*
* @element igc-tabs
*
* @fires igcChange - Emitted when the selected tab changes.
*
* @slot - Renders the `IgcTabComponents` inside default slot.
*
* @csspart start-scroll-button - The start scroll button displayed when the tabs overflow.
* @csspart end-scroll-button - The end scroll button displayed when the tabs overflow.
* @csspart selected-indicator - The indicator that shows which tab is selected.
*/
@themes(all)
export default class IgcTabsComponent extends EventEmitterMixin<
IgcTabsComponentEventMap,
Constructor<LitElement>
>(LitElement) {
public static readonly tagName = 'igc-tabs';
public static styles = [styles, shared];
/* blazorSuppress */
public static register(): void {
registerComponent(
IgcTabsComponent,
IgcTabComponent,
IgcIconButtonComponent
);
}
//#region Private state & properties
private readonly _resizeController = createResizeObserverController(this, {
callback: this._resizeCallback,
options: { box: 'border-box' },
target: null,
});
/** The tabs container reference holding the tab headers. */
private readonly _headerRef = createRef<HTMLElement>();
/** The selected tab indicator reference. */
private readonly _indicatorRef = createRef<HTMLElement>();
private readonly _domHelpers = createTabHelpers(
this,
this._headerRef,
this._indicatorRef
);
@queryAssignedElements({ selector: IgcTabComponent.tagName })
private _tabs!: IgcTabComponent[];
protected get _enabledTabs(): IgcTabComponent[] {
return this._tabs.filter((tab) => !tab.disabled);
}
@state()
private _activeTab?: IgcTabComponent;
//#endregion
//#region Public properties
/**
* Sets the alignment for the tab headers
* @attr
*/
@property({ reflect: true })
public alignment: TabsAlignment = 'start';
/**
* Determines the tab activation. When set to auto,
* the tab is instantly selected while navigating with the Left/Right Arrows, Home or End keys
* and the corresponding panel is displayed.
* When set to manual, the tab is only focused. The selection happens after pressing Space or Enter.
* @attr
*/
@property()
public activation: TabsActivation = 'auto';
/* blazorSuppress */
/** Returns the direct `igc-tab` elements that are children of this element. */
public get tabs(): IgcTabComponent[] {
return this._tabs;
}
/** Returns the currently selected tab label or IDREF if no label property is set. */
public get selected(): string {
if (this._activeTab) {
return this._activeTab.label || this._activeTab.id;
}
return '';
}
//#endregion
@watch('alignment', { waitUntilFirstUpdate: true })
protected _alignmentChanged(): void {
this._domHelpers.setIndicator(this._activeTab);
}
//#region Life-cycle hooks
constructor() {
super();
addKeybindings(this, {
ref: this._headerRef,
skip: this._skipKeyboard,
bindingDefaults: { preventDefault: true },
})
.set(arrowLeft, () => this._handleArrowKeys(isLTR(this) ? -1 : 1))
.set(arrowRight, () => this._handleArrowKeys(isLTR(this) ? 1 : -1))
.set(homeKey, this._handleHomeKey)
.set(endKey, this._handleEndKey)
.setActivateHandler(this._handleActivationKeys, {
preventDefault: false,
});
createMutationController(this, {
callback: this._mutationCallback,
config: {
attributeFilter: ['selected'],
childList: true,
subtree: true,
},
filter: [IgcTabComponent.tagName],
});
}
protected override async firstUpdated() {
await this.updateComplete;
const selectedTab =
this._tabs.findLast((tab) => tab.selected) ?? first(this._enabledTabs);
this._domHelpers.setStyleProperties();
this._domHelpers.setScrollButtonState();
this._setSelectedTab(selectedTab, false);
this._resizeController.observe(this._headerRef.value!);
}
/** @internal */
public override connectedCallback(): void {
super.connectedCallback();
this.role = 'tablist';
}
protected override updated(): void {
if (this._domHelpers.isLeftToRightChanged) {
this._domHelpers.setIndicator(this._activeTab);
}
}
//#endregion
//#region Observers callbacks
private _resizeCallback(): void {
this._domHelpers.setStyleProperties();
this._domHelpers.setScrollButtonState();
this._domHelpers.setIndicator(this._activeTab);
}
private _mutationCallback(
parameters: MutationControllerParams<IgcTabComponent>
): void {
this._selectedAttributeChanged(parameters);
this._handleTabsRemoved(parameters);
this._handleTabsAdded(parameters);
this._domHelpers.setStyleProperties();
this._domHelpers.setScrollButtonState();
this._domHelpers.setIndicator(this._activeTab);
}
private _selectedAttributeChanged({
changes,
}: MutationControllerParams<IgcTabComponent>): void {
const selected = changes.attributes.find(
(tab) => this._tabs.includes(tab) && tab.selected
);
this._setSelectedTab(selected, false);
}
private _handleTabsAdded({
changes,
}: MutationControllerParams<IgcTabComponent>): void {
if (!isEmpty(changes.added)) {
const lastAdded = changes.added.findLast(
(change) =>
change.target.closest(this.tagName) === this && change.node.selected
)?.node;
this._setSelectedTab(lastAdded, false);
}
}
private _handleTabsRemoved({
changes,
}: MutationControllerParams<IgcTabComponent>): void {
if (!isEmpty(changes.removed)) {
let nextSelectedTab: IgcTabComponent | null = null;
const removed = changes.removed.filter(
(change) => change.target.closest(this.tagName) === this
);
for (const each of removed) {
if (each.node.selected && each.node === this._activeTab) {
nextSelectedTab = first(this._enabledTabs);
break;
}
}
if (nextSelectedTab) {
this._setSelectedTab(nextSelectedTab, false);
}
}
}
//#endregion
//#region Private API
private _getClosestActiveTabIndex(): number {
const active = getRoot(this).activeElement;
const tab = active ? active.closest(IgcTabComponent.tagName) : null;
return tab ? this._enabledTabs.indexOf(tab) : -1;
}
private _setSelectedTab(tab?: IgcTabComponent, shouldEmit = true): void {
if (!tab || tab === this._activeTab) {
return;
}
if (this._activeTab) {
this._activeTab.selected = false;
}
tab.selected = true;
this._activeTab = tab;
scrollIntoView(getTabHeader(tab));
this._domHelpers.setIndicator(this._activeTab);
if (shouldEmit) {
this.emitEvent('igcChange', { detail: this._activeTab });
}
}
private _keyboardActivateTab(tab: IgcTabComponent) {
const header = getTabHeader(tab);
this._domHelpers.setScrollSnap();
scrollIntoView(header);
header.focus({ preventScroll: true });
if (this.activation === 'auto') {
this._setSelectedTab(tab);
}
}
private _skipKeyboard(node: Element, event: KeyboardEvent): boolean {
return !(
this._isEventFromTabHeader(event) &&
this._tabs.includes(node.closest(IgcTabComponent.tagName)!)
);
}
private _isEventFromTabHeader(event: Event) {
return findElementFromEventPath('[part~="tab-header"]', event);
}
//#endregion
//#region Event handlers
protected _handleArrowKeys(delta: -1 | 1): void {
const tabs = this._enabledTabs;
this._keyboardActivateTab(
tabs[wrap(0, tabs.length - 1, this._getClosestActiveTabIndex() + delta)]
);
}
protected _handleHomeKey(): void {
this._keyboardActivateTab(first(this._enabledTabs));
}
protected _handleEndKey(): void {
this._keyboardActivateTab(last(this._enabledTabs));
}
protected _handleActivationKeys(): void {
const tabs = this._enabledTabs;
const index = this._getClosestActiveTabIndex();
if (index > -1) {
this._setSelectedTab(tabs[index], false);
this._keyboardActivateTab(tabs[index]);
}
}
protected _handleClick(event: PointerEvent): void {
if (!this._isEventFromTabHeader(event)) {
return;
}
const tab = findElementFromEventPath<IgcTabComponent>(
IgcTabComponent.tagName,
event
);
if (!(tab && this._tabs.includes(tab)) || tab?.disabled) {
return;
}
this._domHelpers.setScrollSnap();
getTabHeader(tab).focus();
this._setSelectedTab(tab);
}
@eventOptions({ passive: true })
protected _handleScroll(): void {
this._domHelpers.setScrollButtonState();
}
//#endregion
//#region Public API methods
/** Selects the specified tab and displays the corresponding panel. */
public select(ref: IgcTabComponent | string): void {
const tab = isString(ref) ? this._tabs.find((t) => t.id === ref) : ref;
if (tab) {
this._setSelectedTab(tab, false);
}
}
//#endregion
//#region Render
protected _renderScrollButton(direction: 'start' | 'end') {
const isStart = direction === 'start';
const { start, end } = this._domHelpers.scrollButtonsDisabled;
return html`${cache(
this._domHelpers.hasScrollButtons
? html`
<igc-icon-button
tabindex="-1"
variant="flat"
collection="default"
part="${direction}-scroll-button"
exportparts="icon"
name=${isStart ? 'prev' : 'next'}
?disabled=${isStart ? start : end}
@click=${() => this._domHelpers.scrollTabs(direction)}
>
</igc-icon-button>
`
: nothing
)}`;
}
protected override render() {
const part = partNameMap({
inner: true,
scrollable: this._domHelpers.hasScrollButtons,
});
return html`
<div
${ref(this._headerRef)}
part="tabs"
style=${styleMap(this._domHelpers.styleProperties)}
@scroll=${this._handleScroll}
>
<div part=${part}>
${this._renderScrollButton('start')}
<slot @click=${this._handleClick}></slot>
${this._renderScrollButton('end')}
<div part="selected-indicator">
<span ${ref(this._indicatorRef)}></span>
</div>
</div>
</div>
`;
}
//#endregion
}
declare global {
interface HTMLElementTagNameMap {
'igc-tabs': IgcTabsComponent;
}
}