|
| 1 | +import Integer from "@ui5/webcomponents-base/dist/types/Integer.js"; |
| 2 | +import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js"; |
| 3 | +import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js"; |
| 4 | +import { getRTL } from "@ui5/webcomponents-base/dist/config/RTL.js"; |
| 5 | +import ToastTemplate from "./generated/templates/ToastTemplate.lit.js"; |
| 6 | +import ToastPlacement from "./types/ToastPlacement.js"; |
| 7 | + |
| 8 | +// Styles |
| 9 | +import ToastCss from "./generated/themes/Toast.css.js"; |
| 10 | + |
| 11 | +// Static Constants |
| 12 | +const MAXIMUM_ALLOWED_TRANSITION_DURATION_IN_MILLISECONDS = 1000; |
| 13 | +const MINIMUM_ALLOWED_DURATION_IN_MILLISECONDS = 500; |
| 14 | + |
| 15 | +/** |
| 16 | + * @public |
| 17 | + */ |
| 18 | +const metadata = { |
| 19 | + tag: "ui5-toast", |
| 20 | + properties: /** @lends sap.ui.webcomponents.main.Toast.prototype */ { |
| 21 | + |
| 22 | + /** |
| 23 | + * Defines the duration in milliseconds for which <code>ui5-toast</code> |
| 24 | + * remains on the screen before it's automatically closed. |
| 25 | + * <br> |
| 26 | + * <b>Note:</b> The minimum supported value is <code>500</code>. |
| 27 | + * If a lower value is passed, it's forced to be <code>500</code>. |
| 28 | + * |
| 29 | + * @type {Integer} |
| 30 | + * @defaultvalue 3000 |
| 31 | + * @public |
| 32 | + */ |
| 33 | + duration: { |
| 34 | + type: Integer, |
| 35 | + defaultValue: 3000, |
| 36 | + }, |
| 37 | + |
| 38 | + /** |
| 39 | + * Defines the placement of the <code>ui5-toast</code> web component. |
| 40 | + * <br><br> |
| 41 | + * Available options are: |
| 42 | + * <ul> |
| 43 | + * <li><code>TopStart</code></li> |
| 44 | + * <li><code>TopCenter</code></li> |
| 45 | + * <li><code>TopEnd</code></li> |
| 46 | + * <li><code>MiddleStart</code></li> |
| 47 | + * <li><code>MiddleCenter</code></li> |
| 48 | + * <li><code>MiddleEnd</code></li> |
| 49 | + * <li><code>BottomStart</code></li> |
| 50 | + * <li><code>BottomCenter</code></li> |
| 51 | + * <li><code>BottomEnd</code></li> |
| 52 | + * <ul> |
| 53 | + * |
| 54 | + * @type {string} |
| 55 | + * @defaultvalue "BottomCenter" |
| 56 | + * @public |
| 57 | + */ |
| 58 | + placement: { |
| 59 | + type: ToastPlacement, |
| 60 | + defaultValue: ToastPlacement.BottomCenter, |
| 61 | + }, |
| 62 | + |
| 63 | + /** |
| 64 | + * Indicates whether <code>ui5-toast</code> is open (visible). |
| 65 | + * @type {boolean} |
| 66 | + * @private |
| 67 | + */ |
| 68 | + open: { |
| 69 | + type: Boolean, |
| 70 | + }, |
| 71 | + }, |
| 72 | + slots: /** @lends sap.ui.webcomponents.main.Toast.prototype */ { |
| 73 | + /** |
| 74 | + * Defines the text of the <code>ui5-toast</code> web component. |
| 75 | + * <br><b>Note:</b> Аlthough this slot accepts HTML Elements, it is strongly recommended that you only use text in order to preserve the intended design. |
| 76 | + * |
| 77 | + * @type {Node[]} |
| 78 | + * @slot |
| 79 | + * @public |
| 80 | + */ |
| 81 | + "default": { |
| 82 | + type: Node, |
| 83 | + }, |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * @class |
| 89 | + * |
| 90 | + * <h3 class="comment-api-title">Overview</h3> |
| 91 | + * |
| 92 | + * The <code>ui5-toast</code> is a small, non-disruptive popup for success or information messages that |
| 93 | + * disappears automatically after a few seconds. |
| 94 | + * |
| 95 | + * |
| 96 | + * <h3>Usage</h3> |
| 97 | + * |
| 98 | + * <h4>When to use:</h4> |
| 99 | + * <ul> |
| 100 | + * <li>You want to display a short success or information message.</li> |
| 101 | + * <li>You do not want to interrupt users while they are performing an action.</li> |
| 102 | + * <li>You want to confirm a successful action.</li> |
| 103 | + * </ul> |
| 104 | + * <h4>When not to use:</h4> |
| 105 | + * <ul> |
| 106 | + * <li>You want to display error or warning message.</li> |
| 107 | + * <li>You want to interrupt users while they are performing an action.</li> |
| 108 | + * <li>You want to make sure that users read the message before they leave the page.</li> |
| 109 | + * <li>You want users to be able to copy some part of the message text.</li> |
| 110 | + * </ul> |
| 111 | + * |
| 112 | + * <h3>ES6 Module Import</h3> |
| 113 | + * |
| 114 | + * <code>import "@ui5/webcomponents/dist/Toast";</code> |
| 115 | + * |
| 116 | + * @constructor |
| 117 | + * @author SAP SE |
| 118 | + * @alias sap.ui.webcomponents.main.Toast |
| 119 | + * @extends UI5Element |
| 120 | + * @tagname ui5-toast |
| 121 | + * @public |
| 122 | + * @since 1.0.0-rc.6 |
| 123 | + */ |
| 124 | +class Toast extends UI5Element { |
| 125 | + static get metadata() { |
| 126 | + return metadata; |
| 127 | + } |
| 128 | + |
| 129 | + static get render() { |
| 130 | + return litRender; |
| 131 | + } |
| 132 | + |
| 133 | + static get styles() { |
| 134 | + return ToastCss; |
| 135 | + } |
| 136 | + |
| 137 | + static get template() { |
| 138 | + return ToastTemplate; |
| 139 | + } |
| 140 | + |
| 141 | + static get maximumAllowedTransition() { |
| 142 | + return MAXIMUM_ALLOWED_TRANSITION_DURATION_IN_MILLISECONDS; |
| 143 | + } |
| 144 | + |
| 145 | + static get minimumAllowedDuration() { |
| 146 | + return MINIMUM_ALLOWED_DURATION_IN_MILLISECONDS; |
| 147 | + } |
| 148 | + |
| 149 | + onBeforeRendering() { |
| 150 | + // If the minimum duration is lower than 500ms, we force |
| 151 | + // it to be 500ms, as described in the documentation. |
| 152 | + if (this.duration < Toast.minimumAllowedDuration) { |
| 153 | + this.duration = Toast.minimumAllowedDuration; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + onAfterRendering() { |
| 158 | + if (this._reopen) { |
| 159 | + this._reopen = false; |
| 160 | + this._initiateOpening(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Shows the <code>ui5-toast</code>. |
| 166 | + * @public |
| 167 | + */ |
| 168 | + show() { |
| 169 | + if (this.open) { |
| 170 | + // If the Toast is already opened, we set the _reopen flag to true, in |
| 171 | + // order to trigger re-rendering after an animation frame |
| 172 | + // in the onAfterRendering hook. |
| 173 | + // This is needed for properly resetting the opacity transition. |
| 174 | + this._reopen = true; |
| 175 | + this.open = false; |
| 176 | + } else { |
| 177 | + this._initiateOpening(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + get rtl() { |
| 182 | + return getRTL() ? "rtl" : undefined; |
| 183 | + } |
| 184 | + |
| 185 | + get styles() { |
| 186 | + // Transition duration (animation) should be a third of the duration |
| 187 | + // property, but not bigger than the maximum allowed (1000ms). |
| 188 | + const transitionDuration = Math.min(this.duration / 3, Toast.maximumAllowedTransition); |
| 189 | + |
| 190 | + return { |
| 191 | + root: { |
| 192 | + "transition-duration": this.open ? `${transitionDuration}ms` : "", |
| 193 | + |
| 194 | + // Transition delay is the duration property minus the |
| 195 | + // transition duration (animation). |
| 196 | + "transition-delay": this.open ? `${this.duration - transitionDuration}ms` : "", |
| 197 | + |
| 198 | + // We alter the opacity property, in order to trigger transition |
| 199 | + "opacity": this.open ? "0" : "", |
| 200 | + }, |
| 201 | + }; |
| 202 | + } |
| 203 | + |
| 204 | + _initiateOpening() { |
| 205 | + requestAnimationFrame(_ => { |
| 206 | + this.open = true; |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + _ontransitionend() { |
| 211 | + this.open = false; |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +Toast.define(); |
| 216 | + |
| 217 | +export default Toast; |
0 commit comments