Skip to content

Commit c66f6ef

Browse files
committed
feat(tooltip): add class to tooltip element based on the current position
Adds a class on the tooltip overlay element that indicates the current position of the tooltip. This allows for the tooltip to be customized to add position-based arrows or box shadows. Fixes angular#15216.
1 parent f0c7a25 commit c66f6ef

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

src/material/tooltip/tooltip.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ the positions `before` and `after` should be used instead of `left` and `right`,
1414
| Position | Description |
1515
|-----------|--------------------------------------------------------------------------------------|
1616
| `above` | Always display above the element |
17-
| `below ` | Always display beneath the element |
17+
| `below` | Always display beneath the element |
1818
| `left` | Always display to the left of the element |
1919
| `right` | Always display to the right of the element |
2020
| `before` | Display to the left in left-to-right layout and to the right in right-to-left layout |
21-
| `after` | Display to the right in left-to-right layout and to the left in right-to-left layout|
21+
| `after` | Display to the right in left-to-right layout and to the left in right-to-left layout |
22+
23+
Based on the position in which the tooltip is shown, the `.mat-tooltip-panel` element will receive a
24+
CSS class that can be used for style (e.g. to add an arrow). The possible classes are
25+
`mat-tooltip-panel-above`, `mat-tooltip-panel-below`, `mat-tooltip-panel-left`,
26+
`mat-tooltip-panel-right`.
2227

2328
<!-- example(tooltip-position) -->
2429

src/material/tooltip/tooltip.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
TOOLTIP_PANEL_CLASS,
4040
MAT_TOOLTIP_DEFAULT_OPTIONS,
4141
TooltipTouchGestures,
42+
TooltipPosition,
4243
} from './index';
4344

4445

@@ -731,6 +732,80 @@ describe('MatTooltip', () => {
731732
expect(overlayRef.detach).not.toHaveBeenCalled();
732733
}));
733734

735+
it('should set a class on the overlay panel that reflects the position', fakeAsync(() => {
736+
// Move the element so that the primary position is always used.
737+
buttonElement.style.position = 'fixed';
738+
buttonElement.style.top = buttonElement.style.left = '200px';
739+
740+
fixture.componentInstance.message = 'hi';
741+
fixture.detectChanges();
742+
setPositionAndShow('below');
743+
744+
const classList = tooltipDirective._overlayRef!.overlayElement.classList;
745+
expect(classList).toContain('mat-tooltip-panel-below');
746+
747+
setPositionAndShow('above');
748+
expect(classList).not.toContain('mat-tooltip-panel-below');
749+
expect(classList).toContain('mat-tooltip-panel-above');
750+
751+
setPositionAndShow('left');
752+
expect(classList).not.toContain('mat-tooltip-panel-above');
753+
expect(classList).toContain('mat-tooltip-panel-left');
754+
755+
setPositionAndShow('right');
756+
expect(classList).not.toContain('mat-tooltip-panel-left');
757+
expect(classList).toContain('mat-tooltip-panel-right');
758+
759+
function setPositionAndShow(position: TooltipPosition) {
760+
tooltipDirective.hide(0);
761+
fixture.detectChanges();
762+
tick(0);
763+
tooltipDirective.position = position;
764+
tooltipDirective.show(0);
765+
fixture.detectChanges();
766+
tick(0);
767+
fixture.detectChanges();
768+
tick(500);
769+
}
770+
}));
771+
772+
it('should account for RTL when setting the tooltip position class', fakeAsync(() => {
773+
// Move the element so that the primary position is always used.
774+
buttonElement.style.position = 'fixed';
775+
buttonElement.style.top = buttonElement.style.left = '200px';
776+
fixture.componentInstance.message = 'hi';
777+
fixture.detectChanges();
778+
779+
dir.value = 'ltr';
780+
tooltipDirective.position = 'after';
781+
tooltipDirective.show(0);
782+
fixture.detectChanges();
783+
tick(0);
784+
fixture.detectChanges();
785+
tick(500);
786+
787+
const classList = tooltipDirective._overlayRef!.overlayElement.classList;
788+
expect(classList).not.toContain('mat-tooltip-panel-after');
789+
expect(classList).not.toContain('mat-tooltip-panel-before');
790+
expect(classList).not.toContain('mat-tooltip-panel-left');
791+
expect(classList).toContain('mat-tooltip-panel-right');
792+
793+
tooltipDirective.hide(0);
794+
fixture.detectChanges();
795+
tick(0);
796+
dir.value = 'rtl';
797+
tooltipDirective.show(0);
798+
fixture.detectChanges();
799+
tick(0);
800+
fixture.detectChanges();
801+
tick(500);
802+
803+
expect(classList).not.toContain('mat-tooltip-panel-after');
804+
expect(classList).not.toContain('mat-tooltip-panel-before');
805+
expect(classList).not.toContain('mat-tooltip-panel-right');
806+
expect(classList).toContain('mat-tooltip-panel-left');
807+
}));
808+
734809
});
735810

736811
describe('fallback positions', () => {

src/material/tooltip/tooltip.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
OverlayRef,
2121
ScrollStrategy,
2222
VerticalConnectionPos,
23+
ConnectionPositionPair,
2324
} from '@angular/cdk/overlay';
2425
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
2526
import {ComponentPortal} from '@angular/cdk/portal';
@@ -146,6 +147,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
146147
private _scrollStrategy: () => ScrollStrategy;
147148
private _viewInitialized = false;
148149
private _pointerExitEventsInitialized = false;
150+
private _currentPosition: TooltipPosition;
149151

150152
/** Allows the user to define the position of the tooltip relative to the parent element */
151153
@Input('matTooltipPosition')
@@ -390,6 +392,8 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
390392
.withScrollableContainers(scrollableAncestors);
391393

392394
strategy.positionChanges.pipe(takeUntil(this._destroyed)).subscribe(change => {
395+
this._updateCurrentPositionClass(change.connectionPair);
396+
393397
if (this._tooltipInstance) {
394398
if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) {
395399
// After position changes occur and the overlay is clipped by
@@ -548,6 +552,39 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
548552
return {x, y};
549553
}
550554

555+
/** Updates the class on the overlay panel based on the current position of the tooltip. */
556+
private _updateCurrentPositionClass(connectionPair: ConnectionPositionPair): void {
557+
const {overlayY, originX, originY} = connectionPair;
558+
let newPosition: TooltipPosition;
559+
560+
// If the overlay is in the middle along the Y axis,
561+
// it means that it's either before or after.
562+
if (overlayY === 'center') {
563+
// Note that since this information is used for styling, we want to
564+
// resolve `start` and `end` to their real values, otherwise consumers
565+
// would have to remember to do it themselves on each consumption.
566+
if (this._dir && this._dir.value === 'rtl') {
567+
newPosition = originX === 'end' ? 'left' : 'right';
568+
} else {
569+
newPosition = originX === 'start' ? 'left' : 'right';
570+
}
571+
} else {
572+
newPosition = overlayY === 'bottom' && originY === 'top' ? 'above' : 'below';
573+
}
574+
575+
if (newPosition !== this._currentPosition) {
576+
const overlayRef = this._overlayRef;
577+
578+
if (overlayRef) {
579+
const classPrefix = 'mat-tooltip-panel-';
580+
overlayRef.removePanelClass(classPrefix + this._currentPosition);
581+
overlayRef.addPanelClass(classPrefix + newPosition);
582+
}
583+
584+
this._currentPosition = newPosition;
585+
}
586+
}
587+
551588
/** Binds the pointer events to the tooltip trigger. */
552589
private _setupPointerEnterEventsIfNeeded() {
553590
// Optimization: Defer hooking up events if there's no message or the tooltip is disabled.

0 commit comments

Comments
 (0)