1 <!-- 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 8 --> 9 10 <!-- 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. 14 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. 18 19 Example: 20 21 <paper-toast text="Your draft has been discarded." onclick="discardDraft(el)"></paper-toast> 22 23 <script> 24 function discardDraft(el) { 25 el.show(); 26 } 27 </script> 28 29 An action button can be presented in the toast. 30 31 Example (using Polymer's data-binding features): 32 33 <paper-toast id="toast2" text="Connection timed out. Showing limited messages."> 34 <div style="color: blue;" on-tap="{{retry}}">Retry</div> 35 </paper-toast> 36 37 Positioning toast: 38 39 A standard toast appears near the lower left of the screen. You can change the 40 position by overriding bottom and left positions. 41 42 paper-toast { 43 bottom: 40px; 44 left: 10px; 45 } 46 47 To make it fit at the bottom of the screen: 48 49 paper-toast { 50 bottom: 0; 51 left: 0; 52 width: 100%; 53 } 54 55 When the screen size is smaller than the `responsiveWidth` (default to 480px), 56 the toast will automatically fits at the bottom of the screen. 57 58 @group Paper Elements 59 @element paper-toast 60 @homepage github.io 61 --> 62 63 <link rel="import" href="../core-overlay/core-overlay.html"> 64 <link rel="import" href="../core-transition/core-transition-css.html"> 65 <link rel="import" href="../core-media-query/core-media-query.html"> 66 67 <polymer-element name="paper-toast" attributes="text duration opened responsiveWidth swipeDisabled" role="status"> 68 69 <template> 70 71 <link rel="stylesheet" href="paper-toast.css" > 72 73 <core-overlay opened="{{opened}}" target="{{}}" sizingTarget="{{$.container}}" transition="core-transition-bottom"></core-overlay> 74 75 <div class="toast-container" horizontal layout> 76 77 <div class="toast-text" flex>{{text}}</div> 78 79 <div class="toast-text toast-action" on-tap="{{dismiss}}"> 80 <content></content> 81 </div> 82 83 </div> 84 85 <core-media-query query="max-width: {{responsiveWidth}}" queryMatches="{{narrowMode}}"></core-media-query> 86 87 </template> 88 <script> 89 90 (function() { 91 92 var currentToast; 93 94 Polymer('paper-toast', { 95 96 /** 97 * The text shows in a toast. 98 * 99 * @attribute text 100 * @type string 101 * @default '' 102 */ 103 text: '', 104 105 /** 106 * The duration in milliseconds to show the toast. 107 * 108 * @attribute duration 109 * @type number 110 * @default 3000 111 */ 112 duration: 3000, 113 114 /** 115 * Set opened to true to show the toast and to false to hide it. 116 * 117 * @attribute opened 118 * @type boolean 119 * @default false 120 */ 121 opened: false, 122 123 /** 124 * Min-width when the toast changes to narrow layout. In narrow layout, 125 * the toast fits at the bottom of the screen when opened. 126 * 127 * @attribute responsiveWidth 128 * @type string 129 * @default '480px' 130 */ 131 responsiveWidth: '480px', 132 133 /** 134 * If true, the toast can't be swiped. 135 * 136 * @attribute swipeDisabled 137 * @type boolean 138 * @default false 139 */ 140 swipeDisabled: false, 141 142 eventDelegates: { 143 trackstart: 'trackStart', 144 track: 'track', 145 trackend: 'trackEnd', 146 transitionend: 'transitionEnd' 147 }, 148 149 narrowModeChanged: function() { 150 this.classList.toggle('fit-bottom', this.narrowMode); 151 }, 152 153 openedChanged: function() { 154 if (this.opened) { 155 this.dismissJob = this.job(this.dismissJob, this.dismiss, this.duration); 156 } else { 157 this.dismissJob && this.dismissJob.stop(); 158 this.dismiss(); 159 } 160 }, 161 162 /** 163 * Toggle the opened state of the toast. 164 * @method toggle 165 */ 166 toggle: function() { 167 this.opened = !this.opened; 168 }, 169 170 /** 171 * Show the toast for the specified duration 172 * @method show 173 */ 174 show: function() { 175 if (currentToast) { 176 currentToast.dismiss(); 177 } 178 currentToast = this; 179 this.opened = true; 180 }, 181 182 /** 183 * Dismiss the toast and hide it. 184 * @method dismiss 185 */ 186 dismiss: function() { 187 if (this.dragging) { 188 this.shouldDismiss = true; 189 } else { 190 this.opened = false; 191 if (currentToast === this) { 192 currentToast = null; 193 } 194 } 195 }, 196 197 trackStart: function(e) { 198 if (!this.swipeDisabled) { 199 e.preventTap(); 200 this.vertical = e.yDirection; 201 this.w = this.offsetWidth; 202 this.h = this.offsetHeight; 203 this.dragging = true; 204 this.classList.add('dragging'); 205 } 206 }, 207 208 track: function(e) { 209 if (this.dragging) { 210 var s = this.style; 211 if (this.vertical) { 212 var y = e.dy; 213 s.opacity = (this.h - Math.abs(y)) / this.h; 214 s.webkitTransform = s.transform = 'translate3d(0, ' + y + 'px, 0)'; 215 } else { 216 var x = e.dx; 217 s.opacity = (this.w - Math.abs(x)) / this.w; 218 s.webkitTransform = s.transform = 'translate3d(' + x + 'px, 0, 0)'; 219 } 220 } 221 }, 222 223 trackEnd: function(e) { 224 if (this.dragging) { 225 this.classList.remove('dragging'); 226 this.style.opacity = null; 227 this.style.webkitTransform = this.style.transform = null; 228 var cl = this.classList; 229 if (this.vertical) { 230 cl.toggle('fade-out-down', e.yDirection === 1 && e.dy > 0); 231 cl.toggle('fade-out-up', e.yDirection === -1 && e.dy < 0); 232 } else { 233 cl.toggle('fade-out-right', e.xDirection === 1 && e.dx > 0); 234 cl.toggle('fade-out-left', e.xDirection === -1 && e.dx < 0); 235 } 236 this.dragging = false; 237 } 238 }, 239 240 transitionEnd: function() { 241 var cl = this.classList; 242 if (cl.contains('fade-out-right') || cl.contains('fade-out-left') || 243 cl.contains('fade-out-down') || cl.contains('fade-out-up')) { 244 this.dismiss(); 245 cl.remove('fade-out-right', 'fade-out-left', 246 'fade-out-down', 'fade-out-up'); 247 } else if (this.shouldDismiss) { 248 this.dismiss(); 249 } 250 this.shouldDismiss = false; 251 } 252 253 }); 254 255 })(); 256 257 </script> 258 </polymer-element> 259