2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 `paper-toast` provides lightweight feedback about an operation in a small popup
12 at the base of the screen on mobile and at the lower left on desktop. Toasts are
13 above all other elements on screen, including the FAB.
15 Toasts automatically disappear after a timeout or after user interaction
16 elsewhere on the screen, whichever comes first. Toasts can be swiped off
17 screen. There can be only one on the screen at a time.
21 <paper-toast text="Your draft has been discarded." onclick="discardDraft(el)"></paper-toast>
24 function discardDraft(el) {
29 An action button can be presented in the toast.
31 Example (using Polymer's data-binding features):
33 <paper-toast id="toast2" text="Connection timed out. Showing limited messages.">
34 <div style="color: blue;" on-tap="{{retry}}">Retry</div>
39 A standard toast appears near the lower left of the screen. You can change the
40 position by overriding bottom and left positions.
47 To position the toast to the right:
54 To make it fit at the bottom of the screen:
62 When the screen size is smaller than the `responsiveWidth` (default to 480px),
63 the toast will automatically fits at the bottom of the screen.
70 Fired when the `paper-toast`'s `opened` property changes.
72 @event core-overlay-open
73 @param {boolean} detail the opened state
76 Fired when the `paper-toast` has completely opened.
78 @event core-overlay-open-completed
81 Fired when the `paper-toast` has completely closed.
83 @event core-overlay-close-completed
85 <link rel="import" href="../core-overlay/core-overlay.html">
86 <link rel="import" href="../core-transition/core-transition-css.html">
87 <link rel="import" href="../core-media-query/core-media-query.html">
89 <polymer-element name="paper-toast" attributes="text duration opened responsiveWidth swipeDisabled autoCloseDisabled" role="status">
93 <link rel="stylesheet" href="paper-toast.css" >
95 <core-overlay id="overlay" autoFocusDisabled autoCloseDisabled="{{autoCloseDisabled}}" opened="{{opened}}" target="{{}}" transition="core-transition-bottom"></core-overlay>
97 <div class="toast-container" horizontal layout>
99 <div class="toast-text" flex>{{text}}</div>
101 <div class="toast-text toast-action" on-tap="{{dismiss}}">
107 <core-media-query query="max-width: {{responsiveWidth}}" queryMatches="{{narrowMode}}"></core-media-query>
116 Polymer('paper-toast', {
119 * The text shows in a toast.
128 * The duration in milliseconds to show the toast.
130 * @attribute duration
137 * Set opened to true to show the toast and to false to hide it.
146 * Min-width when the toast changes to narrow layout. In narrow layout,
147 * the toast fits at the bottom of the screen when opened.
149 * @attribute responsiveWidth
153 responsiveWidth: '480px',
156 * If true, the toast can't be swiped.
158 * @attribute swipeDisabled
162 swipeDisabled: false,
165 * By default, the toast will close automatically if the user taps
166 * outside it or presses the escape key. Disable this behavior by setting
167 * the `autoCloseDisabled` property to true.
169 * @attribute autoCloseDisabled
173 autoCloseDisabled: false,
178 trackstart: 'trackStart',
180 trackend: 'trackEnd',
181 transitionend: 'transitionEnd'
184 narrowModeChanged: function() {
185 this.classList.toggle('fit-bottom', this.narrowMode);
187 this.$.overlay.resizeHandler();
191 openedChanged: function() {
193 this.dismissJob = this.job(this.dismissJob, this.dismiss, this.duration);
195 this.dismissJob && this.dismissJob.stop();
201 * Toggle the opened state of the toast.
205 this.opened = !this.opened;
209 * Show the toast for the specified duration
214 currentToast.dismiss();
221 * Dismiss the toast and hide it.
224 dismiss: function() {
226 this.shouldDismiss = true;
229 if (currentToast === this) {
235 trackStart: function(e) {
236 if (!this.swipeDisabled) {
238 this.vertical = e.yDirection;
239 this.w = this.offsetWidth;
240 this.h = this.offsetHeight;
241 this.dragging = true;
242 this.classList.add('dragging');
251 s.opacity = (this.h - Math.abs(y)) / this.h;
252 s.transform = s.webkitTransform = 'translate3d(0, ' + y + 'px, 0)';
255 s.opacity = (this.w - Math.abs(x)) / this.w;
256 s.transform = s.webkitTransform = 'translate3d(' + x + 'px, 0, 0)';
261 trackEnd: function(e) {
263 this.classList.remove('dragging');
264 this.style.opacity = '';
265 this.style.transform = this.style.webkitTransform = '';
266 var cl = this.classList;
268 cl.toggle('fade-out-down', e.yDirection === 1 && e.dy > 0);
269 cl.toggle('fade-out-up', e.yDirection === -1 && e.dy < 0);
271 cl.toggle('fade-out-right', e.xDirection === 1 && e.dx > 0);
272 cl.toggle('fade-out-left', e.xDirection === -1 && e.dx < 0);
274 this.dragging = false;
278 transitionEnd: function() {
279 var cl = this.classList;
280 if (cl.contains('fade-out-right') || cl.contains('fade-out-left') ||
281 cl.contains('fade-out-down') || cl.contains('fade-out-up')) {
283 cl.remove('fade-out-right', 'fade-out-left',
284 'fade-out-down', 'fade-out-up');
285 } else if (this.shouldDismiss) {
288 this.shouldDismiss = false;