forked from zyra/ionic-super-tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper-tab-button.component.tsx
81 lines (66 loc) · 1.98 KB
/
super-tab-button.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
import { Component, ComponentInterface, Element, h, Host, Prop, State } from '@stencil/core';
const maxRetryAttempts = 1e3;
@Component({
tag: 'super-tab-button',
styleUrl: 'super-tab-button.component.scss',
shadow: true,
})
export class SuperTabButtonComponent implements ComponentInterface {
@Element() el!: HTMLSuperTabButtonElement;
/** @internal */
@Prop({ reflectToAttr: true }) active?: boolean;
/** @internal */
@Prop({ reflectToAttr: true }) index?: number;
/**
* Whether the button is disabled
*/
@Prop({ reflectToAttr: true }) disabled?: boolean;
/** @internal */
@Prop() scrollableContainer: boolean = false;
@State() label!: HTMLElement | null;
@State() icon!: HTMLElement | null;
private retryAttempts: number = 0;
componentDidLoad() {
this.indexChildren();
this.initCmp();
}
private initCmp() {
if (!this.el || !this.el.shadowRoot) {
if (++this.retryAttempts < maxRetryAttempts) {
requestAnimationFrame(() => this.initCmp());
return;
}
}
if (!this.label && !this.icon) {
this.indexChildren();
}
const slot = this.el!.shadowRoot!.querySelector('slot');
slot!.addEventListener('slotchange', () => {
this.indexChildren();
});
}
private indexChildren() {
this.label = this.el.querySelector('ion-label');
this.icon = this.el.querySelector('ion-icon');
}
render() {
return (
<Host
role="button"
aria-label={this.label ? this.label.innerText : false}
aria-disabled={this.disabled ? 'true' : false}
aria-selected={this.active ? 'true' : 'false'}
class={{
'ion-activatable': true,
'ion-focusable': true,
'icon-only': !!this.icon && !this.label,
'label-only': !!this.label && !this.icon,
active: Boolean(this.active),
scrollableContainer: this.scrollableContainer,
}}>
<slot/>
<ion-ripple-effect type="unbounded"/>
</Host>
);
}
}