UNPKG

@ionic/core

Version:
161 lines (160 loc) 5.12 kB
import { dismiss, eventMethod, present } from '../../utils/overlays'; import { createColorClasses, getClassMap } from '../../utils/theme'; import { iosEnterAnimation } from './animations/ios.enter'; import { iosLeaveAnimation } from './animations/ios.leave'; import { mdEnterAnimation } from './animations/md.enter'; import { mdLeaveAnimation } from './animations/md.leave'; export class Toast { constructor() { this.presented = false; this.duration = 0; this.keyboardClose = false; this.position = 'bottom'; this.showCloseButton = false; this.translucent = false; this.animated = true; } async present() { await present(this, 'toastEnter', iosEnterAnimation, mdEnterAnimation, this.position); if (this.duration > 0) { this.durationTimeout = setTimeout(() => this.dismiss(undefined, 'timeout'), this.duration); } } dismiss(data, role) { if (this.durationTimeout) { clearTimeout(this.durationTimeout); } return dismiss(this, data, role, 'toastLeave', iosLeaveAnimation, mdLeaveAnimation, this.position); } onDidDismiss() { return eventMethod(this.el, 'ionToastDidDismiss'); } onWillDismiss() { return eventMethod(this.el, 'ionToastWillDismiss'); } hostData() { return { style: { zIndex: 60000 + this.overlayIndex, }, class: Object.assign({}, createColorClasses(this.color), getClassMap(this.cssClass), { 'toast-translucent': this.translucent }) }; } render() { const wrapperClass = { 'toast-wrapper': true, [`toast-${this.position}`]: true }; return (h("div", { class: wrapperClass }, h("div", { class: "toast-container" }, this.message !== undefined && h("div", { class: "toast-message" }, this.message), this.showCloseButton && h("ion-button", { fill: "clear", class: "toast-button", onClick: () => this.dismiss(undefined, 'cancel') }, this.closeButtonText || 'Close')))); } static get is() { return "ion-toast"; } static get encapsulation() { return "shadow"; } static get properties() { return { "animated": { "type": Boolean, "attr": "animated" }, "closeButtonText": { "type": String, "attr": "close-button-text" }, "color": { "type": String, "attr": "color" }, "config": { "context": "config" }, "cssClass": { "type": String, "attr": "css-class" }, "dismiss": { "method": true }, "duration": { "type": Number, "attr": "duration" }, "el": { "elementRef": true }, "enterAnimation": { "type": "Any", "attr": "enter-animation" }, "keyboardClose": { "type": Boolean, "attr": "keyboard-close" }, "leaveAnimation": { "type": "Any", "attr": "leave-animation" }, "message": { "type": String, "attr": "message" }, "mode": { "type": String, "attr": "mode" }, "onDidDismiss": { "method": true }, "onWillDismiss": { "method": true }, "overlayIndex": { "type": Number, "attr": "overlay-index" }, "position": { "type": String, "attr": "position" }, "present": { "method": true }, "showCloseButton": { "type": Boolean, "attr": "show-close-button" }, "translucent": { "type": Boolean, "attr": "translucent" } }; } static get events() { return [{ "name": "ionToastDidPresent", "method": "didPresent", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionToastWillPresent", "method": "willPresent", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionToastWillDismiss", "method": "willDismiss", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionToastDidDismiss", "method": "didDismiss", "bubbles": true, "cancelable": true, "composed": true }]; } static get style() { return "/**style-placeholder:ion-toast:**/"; } static get styleMode() { return "/**style-id-placeholder:ion-toast:**/"; } }