Skip to content

Commit 630925e

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 8daaf4d commit 630925e

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

@@ -719,6 +720,80 @@ describe('MatTooltip', () => {
719720
expect(overlayRef.detach).not.toHaveBeenCalled();
720721
}));
721722

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

724799
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')
@@ -361,6 +363,8 @@ export class MatTooltip implements OnDestroy, OnInit {
361363
.withScrollableContainers(scrollableAncestors);
362364

363365
strategy.positionChanges.pipe(takeUntil(this._destroyed)).subscribe(change => {
366+
this._updateCurrentPositionClass(change.connectionPair);
367+
364368
if (this._tooltipInstance) {
365369
if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) {
366370
// After position changes occur and the overlay is clipped by
@@ -518,6 +522,39 @@ export class MatTooltip implements OnDestroy, OnInit {
518522

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

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

0 commit comments

Comments
 (0)