-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtab-mixin.js
154 lines (136 loc) · 3.56 KB
/
tab-mixin.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
import '../colors/colors.js';
import { css, html } from 'lit';
import { SkeletonMixin } from '../skeleton/skeleton-mixin.js';
const keyCodes = {
ENTER: 13,
SPACE: 32
};
export const TabMixin = superclass => class extends SkeletonMixin(superclass) {
static get properties() {
return {
/**
* Use to select the tab. Only one tab can be selected at a time.
* @type {boolean}
*/
selected: { type: Boolean, reflect: true },
/**
* @ignore
*/
// eslint-disable-next-line lit/no-native-attributes
role: { type: String, reflect: true },
/**
* @ignore
*/
tabIndex: { type: Number, reflect: true, attribute: 'tabindex' }
};
}
static get styles() {
const styles = [ css`
:host {
box-sizing: border-box;
display: inline-block;
max-width: 200px;
outline: none;
position: relative;
vertical-align: middle;
}
.d2l-tab-content {
margin: 0.5rem;
}
:host(:first-child) .d2l-tab-content {
margin-inline-start: 0;
}
.d2l-tab-selected-indicator {
border-top: 4px solid var(--d2l-color-celestine);
border-top-left-radius: 4px;
border-top-right-radius: 4px;
bottom: 0;
display: none;
margin: 1px 0.6rem 0 0.6rem;
position: absolute;
transition: box-shadow 0.2s;
width: calc(100% - 1.2rem);
}
:host(:first-child) .d2l-tab-selected-indicator {
margin-inline-start: 0;
width: calc(100% - 0.6rem);
}
:host([selected]:focus) {
text-decoration: none;
}
:host(:hover) {
color: var(--d2l-color-celestine);
cursor: pointer;
}
:host([selected]:hover) {
color: inherit;
cursor: default;
}
:host([selected]) .d2l-tab-selected-indicator {
display: block;
}
:host([skeleton]) .d2l-tab-selected-indicator {
position: absolute !important; /* make sure skeleton styles do not override this */
}
`];
super.styles && styles.unshift(super.styles);
return styles;
}
constructor() {
super();
this.role = 'tab';
this.selected = false;
this.tabIndex = -1;
}
firstUpdated(changedProperties) {
super.firstUpdated(changedProperties);
this.addEventListener('click', this.#handleClick);
this.addEventListener('keydown', this.#handleKeydown);
this.addEventListener('keyup', this.#handleKeyup);
}
render() {
return html`
<div class="d2l-skeletize d2l-tab-content">${this.renderContent()}</div>
<div class="d2l-tab-selected-indicator d2l-skeletize-container"></div>
`;
}
update(changedProperties) {
super.update(changedProperties);
if (changedProperties.has('selected')) {
this.ariaSelected = `${this.selected}`;
if (this.selected) {
/** Dispatched when a tab is selected */
this.dispatchEvent(new CustomEvent(
'd2l-tab-selected', { bubbles: true, composed: true }
));
}
}
}
/**
* IMPORTANT: Call this in any consumer when anything changes that could impact the tab's size
* Notifies the parent d2l-tabs component of a change so that it can update virtual scrolling calculations
* */
dispatchContentChangeEvent() {
this.dispatchEvent(new CustomEvent(
'd2l-tab-content-change', { bubbles: true, composed: true }
));
}
renderContent() {
console.warn('Subclasses to implement/override renderContent');
return html`<div>Default Tab Content</div>`;
}
#handleClick() {
this.selected = true;
};
#handleKeydown(e) {
if (e.keyCode === keyCodes.SPACE || e.keyCode === keyCodes.ENTER) {
e.stopPropagation();
e.preventDefault();
}
};
#handleKeyup(e) {
if (e.keyCode === keyCodes.SPACE || e.keyCode === keyCodes.ENTER) {
this.#handleClick();
}
};
};