Skip to content

Commit

Permalink
refactor(ui5-toast): replace show method with open property (#8855)
Browse files Browse the repository at this point in the history
Users now can open `ui5-toast` using the `open` property. A new event `after-close` is introduced.
It may be used to sync app state with the `open` state of the `ui5-toast`.

BREAKING CHANGE: The Toast#show method has been replaced by  `open` property. If you previously used  `toast.show()` to show the toast, you must now se `toast.open=true`.

Related to: #8461
  • Loading branch information
MapTo0 committed Apr 26, 2024
1 parent 3b86d79 commit 372d27d
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 57 deletions.
12 changes: 6 additions & 6 deletions packages/fiori/test/pages/NotificationListGroupItem.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,28 +264,28 @@
<script>
notificationList.addEventListener("item-click", function(event) {
wcToastBS.textContent = event.detail.item.titleText;
wcToastBS.show();
wcToastBS.open = true;
});

notificationList.addEventListener("item-close", function(event) {
wcToastBS.textContent = event.detail.item.titleText;
wcToastBS.show();
wcToastBS.open = true;
});

notificationList.addEventListener("item-toggle", function(event) {
var item = event.detail.item;
wcToastBS.textContent = item.titleText + " has been " + (item.collapsed ? "collapsed" : "expanded");
wcToastBS.show();
wcToastBS.open = true;
});

acceptBtnInPopover.addEventListener("ui5-click", function(event) {
wcToastBS.textContent = "Accept btn In popover btn clicked";
wcToastBS.show();
wcToastBS.open = true;
});

rejectBtnInPopover.addEventListener("ui5-click", function(event) {
wcToastBS.textContent = "Reject btn In popover btn clicked";
wcToastBS.show();
wcToastBS.open = true;
});

openNotifications.addEventListener("click", function(event) {
Expand All @@ -307,7 +307,7 @@

notificationListTop.addEventListener("ui5-item-close", function (event) {
wcToastBS.textContent = event.detail.item.titleText;
wcToastBS.show();
wcToastBS.open = true;
});

btnCompact.addEventListener("click", function() {
Expand Down
16 changes: 8 additions & 8 deletions packages/fiori/test/pages/NotificationListItem.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@
<script>
notificationList.addEventListener("ui5-item-click", function(event) {
wcToastBS.textContent = event.detail.item.titleText;
wcToastBS.show();
wcToastBS.open = true;
});

notificationList.addEventListener("ui5-item-close", function(event) {
wcToastBS.textContent = event.detail.item.titleText;
wcToastBS.show();
wcToastBS.open = true;
});

notificationList2.addEventListener("ui5-item-click", function(event) {
Expand All @@ -234,32 +234,32 @@

item.busy = true;
wcToastBS.textContent = item.titleText;
wcToastBS.show();
wcToastBS.open = true;
});

notificationList2.addEventListener("ui5-item-close", function(event) {
wcToastBS.textContent = event.detail.item.titleText;
wcToastBS.show();
wcToastBS.open = true;
});

acceptBtn.addEventListener("ui5-click", function(event) {
wcToastBS.textContent = "Accept btn clicked";
wcToastBS.show();
wcToastBS.open = true;
});

acceptBtnInPopover.addEventListener("ui5-click", function(event) {
wcToastBS.textContent = "Accept btn In popover btn clicked";
wcToastBS.show();
wcToastBS.open = true;
});

rejectBtn.addEventListener("ui5-click", function(event) {
wcToastBS.textContent = "Reject btn clicked";
wcToastBS.show();
wcToastBS.open = true;
});

rejectBtnInPopover.addEventListener("ui5-click", function(event) {
wcToastBS.textContent = "Reject btn In popover btn clicked";
wcToastBS.show();
wcToastBS.open = true;
});

openNotifications.addEventListener("click", function(event) {
Expand Down
2 changes: 1 addition & 1 deletion packages/fiori/test/pages/ShellBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ <h3>ShellBar in Compact</h3>

Array.from(document.querySelectorAll("ui5-shellbar-item")).forEach(function(item) {
item.addEventListener("ui5-click", function(event) {
wcToastTC.show();
wcToastTC.open = true;
});
});

Expand Down
83 changes: 49 additions & 34 deletions packages/main/src/Toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isEscape } from "@ui5/webcomponents-base/dist/Keys.js";
import { isMac } from "@ui5/webcomponents-base/dist/Device.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import event from "@ui5/webcomponents-base/dist/decorators/event.js";
import ToastPlacement from "./types/ToastPlacement.js";

// Template
Expand Down Expand Up @@ -84,6 +85,14 @@ const handleGlobalKeydown = (e: KeyboardEvent) => {
styles: ToastCss,
template: ToastTemplate,
})

/**
* Fired after the component is auto closed.
* @public
* @since 2.0.0
*/
@event("after-close")

class Toast extends UI5Element {
/**
* Defines the duration in milliseconds for which component
Expand All @@ -107,7 +116,9 @@ class Toast extends UI5Element {

/**
* Indicates whether the component is open (visible).
* @private
* @default false
* @public
* @since 2.0.0
*/
@property({ type: Boolean })
open!: boolean;
Expand Down Expand Up @@ -142,22 +153,29 @@ class Toast extends UI5Element {
@property({ type: Boolean })
focused!: boolean;

_reopen: boolean;
_onfocusinFn: () => void;
_onfocusoutFn: () => void;
_onkeydownFn: (e: KeyboardEvent) => void;
_onmouseoverFn: () => void;
_onmouseleaveFn: () => void;
_ontransitionendFn: () => void;

constructor() {
super();

this._reopen = false;

this.addEventListener("focusin", this._onfocusin.bind(this));
this.addEventListener("focusout", this._onfocusout.bind(this));
this.addEventListener("keydown", this._onkeydown.bind(this));
this.addEventListener("mouseover", this._onmouseover.bind(this));
this.addEventListener("mouseleave", this._onmouseleave.bind(this));
this.addEventListener("transitionend", this._ontransitionend.bind(this));
this._onfocusinFn = this._onfocusin.bind(this);
this._onfocusoutFn = this._onfocusout.bind(this);
this._onkeydownFn = this._onkeydown.bind(this);
this._onmouseoverFn = this._onmouseover.bind(this);
this._onmouseleaveFn = this._onmouseleave.bind(this);
this._ontransitionendFn = this._ontransitionend.bind(this);
}

onBeforeRendering() {
if (this.open) {
this._initiateOpening();
}

// Transition duration (animation) should be a third of the duration
// property, but not bigger than the maximum allowed (1000ms).
const transitionDuration = Math.min(this.effectiveDuration / 3, MAX_DURATION);
Expand All @@ -172,30 +190,6 @@ class Toast extends UI5Element {
}
}

onAfterRendering() {
if (this._reopen) {
this._reopen = false;
this._initiateOpening();
}
}

/**
* Shows the component.
* @public
*/
show(): void {
if (this.open) {
// If the Toast is already opened, we set the _reopen flag to true, in
// order to trigger re-rendering after an animation frame
// in the onAfterRendering hook.
// This is needed for properly resetting the opacity transition.
this._reopen = true;
this.open = false;
} else {
this._initiateOpening();
}
}

_onfocusin() {
if (this.focusable) {
this.focused = true;
Expand Down Expand Up @@ -232,6 +226,7 @@ class Toast extends UI5Element {
this.open = false;
this.focusable = false;
this.focused = false;
this.fireEvent("after-close");
}

_onmouseover() {
Expand All @@ -252,6 +247,26 @@ class Toast extends UI5Element {
get _tabindex() {
return this.focused ? "0" : "-1";
}

onEnterDOM(): void {
this.addEventListener("focusin", this._onfocusinFn);
this.addEventListener("focusout", this._onfocusoutFn);
this.addEventListener("keydown", this._onkeydownFn);
this.addEventListener("mouseover", this._onmouseoverFn);
this.addEventListener("mouseleave", this._onmouseleaveFn);
this.addEventListener("transitionend", this._ontransitionendFn);
this.addEventListener("transitioncancel", this._ontransitionendFn);
}

onExitDOM(): void {
this.removeEventListener("focusin", this._onfocusinFn);
this.removeEventListener("focusout", this._onfocusoutFn);
this.removeEventListener("keydown", this._onkeydownFn);
this.removeEventListener("mouseover", this._onmouseoverFn);
this.removeEventListener("mouseleave", this._onmouseleaveFn);
this.removeEventListener("transitionend", this._ontransitionendFn);
this.removeEventListener("transitioncancel", this._ontransitionendFn);
}
}

Toast.define();
Expand Down
2 changes: 1 addition & 1 deletion packages/main/test/pages/Popover.html
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ <h2>Horizontal Align</h2>
var toast = document.createElement("ui5-toast");
toast.appendChild(document.createTextNode("Closed chained popover 2"));
document.body.appendChild(toast);
toast.show();
toast.open = true;
});

popoverFocusButton.addEventListener("click", function () {
Expand Down
8 changes: 4 additions & 4 deletions packages/main/test/pages/Toast.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ <h3>Test Dialog and Toast</h3>
// Attaching click listeners to the buttons which show the toasts
document.querySelectorAll("ui5-button").forEach(function(button){
button.addEventListener('click', function () {
document.querySelector("#" + button.id.replace("BtnShow", "")).show();
document.querySelector("#" + button.id.replace("BtnShow", "")).open = true;
});
});

btnDialog.addEventListener('click', function () {
dialog.show();
dialog.open = true;
});

btnDialog2.addEventListener('click', function () {
dialog.close();
dialog.open = false;
});

btnToastInDialog.addEventListener('click', function () {
toastInDialog.show();
toastInDialog.open = true;
});

</script>
Expand Down
2 changes: 1 addition & 1 deletion packages/website/docs/_samples/main/Toast/Basic/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const btn = [...document.getElementsByTagName("ui5-button")][0];
const toast = [...document.getElementsByTagName("ui5-toast")][0];

btn.addEventListener("click", () => {
toast.show();
toast.open = true;
});
2 changes: 1 addition & 1 deletion packages/website/docs/_samples/main/Toast/Duration/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const btn = [...document.getElementsByTagName("ui5-button")][0];
const toast = [...document.getElementsByTagName("ui5-toast")][0];

btn.addEventListener("click", () => {
toast.show();
toast.open = true;
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const btn = [...document.getElementsByTagName("ui5-button")][0];
const toast = [...document.getElementsByTagName("ui5-toast")][0];

btn.addEventListener("click", () => {
toast.show();
toast.open = true;
});

0 comments on commit 372d27d

Please sign in to comment.