Skip to content

Commit 0c32950

Browse files
authored
feat(ui5-tabcontainer): add expand/collapse animation (#1617)
The TabContainer now toggles with animation. Related to: #1540
1 parent 2785daf commit 0c32950

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

packages/main/src/TabContainer.js

+74-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
44
import ScrollEnablement from "@ui5/webcomponents-base/dist/delegate/ScrollEnablement.js";
5+
import slideDown from "@ui5/webcomponents-base/dist/animations/slideDown.js";
6+
import slideUp from "@ui5/webcomponents-base/dist/animations/slideUp.js";
7+
import AnimationMode from "@ui5/webcomponents-base/dist/types/AnimationMode.js";
8+
import { getAnimationMode } from "@ui5/webcomponents-base/dist/config/AnimationMode.js";
59
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
610
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
711
import { getRTL } from "@ui5/webcomponents-base/dist/config/RTL.js";
@@ -151,6 +155,16 @@ const metadata = {
151155
type: Boolean,
152156
noAttribute: true,
153157
},
158+
159+
_animationRunning: {
160+
type: Boolean,
161+
noAttribute: true,
162+
},
163+
164+
_contentCollapsed: {
165+
type: Boolean,
166+
noAttribute: true,
167+
},
154168
},
155169
events: /** @lends sap.ui.webcomponents.main.TabContainer.prototype */ {
156170

@@ -268,6 +282,10 @@ class TabContainer extends UI5Element {
268282
};
269283
item._itemSelectCallback = this._onItemSelect.bind(this);
270284
});
285+
286+
if (!this._animationRunning) {
287+
this._contentCollapsed = this.collapsed;
288+
}
271289
}
272290

273291
onAfterRendering() {
@@ -353,15 +371,51 @@ class TabContainer extends UI5Element {
353371
}
354372
}, this);
355373

356-
// update collapsed state
357-
if (!this.fixed) {
358-
if (selectedTab === this._selectedTab) {
359-
this.collapsed = !this.collapsed;
360-
} else {
361-
this.collapsed = false;
362-
}
374+
if (this.fixed) {
375+
this.selectTab(selectedTab, selectedTabIndex);
376+
return;
377+
}
378+
379+
if (!this.animate) {
380+
this.toggle(selectedTab);
381+
this.selectTab(selectedTab, selectedTabIndex);
382+
return;
383+
}
384+
385+
this.toggleAnimated(selectedTab);
386+
this.selectTab(selectedTab, selectedTabIndex);
387+
}
388+
389+
async toggleAnimated(selectedTab) {
390+
const content = this.shadowRoot.querySelector(".ui5-tc__content");
391+
let animationPromise = null;
392+
393+
this._animationRunning = true;
394+
395+
if (selectedTab === this._selectedTab) {
396+
// click on already selected tab - animate both directions
397+
this.collapsed = !this.collapsed;
398+
animationPromise = this.collapsed ? this.slideContentUp(content) : this.slideContentDown(content);
399+
} else {
400+
// click on new tab - animate if the content is currently collapsed
401+
animationPromise = this.collapsed ? this.slideContentDown(content) : Promise.resolve();
402+
this.collapsed = false;
403+
}
404+
405+
await animationPromise;
406+
this._contentCollapsed = this.collapsed;
407+
this._animationRunning = false;
408+
}
409+
410+
toggle(selectedTab) {
411+
if (selectedTab === this._selectedTab) {
412+
this.collapsed = !this.collapsed;
413+
} else {
414+
this.collapsed = false;
363415
}
416+
}
364417

418+
selectTab(selectedTab, selectedTabIndex) {
365419
// select the tab
366420
this._selectedTab = selectedTab;
367421
this.fireEvent("tabSelect", {
@@ -370,6 +424,14 @@ class TabContainer extends UI5Element {
370424
});
371425
}
372426

427+
slideContentDown(element) {
428+
return slideDown({ element }).promise();
429+
}
430+
431+
slideContentUp(element) {
432+
return slideUp({ element }).promise();
433+
}
434+
373435
async _onOverflowButtonClick(event) {
374436
this.responsivePopover = await this._respPopover();
375437
this.updateStaticAreaItemContentDensity();
@@ -454,7 +516,7 @@ class TabContainer extends UI5Element {
454516
},
455517
content: {
456518
"ui5-tc__content": true,
457-
"ui5-tc__content--collapsed": this.collapsed,
519+
"ui5-tc__content--collapsed": this._contentCollapsed,
458520
},
459521
};
460522
}
@@ -491,6 +553,10 @@ class TabContainer extends UI5Element {
491553
return getRTL() ? "rtl" : undefined;
492554
}
493555

556+
get animate() {
557+
return getAnimationMode() !== AnimationMode.None;
558+
}
559+
494560
static async onDefine() {
495561
await Promise.all([
496562
Button.define(),

0 commit comments

Comments
 (0)