/
vaadin-tabs.html
270 lines (230 loc) · 8.01 KB
/
vaadin-tabs.html
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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../../polymer/polymer-element.html">
<link rel="import" href="../../vaadin-themable-mixin/vaadin-themable-mixin.html">
<link rel="import" href="../../vaadin-list-mixin/vaadin-list-mixin.html">
<link rel="import" href="../../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../../vaadin-element-mixin/vaadin-element-mixin.html">
<link rel="import" href="vaadin-tab.html">
<dom-module id="vaadin-tabs">
<template>
<style>
:host {
display: flex;
align-items: center;
}
:host([hidden]) {
display: none !important;
}
:host([orientation="vertical"]) {
display: block;
}
:host([orientation="horizontal"]) [part="tabs"] {
flex-grow: 1;
display: flex;
align-self: stretch;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
-ms-overflow-style: none;
}
/* This seems more future-proof than `overflow: -moz-scrollbars-none` which is marked obsolete
and is no longer guaranteed to work:
https://developer.mozilla.org/en-US/docs/Web/CSS/overflow#Mozilla_Extensions */
@-moz-document url-prefix() {
:host([orientation="horizontal"]) [part="tabs"] {
overflow: hidden;
}
}
:host([orientation="horizontal"]) [part="tabs"]::-webkit-scrollbar {
display: none;
}
:host([orientation="vertical"]) [part="tabs"] {
height: 100%;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
[part="back-button"],
[part="forward-button"] {
pointer-events: none;
opacity: 0;
cursor: default;
}
:host([overflow~="start"]) [part="back-button"],
:host([overflow~="end"]) [part="forward-button"] {
pointer-events: auto;
opacity: 1;
}
[part="back-button"]::after {
content: '◀';
}
[part="forward-button"]::after {
content: '▶';
}
:host([orientation="vertical"]) [part="back-button"],
:host([orientation="vertical"]) [part="forward-button"] {
display: none;
}
/* RTL specific styles */
:host([dir="rtl"]) [part="back-button"]::after {
content: '▶';
}
:host([dir="rtl"]) [part="forward-button"]::after {
content: '◀';
}
</style>
<div on-click="_scrollBack" part="back-button"></div>
<div id="scroll" part="tabs">
<slot></slot>
</div>
<div on-click="_scrollForward" part="forward-button"></div>
</template>
<script>
(function() {
const safari10 = /Apple.* Version\/(9|10)/.test(navigator.userAgent);
/**
* `<vaadin-tabs>` is a Web Component for easy switching between different views.
*
* ```
* <vaadin-tabs selected="4">
* <vaadin-tab>Page 1</vaadin-tab>
* <vaadin-tab>Page 2</vaadin-tab>
* <vaadin-tab>Page 3</vaadin-tab>
* <vaadin-tab>Page 4</vaadin-tab>
* </vaadin-tabs>
* ```
*
* ### Styling
*
* The following shadow DOM parts are available for styling:
*
* Part name | Description
* ------------------|--------------------------------------
* `back-button` | Button for moving the scroll back
* `tabs` | The tabs container
* `forward-button` | Button for moving the scroll forward
*
* The following state attributes are available for styling:
*
* Attribute | Description | Part name
* -----------|-------------|------------
* `orientation` | Tabs disposition, valid values are `horizontal` and `vertical`. | :host
* `overflow` | It's set to `start`, `end`, none or both. | :host
*
* See [ThemableMixin – how to apply styles for shadow parts](https://github.com/vaadin/vaadin-themable-mixin/wiki)
*
* @memberof Vaadin
* @mixes Vaadin.ElementMixin
* @mixes Vaadin.ListMixin
* @mixes Vaadin.ThemableMixin
* @demo demo/index.html
*/
class TabsElement extends
Vaadin.ElementMixin(
Vaadin.ListMixin(
Vaadin.ThemableMixin(
Polymer.mixinBehaviors([Polymer.IronResizableBehavior], Polymer.Element)))) {
static get is() {
return 'vaadin-tabs';
}
static get version() {
return '3.2.0';
}
static get properties() {
return {
/**
* Set tabs disposition. Possible values are `horizontal|vertical`
* @type {!ListOrientation}
*/
orientation: {
value: 'horizontal',
type: String
},
/**
* The index of the selected tab.
*/
selected: {
value: 0,
type: Number
}
};
}
static get observers() {
return ['_updateOverflow(items.*, vertical)'];
}
ready() {
super.ready();
this.addEventListener('iron-resize', () => this._updateOverflow());
this._scrollerElement.addEventListener('scroll', () => this._updateOverflow());
this.setAttribute('role', 'tablist');
// Wait for the vaadin-tab elements to upgrade and get styled
Polymer.RenderStatus.afterNextRender(this, () => {
this._updateOverflow();
});
}
/** @private */
_scrollForward() {
this._scroll(-this.__direction * this._scrollOffset);
}
/** @private */
_scrollBack() {
this._scroll(this.__direction * this._scrollOffset);
}
/**
* @return {number}
* @protected
*/
get _scrollOffset() {
return this._vertical ? this._scrollerElement.offsetHeight : this._scrollerElement.offsetWidth;
}
/**
* @return {!HTMLElement}
* @protected
*/
get _scrollerElement() {
return this.$.scroll;
}
/** @private */
get __direction() {
return !this._vertical && this.getAttribute('dir') === 'rtl' ? 1 : -1;
}
/** @private */
_updateOverflow() {
const scrollPosition = this._vertical ? this._scrollerElement.scrollTop : this.__getNormalizedScrollLeft(this._scrollerElement);
let scrollSize = this._vertical ? this._scrollerElement.scrollHeight : this._scrollerElement.scrollWidth;
// In Edge we need to adjust the size in 1 pixel
scrollSize -= 1;
let overflow = scrollPosition > 0 ? 'start' : '';
overflow += scrollPosition + this._scrollOffset < scrollSize ? ' end' : '';
if (this.__direction == 1) {
overflow = overflow.replace(/start|end/gi, matched => {
return matched === 'start' ? 'end' : 'start';
});
}
overflow ? this.setAttribute('overflow', overflow.trim()) : this.removeAttribute('overflow');
this._repaintShadowNodesHack();
}
/** @private */
_repaintShadowNodesHack() {
// Safari 10 has an issue with repainting shadow root element styles when a host attribute changes.
// Need this workaround (toggle any inline css property on and off) until the issue gets fixed.
/* istanbul ignore if */
if (safari10 && this.root) {
const WEBKIT_PROPERTY = '-webkit-backface-visibility';
this.root.querySelectorAll('*').forEach(el => {
el.style[WEBKIT_PROPERTY] = 'visible';
el.style[WEBKIT_PROPERTY] = '';
});
}
}
}
customElements.define(TabsElement.is, TabsElement);
/**
* @namespace Vaadin
*/
window.Vaadin.TabsElement = TabsElement;
})();
</script>
</dom-module>