Skip to content

Commit a47c834

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

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ class SimpleTabHeaderApp {
743743
focusedIndex: number;
744744
disablePagination: boolean;
745745
disabledTabIndex = 1;
746-
tabs: Tab[] = [{label: 'tab one'}, {label: 'tab one'}, {label: 'tab one'}, {label: 'tab one'}];
746+
tabs: Tab[] = [{label: 'tab one'}, {label: 'tab two'}, {label: 'tab three'}, {label: 'tab four'}];
747747
dir: Direction = 'ltr';
748748

749749
@ViewChild(MatLegacyTabHeader, {static: true}) tabHeader: MatLegacyTabHeader;

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ class SimpleTabHeaderApp {
742742
selectedIndex: number = 0;
743743
focusedIndex: number;
744744
disabledTabIndex = 1;
745-
tabs: Tab[] = [{label: 'tab one'}, {label: 'tab one'}, {label: 'tab one'}, {label: 'tab one'}];
745+
tabs: Tab[] = [{label: 'tab one'}, {label: 'tab two'}, {label: 'tab three'}, {label: 'tab four'}];
746746
dir: Direction = 'ltr';
747747
disablePagination: boolean;
748748

0 commit comments

Comments
 (0)