Skip to content

Commit 2f2addf

Browse files
committed
fix(tabs): better handling of animationDuration without units
Based off of the discussions on #13428. Handles values passed to `animationDuration` that don't have units, rather than allowing them to continue through to the `BrowserAnimationsModule` and to throw an error.
1 parent a9e8776 commit 2f2addf

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/lib/tabs/tab-group.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ describe('nested MatTabGroup with enabled animations', () => {
622622
beforeEach(fakeAsync(() => {
623623
TestBed.configureTestingModule({
624624
imports: [MatTabsModule, BrowserAnimationsModule],
625-
declarations: [NestedTabs]
625+
declarations: [NestedTabs, TabsWithCustomAnimationDuration]
626626
});
627627

628628
TestBed.compileComponents();
@@ -635,6 +635,14 @@ describe('nested MatTabGroup with enabled animations', () => {
635635
tick();
636636
}).not.toThrow();
637637
}));
638+
639+
it('should not throw when setting an animationDuration without units', fakeAsync(() => {
640+
expect(() => {
641+
let fixture = TestBed.createComponent(TabsWithCustomAnimationDuration);
642+
fixture.detectChanges();
643+
tick();
644+
}).not.toThrow();
645+
}));
638646
});
639647

640648

@@ -861,3 +869,14 @@ class TabGroupWithAriaInputs {
861869
})
862870
class TabGroupWithIsActiveBinding {
863871
}
872+
873+
874+
@Component({
875+
template: `
876+
<mat-tab-group animationDuration="500">
877+
<mat-tab label="One">Tab one content</mat-tab>
878+
<mat-tab label="Two">Tab two content</mat-tab>
879+
</mat-tab-group>
880+
`,
881+
})
882+
class TabsWithCustomAnimationDuration {}

src/lib/tabs/tab-group.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ import {MatTabHeader} from './tab-header';
4343
/** Used to generate unique ID's for each tab component */
4444
let nextId = 0;
4545

46+
/** Regex used to check whether a value has CSS units. */
47+
const cssUnitPattern = /([A-Za-z%]+)$/;
48+
4649
/** A simple change event emitted on focus or selection changes. */
4750
export class MatTabChangeEvent {
4851
/** Index of the currently-selected tab. */
@@ -129,8 +132,14 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn
129132
/** Position of the tab header. */
130133
@Input() headerPosition: MatTabHeaderPosition = 'above';
131134

132-
/** Duration for the tab animation. Must be a valid CSS value (e.g. 600ms). */
133-
@Input() animationDuration: string;
135+
/** Duration for the tab animation. Will be normalized to milliseconds if no units are set. */
136+
@Input()
137+
get animationDuration(): string { return this._animationDuration; }
138+
set animationDuration(value: string) {
139+
this._animationDuration = (typeof value === 'number' || !cssUnitPattern.test(value)) ?
140+
value + 'ms' : value;
141+
}
142+
private _animationDuration: string;
134143

135144
/** Background color of the tab group. */
136145
@Input()

0 commit comments

Comments
 (0)