Skip to content

Commit 342e6f0

Browse files
crisbetoandrewseguin
authored andcommittedJan 23, 2019
feat(tooltip): allow for default position to be configured (#14872)
Allows for the default position of `MatTooltip` to be set via an injection token. Fixes #14862.
1 parent 5542d0a commit 342e6f0

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
 

‎src/lib/tooltip/tooltip.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,33 @@ describe('MatTooltip', () => {
204204
expect(tooltipDirective._isTooltipVisible()).toBe(false);
205205
}));
206206

207+
it('should be able to override the default position', fakeAsync(() => {
208+
TestBed
209+
.resetTestingModule()
210+
.configureTestingModule({
211+
imports: [MatTooltipModule, OverlayModule, NoopAnimationsModule],
212+
declarations: [TooltipDemoWithoutPositionBinding],
213+
providers: [{
214+
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
215+
useValue: {position: 'right'}
216+
}]
217+
})
218+
.compileComponents();
219+
220+
const newFixture = TestBed.createComponent(TooltipDemoWithoutPositionBinding);
221+
newFixture.detectChanges();
222+
tooltipDirective = newFixture.debugElement.query(By.css('button'))
223+
.injector.get<MatTooltip>(MatTooltip);
224+
225+
tooltipDirective.show();
226+
newFixture.detectChanges();
227+
tick();
228+
229+
expect(tooltipDirective.position).toBe('right');
230+
expect(tooltipDirective._getOverlayPosition().main.overlayX).toBe('start');
231+
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
232+
}));
233+
207234
it('should set a css class on the overlay panel element', fakeAsync(() => {
208235
tooltipDirective.show();
209236
fixture.detectChanges();
@@ -995,6 +1022,15 @@ class TooltipOnDraggableElement {
9951022
@ViewChild('button') button: ElementRef;
9961023
}
9971024

1025+
@Component({
1026+
selector: 'app',
1027+
template: `<button #button [matTooltip]="message">Button</button>`
1028+
})
1029+
class TooltipDemoWithoutPositionBinding {
1030+
message: any = initialTooltipMessage;
1031+
@ViewChild(MatTooltip) tooltip: MatTooltip;
1032+
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
1033+
}
9981034

9991035
/** Asserts whether a tooltip directive has a tooltip instance. */
10001036
function assertTooltipInstance(tooltip: MatTooltip, shouldExist: boolean): void {

‎src/lib/tooltip/tooltip.ts

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export interface MatTooltipDefaultOptions {
8282
showDelay: number;
8383
hideDelay: number;
8484
touchendHideDelay: number;
85+
position?: TooltipPosition;
8586
}
8687

8788
/** Injection token to be used to override the default options for `matTooltip`. */
@@ -254,6 +255,10 @@ export class MatTooltip implements OnDestroy {
254255
_ngZone.run(() => this.show());
255256
}
256257
});
258+
259+
if (_defaultOptions && _defaultOptions.position) {
260+
this.position = _defaultOptions.position;
261+
}
257262
}
258263

259264
/**

‎tools/public_api_guard/lib/tooltip.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export declare const matTooltipAnimations: {
4949

5050
export interface MatTooltipDefaultOptions {
5151
hideDelay: number;
52+
position?: TooltipPosition;
5253
showDelay: number;
5354
touchendHideDelay: number;
5455
}

0 commit comments

Comments
 (0)
Please sign in to comment.