Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui5-tabcontainer): add expand/collapse animation #1617

Merged
merged 2 commits into from
May 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 74 additions & 8 deletions packages/main/src/TabContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import ScrollEnablement from "@ui5/webcomponents-base/dist/delegate/ScrollEnablement.js";
import slideDown from "@ui5/webcomponents-base/dist/animations/slideDown.js";
import slideUp from "@ui5/webcomponents-base/dist/animations/slideUp.js";
import AnimationMode from "@ui5/webcomponents-base/dist/types/AnimationMode.js";
import { getAnimationMode } from "@ui5/webcomponents-base/dist/config/AnimationMode.js";
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
import { getRTL } from "@ui5/webcomponents-base/dist/config/RTL.js";
Expand Down Expand Up @@ -151,6 +155,16 @@ const metadata = {
type: Boolean,
noAttribute: true,
},

_animationRunning: {
type: Boolean,
noAttribute: true,
},

_contentCollapsed: {
type: Boolean,
noAttribute: true,
},
},
events: /** @lends sap.ui.webcomponents.main.TabContainer.prototype */ {

Expand Down Expand Up @@ -268,6 +282,10 @@ class TabContainer extends UI5Element {
};
item._itemSelectCallback = this._onItemSelect.bind(this);
});

if (!this._animationRunning) {
this._contentCollapsed = this.collapsed;
}
}

onAfterRendering() {
Expand Down Expand Up @@ -353,15 +371,51 @@ class TabContainer extends UI5Element {
}
}, this);

// update collapsed state
if (!this.fixed) {
if (selectedTab === this._selectedTab) {
this.collapsed = !this.collapsed;
} else {
this.collapsed = false;
}
if (this.fixed) {
this.selectTab(selectedTab, selectedTabIndex);
return;
}

if (!this.animate) {
this.toggle(selectedTab);
this.selectTab(selectedTab, selectedTabIndex);
return;
}

this.toggleAnimated(selectedTab);
this.selectTab(selectedTab, selectedTabIndex);
}

async toggleAnimated(selectedTab) {
const content = this.shadowRoot.querySelector(".ui5-tc__content");
let animationPromise = null;

this._animationRunning = true;

if (selectedTab === this._selectedTab) {
// click on already selected tab - animate both directions
this.collapsed = !this.collapsed;
animationPromise = this.collapsed ? this.slideContentUp(content) : this.slideContentDown(content);
} else {
// click on new tab - animate if the content is currently collapsed
animationPromise = this.collapsed ? this.slideContentDown(content) : Promise.resolve();
this.collapsed = false;
}

await animationPromise;
this._contentCollapsed = this.collapsed;
this._animationRunning = false;
}

toggle(selectedTab) {
if (selectedTab === this._selectedTab) {
this.collapsed = !this.collapsed;
} else {
this.collapsed = false;
}
}

selectTab(selectedTab, selectedTabIndex) {
// select the tab
this._selectedTab = selectedTab;
this.fireEvent("tabSelect", {
Expand All @@ -370,6 +424,14 @@ class TabContainer extends UI5Element {
});
}

slideContentDown(element) {
return slideDown({ element }).promise();
}

slideContentUp(element) {
return slideUp({ element }).promise();
}

async _onOverflowButtonClick(event) {
this.responsivePopover = await this._respPopover();
this.updateStaticAreaItemContentDensity();
Expand Down Expand Up @@ -454,7 +516,7 @@ class TabContainer extends UI5Element {
},
content: {
"ui5-tc__content": true,
"ui5-tc__content--collapsed": this.collapsed,
"ui5-tc__content--collapsed": this._contentCollapsed,
},
};
}
Expand Down Expand Up @@ -491,6 +553,10 @@ class TabContainer extends UI5Element {
return getRTL() ? "rtl" : undefined;
}

get animate() {
return getAnimationMode() !== AnimationMode.None;
}

static async onDefine() {
await Promise.all([
Button.define(),
Expand Down