Skip to content

Commit 48400cd

Browse files
plamenivanov91ilhan007
authored andcommitted
feat(ui5-toast): introduce new component (#1014)
New ui5-toast component is introduced. Sample changes: More samples are added in the playground, which demonstrate default Toast, as well as some short/long duration options for the component. Also the script for the handlers in now simplified/shortened in both playground and test pages. Style changes: Added "--_ui5_toast_background" and "--_ui5_toast_shadow" CSS parameters. Font-family, color, and font-size of the "ui5-toast-text" are now on the host. These changes give the web-component user, the freedom to alter the styling of the Toast. CSS class name changes: The "ui5-toast-root-parent" class is now renamed to "ui5-toast-root". The "ui5-toast-root" class is now called "ui5-toast-content". Animation changes: The transition (fade out animation) is now handled differently. It is still a third of the Toast's duration property, but if it happens to be more than 1000ms, we force it to be 1000ms. This way we omit prolonged fade out times, when the duration property is longer, than 3000ms (default). Tests changes: Spec files are now aligned with the new transition behaviour. Also some new tests are now added in order to cover mose cases. Now if the user inputs duration lower than 500ms, we force it to be 500ms. Additionally the following changes are made: * CSS variables are now aligned with the new convention. * Toast's text styling is now part of the ui5-toast-content class. * A span which is the container of the Toast's text is now removed from the hbs. * One of the Toasts in the html sample is now with invalid duration "-1". This is in order to test if the minimum duration is applied correctly. * Added test in the spec file, which tests the new minimum duration behaviour. * The sample.html is reduced in volume.
1 parent c22a660 commit 48400cd

File tree

13 files changed

+783
-0
lines changed

13 files changed

+783
-0
lines changed

packages/main/bundle.esm.js

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import TextArea from "./dist/TextArea.js";
4747
import Timeline from "./dist/Timeline.js";
4848
import TimelineItem from "./dist/TimelineItem.js";
4949
import Title from "./dist/Title.js";
50+
import Toast from "./dist/Toast.js";
5051
import ToggleButton from "./dist/ToggleButton.js";
5152

5253
import List from "./dist/List.js";

packages/main/src/Toast.hbs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="ui5-toast-root"
2+
dir="{{rtl}}">
3+
<div class="ui5-toast-content"
4+
role="alert"
5+
style="{{styles.root}}"
6+
@transitionend="{{_ontransitionend}}">
7+
<bdi>
8+
<slot></slot>
9+
</bdi>
10+
</div>
11+
</div>

packages/main/src/Toast.js

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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;

packages/main/src/themes/Toast.css

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
:host {
2+
font-family: var(--sapFontFamily);
3+
color: var(--sapTextColor);
4+
font-size: var(--sapFontMediumSize);
5+
}
6+
7+
.ui5-toast-root {
8+
position: fixed;
9+
left: 0;
10+
right: 0;
11+
top: 0;
12+
bottom: 0;
13+
width: 100%;
14+
display: none;
15+
height: 100%;
16+
pointer-events: none;
17+
overflow: hidden;
18+
z-index: 999;
19+
justify-content: center;
20+
align-items: flex-end;
21+
}
22+
23+
.ui5-toast-content {
24+
box-sizing: border-box;
25+
max-width: 15rem;
26+
overflow: hidden;
27+
padding: 1rem;
28+
background: var(--_ui5_toast_background);
29+
box-shadow: var(--_ui5_toast_shadow);
30+
border-radius: .25rem;
31+
transition-property: opacity;
32+
opacity: 1;
33+
margin: 2rem;
34+
35+
/* Text styling */
36+
font-family: inherit;
37+
color: inherit;
38+
font-weight: inherit;
39+
font-size: inherit;
40+
word-wrap: break-word;
41+
text-align: center;
42+
text-overflow: ellipsis;
43+
white-space: pre-line;
44+
}
45+
46+
:host([open]) .ui5-toast-root {
47+
display: flex;
48+
}
49+
50+
:host([placement="TopStart"]) .ui5-toast-root {
51+
justify-content: flex-start;
52+
align-items: flex-start;
53+
}
54+
55+
:host([placement="TopCenter"]) .ui5-toast-root {
56+
justify-content: center;
57+
align-items: flex-start;
58+
}
59+
60+
:host([placement="TopEnd"]) .ui5-toast-root {
61+
justify-content: flex-end;
62+
align-items: flex-start;
63+
}
64+
65+
:host([placement="MiddleStart"]) .ui5-toast-root {
66+
justify-content: flex-start;
67+
align-items: center;
68+
}
69+
70+
:host([placement="MiddleCenter"]) .ui5-toast-root {
71+
justify-content: center;
72+
align-items: center;
73+
}
74+
75+
:host([placement="MiddleEnd"]) .ui5-toast-root {
76+
justify-content: flex-end;
77+
align-items: center;
78+
}
79+
80+
:host([placement="BottomStart"]) .ui5-toast-root {
81+
justify-content: flex-start;
82+
align-items: flex-end;
83+
}
84+
85+
:host([placement="BottomEnd"]) .ui5-toast-root {
86+
justify-content: flex-end;
87+
align-items: flex-end;
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:root {
2+
--_ui5_toast_background: var(--sapList_Background);
3+
--_ui5_toast_shadow: var(--sapContent_Shadow2);
4+
}

packages/main/src/themes/sap_belize/parameters-bundle.css

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@import "../base/TextArea-parameters.css";
2626
@import "../base/TimelineItem-parameters.css";
2727
@import "../base/Title-parameters.css";
28+
@import "../base/Toast-parameters.css";
2829
@import "../base/ToggleButton-parameters.css";
2930
@import "../base/YearPicker-parameters.css";
3031
@import "../base/Token-parameters.css";

packages/main/src/themes/sap_belize_hcb/parameters-bundle.css

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@import "./TextArea-parameters.css";
2626
@import "./TimelineItem-parameters.css";
2727
@import "../base/Title-parameters.css";
28+
@import "../base/Toast-parameters.css";
2829
@import "./ToggleButton-parameters.css";
2930
@import "./YearPicker-parameters.css";
3031
@import "./Token-parameters.css";

packages/main/src/themes/sap_fiori_3/parameters-bundle.css

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
@import "./TabContainer-parameters.css";
2222
@import "../base/TextArea-parameters.css";
2323
@import "../base/TimelineItem-parameters.css";
24+
@import "../base/Toast-parameters.css";
2425
@import "./ToggleButton-parameters.css";
2526
@import "./YearPicker-parameters.css";
2627
@import "./CalendarHeader-parameters.css";

packages/main/src/themes/sap_fiori_3_dark/parameters-bundle.css

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
@import "./TabContainer-parameters.css";
2222
@import "../base/TextArea-parameters.css";
2323
@import "../base/TimelineItem-parameters.css";
24+
@import "../base/Toast-parameters.css";
2425
@import "./ToggleButton-parameters.css";
2526
@import "./YearPicker-parameters.css";
2627
@import "./CalendarHeader-parameters.css";

0 commit comments

Comments
 (0)