Skip to content

Commit c0baf8f

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 #15216.
1 parent 4778f49 commit c0baf8f

File tree

3 files changed

+123
-6
lines changed

3 files changed

+123
-6
lines changed

src/material/tooltip/tooltip.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,32 @@ 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

2530
### Showing and hiding
2631

2732
By default, the tooltip will be immediately shown when the user's mouse hovers over the tooltip's
28-
trigger element and immediately hides when the user's mouse leaves.
33+
trigger element and immediately hides when the user's mouse leaves.
2934

3035
On mobile, the tooltip is displayed when the user longpresses the element and hides after a
3136
delay of 1500ms. The longpress behavior requires HammerJS to be loaded on the page. To learn more
32-
about adding HammerJS to your app, check out the Gesture Support section of the Getting Started
37+
about adding HammerJS to your app, check out the Gesture Support section of the Getting Started
3338
guide.
3439

3540
#### Show and hide delays
3641

37-
To add a delay before showing or hiding the tooltip, you can use the inputs `matTooltipShowDelay`
42+
To add a delay before showing or hiding the tooltip, you can use the inputs `matTooltipShowDelay`
3843
and `matTooltipHideDelay` to provide a delay time in milliseconds.
3944

4045
The following example has a tooltip that waits one second to display after the user
@@ -58,7 +63,7 @@ which both accept a number in milliseconds to delay before applying the display
5863

5964
#### Disabling the tooltip from showing
6065

61-
To completely disable a tooltip, set `matTooltipDisabled`. While disabled, a tooltip will never be
66+
To completely disable a tooltip, set `matTooltipDisabled`. While disabled, a tooltip will never be
6267
shown.
6368

6469
### Accessibility

src/material/tooltip/tooltip.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
SCROLL_THROTTLE_MS,
3939
TOOLTIP_PANEL_CLASS,
4040
MAT_TOOLTIP_DEFAULT_OPTIONS,
41+
TooltipPosition,
4142
} from './index';
4243

4344

@@ -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} from '@angular/cdk/platform';
2526
import {ComponentPortal} from '@angular/cdk/portal';
@@ -127,6 +128,7 @@ export class MatTooltip implements OnDestroy, OnInit {
127128
private _disabled: boolean = false;
128129
private _tooltipClass: string|string[]|Set<string>|{[key: string]: any};
129130
private _scrollStrategy: () => ScrollStrategy;
131+
private _currentPosition: TooltipPosition;
130132

131133
/** Allows the user to define the position of the tooltip relative to the parent element */
132134
@Input('matTooltipPosition')
@@ -369,6 +371,8 @@ export class MatTooltip implements OnDestroy, OnInit {
369371
.withScrollableContainers(scrollableAncestors);
370372

371373
strategy.positionChanges.pipe(takeUntil(this._destroyed)).subscribe(change => {
374+
this._updateCurrentPositionClass(change.connectionPair);
375+
372376
if (this._tooltipInstance) {
373377
if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) {
374378
// After position changes occur and the overlay is clipped by
@@ -526,6 +530,39 @@ export class MatTooltip implements OnDestroy, OnInit {
526530

527531
return {x, y};
528532
}
533+
534+
/** Updates the class on the overlay panel based on the current position of the tooltip. */
535+
private _updateCurrentPositionClass(connectionPair: ConnectionPositionPair): void {
536+
const {overlayY, originX, originY} = connectionPair;
537+
let newPosition: TooltipPosition;
538+
539+
// If the overlay is in the middle along the Y axis,
540+
// it means that it's either before or after.
541+
if (overlayY === 'center') {
542+
// Note that since this information is used for styling, we want to
543+
// resolve `start` and `end` to their real values, otherwise consumers
544+
// would have to remember to do it themselves on each consumption.
545+
if (this._dir && this._dir.value === 'rtl') {
546+
newPosition = originX === 'end' ? 'left' : 'right';
547+
} else {
548+
newPosition = originX === 'start' ? 'left' : 'right';
549+
}
550+
} else {
551+
newPosition = overlayY === 'bottom' && originY === 'top' ? 'above' : 'below';
552+
}
553+
554+
if (newPosition !== this._currentPosition) {
555+
const overlayRef = this._overlayRef;
556+
557+
if (overlayRef) {
558+
const classPrefix = 'mat-tooltip-panel-';
559+
overlayRef.removePanelClass(classPrefix + this._currentPosition);
560+
overlayRef.addPanelClass(classPrefix + newPosition);
561+
}
562+
563+
this._currentPosition = newPosition;
564+
}
565+
}
529566
}
530567

531568
export type TooltipVisibility = 'initial' | 'visible' | 'hidden';

0 commit comments

Comments
 (0)