Skip to content

Commit 28f26cb

Browse files
authored
Merge pull request #2486 from patternfly/fix/tabs/scroll-on-small-screen
fix(core): overflow controller scroll on small screens
2 parents 32eecd6 + 3c310e3 commit 28f26cb

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

.changeset/two-pugs-add.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@patternfly/pfe-core": patch
3+
---
4+
5+
`overflow-controller`:
6+
- improves display calculations for overflow scroll buttons
7+
- adds smooth scroll behavior

core/pfe-core/controllers/overflow-controller.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ export class OverflowController implements ReactiveController {
4242
}
4343
this.overflowLeft = !this.#hideOverflowButtons && !isElementInView(this.#container, this.firstItem);
4444
this.overflowRight = !this.#hideOverflowButtons && !isElementInView(this.#container, this.lastItem);
45-
this.showScrollButtons = !this.#hideOverflowButtons && (this.overflowLeft || this.overflowRight);
45+
let scrollButtonsWidth = 0;
46+
if (this.overflowLeft || this.overflowRight) {
47+
scrollButtonsWidth = (this.#container.parentElement?.querySelector('button')?.getBoundingClientRect().width || 0) * 2;
48+
}
49+
this.showScrollButtons = !this.#hideOverflowButtons &&
50+
this.#container.scrollWidth > (this.#container.clientWidth + scrollButtonsWidth);
4651
this.host.requestUpdate();
4752
}
4853

@@ -61,35 +66,17 @@ export class OverflowController implements ReactiveController {
6166
if (!this.#container) {
6267
return;
6368
}
64-
let firstElementInView: HTMLElement | undefined;
65-
let lastElementOutOfView: HTMLElement | undefined;
66-
for (let i = 0; i < this.#items.length && !firstElementInView; i++) {
67-
if (isElementInView(this.#container, this.#items[i] as HTMLElement, false)) {
68-
firstElementInView = this.#items[i];
69-
lastElementOutOfView = this.#items[i - 1];
70-
}
71-
}
72-
if (lastElementOutOfView) {
73-
this.#container.scrollLeft -= lastElementOutOfView.scrollWidth;
74-
}
69+
const leftScroll = this.#container.scrollLeft - this.#container.clientWidth;
70+
this.#container.scroll({ left: leftScroll, behavior: 'smooth' });
7571
this.#setOverflowState();
7672
}
7773

7874
scrollRight() {
7975
if (!this.#container) {
8076
return;
8177
}
82-
let lastElementInView: HTMLElement | undefined;
83-
let firstElementOutOfView: HTMLElement | undefined;
84-
for (let i = this.#items.length - 1; i >= 0 && !lastElementInView; i--) {
85-
if (isElementInView(this.#container, this.#items[i] as HTMLElement, false)) {
86-
lastElementInView = this.#items[i];
87-
firstElementOutOfView = this.#items[i + 1];
88-
}
89-
}
90-
if (firstElementOutOfView) {
91-
this.#container.scrollLeft += firstElementOutOfView.scrollWidth;
92-
}
78+
const leftScroll = this.#container.scrollLeft + this.#container.clientWidth;
79+
this.#container.scroll({ left: leftScroll, behavior: 'smooth' });
9380
this.#setOverflowState();
9481
}
9582

0 commit comments

Comments
 (0)