UNPKG

19.2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5const index = require('./index-a0a08b2a.js');
6const ionicGlobal = require('./ionic-global-06f21c1a.js');
7const overlays = require('./overlays-59863ad4.js');
8const index$1 = require('./index-e1bb33c3.js');
9const theme = require('./theme-30b7a575.js');
10const animation = require('./animation-13cbbb20.js');
11require('./hardware-back-button-148ce546.js');
12require('./helpers-d381ec4d.js');
13
14/**
15 * iOS Toast Enter Animation
16 */
17const iosEnterAnimation = (baseEl, position) => {
18 const baseAnimation = animation.createAnimation();
19 const wrapperAnimation = animation.createAnimation();
20 const hostEl = baseEl.host || baseEl;
21 const wrapperEl = baseEl.querySelector('.toast-wrapper');
22 const bottom = `calc(-10px - var(--ion-safe-area-bottom, 0px))`;
23 const top = `calc(10px + var(--ion-safe-area-top, 0px))`;
24 wrapperAnimation.addElement(wrapperEl);
25 switch (position) {
26 case 'top':
27 wrapperAnimation.fromTo('transform', 'translateY(-100%)', `translateY(${top})`);
28 break;
29 case 'middle':
30 const topPosition = Math.floor(hostEl.clientHeight / 2 - wrapperEl.clientHeight / 2);
31 wrapperEl.style.top = `${topPosition}px`;
32 wrapperAnimation.fromTo('opacity', 0.01, 1);
33 break;
34 default:
35 wrapperAnimation.fromTo('transform', 'translateY(100%)', `translateY(${bottom})`);
36 break;
37 }
38 return baseAnimation
39 .addElement(hostEl)
40 .easing('cubic-bezier(.155,1.105,.295,1.12)')
41 .duration(400)
42 .addAnimation(wrapperAnimation);
43};
44
45/**
46 * iOS Toast Leave Animation
47 */
48const iosLeaveAnimation = (baseEl, position) => {
49 const baseAnimation = animation.createAnimation();
50 const wrapperAnimation = animation.createAnimation();
51 const hostEl = baseEl.host || baseEl;
52 const wrapperEl = baseEl.querySelector('.toast-wrapper');
53 const bottom = `calc(-10px - var(--ion-safe-area-bottom, 0px))`;
54 const top = `calc(10px + var(--ion-safe-area-top, 0px))`;
55 wrapperAnimation.addElement(wrapperEl);
56 switch (position) {
57 case 'top':
58 wrapperAnimation.fromTo('transform', `translateY(${top})`, 'translateY(-100%)');
59 break;
60 case 'middle':
61 wrapperAnimation.fromTo('opacity', 0.99, 0);
62 break;
63 default:
64 wrapperAnimation.fromTo('transform', `translateY(${bottom})`, 'translateY(100%)');
65 break;
66 }
67 return baseAnimation
68 .addElement(hostEl)
69 .easing('cubic-bezier(.36,.66,.04,1)')
70 .duration(300)
71 .addAnimation(wrapperAnimation);
72};
73
74/**
75 * MD Toast Enter Animation
76 */
77const mdEnterAnimation = (baseEl, position) => {
78 const baseAnimation = animation.createAnimation();
79 const wrapperAnimation = animation.createAnimation();
80 const hostEl = baseEl.host || baseEl;
81 const wrapperEl = baseEl.querySelector('.toast-wrapper');
82 const bottom = `calc(8px + var(--ion-safe-area-bottom, 0px))`;
83 const top = `calc(8px + var(--ion-safe-area-top, 0px))`;
84 wrapperAnimation.addElement(wrapperEl);
85 switch (position) {
86 case 'top':
87 wrapperEl.style.top = top;
88 wrapperAnimation.fromTo('opacity', 0.01, 1);
89 break;
90 case 'middle':
91 const topPosition = Math.floor(hostEl.clientHeight / 2 - wrapperEl.clientHeight / 2);
92 wrapperEl.style.top = `${topPosition}px`;
93 wrapperAnimation.fromTo('opacity', 0.01, 1);
94 break;
95 default:
96 wrapperEl.style.bottom = bottom;
97 wrapperAnimation.fromTo('opacity', 0.01, 1);
98 break;
99 }
100 return baseAnimation
101 .addElement(hostEl)
102 .easing('cubic-bezier(.36,.66,.04,1)')
103 .duration(400)
104 .addAnimation(wrapperAnimation);
105};
106
107/**
108 * md Toast Leave Animation
109 */
110const mdLeaveAnimation = (baseEl) => {
111 const baseAnimation = animation.createAnimation();
112 const wrapperAnimation = animation.createAnimation();
113 const hostEl = baseEl.host || baseEl;
114 const wrapperEl = baseEl.querySelector('.toast-wrapper');
115 wrapperAnimation
116 .addElement(wrapperEl)
117 .fromTo('opacity', 0.99, 0);
118 return baseAnimation
119 .addElement(hostEl)
120 .easing('cubic-bezier(.36,.66,.04,1)')
121 .duration(300)
122 .addAnimation(wrapperAnimation);
123};
124
125const toastIosCss = ":host{--border-width:0;--border-style:none;--border-color:initial;--box-shadow:none;--min-width:auto;--width:auto;--min-height:auto;--height:auto;--max-height:auto;--white-space:pre-wrap;left:0;top:0;display:block;position:absolute;width:100%;height:100%;outline:none;color:var(--color);font-family:var(--ion-font-family, inherit);contain:strict;z-index:1001;pointer-events:none}:host-context([dir=rtl]){left:unset;right:unset;right:0}:host(.overlay-hidden){display:none}:host(.ion-color){--button-color:inherit;color:var(--ion-color-contrast)}:host(.ion-color) .toast-button-cancel{color:inherit}:host(.ion-color) .toast-wrapper{background:var(--ion-color-base)}.toast-wrapper{border-radius:var(--border-radius);left:var(--start);right:var(--end);width:var(--width);min-width:var(--min-width);max-width:var(--max-width);height:var(--height);min-height:var(--min-height);max-height:var(--max-height);border-width:var(--border-width);border-style:var(--border-style);border-color:var(--border-color);background:var(--background);-webkit-box-shadow:var(--box-shadow);box-shadow:var(--box-shadow)}[dir=rtl] .toast-wrapper,:host-context([dir=rtl]) .toast-wrapper{left:unset;right:unset;left:var(--end);right:var(--start)}.toast-container{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;pointer-events:auto;height:inherit;min-height:inherit;max-height:inherit;contain:content}.toast-content{display:-ms-flexbox;display:flex;-ms-flex:1;flex:1;-ms-flex-direction:column;flex-direction:column;-ms-flex-pack:center;justify-content:center}.toast-message{-ms-flex:1;flex:1;white-space:var(--white-space)}.toast-button-group{display:-ms-flexbox;display:flex}.toast-button{border:0;outline:none;color:var(--button-color);z-index:0}.toast-icon{font-size:1.4em}.toast-button-inner{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}@media (any-hover: hover){.toast-button:hover{cursor:pointer}}:host{--background:var(--ion-color-step-50, #f2f2f2);--border-radius:14px;--button-color:var(--ion-color-primary, #3880ff);--color:var(--ion-color-step-850, #262626);--max-width:700px;--start:10px;--end:10px;font-size:14px}.toast-wrapper{margin-left:auto;margin-right:auto;margin-top:auto;margin-bottom:auto;display:block;position:absolute;z-index:10}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-wrapper{margin-left:unset;margin-right:unset;-webkit-margin-start:auto;margin-inline-start:auto;-webkit-margin-end:auto;margin-inline-end:auto}}@supports ((-webkit-backdrop-filter: blur(0)) or (backdrop-filter: blur(0))){:host(.toast-translucent) .toast-wrapper{background:rgba(var(--ion-background-color-rgb, 255, 255, 255), 0.8);-webkit-backdrop-filter:saturate(180%) blur(20px);backdrop-filter:saturate(180%) blur(20px)}}.toast-wrapper.toast-top{-webkit-transform:translate3d(0, -100%, 0);transform:translate3d(0, -100%, 0);top:0}.toast-wrapper.toast-middle{opacity:0.01}.toast-wrapper.toast-bottom{-webkit-transform:translate3d(0, 100%, 0);transform:translate3d(0, 100%, 0);bottom:0}.toast-content{padding-left:15px;padding-right:15px;padding-top:15px;padding-bottom:15px}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-content{padding-left:unset;padding-right:unset;-webkit-padding-start:15px;padding-inline-start:15px;-webkit-padding-end:15px;padding-inline-end:15px}}.toast-header{margin-bottom:2px;font-weight:500}.toast-button{padding-left:15px;padding-right:15px;padding-top:10px;padding-bottom:10px;height:44px;-webkit-transition:background-color, opacity 100ms linear;transition:background-color, opacity 100ms linear;border:0;background-color:transparent;font-family:var(--ion-font-family);font-size:17px;font-weight:500;overflow:hidden}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-button{padding-left:unset;padding-right:unset;-webkit-padding-start:15px;padding-inline-start:15px;-webkit-padding-end:15px;padding-inline-end:15px}}.toast-button.ion-activated{opacity:0.4}@media (any-hover: hover){.toast-button:hover{opacity:0.6}}";
126
127const toastMdCss = ":host{--border-width:0;--border-style:none;--border-color:initial;--box-shadow:none;--min-width:auto;--width:auto;--min-height:auto;--height:auto;--max-height:auto;--white-space:pre-wrap;left:0;top:0;display:block;position:absolute;width:100%;height:100%;outline:none;color:var(--color);font-family:var(--ion-font-family, inherit);contain:strict;z-index:1001;pointer-events:none}:host-context([dir=rtl]){left:unset;right:unset;right:0}:host(.overlay-hidden){display:none}:host(.ion-color){--button-color:inherit;color:var(--ion-color-contrast)}:host(.ion-color) .toast-button-cancel{color:inherit}:host(.ion-color) .toast-wrapper{background:var(--ion-color-base)}.toast-wrapper{border-radius:var(--border-radius);left:var(--start);right:var(--end);width:var(--width);min-width:var(--min-width);max-width:var(--max-width);height:var(--height);min-height:var(--min-height);max-height:var(--max-height);border-width:var(--border-width);border-style:var(--border-style);border-color:var(--border-color);background:var(--background);-webkit-box-shadow:var(--box-shadow);box-shadow:var(--box-shadow)}[dir=rtl] .toast-wrapper,:host-context([dir=rtl]) .toast-wrapper{left:unset;right:unset;left:var(--end);right:var(--start)}.toast-container{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;pointer-events:auto;height:inherit;min-height:inherit;max-height:inherit;contain:content}.toast-content{display:-ms-flexbox;display:flex;-ms-flex:1;flex:1;-ms-flex-direction:column;flex-direction:column;-ms-flex-pack:center;justify-content:center}.toast-message{-ms-flex:1;flex:1;white-space:var(--white-space)}.toast-button-group{display:-ms-flexbox;display:flex}.toast-button{border:0;outline:none;color:var(--button-color);z-index:0}.toast-icon{font-size:1.4em}.toast-button-inner{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}@media (any-hover: hover){.toast-button:hover{cursor:pointer}}:host{--background:var(--ion-color-step-800, #333333);--border-radius:4px;--box-shadow:0 3px 5px -1px rgba(0, 0, 0, 0.2), 0 6px 10px 0 rgba(0, 0, 0, 0.14), 0 1px 18px 0 rgba(0, 0, 0, 0.12);--button-color:var(--ion-color-primary, #3880ff);--color:var(--ion-color-step-50, #f2f2f2);--max-width:700px;--start:8px;--end:8px;font-size:14px}.toast-wrapper{margin-left:auto;margin-right:auto;margin-top:auto;margin-bottom:auto;display:block;position:absolute;opacity:0.01;z-index:10}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-wrapper{margin-left:unset;margin-right:unset;-webkit-margin-start:auto;margin-inline-start:auto;-webkit-margin-end:auto;margin-inline-end:auto}}.toast-content{padding-left:16px;padding-right:16px;padding-top:14px;padding-bottom:14px}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-content{padding-left:unset;padding-right:unset;-webkit-padding-start:16px;padding-inline-start:16px;-webkit-padding-end:16px;padding-inline-end:16px}}.toast-header{margin-bottom:2px;font-weight:500;line-height:20px}.toast-message{line-height:20px}.toast-button-group-start{margin-left:8px}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-button-group-start{margin-left:unset;-webkit-margin-start:8px;margin-inline-start:8px}}.toast-button-group-end{margin-right:8px}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-button-group-end{margin-right:unset;-webkit-margin-end:8px;margin-inline-end:8px}}.toast-button{padding-left:15px;padding-right:15px;padding-top:10px;padding-bottom:10px;position:relative;background-color:transparent;font-family:var(--ion-font-family);font-size:14px;font-weight:500;letter-spacing:0.84px;text-transform:uppercase;overflow:hidden}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-button{padding-left:unset;padding-right:unset;-webkit-padding-start:15px;padding-inline-start:15px;-webkit-padding-end:15px;padding-inline-end:15px}}.toast-button-cancel{color:var(--ion-color-step-100, #e6e6e6)}.toast-button-icon-only{border-radius:50%;padding-left:9px;padding-right:9px;padding-top:9px;padding-bottom:9px;width:36px;height:36px}@supports ((-webkit-margin-start: 0) or (margin-inline-start: 0)) or (-webkit-margin-start: 0){.toast-button-icon-only{padding-left:unset;padding-right:unset;-webkit-padding-start:9px;padding-inline-start:9px;-webkit-padding-end:9px;padding-inline-end:9px}}@media (any-hover: hover){.toast-button:hover{background-color:rgba(var(--ion-color-primary-rgb, 56, 128, 255), 0.08)}.toast-button-cancel:hover{background-color:rgba(var(--ion-background-color-rgb, 255, 255, 255), 0.08)}}";
128
129const Toast = class {
130 constructor(hostRef) {
131 index.registerInstance(this, hostRef);
132 this.didPresent = index.createEvent(this, "ionToastDidPresent", 7);
133 this.willPresent = index.createEvent(this, "ionToastWillPresent", 7);
134 this.willDismiss = index.createEvent(this, "ionToastWillDismiss", 7);
135 this.didDismiss = index.createEvent(this, "ionToastDidDismiss", 7);
136 this.presented = false;
137 /**
138 * How many milliseconds to wait before hiding the toast. By default, it will show
139 * until `dismiss()` is called.
140 */
141 this.duration = 0;
142 /**
143 * If `true`, the keyboard will be automatically dismissed when the overlay is presented.
144 */
145 this.keyboardClose = false;
146 /**
147 * The position of the toast on the screen.
148 */
149 this.position = 'bottom';
150 /**
151 * If `true`, the toast will be translucent.
152 * Only applies when the mode is `"ios"` and the device supports
153 * [`backdrop-filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter#Browser_compatibility).
154 */
155 this.translucent = false;
156 /**
157 * If `true`, the toast will animate.
158 */
159 this.animated = true;
160 this.dispatchCancelHandler = (ev) => {
161 const role = ev.detail.role;
162 if (overlays.isCancel(role)) {
163 const cancelButton = this.getButtons().find(b => b.role === 'cancel');
164 this.callButtonHandler(cancelButton);
165 }
166 };
167 }
168 connectedCallback() {
169 overlays.prepareOverlay(this.el);
170 }
171 /**
172 * Present the toast overlay after it has been created.
173 */
174 async present() {
175 await overlays.present(this, 'toastEnter', iosEnterAnimation, mdEnterAnimation, this.position);
176 if (this.duration > 0) {
177 this.durationTimeout = setTimeout(() => this.dismiss(undefined, 'timeout'), this.duration);
178 }
179 }
180 /**
181 * Dismiss the toast overlay after it has been presented.
182 *
183 * @param data Any data to emit in the dismiss events.
184 * @param role The role of the element that is dismissing the toast.
185 * This can be useful in a button handler for determining which button was
186 * clicked to dismiss the toast.
187 * Some examples include: ``"cancel"`, `"destructive"`, "selected"`, and `"backdrop"`.
188 */
189 dismiss(data, role) {
190 if (this.durationTimeout) {
191 clearTimeout(this.durationTimeout);
192 }
193 return overlays.dismiss(this, data, role, 'toastLeave', iosLeaveAnimation, mdLeaveAnimation, this.position);
194 }
195 /**
196 * Returns a promise that resolves when the toast did dismiss.
197 */
198 onDidDismiss() {
199 return overlays.eventMethod(this.el, 'ionToastDidDismiss');
200 }
201 /**
202 * Returns a promise that resolves when the toast will dismiss.
203 */
204 onWillDismiss() {
205 return overlays.eventMethod(this.el, 'ionToastWillDismiss');
206 }
207 getButtons() {
208 const buttons = this.buttons
209 ? this.buttons.map(b => {
210 return (typeof b === 'string')
211 ? { text: b }
212 : b;
213 })
214 : [];
215 return buttons;
216 }
217 async buttonClick(button) {
218 const role = button.role;
219 if (overlays.isCancel(role)) {
220 return this.dismiss(undefined, role);
221 }
222 const shouldDismiss = await this.callButtonHandler(button);
223 if (shouldDismiss) {
224 return this.dismiss(undefined, role);
225 }
226 return Promise.resolve();
227 }
228 async callButtonHandler(button) {
229 if (button && button.handler) {
230 // a handler has been provided, execute it
231 // pass the handler the values from the inputs
232 try {
233 const rtn = await overlays.safeCall(button.handler);
234 if (rtn === false) {
235 // if the return value of the handler is false then do not dismiss
236 return false;
237 }
238 }
239 catch (e) {
240 console.error(e);
241 }
242 }
243 return true;
244 }
245 renderButtons(buttons, side) {
246 if (buttons.length === 0) {
247 return;
248 }
249 const mode = ionicGlobal.getIonMode(this);
250 const buttonGroupsClasses = {
251 'toast-button-group': true,
252 [`toast-button-group-${side}`]: true
253 };
254 return (index.h("div", { class: buttonGroupsClasses }, buttons.map(b => index.h("button", { type: "button", class: buttonClass(b), tabIndex: 0, onClick: () => this.buttonClick(b), part: "button" }, index.h("div", { class: "toast-button-inner" }, b.icon &&
255 index.h("ion-icon", { icon: b.icon, slot: b.text === undefined ? 'icon-only' : undefined, class: "toast-icon" }), b.text), mode === 'md' && index.h("ion-ripple-effect", { type: b.icon !== undefined && b.text === undefined ? 'unbounded' : 'bounded' })))));
256 }
257 render() {
258 const allButtons = this.getButtons();
259 const startButtons = allButtons.filter(b => b.side === 'start');
260 const endButtons = allButtons.filter(b => b.side !== 'start');
261 const mode = ionicGlobal.getIonMode(this);
262 const wrapperClass = {
263 'toast-wrapper': true,
264 [`toast-${this.position}`]: true
265 };
266 return (index.h(index.Host, { style: {
267 zIndex: `${60000 + this.overlayIndex}`,
268 }, class: theme.createColorClasses(this.color, Object.assign(Object.assign({ [mode]: true }, theme.getClassMap(this.cssClass)), { 'toast-translucent': this.translucent })), tabindex: "-1", onIonToastWillDismiss: this.dispatchCancelHandler }, index.h("div", { class: wrapperClass }, index.h("div", { class: "toast-container", part: "container" }, this.renderButtons(startButtons, 'start'), index.h("div", { class: "toast-content" }, this.header !== undefined &&
269 index.h("div", { class: "toast-header", part: "header" }, this.header), this.message !== undefined &&
270 index.h("div", { class: "toast-message", part: "message", innerHTML: index$1.sanitizeDOMString(this.message) })), this.renderButtons(endButtons, 'end')))));
271 }
272 get el() { return index.getElement(this); }
273};
274const buttonClass = (button) => {
275 return Object.assign({ 'toast-button': true, 'toast-button-icon-only': button.icon !== undefined && button.text === undefined, [`toast-button-${button.role}`]: button.role !== undefined, 'ion-focusable': true, 'ion-activatable': true }, theme.getClassMap(button.cssClass));
276};
277Toast.style = {
278 ios: toastIosCss,
279 md: toastMdCss
280};
281
282exports.ion_toast = Toast;