This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bottom-tab.html
245 lines (205 loc) · 6.05 KB
/
bottom-tab.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymer/lib/utils/render-status.html">
<link rel="import" href="../iron-behaviors/iron-button-state.html">
<link rel="import" href="../iron-behaviors/iron-control-state.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<!--
-->
<dom-module id="bottom-tab">
<template>
<style>
:host {
@apply --layout-inline;
@apply --layout-center;
@apply --layout-center-justified;
@apply --layout-flex-auto;
position: relative;
padding: 0 12px;
overflow: hidden;
cursor: pointer;
vertical-align: middle;
@apply --paper-font-common-base;
@apply --bottom-tab;
}
:host(:focus) {
outline: none;
}
:host([link]) {
padding: 0;
}
.tab-content {
height: 100%;
transform: translateZ(0);
-webkit-transform: translateZ(0);
padding-bottom: 10px;
@apply --layout-vertical;
@apply --layout-center-center;
@apply --bottom-tab-content;
}
:host(:not(.iron-selected)) {
transition: width .15s cubic-bezier(0.4, 0.0, 1, 1);
width: 2em;
}
:host(:not(.iron-selected)) .tab-content {
opacity: 0.8;
padding-top: 8px;
@apply --bottom-tab-content-unselected;
}
:host(:not(.iron-selected)) :not([show]).label {
display: none;
}
:host(:not(.iron-selected)) .label {
font-size: 12px;
transition: font-size .1s ease-in;
}
:host(:focus) .tab-content {
opacity: 1;
}
.tab-content > ::slotted(a) {
height: 100%;
width: 2em;
}
:host(.iron-selected:not([show-label])) {
width: 3.5em;
}
:host(.iron-selected) {
width: 2em;
}
:host(.iron-selected) > .tab-content {
font-size: 14px;
font-weight: normal;
padding-top: 6px;
transition: padding-top .1s ease-in;
@apply --bottom-tab-content-selected;
}
</style>
<div class="tab-content">
<iron-icon icon="[[icon]]"></iron-icon>
<span show$="[[showLabel]]" class="label">[[label]]</span>
<slot></slot>
</div>
</template>
<script>
/* `bottom-tab` is a tab element for navigating the pages of a bottom-nav. An iron-icon
* can be used to assign a logo to the tab. An optional label can also be used to
* clarify the tab's meaning.
*
* <bottom-nav>
* <bottom-toolbar selected="0">
* <bottom-tab label="Home" icon="icons:home"></bottom-tab>
* <bottom-tab label="Favorites" icon="icons:favorite"></bottom-tab>
* <bottom-tab label="Recent" icon="icons:clock"></bottom-tab>
* </bottom-toolbar>
* </bottom-nav>
*
* ## Styling
*
* Mixin | Description | Default
* ------|-------------|----------
* `--bottom-tab` | Mixin applied to the tab | `{}`
* `--bottom-tab-content` | Mixin applied to the tab content | `{}`
* `--bottom-tab-content-unselected` | Mixin applied to the tab content when the tab is not selected | `{}`
*
* @demo demo/index.html */
class BottomTab extends Polymer.mixinBehaviors([
Polymer.IronControlState,
Polymer.IronButtonState
], Polymer.Element) {
static get is() { return 'bottom-tab'; }
static get properties() {
return {
/**
* If true, the tab will forward keyboard clicks (enter/space) to
* the first anchor element found in its descendants
*/
link: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* A label for the tab that is hidden when not active.
*/
label: String,
/**
* Whether the label should be displayed or hidden, default hidden
*/
showLabel: {
type: Boolean,
value: false
},
/**
* The name of an icon to display
*/
icon: String,
/**
* A CSS class to attach to the parent element when the tab is selected
*/
selectedClass: String
}
}
constructor() {
super();
this.setAttribute('role', 'tab');
}
connectedCallback() {
super.connectedCallback();
this._updateNoink();
Polymer.RenderStatus.beforeNextRender(this, function() {
this.addEventListener('down', this._updateNoink);
this.addEventListener('tap', this._onTap);
});
}
get _parentNoink() {
var parent = Polymer.dom(this).parentNode;
return !!parent && !!parent.noink;
}
_updateNoink() {
this.noink = !!this.noink || !!this._parentNoink;
}
/**
* Returns true when the current tab is selected
*/
_isSelected() {
return Polymer.dom(this).node.hasAttribute('aria-selected');
}
/**
* Fire an event to update the CSS class of the parent element.
*/
_updateClass() {
var details = {className: this.selectedClass};
this.fire('bottom-tab-update-class', details);
}
/**
* Fire a ripple event when the tab is selected
* @param {Event} event
*/
_activateRipple(event) {
if (!this._isSelected() && !this.noink) {
var x = event.detail.x,
y = event.detail.y,
details = {x: x, y: y};
this.fire('bottom-tab-ripple', details);
this._updateClass();
}
}
_onTap(event) {
if (this.link) {
var anchor = this.queryEffectiveChildren('a');
if (!anchor) {
return;
}
// Don't get stuck in a loop delegating
// the listener from the child anchor
if (event.target === anchor) {
return;
}
anchor.click();
}
this._activateRipple(event);
}
}
customElements.define(BottomTab.is, BottomTab);
</script>
</dom-module>