Skip to content

Commit

Permalink
fix(core/tabs): minimize tab auto scroll distance (#1390)
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Berliner <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 17, 2024
1 parent 55783f7 commit 6c8e3b7
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-sloths-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siemens/ix": patch
---

fix(core/tabs): minimize tab auto scroll distance
2 changes: 1 addition & 1 deletion packages/core/component-doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14312,7 +14312,7 @@
"type": "number"
}
],
"optional": false,
"optional": true,
"required": false
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ export namespace Components {
/**
* Set counter value
*/
"counter": number;
"counter"?: number;
/**
* Set disabled tab
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/components/tab-item/tab-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class TabItem {
/**
* Set counter value
*/
@Prop() counter: number;
@Prop() counter?: number;

/**
* Set layout width style
Expand All @@ -64,7 +64,7 @@ export class TabItem {
*
* @since 2.0.0
*/
@Event() tabClick: EventEmitter<TabClickDetail>;
@Event() tabClick!: EventEmitter<TabClickDetail>;

private tabItemClasses(props: {
selected: boolean;
Expand Down
51 changes: 34 additions & 17 deletions packages/core/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Listen,
Prop,
State,
Watch,
} from '@stencil/core';
import { requestAnimationFrameNoNgZone } from '../utils/requestAnimationFrame';

Expand Down Expand Up @@ -126,7 +127,7 @@ export class Tabs {

private showPreviousArrow() {
try {
return this.showArrows() && this.scrollActionAmount < 0;
return this.showArrows() === true && this.scrollActionAmount < 0;
} catch (error) {
return false;
}
Expand All @@ -143,7 +144,7 @@ export class Tabs {
const tabWrapperRect = tabWrapper.getBoundingClientRect();

return (
this.showArrows() &&
this.showArrows() === true &&
this.scrollActionAmount >
(tabWrapper.scrollWidth - tabWrapperRect.width) * -1
);
Expand All @@ -153,38 +154,54 @@ export class Tabs {
}

private move(amount: number, click = false) {
const tabWrapper = this.getTabsWrapper();
const tabsWrapper = this.getTabsWrapper();

if (!tabsWrapper) {
return;
}

const tabsWrapperVisibleWidth = tabsWrapper.getBoundingClientRect().width;
const maxScrollWidth =
(tabWrapper.scrollWidth - tabWrapper.getBoundingClientRect().width) * -1;
-this.currentScrollAmount +
tabsWrapperVisibleWidth -
tabsWrapper.scrollWidth;

amount = this.currentScrollAmount + amount;
amount = amount > 0 ? 0 : amount < maxScrollWidth ? maxScrollWidth : amount;
amount = amount < maxScrollWidth ? maxScrollWidth : amount;
amount += this.currentScrollAmount;
amount = Math.min(amount, 0);

const styles = [
`transform: translateX(${amount}px);`,
click ? 'transition: all ease-in-out 400ms;' : '',
].join('');

tabWrapper.setAttribute('style', styles);
tabsWrapper.setAttribute('style', styles);

if (click) this.currentScrollAmount = this.scrollActionAmount = amount;
else this.scrollActionAmount = amount;
}

private moveTabToView(tabIndex: number) {
@Watch('selected')
onSelectedChange(newValue: number) {
if (!this.showArrows()) return;

const tab = this.getTab(tabIndex).getBoundingClientRect();
const amount = tab.x * -1;
this.move(amount, true);
const tabRect = this.getTab(newValue).getBoundingClientRect();
const wrapperWidth = this.getTabsWrapper()?.clientWidth;
const arrowWidth = 32;

if (tabRect.left < arrowWidth) {
this.move(-tabRect.left + arrowWidth, true);
} else if (wrapperWidth && tabRect.right > wrapperWidth - arrowWidth) {
this.move(wrapperWidth - tabRect.right - arrowWidth, true);
}
}

private setSelected(index: number) {
this.selected = index;
}

private clickTab(index: number) {
if (this.dragStop()) {
if (!this.clickAction.isClick || this.dragStop()) {
return;
}

Expand All @@ -194,7 +211,6 @@ export class Tabs {
}

this.setSelected(index);
this.moveTabToView(index);
}

private dragStart(element: HTMLIxTabItemElement, event: MouseEvent) {
Expand All @@ -210,11 +226,12 @@ export class Tabs {
const mousedownPositionX = event.clientX;
const move = (event: MouseEvent) =>
this.dragMove(event, tabPositionX, mousedownPositionX);

window.addEventListener('mouseup', () => {
const windowClick = () => {
window.removeEventListener('mousemove', move, false);
window.removeEventListener('click', windowClick, false);
this.dragStop();
});
};
window.addEventListener('click', windowClick);
window.addEventListener('mousemove', move, false);
}

Expand All @@ -225,8 +242,8 @@ export class Tabs {
private dragStop() {
if (this.clickAction.timeout) {
clearTimeout(this.clickAction.timeout);
this.clickAction.timeout = null;
}
this.clickAction.timeout = null;

if (this.clickAction.isClick) return false;

Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/components/tabs/test/tabs.ct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,23 @@ test('should update layout on resize', async ({ mount, page }) => {
await page.setViewportSize({ width: 1000, height: 100 });
await expect(arrowNext).not.toBeVisible();
});

test('should scroll selected tab into view', async ({ mount, page }) => {
await mount(`
<ix-tabs>
<ix-tab-item>Item 1</ix-tab-item>
<ix-tab-item>Item 2</ix-tab-item>
<ix-tab-item>Item 3</ix-tab-item>
<ix-tab-item>Item 4</ix-tab-item>
<ix-tab-item>Item 5</ix-tab-item>
<ix-tab-item>Item 6</ix-tab-item>
</ix-tabs>
`);
await page.setViewportSize({ width: 300, height: 100 });
const clickedTab = page.locator('ix-tab-item').nth(3);
const lastTab = page.locator('ix-tab-item').last();
await clickedTab.click();
await page.waitForTimeout(500);
await expect(clickedTab).toBeInViewport();
await expect(lastTab).not.toBeInViewport();
});
36 changes: 0 additions & 36 deletions packages/core/src/tests/tabs/overflow/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@
<body>
<style>
.example {
display: block;
position: relative;
width: 50%;
}

div[data-tab-content] {
display: none;
}

div[data-tab-content].show {
display: block;
}
</style>
<div class="example">
<ix-tabs layout="auto">
Expand All @@ -40,33 +30,7 @@
<ix-tab-item data-tab-id="7">Biggest Tab 7</ix-tab-item>
<ix-tab-item data-tab-id="8">Biggest Tab 8</ix-tab-item>
</ix-tabs>
<div data-tab-content="1" class="show">Content Tab 1</div>
<div data-tab-content="2">Content Tab 2</div>
<div data-tab-content="3">Content Tab 3</div>
<div data-tab-content="7">Should be in overflow</div>
</div>

<script>
(async function () {
await window.customElements.whenDefined('ix-tabs');
const tabs = document.querySelectorAll('ix-tab-item[data-tab-id]');

function registerClickListener(tab) {
tab.addEventListener('click', (tabContent) => {
const contentList = document.querySelectorAll('[data-tab-content]');
contentList.forEach((content) => {
if (content.dataset.tabContent === tab.dataset.tabId) {
content.classList.add('show');
} else {
content.classList.remove('show');
}
});
});
}

tabs.forEach(registerClickListener);
})();
</script>
<script src="http://127.0.0.1:8080/scripts/e2e/load-e2e-runtime.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion packages/core/src/tests/tabs/tabs.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ regressionTest.describe('tabs', () => {
regressionTest('overflow', async ({ page }) => {
await page.goto('tabs/overflow');

const selectItem = await page.waitForSelector("[data-tab-id='7']");
const selectItem = await page.waitForSelector("[data-tab-id='5']");
await selectItem.click();

await page.waitForTimeout(500);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6c8e3b7

Please sign in to comment.