@@ -18,6 +18,8 @@ import {
18
18
AfterViewChecked ,
19
19
ViewEncapsulation ,
20
20
ChangeDetectionStrategy ,
21
+ OnChanges ,
22
+ SimpleChanges ,
21
23
} from '@angular/core' ;
22
24
import {
23
25
trigger ,
@@ -65,6 +67,7 @@ export type MdTabBodyOriginState = 'left' | 'right';
65
67
changeDetection : ChangeDetectionStrategy . OnPush ,
66
68
host : {
67
69
'class' : 'mat-tab-body' ,
70
+ '[class.mat-tab-body-active]' : 'active' ,
68
71
} ,
69
72
animations : [
70
73
trigger ( 'translateTab' , [
@@ -87,7 +90,7 @@ export type MdTabBodyOriginState = 'left' | 'right';
87
90
] )
88
91
]
89
92
} )
90
- export class MdTabBody implements OnInit , AfterViewChecked {
93
+ export class MdTabBody implements OnInit , OnChanges , AfterViewChecked {
91
94
/** The portal host inside of this container into which the tab body content will be loaded. */
92
95
@ViewChild ( PortalHostDirective ) _portalHost : PortalHostDirective ;
93
96
@@ -103,6 +106,9 @@ export class MdTabBody implements OnInit, AfterViewChecked {
103
106
/** The tab body content to display. */
104
107
@Input ( 'content' ) _contentPortal : TemplatePortal < any > ;
105
108
109
+ /** Whether the tab is currently active. */
110
+ @Input ( ) active : boolean ;
111
+
106
112
/** Scroll position of the tab before the user switched away. */
107
113
private _lastScrollPosition = 0 ;
108
114
@@ -153,7 +159,24 @@ export class MdTabBody implements OnInit, AfterViewChecked {
153
159
ngAfterViewChecked ( ) {
154
160
if ( this . _isCenterPosition ( this . _position ) && ! this . _portalHost . hasAttached ( ) ) {
155
161
this . _portalHost . attach ( this . _contentPortal ) ;
156
- this . _contentElement . nativeElement . scrollTop = this . _lastScrollPosition ;
162
+
163
+ if ( this . _lastScrollPosition ) {
164
+ // Depending on the browser, the scrollable element can end up being
165
+ // either the host element or the element with all the content.
166
+ this . _contentElement . nativeElement . scrollTop =
167
+ this . _elementRef . nativeElement . scrollTop =
168
+ this . _lastScrollPosition ;
169
+ }
170
+ }
171
+ }
172
+
173
+ ngOnChanges ( changes : SimpleChanges ) {
174
+ // Cache the scroll position before moving away from the tab. Note that this has to be done
175
+ // throught change detection and as early as possible, because some browsers (namely Safari)
176
+ // will reset the scroll position when we switch from an absolute to a relative position.
177
+ if ( changes . active && changes . active . previousValue ) {
178
+ this . _lastScrollPosition = this . _elementRef . nativeElement . scrollTop ||
179
+ this . _contentElement . nativeElement . scrollTop ;
157
180
}
158
181
}
159
182
@@ -166,7 +189,6 @@ export class MdTabBody implements OnInit, AfterViewChecked {
166
189
_onTranslateTabComplete ( e : AnimationEvent ) {
167
190
// If the end state is that the tab is not centered, then detach the content.
168
191
if ( ! this . _isCenterPosition ( e . toState ) && ! this . _isCenterPosition ( this . _position ) ) {
169
- this . _lastScrollPosition = this . _contentElement . nativeElement . scrollTop || 0 ;
170
192
this . _portalHost . detach ( ) ;
171
193
}
172
194
0 commit comments