-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(tabs): reposition tab body on direction change #12229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
import { | ||
Component, | ||
ChangeDetectorRef, | ||
Input, | ||
Inject, | ||
Output, | ||
|
@@ -113,7 +114,17 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr | |
'class': 'mat-tab-body', | ||
}, | ||
}) | ||
export class MatTabBody implements OnInit { | ||
export class MatTabBody implements OnInit, OnDestroy { | ||
|
||
/** Current position of the tab-body in the tab-group. Zero means that the tab is visible. */ | ||
private _positionIndex: number; | ||
|
||
/** Subscription to the directionality change observable. */ | ||
private _dirChangeSubscription: Subscription = Subscription.EMPTY; | ||
|
||
/** Tab body position state. Used by the animation trigger for the current state. */ | ||
_position: MatTabBodyPositionState; | ||
|
||
/** Event emitted when the tab begins to animate towards the center as the active tab. */ | ||
@Output() readonly _onCentering: EventEmitter<number> = new EventEmitter<number>(); | ||
|
||
|
@@ -132,46 +143,43 @@ export class MatTabBody implements OnInit { | |
/** The tab body content to display. */ | ||
@Input('content') _content: TemplatePortal; | ||
|
||
/** Position that will be used when the tab is immediately becoming visible after creation. */ | ||
@Input() origin: number; | ||
|
||
/** The shifted index position of the tab body, where zero represents the active center tab. */ | ||
@Input() | ||
set position(position: number) { | ||
if (position < 0) { | ||
this._position = this._getLayoutDirection() == 'ltr' ? 'left' : 'right'; | ||
} else if (position > 0) { | ||
this._position = this._getLayoutDirection() == 'ltr' ? 'right' : 'left'; | ||
} else { | ||
this._position = 'center'; | ||
} | ||
this._positionIndex = position; | ||
this._computePositionAnimationState(); | ||
} | ||
_position: MatTabBodyPositionState; | ||
|
||
/** The origin position from which this tab should appear when it is centered into view. */ | ||
@Input() | ||
set origin(origin: number) { | ||
if (origin == null) { return; } | ||
|
||
const dir = this._getLayoutDirection(); | ||
if ((dir == 'ltr' && origin <= 0) || (dir == 'rtl' && origin > 0)) { | ||
this._origin = 'left'; | ||
} else { | ||
this._origin = 'right'; | ||
constructor(private _elementRef: ElementRef, | ||
@Optional() private _dir: Directionality, | ||
// TODO(paul): make the changeDetectorRef required when doing breaking changes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mark this with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure about that because |
||
changeDetectorRef?: ChangeDetectorRef) { | ||
|
||
if (this._dir && changeDetectorRef) { | ||
this._dir.change.subscribe(dir => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this assign to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Missed that. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
this._computePositionAnimationState(dir); | ||
changeDetectorRef.markForCheck(); | ||
}); | ||
} | ||
} | ||
_origin: MatTabBodyOriginState; | ||
|
||
constructor(private _elementRef: ElementRef, | ||
@Optional() private _dir: Directionality) { } | ||
|
||
/** | ||
* After initialized, check if the content is centered and has an origin. If so, set the | ||
* special position states that transition the tab from the left or right before centering. | ||
*/ | ||
ngOnInit() { | ||
if (this._position == 'center' && this._origin) { | ||
this._position = this._origin == 'left' ? 'left-origin-center' : 'right-origin-center'; | ||
if (this._position == 'center' && this.origin !== undefined) { | ||
this._position = this._computePositionFromOrigin(); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this._dirChangeSubscription.unsubscribe(); | ||
} | ||
|
||
_onTranslateTabStarted(e: AnimationEvent): void { | ||
const isCentering = this._isCenterPosition(e.toState); | ||
this._beforeCentering.emit(isCentering); | ||
|
@@ -202,4 +210,29 @@ export class MatTabBody implements OnInit { | |
position == 'left-origin-center' || | ||
position == 'right-origin-center'; | ||
} | ||
|
||
/** Computes the position state that will be used for the tab-body animation trigger. */ | ||
private _computePositionAnimationState(dir: Direction = this._getLayoutDirection()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I can tell you're never passing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
if (this._positionIndex < 0) { | ||
this._position = dir == 'ltr' ? 'left' : 'right'; | ||
} else if (this._positionIndex > 0) { | ||
this._position = dir == 'ltr' ? 'right' : 'left'; | ||
} else { | ||
this._position = 'center'; | ||
} | ||
} | ||
|
||
/** | ||
* Computes the position state based on the specified origin position. This is used if the | ||
* tab is becoming visible immediately after creation. | ||
*/ | ||
private _computePositionFromOrigin(): MatTabBodyPositionState { | ||
const dir = this._getLayoutDirection(); | ||
|
||
if ((dir == 'ltr' && this.origin <= 0) || (dir == 'rtl' && this.origin > 0)) { | ||
return 'left-origin-center'; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just took that logic from above, but makes sense. Will improve it. |
||
return 'right-origin-center'; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Subscription
type here is inferred already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.