forked from googlearchive/paper-toast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper-toast.html
312 lines (255 loc) · 8.71 KB
/
paper-toast.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
`paper-toast` provides lightweight feedback about an operation in a small popup
at the base of the screen on mobile and at the lower left on desktop. Toasts are
above all other elements on screen, including the FAB.
Toasts automatically disappear after a timeout or after user interaction
elsewhere on the screen, whichever comes first. Toasts can be swiped off
screen. There can be only one on the screen at a time.
Example:
<paper-toast text="Your draft has been discarded." onclick="discardDraft(el)"></paper-toast>
<script>
function discardDraft(el) {
el.show();
}
</script>
The fading out method can be customized by setting the `transition` attribute.
Example :
<paper-toast id="toast4" transition="core-transition-top" text="Fading to the top"></paper-toast>
An action button can be presented in the toast.
Example (using Polymer's data-binding features):
<paper-toast id="toast2" text="Connection timed out. Showing limited messages.">
<div style="color: blue;" on-tap="{{retry}}">Retry</div>
</paper-toast>
Positioning toast:
A standard toast appears near the lower left of the screen. You can change the
position by overriding bottom and left positions.
paper-toast {
bottom: 40px;
left: 10px;
}
To position the toast to the right:
paper-toast {
right: 10px;
left: auto;
}
To make it fit at the bottom of the screen:
paper-toast {
bottom: 0;
left: 0;
width: 100%;
}
When the screen size is smaller than the `responsiveWidth` (default to 480px),
the toast will automatically fits at the bottom of the screen.
@group Paper Elements
@element paper-toast
@homepage github.io
-->
<!--
Fired when the `paper-toast`'s `opened` property changes.
@event core-overlay-open
@param {boolean} detail the opened state
-->
<!--
Fired when the `paper-toast` has completely opened.
@event core-overlay-open-completed
-->
<!--
Fired when the `paper-toast` has completely closed.
@event core-overlay-close-completed
-->
<link rel="import" href="../core-overlay/core-overlay.html">
<link rel="import" href="../core-transition/core-transition-css.html">
<link rel="import" href="../core-media-query/core-media-query.html">
<polymer-element name="paper-toast" attributes="text duration opened responsiveWidth swipeDisabled autoCloseDisabled transition" role="status">
<template>
<link rel="stylesheet" href="paper-toast.css" >
<core-overlay id="overlay" autoFocusDisabled autoCloseDisabled="{{autoCloseDisabled}}" opened="{{opened}}" target="{{}}" transition="{{transition}}"></core-overlay>
<div class="toast-container" horizontal layout>
<div class="toast-text" flex>{{text}}</div>
<div class="toast-text toast-action" on-tap="{{dismiss}}">
<content></content>
</div>
</div>
<core-media-query query="max-width: {{responsiveWidth}}" queryMatches="{{narrowMode}}"></core-media-query>
</template>
<script>
(function() {
var currentToast;
Polymer('paper-toast', {
/**
* The text shows in a toast.
*
* @attribute text
* @type string
* @default ''
*/
text: '',
/**
* The duration in milliseconds to show the toast.
*
* @attribute duration
* @type number
* @default 3000
*/
duration: 3000,
/**
* Set opened to true to show the toast and to false to hide it.
*
* @attribute opened
* @type boolean
* @default false
*/
opened: false,
/**
* Min-width when the toast changes to narrow layout. In narrow layout,
* the toast fits at the bottom of the screen when opened.
*
* @attribute responsiveWidth
* @type string
* @default '480px'
*/
responsiveWidth: '480px',
/**
* If true, the toast can't be swiped.
*
* @attribute swipeDisabled
* @type boolean
* @default false
*/
swipeDisabled: false,
/**
* By default, the toast will close automatically if the user taps
* outside it or presses the escape key. Disable this behavior by setting
* the `autoCloseDisabled` property to true.
*
* @attribute autoCloseDisabled
* @type boolean
* @default false
*/
autoCloseDisabled: false,
/**
* Fading out transition type.
* Valid values can be found in core-transition/core-transition-css.html
*
* @attribute transition
* @type string
* @default core-transition-bottom
*/
transition: 'core-transition-bottom',
narrowMode: false,
eventDelegates: {
trackstart: 'trackStart',
track: 'track',
trackend: 'trackEnd',
transitionend: 'transitionEnd'
},
narrowModeChanged: function() {
this.classList.toggle('fit-bottom', this.narrowMode);
if (this.opened) {
this.$.overlay.resizeHandler();
}
},
openedChanged: function() {
if (this.opened) {
this.dismissJob = this.job(this.dismissJob, this.dismiss, this.duration);
} else {
this.dismissJob && this.dismissJob.stop();
this.dismiss();
}
},
/**
* Toggle the opened state of the toast.
* @method toggle
*/
toggle: function() {
this.opened = !this.opened;
},
/**
* Show the toast for the specified duration
* @method show
*/
show: function() {
if (currentToast) {
currentToast.dismiss();
}
currentToast = this;
this.opened = true;
},
/**
* Dismiss the toast and hide it.
* @method dismiss
*/
dismiss: function() {
if (this.dragging) {
this.shouldDismiss = true;
} else {
this.opened = false;
if (currentToast === this) {
currentToast = null;
}
}
},
trackStart: function(e) {
if (!this.swipeDisabled) {
e.preventTap();
this.vertical = e.yDirection;
this.w = this.offsetWidth;
this.h = this.offsetHeight;
this.dragging = true;
this.classList.add('dragging');
}
},
track: function(e) {
if (this.dragging) {
var s = this.style;
if (this.vertical) {
var y = e.dy;
s.opacity = (this.h - Math.abs(y)) / this.h;
s.transform = s.webkitTransform = 'translate3d(0, ' + y + 'px, 0)';
} else {
var x = e.dx;
s.opacity = (this.w - Math.abs(x)) / this.w;
s.transform = s.webkitTransform = 'translate3d(' + x + 'px, 0, 0)';
}
}
},
trackEnd: function(e) {
if (this.dragging) {
this.classList.remove('dragging');
this.style.opacity = '';
this.style.transform = this.style.webkitTransform = '';
var cl = this.classList;
if (this.vertical) {
cl.toggle('fade-out-down', e.yDirection === 1 && e.dy > 0);
cl.toggle('fade-out-up', e.yDirection === -1 && e.dy < 0);
} else {
cl.toggle('fade-out-right', e.xDirection === 1 && e.dx > 0);
cl.toggle('fade-out-left', e.xDirection === -1 && e.dx < 0);
}
this.dragging = false;
}
},
transitionEnd: function() {
var cl = this.classList;
if (cl.contains('fade-out-right') || cl.contains('fade-out-left') ||
cl.contains('fade-out-down') || cl.contains('fade-out-up')) {
this.dismiss();
cl.remove('fade-out-right', 'fade-out-left',
'fade-out-down', 'fade-out-up');
} else if (this.shouldDismiss) {
this.dismiss();
}
this.shouldDismiss = false;
}
});
})();
</script>
</polymer-element>