forked from zyra/ionic-super-tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper-tabs-container.component.tsx
438 lines (356 loc) · 11.4 KB
/
super-tabs-container.component.tsx
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
import {
Component,
ComponentInterface,
Element,
Event,
EventEmitter,
h,
Listen,
Method,
Prop,
QueueApi,
State,
} from '@stencil/core';
import { SuperTabsConfig } from '../interface';
import { checkGesture, debugLog, getTs, pointerCoord, scrollEl, STCoord } from '../utils';
@Component({
tag: 'super-tabs-container',
styleUrl: 'super-tabs-container.component.scss',
shadow: true,
})
export class SuperTabsContainerComponent implements ComponentInterface {
@Element() el!: HTMLSuperTabsContainerElement;
/** @internal */
@Prop({ mutable: true }) config?: SuperTabsConfig;
/** @internal */
@Prop({ context: 'queue' }) queue!: QueueApi;
/**
* Enable/disable swiping
*/
@Prop() swipeEnabled: boolean = true;
/**
* Set to true to automatically scroll to the top of the tab when the button is clicked while the tab is
* already selected.
*/
@Prop() autoScrollTop: boolean = false;
/**
* Emits an event when the active tab changes.
* An active tab is the tab that the user looking at.
*
* This event emitter will not notify you if the user has changed the current active tab.
* If you need that information, you should use the `tabChange` event emitted by the `super-tabs` element.
*/
@Event() activeTabIndexChange!: EventEmitter<number>;
/**
* Emits events when the container moves.
* Selected tab index represents what the user should be seeing.
* If you receive a decimal as the emitted number, it means that the container is moving between tabs.
* This number is used for animations, and can be used for high tab customizations.
*/
@Event() selectedTabIndexChange!: EventEmitter<number>;
@State() tabs: HTMLSuperTabElement[] = [];
private initialCoords: STCoord | undefined;
private lastPosX: number | undefined;
private isDragging: boolean = false;
private initialTimestamp?: number;
private _activeTabIndex: number | undefined;
private _selectedTabIndex?: number;
private leftThreshold: number = 0;
private rightThreshold: number = 0;
private scrollWidth: number = 0;
private slot!: HTMLSlotElement;
private ready?: boolean;
private width: number = 0;
async componentDidLoad() {
this.debug('componentDidLoad');
this.updateWidth();
await this.indexTabs();
this.slot = this.el.shadowRoot!.querySelector('slot') as HTMLSlotElement;
this.slot.addEventListener('slotchange', this.onSlotChange.bind(this));
}
private async onSlotChange() {
this.debug('onSlotChange', this.tabs.length);
this.updateWidth();
this.indexTabs();
}
async componentDidRender() {
this.updateWidth();
}
/**
* @internal
*/
@Method()
async reindexTabs() {
this.updateWidth();
await this.indexTabs();
}
/**
* @internal
*
* Moves the container to align with the specified tab index
* @param index {number} Index of the tab
* @param animate {boolean} Whether to animate the transition
*/
@Method()
moveContainerByIndex(index: number, animate?: boolean): Promise<void> {
const scrollX = this.indexToPosition(index);
if (scrollX === 0 && index > 0) {
return Promise.resolve();
}
return this.moveContainer(scrollX, animate);
}
/**
* @internal
*
* Sets the scrollLeft property of the container
* @param scrollX {number}
* @param animate {boolean}
*/
@Method()
moveContainer(scrollX: number, animate?: boolean): Promise<void> {
if (animate) {
scrollEl(this.el, scrollX, this.config!.nativeSmoothScroll!, this.config!.transitionDuration);
} else {
this.el.scroll(scrollX, 0);
}
return Promise.resolve();
}
/** @internal */
@Method()
async setActiveTabIndex(index: number, moveContainer: boolean = true, animate: boolean = true): Promise<void> {
this.debug('setActiveTabIndex', index);
if (this._activeTabIndex === index) {
if (!this.autoScrollTop) {
return;
}
await this.scrollToTop();
}
if (moveContainer) {
await this.moveContainerByIndex(index, animate);
}
await this.updateActiveTabIndex(index, false);
}
/**
* Scroll the active tab to the top.
*/
@Method()
async scrollToTop() {
if (this._activeTabIndex === undefined || this.tabs === undefined) {
this.debug('activeTabIndex or tabs was undefined');
return;
}
const current = this.tabs[this._activeTabIndex];
this.queue.read(() => {
if (!current) {
this.debug('Current active tab was undefined in scrollToTop');
return;
}
// deepcode ignore PromiseNotCaughtGeneral: <comment the reason here>
current.getRootScrollableEl().then((el) => {
if (el) {
scrollEl(el, 0, this.config!.nativeSmoothScroll!, this.config!.transitionDuration);
}
});
});
}
private updateActiveTabIndex(index: number, emit: boolean = true) {
this.debug('updateActiveTabIndex', index, emit, this._activeTabIndex);
this._activeTabIndex = index;
emit && this.activeTabIndexChange.emit(this._activeTabIndex);
if (this.config!.lazyLoad) {
this.lazyLoadTabs();
}
}
private updateSelectedTabIndex(index: number) {
if (index === this._selectedTabIndex) {
return;
}
this._selectedTabIndex = index;
this.selectedTabIndexChange.emit(this._selectedTabIndex);
}
@Listen('touchstart')
async onTouchStart(ev: TouchEvent) {
if (!this.swipeEnabled) {
return;
}
if (this.config!.avoidElements) {
let avoid: boolean = false;
let element: any = ev.target;
if (element) {
do {
if (typeof element.getAttribute === 'function' && element.getAttribute('avoid-super-tabs')) {
return;
}
element = element.parentElement;
} while (element && !avoid);
}
}
const coords = pointerCoord(ev);
this.updateWidth();
const vw = this.width;
if (coords.x < this.leftThreshold || coords.x > vw - this.rightThreshold) {
// ignore this gesture, it started in the side menu touch zone
return;
}
if (this.config!.shortSwipeDuration! > 0) {
this.initialTimestamp = getTs();
}
this.initialCoords = coords;
this.lastPosX = coords.x;
}
@Listen('click', { passive: false, capture: true })
async onClick(ev: TouchEvent) {
if (this.isDragging) {
ev.stopImmediatePropagation();
ev.preventDefault();
}
}
@Listen('touchmove', { passive: true, capture: true })
async onTouchMove(ev: TouchEvent) {
if (!this.swipeEnabled || !this.initialCoords || typeof this.lastPosX !== 'number') {
return;
}
const coords = pointerCoord(ev);
if (!this.isDragging) {
if (!checkGesture(coords, this.initialCoords, this.config!)) {
if (Math.abs(coords.y - this.initialCoords.y) > 100) {
this.initialCoords = void 0;
this.lastPosX = void 0;
}
return;
}
this.isDragging = true;
}
// stop anything else from capturing these events, to make sure the content doesn't slide
if (!this.config!.allowElementScroll) {
ev.stopImmediatePropagation();
}
// get delta X
const deltaX: number = this.lastPosX! - coords.x;
if (deltaX === 0) {
return;
}
const scrollX = Math.max(0, Math.min(this.scrollWidth - this.width, this.el.scrollLeft + deltaX));
if (Math.floor(scrollX) === Math.floor(this.el.scrollLeft)) {
return;
}
const index = Math.round(this.positionToIndex(scrollX) * 100) / 100;
this.updateSelectedTabIndex(index);
// update last X value
this.lastPosX = coords.x;
this.el.scroll(scrollX, 0);
}
@Listen('touchend', { passive: false, capture: true })
async onTouchEnd(ev: TouchEvent) {
if (!this.swipeEnabled || !this.isDragging) {
return;
}
const coords = pointerCoord(ev);
const deltaTime: number = getTs() - this.initialTimestamp!;
const shortSwipe = this.config!.shortSwipeDuration! > 0 && deltaTime <= this.config!.shortSwipeDuration!;
const shortSwipeDelta = coords.x - this.initialCoords!.x;
let selectedTabIndex = this.calcSelectedTab();
const expectedTabIndex = Math.round(selectedTabIndex);
if (shortSwipe && expectedTabIndex === this._activeTabIndex) {
selectedTabIndex += shortSwipeDelta > 0 ? -1 : 1;
}
selectedTabIndex = this.normalizeSelectedTab(selectedTabIndex);
this.updateActiveTabIndex(selectedTabIndex);
this.moveContainerByIndex(selectedTabIndex, true);
this.isDragging = false;
this.initialCoords = void 0;
this.lastPosX = void 0;
}
private updateWidth() {
const boundingRect = this.el.getBoundingClientRect();
this.width = Math.round(boundingRect.width * 10000) / 10000;
}
private async indexTabs() {
if (this.width === 0) {
requestAnimationFrame(() => {
this.updateWidth();
this.indexTabs();
});
return;
}
const tabs = Array.from(this.el.querySelectorAll('super-tab'));
this.scrollWidth = this.width * tabs.length;
this.debug('indexTab', this.scrollWidth, this.width);
await Promise.all(tabs.map((t) => t.componentOnReady()));
this.tabs = tabs;
if (this.ready && typeof this._activeTabIndex === 'number') {
this.moveContainerByIndex(this._activeTabIndex, true);
}
this.lazyLoadTabs();
if (this.config) {
switch (this.config.sideMenu) {
case 'both':
this.rightThreshold = this.leftThreshold = this.config.sideMenuThreshold || 0;
break;
case 'left':
this.leftThreshold = this.config.sideMenuThreshold || 0;
break;
case 'right':
this.rightThreshold = this.config.sideMenuThreshold || 0;
break;
}
}
if (this._activeTabIndex !== undefined) {
this.moveContainerByIndex(this._activeTabIndex, false).then(() => {
this.ready = true;
});
}
}
private lazyLoadTabs() {
if (typeof this._activeTabIndex === 'undefined') {
this.debug('lazyLoadTabs', 'called when _activeTabIndex is undefined');
return;
}
if (!this.config) {
this.debug('lazyLoadTabs', 'called with no config available');
return;
}
if (!this.config!.lazyLoad) {
this.tabs.forEach((t: HTMLSuperTabElement) => {
t.loaded = true;
t.visible = true;
});
return;
}
const activeTab = this._activeTabIndex;
const tabs = [...this.tabs];
const min = activeTab - 1;
const max = activeTab + 1;
let index = 0;
for (const tab of tabs) {
tab.visible = index >= min && index <= max;
tab.loaded = tab.visible || (this.config!.unloadWhenInvisible ? false : tab.loaded);
index++;
}
this.tabs = tabs;
}
private calcSelectedTab(): number {
const scrollX = Math.max(0, Math.min(this.scrollWidth - this.width, this.el.scrollLeft));
return this.positionToIndex(scrollX);
}
private positionToIndex(scrollX: number) {
const tabWidth = this.width;
return scrollX / tabWidth;
}
private indexToPosition(tabIndex: number) {
return Math.round(tabIndex * this.width * 10000) / 10000;
}
private normalizeSelectedTab(index: number): number {
const scrollX = Math.max(0, Math.min(this.scrollWidth - this.width, this.indexToPosition(index)));
return Math.round(scrollX / this.width);
}
/**
* Internal method to output values in debug mode.
*/
private debug(...vals: any[]) {
debugLog(this.config!, 'container', vals);
}
render() {
return <slot></slot>;
}
}