Skip to content

Commit 5556029

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 aff565a commit 5556029

File tree

3 files changed

+118
-6
lines changed

3 files changed

+118
-6
lines changed

src/lib/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/lib/tooltip/tooltip.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
SCROLL_THROTTLE_MS,
3737
TOOLTIP_PANEL_CLASS,
3838
MAT_TOOLTIP_DEFAULT_OPTIONS,
39+
TooltipPosition,
3940
} from './index';
4041

4142

@@ -691,6 +692,75 @@ describe('MatTooltip', () => {
691692
expect(overlayRef.detach).not.toHaveBeenCalled();
692693
}));
693694

695+
it('should set a class on the overlay panel that reflects the position', fakeAsync(() => {
696+
// Move the element so that the primary position is always used.
697+
buttonElement.style.position = 'fixed';
698+
buttonElement.style.top = buttonElement.style.left = '100px';
699+
700+
setPositionAndShow('below');
701+
702+
const classList = tooltipDirective._overlayRef!.overlayElement.classList;
703+
expect(classList).toContain('mat-tooltip-panel-below');
704+
705+
setPositionAndShow('above');
706+
expect(classList).not.toContain('mat-tooltip-panel-below');
707+
expect(classList).toContain('mat-tooltip-panel-above');
708+
709+
setPositionAndShow('left');
710+
expect(classList).not.toContain('mat-tooltip-panel-above');
711+
expect(classList).toContain('mat-tooltip-panel-left');
712+
713+
setPositionAndShow('right');
714+
expect(classList).not.toContain('mat-tooltip-panel-left');
715+
expect(classList).toContain('mat-tooltip-panel-right');
716+
717+
function setPositionAndShow(position: TooltipPosition) {
718+
tooltipDirective.hide(0);
719+
fixture.detectChanges();
720+
tick(0);
721+
tooltipDirective.position = position;
722+
tooltipDirective.show(0);
723+
fixture.detectChanges();
724+
tick(0);
725+
fixture.detectChanges();
726+
tick(500);
727+
}
728+
}));
729+
730+
it('should account for RTL when setting the tooltip position class', fakeAsync(() => {
731+
// Move the element so that the primary position is always used.
732+
buttonElement.style.position = 'fixed';
733+
buttonElement.style.top = buttonElement.style.left = '100px';
734+
dir.value = 'ltr';
735+
tooltipDirective.position = 'after';
736+
tooltipDirective.show(0);
737+
fixture.detectChanges();
738+
tick(0);
739+
fixture.detectChanges();
740+
tick(500);
741+
742+
const classList = tooltipDirective._overlayRef!.overlayElement.classList;
743+
expect(classList).not.toContain('mat-tooltip-panel-after');
744+
expect(classList).not.toContain('mat-tooltip-panel-before');
745+
expect(classList).not.toContain('mat-tooltip-panel-left');
746+
expect(classList).toContain('mat-tooltip-panel-right');
747+
748+
tooltipDirective.hide(0);
749+
fixture.detectChanges();
750+
tick(0);
751+
dir.value = 'rtl';
752+
tooltipDirective.show(0);
753+
fixture.detectChanges();
754+
tick(0);
755+
fixture.detectChanges();
756+
tick(500);
757+
758+
expect(classList).not.toContain('mat-tooltip-panel-after');
759+
expect(classList).not.toContain('mat-tooltip-panel-before');
760+
expect(classList).not.toContain('mat-tooltip-panel-right');
761+
expect(classList).toContain('mat-tooltip-panel-left');
762+
}));
763+
694764
});
695765

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

src/lib/tooltip/tooltip.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
OverlayRef,
2222
VerticalConnectionPos,
2323
ScrollStrategy,
24+
ConnectionPositionPair,
2425
} from '@angular/cdk/overlay';
2526
import {ScrollDispatcher} from '@angular/cdk/scrolling';
2627
import {Platform} from '@angular/cdk/platform';
@@ -125,6 +126,7 @@ export class MatTooltip implements OnDestroy {
125126
private _disabled: boolean = false;
126127
private _tooltipClass: string|string[]|Set<string>|{[key: string]: any};
127128
private _scrollStrategy: () => ScrollStrategy;
129+
private _currentPosition: TooltipPosition;
128130

129131
/** Allows the user to define the position of the tooltip relative to the parent element */
130132
@Input('matTooltipPosition')
@@ -352,6 +354,8 @@ export class MatTooltip implements OnDestroy {
352354
strategy.withScrollableContainers(scrollableAncestors);
353355

354356
strategy.positionChanges.pipe(takeUntil(this._destroyed)).subscribe(change => {
357+
this._updateCurrentPositionClass(change.connectionPair);
358+
355359
if (this._tooltipInstance) {
356360
if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) {
357361
// After position changes occur and the overlay is clipped by
@@ -509,6 +513,39 @@ export class MatTooltip implements OnDestroy {
509513

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

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

0 commit comments

Comments
 (0)