-
Notifications
You must be signed in to change notification settings - Fork 25
/
tab-internal.js
160 lines (148 loc) · 4.15 KB
/
tab-internal.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
155
156
157
158
159
160
import '../colors/colors.js';
import { css, html, LitElement, unsafeCSS } from 'lit';
import { classMap } from 'lit/directives/class-map.js';
import { getFocusPseudoClass } from '../../helpers/focus.js';
import { RtlMixin } from '../../mixins/rtl/rtl-mixin.js';
import { SkeletonMixin } from '../skeleton/skeleton-mixin.js';
const keyCodes = {
ENTER: 13,
SPACE: 32
};
class Tab extends SkeletonMixin(RtlMixin(LitElement)) {
static get properties() {
return {
ariaSelected: { type: String, reflect: true, attribute: 'aria-selected' },
controlsPanel: { type: String, reflect: true, attribute: 'controls-panel' },
// eslint-disable-next-line lit/no-native-attributes
role: { type: String, reflect: true },
text: { type: String }
};
}
static get styles() {
return [super.styles, css`
:host {
box-sizing: border-box;
display: inline-block;
max-width: 200px;
outline: none;
position: relative;
vertical-align: middle;
}
.d2l-tab-text {
margin: 0.5rem;
overflow: hidden;
padding: 0.1rem;
text-overflow: ellipsis;
white-space: nowrap;
}
:host([skeleton]) .d2l-tab-text.d2l-skeletize::before {
bottom: 0.15rem;
top: 0.15rem;
}
:host(:first-child) .d2l-tab-text {
margin-left: 0;
}
:host([dir="rtl"]:first-child) .d2l-tab-text {
margin-left: 0.6rem;
margin-right: 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;
-webkit-transition: box-shadow 0.2s;
transition: box-shadow 0.2s;
width: calc(100% - 1.2rem);
}
:host([skeleton]) .d2l-tab-selected-indicator {
position: absolute; /* make sure skeleton styles do not override this */
}
.d2l-tab-text-skeletize-override {
min-width: 50px;
}
:host(:first-child) .d2l-tab-selected-indicator {
margin-left: 0;
width: calc(100% - 0.6rem);
}
:host([dir="rtl"]:first-child) .d2l-tab-selected-indicator {
margin-left: 0.6rem;
margin-right: 0;
}
:host(:${unsafeCSS(getFocusPseudoClass())}) > .d2l-tab-text {
border-radius: 0.3rem;
box-shadow: 0 0 0 2px var(--d2l-color-celestine);
color: var(--d2l-color-celestine);
}
:host([aria-selected="true"]:focus) {
text-decoration: none;
}
:host(:hover) {
color: var(--d2l-color-celestine);
cursor: pointer;
}
:host([aria-selected="true"]:hover) {
color: inherit;
cursor: default;
}
:host([aria-selected="true"]) .d2l-tab-selected-indicator {
display: block;
}
@media (prefers-reduced-motion: reduce) {
.d2l-tab-selected-indicator {
-webkit-transition: none;
transition: none;
}
}
`];
}
constructor() {
super();
this.ariaSelected = 'false';
this.role = 'tab';
this.tabIndex = -1;
}
firstUpdated(changedProperties) {
super.firstUpdated(changedProperties);
this.addEventListener('click', () => {
this.ariaSelected = 'true';
});
this.addEventListener('keydown', (e) => {
if (e.keyCode !== keyCodes.SPACE) return;
e.stopPropagation();
e.preventDefault();
});
this.addEventListener('keyup', (e) => {
if (e.keyCode !== keyCodes.ENTER && e.keyCode !== keyCodes.SPACE) return;
this.ariaSelected = 'true';
});
}
render() {
const overrideSkeletonText = this.skeleton && (!this.text || this.text.length === 0);
const textClasses = {
'd2l-tab-text': true,
'd2l-skeletize': true,
'd2l-tab-text-skeletize-override': overrideSkeletonText
};
return html`
<div class="${classMap(textClasses)}">${overrideSkeletonText ? html` ` : this.text}</div>
<div class="d2l-tab-selected-indicator d2l-skeletize-container"></div>
`;
}
update(changedProperties) {
super.update(changedProperties);
changedProperties.forEach((oldVal, prop) => {
if (prop === 'ariaSelected' && this.ariaSelected === 'true') {
this.dispatchEvent(new CustomEvent(
'd2l-tab-selected', { bubbles: true, composed: true }
));
} else if (prop === 'text') {
this.title = this.text;
}
});
}
}
customElements.define('d2l-tab-internal', Tab);