Skip to content

Commit

Permalink
fix(material/tooltip): don't hide when pointer moves to tooltip (#24475)
Browse files Browse the repository at this point in the history
Currently we hide the tooltip as soon as the pointer leaves the trigger element which may be problematic with larger cursors that partially obstruct the content.

These changes allow hover events on the tooltip and add extra logic so that moving to it doesn't start the hiding sequence.

Fixes #4942.

(cherry picked from commit 0dfc490)
  • Loading branch information
crisbeto committed Feb 25, 2022
1 parent f31fd3f commit 7201953
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 24 deletions.
23 changes: 17 additions & 6 deletions src/material-experimental/mdc-tooltip/tooltip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
@include tooltip.core-styles($query: structure);

.mat-mdc-tooltip {
// We don't use MDC's positioning so this has to be static.
position: static;
// We don't use MDC's positioning so this has to be relative.
position: relative;

// The overlay reference updates the pointer-events style property directly on the HTMLElement
// depending on the state of the overlay. For tooltips the overlay panel should never enable
// pointer events. To overwrite the inline CSS from the overlay reference `!important` is needed.
pointer-events: none !important;
// Increases the area of the tooltip so the user's pointer can go from the trigger directly to it.
&::before {
$offset: -8px;
content: '';
top: $offset;
right: $offset;
bottom: $offset;
left: $offset;
z-index: -1;
position: absolute;
}
}

.mat-mdc-tooltip-panel-non-interactive {
pointer-events: none;
}
115 changes: 115 additions & 0 deletions src/material-experimental/mdc-tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Platform} from '@angular/cdk/platform';
import {
createFakeEvent,
createKeyboardEvent,
createMouseEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
Expand Down Expand Up @@ -237,6 +238,35 @@ describe('MDC-based MatTooltip', () => {
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
}));

it('should be able to disable tooltip interactivity', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule, NoopAnimationsModule],
declarations: [TooltipDemoWithoutPositionBinding],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {disableTooltipInteractivity: true},
},
],
})
.compileComponents();

const newFixture = TestBed.createComponent(TooltipDemoWithoutPositionBinding);
newFixture.detectChanges();
tooltipDirective = newFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);

tooltipDirective.show();
newFixture.detectChanges();
tick();

expect(tooltipDirective._overlayRef?.overlayElement.classList).toContain(
'mat-mdc-tooltip-panel-non-interactive',
);
}));

it('should set a css class on the overlay panel element', fakeAsync(() => {
tooltipDirective.show();
fixture.detectChanges();
Expand Down Expand Up @@ -926,6 +956,91 @@ describe('MDC-based MatTooltip', () => {
expect(tooltipElement.classList).toContain('mdc-tooltip--multiline');
expect(tooltipDirective._tooltipInstance?._isMultiline).toBeTrue();
}));

it('should hide on mouseleave on the trigger', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseleave');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));

it('should not hide on mouseleave if the pointer goes from the trigger to the tooltip', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

const tooltipElement = overlayContainerElement.querySelector(
'.mat-mdc-tooltip',
) as HTMLElement;
const event = createMouseEvent('mouseleave');
Object.defineProperty(event, 'relatedTarget', {value: tooltipElement});

dispatchEvent(fixture.componentInstance.button.nativeElement, event);
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));

it('should hide on mouseleave on the tooltip', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

const tooltipElement = overlayContainerElement.querySelector(
'.mat-mdc-tooltip',
) as HTMLElement;
dispatchMouseEvent(tooltipElement, 'mouseleave');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));

it('should not hide on mouseleave if the pointer goes from the tooltip to the trigger', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

const tooltipElement = overlayContainerElement.querySelector(
'.mat-mdc-tooltip',
) as HTMLElement;
const event = createMouseEvent('mouseleave');
Object.defineProperty(event, 'relatedTarget', {
value: fixture.componentInstance.button.nativeElement,
});

dispatchEvent(tooltipElement, event);
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));
});

describe('fallback positions', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/material-experimental/mdc-tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ export class MatTooltip extends _MatTooltipBase<TooltipComponent> {
// Forces the element to have a layout in IE and Edge. This fixes issues where the element
// won't be rendered if the animations are disabled or there is no web animations polyfill.
'[style.zoom]': '_visibility === "visible" ? 1 : null',
'(mouseleave)': '_handleMouseLeave($event)',
'aria-hidden': 'true',
},
})
export class TooltipComponent extends _TooltipComponentBase {
/* Whether the tooltip text overflows to multiple lines */
_isMultiline: boolean = false;
_isMultiline = false;

constructor(changeDetectorRef: ChangeDetectorRef, private _elementRef: ElementRef) {
super(changeDetectorRef);
Expand Down
11 changes: 4 additions & 7 deletions src/material/tooltip/tooltip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ $margin: 14px;
$handset-horizontal-padding: 16px;
$handset-margin: 24px;

.mat-tooltip-panel {
// The overlay reference updates the pointer-events style property directly on the HTMLElement
// depending on the state of the overlay. For tooltips the overlay panel should never enable
// pointer events. To overwrite the inline CSS from the overlay reference `!important` is needed.
pointer-events: none !important;
}

.mat-tooltip {
color: white;
border-radius: 4px;
Expand All @@ -34,3 +27,7 @@ $handset-margin: 24px;
padding-left: $handset-horizontal-padding;
padding-right: $handset-horizontal-padding;
}

.mat-tooltip-panel-non-interactive {
pointer-events: none;
}
109 changes: 109 additions & 0 deletions src/material/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Platform} from '@angular/cdk/platform';
import {
createFakeEvent,
createKeyboardEvent,
createMouseEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
Expand Down Expand Up @@ -235,6 +236,35 @@ describe('MatTooltip', () => {
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
}));

it('should be able to disable tooltip interactivity', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule, NoopAnimationsModule],
declarations: [TooltipDemoWithoutPositionBinding],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {disableTooltipInteractivity: true},
},
],
})
.compileComponents();

const newFixture = TestBed.createComponent(TooltipDemoWithoutPositionBinding);
newFixture.detectChanges();
tooltipDirective = newFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);

tooltipDirective.show();
newFixture.detectChanges();
tick();

expect(tooltipDirective._overlayRef?.overlayElement.classList).toContain(
'mat-tooltip-panel-non-interactive',
);
}));

it('should set a css class on the overlay panel element', fakeAsync(() => {
tooltipDirective.show();
fixture.detectChanges();
Expand Down Expand Up @@ -903,6 +933,85 @@ describe('MatTooltip', () => {
// throw if we have any timers by the end of the test.
fixture.destroy();
}));

it('should hide on mouseleave on the trigger', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseleave');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));

it('should not hide on mouseleave if the pointer goes from the trigger to the tooltip', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

const tooltipElement = overlayContainerElement.querySelector('.mat-tooltip') as HTMLElement;
const event = createMouseEvent('mouseleave');
Object.defineProperty(event, 'relatedTarget', {value: tooltipElement});

dispatchEvent(fixture.componentInstance.button.nativeElement, event);
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));

it('should hide on mouseleave on the tooltip', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

const tooltipElement = overlayContainerElement.querySelector('.mat-tooltip') as HTMLElement;
dispatchMouseEvent(tooltipElement, 'mouseleave');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));

it('should not hide on mouseleave if the pointer goes from the tooltip to the trigger', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

dispatchMouseEvent(fixture.componentInstance.button.nativeElement, 'mouseenter');
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

const tooltipElement = overlayContainerElement.querySelector('.mat-tooltip') as HTMLElement;
const event = createMouseEvent('mouseleave');
Object.defineProperty(event, 'relatedTarget', {
value: fixture.componentInstance.button.nativeElement,
});

dispatchEvent(tooltipElement, event);
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));
});

describe('fallback positions', () => {
Expand Down
Loading

0 comments on commit 7201953

Please sign in to comment.