Skip to content

Commit 7b71cfb

Browse files
committed
feat(tabs): add the ability to customize the animation duration
Adds the `animationDuration` input and the `MAT_TABS_CONFIG` injection token that can be used to configure the duration of the tab animation. Fixes #13428.
1 parent 2e4a511 commit 7b71cfb

9 files changed

+65
-5
lines changed

src/lib/tabs/tab-body.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<div class="mat-tab-body-content" #content
2-
[@translateTab]="_position"
2+
[@translateTab]="{
3+
value: _position,
4+
params: {animationDuration: animationDuration}
5+
}"
36
(@translateTab.start)="_onTranslateTabStarted($event)"
47
(@translateTab.done)="_onTranslateTabComplete($event)">
58
<ng-template matTabBodyHost></ng-template>

src/lib/tabs/tab-body.ts

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ export class MatTabBody implements OnInit, OnDestroy {
146146
/** Position that will be used when the tab is immediately becoming visible after creation. */
147147
@Input() origin: number;
148148

149+
/** Duration for the tab's animation. */
150+
@Input() animationDuration: string;
151+
149152
/** The shifted index position of the tab body, where zero represents the active center tab. */
150153
@Input()
151154
set position(position: number) {

src/lib/tabs/tab-group.html

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
[content]="tab.content"
4141
[position]="tab.position"
4242
[origin]="tab.origin"
43+
[animationDuration]="animationDuration"
4344
(_onCentered)="_removeTabBodyWrapperHeight()"
4445
(_onCentering)="_setTabBodyWrapperHeight($event)">
4546
</mat-tab-body>

src/lib/tabs/tab-group.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import {
2222
QueryList,
2323
ViewChild,
2424
ViewEncapsulation,
25+
InjectionToken,
26+
Inject,
27+
Optional,
2528
} from '@angular/core';
2629
import {
2730
CanColor,
@@ -51,6 +54,15 @@ export class MatTabChangeEvent {
5154
/** Possible positions for the tab header. */
5255
export type MatTabHeaderPosition = 'above' | 'below';
5356

57+
/** Object that can be used to configure the default options for the tabs module. */
58+
export interface MatTabsConfig {
59+
/** Duration for the tab animation. Must be a valid CSS value (e.g. 600ms). */
60+
animationDuration?: string;
61+
}
62+
63+
/** Injection token that can be used to provide the default options the tabs module. */
64+
export const MAT_TABS_CONFIG = new InjectionToken('MAT_TABS_CONFIG');
65+
5466
// Boilerplate for applying mixins to MatTabGroup.
5567
/** @docs-private */
5668
export class MatTabGroupBase {
@@ -117,6 +129,9 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn
117129
/** Position of the tab header. */
118130
@Input() headerPosition: MatTabHeaderPosition = 'above';
119131

132+
/** Duration for the tab animation. Must be a valid CSS value (e.g. 600ms). */
133+
@Input() animationDuration: string;
134+
120135
/** Background color of the tab group. */
121136
@Input()
122137
get backgroundColor(): ThemePalette { return this._backgroundColor; }
@@ -150,9 +165,12 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn
150165
private _groupId: number;
151166

152167
constructor(elementRef: ElementRef,
153-
private _changeDetectorRef: ChangeDetectorRef) {
168+
private _changeDetectorRef: ChangeDetectorRef,
169+
@Inject(MAT_TABS_CONFIG) @Optional() defaultConfig?: MatTabsConfig) {
154170
super(elementRef);
155171
this._groupId = nextId++;
172+
this.animationDuration = defaultConfig && defaultConfig.animationDuration ?
173+
defaultConfig.animationDuration : '500ms';
156174
}
157175

158176
/**

src/lib/tabs/tabs-animations.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ export const matTabsAnimations: {
3434
state('right', style({transform: 'translate3d(100%, 0, 0)', minHeight: '1px'})),
3535

3636
transition('* => left, * => right, left => center, right => center',
37-
animate('500ms cubic-bezier(0.35, 0, 0.25, 1)')),
37+
animate('{{animationDuration}} cubic-bezier(0.35, 0, 0.25, 1)')),
3838
transition('void => left-origin-center', [
3939
style({transform: 'translate3d(-100%, 0, 0)'}),
40-
animate('500ms cubic-bezier(0.35, 0, 0.25, 1)')
40+
animate('{{animationDuration}} cubic-bezier(0.35, 0, 0.25, 1)')
4141
]),
4242
transition('void => right-origin-center', [
4343
style({transform: 'translate3d(100%, 0, 0)'}),
44-
animate('500ms cubic-bezier(0.35, 0, 0.25, 1)')
44+
animate('{{animationDuration}} cubic-bezier(0.35, 0, 0.25, 1)')
4545
])
4646
])
4747
};

src/lib/tabs/tabs.md

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ do so using the `[mat-align-tabs]` attribute.
114114

115115
<!-- example(tab-group-align) -->
116116

117+
### Controlling the tab animation
118+
You can control the duration of the tabs' animation using the `animationDuration` input. If you
119+
want to disable the animation completely, you can do so by setting the properties to `0ms`. The
120+
duration can be configured globally using the `MAT_TABS_CONFIG` injection token.
121+
122+
<!-- example(tab-group-animations) -->
123+
117124
### Accessibility
118125
Tabs without text or labels should be given a meaningful label via `aria-label` or
119126
`aria-labelledby`. For `MatTabNav`, the `<nav>` element should have a label as well.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mat-tab-group {
2+
margin-bottom: 48px;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h3>No animation</h3>
2+
3+
<mat-tab-group animationDuration="0ms">
4+
<mat-tab label="First">Content 1</mat-tab>
5+
<mat-tab label="Second">Content 2</mat-tab>
6+
<mat-tab label="Third">Content 3</mat-tab>
7+
</mat-tab-group>
8+
9+
<h3>Very slow animation</h3>
10+
<mat-tab-group animationDuration="2000ms">
11+
<mat-tab label="First">Content 1</mat-tab>
12+
<mat-tab label="Second">Content 2</mat-tab>
13+
<mat-tab label="Third">Content 3</mat-tab>
14+
</mat-tab-group>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Tab group animations
5+
*/
6+
@Component({
7+
selector: 'tab-group-animations-example',
8+
templateUrl: 'tab-group-animations-example.html',
9+
styleUrls: ['tab-group-animations-example.css'],
10+
})
11+
export class TabGroupAnimationsExample {}

0 commit comments

Comments
 (0)