Skip to content

Commit 892f69e

Browse files
committed
fix(material/tabs): update MatTab _scrollToLabel function to always display a label from its start (#26736)
1 parent 5ea4ca5 commit 892f69e

File tree

3 files changed

+11
-26
lines changed

3 files changed

+11
-26
lines changed

src/material/legacy-tabs/tab-header.spec.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ describe('MatTabHeader', () => {
272272
// Focus on the last tab, expect this to be the maximum scroll distance.
273273
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
274274
fixture.detectChanges();
275-
expect(appComponent.tabHeader.scrollDistance).toBe(
276-
appComponent.tabHeader._getMaxScrollDistance(),
277-
);
275+
expect(appComponent.tabHeader.scrollDistance).toBe(182);
278276

279277
// Focus on the first tab, expect this to be the maximum scroll distance.
280278
appComponent.tabHeader.focusIndex = 0;
@@ -331,9 +329,7 @@ describe('MatTabHeader', () => {
331329
// Focus the last tab so the header scrolls to the end.
332330
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
333331
fixture.detectChanges();
334-
expect(appComponent.tabHeader.scrollDistance).toBe(
335-
appComponent.tabHeader._getMaxScrollDistance(),
336-
);
332+
expect(appComponent.tabHeader.scrollDistance).toBe(182);
337333

338334
// Remove the first two tabs which includes the selected tab.
339335
appComponent.tabs = appComponent.tabs.slice(2);
@@ -362,9 +358,7 @@ describe('MatTabHeader', () => {
362358
// Focus on the last tab, expect this to be the maximum scroll distance.
363359
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
364360
fixture.detectChanges();
365-
expect(appComponent.tabHeader.scrollDistance).toBe(
366-
appComponent.tabHeader._getMaxScrollDistance(),
367-
);
361+
expect(appComponent.tabHeader.scrollDistance).toBe(190);
368362

369363
// Focus on the first tab, expect this to be the maximum scroll distance.
370364
appComponent.tabHeader.focusIndex = 0;

src/material/tabs/paginated-tab-header.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ const passiveEventListenerOptions = normalizePassiveListenerOptions({
5757
*/
5858
export type ScrollDirection = 'after' | 'before';
5959

60-
/**
61-
* The distance in pixels that will be overshot when scrolling a tab label into view. This helps
62-
* provide a small affordance to the label next to it.
63-
*/
64-
const EXAGGERATED_OVERSCROLL = 60;
65-
6660
/**
6761
* Amount of milliseconds to wait before starting to scroll the header automatically.
6862
* Set a little conservatively in order to handle fake events dispatched on touch devices.
@@ -524,10 +518,13 @@ export abstract class MatPaginatedTabHeader
524518

525519
if (labelBeforePos < beforeVisiblePos) {
526520
// Scroll header to move label to the before direction
527-
this.scrollDistance -= beforeVisiblePos - labelBeforePos + EXAGGERATED_OVERSCROLL;
521+
this.scrollDistance -= beforeVisiblePos - labelBeforePos;
528522
} else if (labelAfterPos > afterVisiblePos) {
529523
// Scroll header to move label to the after direction
530-
this.scrollDistance += labelAfterPos - afterVisiblePos + EXAGGERATED_OVERSCROLL;
524+
this.scrollDistance += Math.min(
525+
labelAfterPos - afterVisiblePos,
526+
labelBeforePos - beforeVisiblePos,
527+
);
531528
}
532529
}
533530

src/material/tabs/tab-header.spec.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,7 @@ describe('MDC-based MatTabHeader', () => {
266266
// Focus on the last tab, expect this to be the maximum scroll distance.
267267
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
268268
fixture.detectChanges();
269-
expect(appComponent.tabHeader.scrollDistance).toBe(
270-
appComponent.tabHeader._getMaxScrollDistance(),
271-
);
269+
expect(appComponent.tabHeader.scrollDistance).toBe(182);
272270

273271
// Focus on the first tab, expect this to be the maximum scroll distance.
274272
appComponent.tabHeader.focusIndex = 0;
@@ -329,9 +327,7 @@ describe('MDC-based MatTabHeader', () => {
329327
// Focus the last tab so the header scrolls to the end.
330328
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
331329
fixture.detectChanges();
332-
expect(appComponent.tabHeader.scrollDistance).toBe(
333-
appComponent.tabHeader._getMaxScrollDistance(),
334-
);
330+
expect(appComponent.tabHeader.scrollDistance).toBe(182);
335331

336332
// Remove the first two tabs which includes the selected tab.
337333
appComponent.tabs = appComponent.tabs.slice(2);
@@ -360,9 +356,7 @@ describe('MDC-based MatTabHeader', () => {
360356
// Focus on the last tab, expect this to be the maximum scroll distance.
361357
appComponent.tabHeader.focusIndex = appComponent.tabs.length - 1;
362358
fixture.detectChanges();
363-
expect(appComponent.tabHeader.scrollDistance).toBe(
364-
appComponent.tabHeader._getMaxScrollDistance(),
365-
);
359+
expect(appComponent.tabHeader.scrollDistance).toBe(190);
366360

367361
// Focus on the first tab, expect this to be the maximum scroll distance.
368362
appComponent.tabHeader.focusIndex = 0;

0 commit comments

Comments
 (0)