Skip to content

Revert "Revert "feat(material/tooltip): add option to open tooltip at… #25439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
_preferredPositions: ConnectionPositionPair[] = [];

/** The origin element against which the overlay will be positioned. */
private _origin: FlexibleConnectedPositionStrategyOrigin;
_origin: FlexibleConnectedPositionStrategyOrigin;

/** The overlay pane element. */
private _pane: HTMLElement;
Expand Down
3 changes: 3 additions & 0 deletions src/components-examples/material/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {TooltipMessageExample} from './tooltip-message/tooltip-message-example';
import {TooltipModifiedDefaultsExample} from './tooltip-modified-defaults/tooltip-modified-defaults-example';
import {TooltipOverviewExample} from './tooltip-overview/tooltip-overview-example';
import {TooltipPositionExample} from './tooltip-position/tooltip-position-example';
import {TooltipPositionAtOriginExample} from './tooltip-position-at-origin/tooltip-position-at-origin-example';
import {TooltipHarnessExample} from './tooltip-harness/tooltip-harness-example';

export {
Expand All @@ -29,6 +30,7 @@ export {
TooltipModifiedDefaultsExample,
TooltipOverviewExample,
TooltipPositionExample,
TooltipPositionAtOriginExample,
};

const EXAMPLES = [
Expand All @@ -42,6 +44,7 @@ const EXAMPLES = [
TooltipModifiedDefaultsExample,
TooltipOverviewExample,
TooltipPositionExample,
TooltipPositionAtOriginExample,
];

@NgModule({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
button {
width: 500px;
height: 500px;
}

.example-enabled-checkbox {
margin-left: 8px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<button mat-raised-button
matTooltip="Info about the action"
[matTooltipPositionAtOrigin]="enabled.value"
aria-label="Button that displays a tooltip when focused or hovered over">
Action
</button>

<mat-checkbox [formControl]="enabled" class="example-enabled-checkbox">
Position at origin enabled
</mat-checkbox>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';

/**
* @title Basic tooltip
*/
@Component({
selector: 'tooltip-position-at-origin-example',
templateUrl: 'tooltip-position-at-origin-example.html',
styleUrls: ['tooltip-position-at-origin-example.css'],
})
export class TooltipPositionAtOriginExample {
enabled = new FormControl(false);
}
3 changes: 3 additions & 0 deletions src/dev-app/tooltip/tooltip-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ <h3>Tooltip overview</h3>

<h3>Tooltip positioning</h3>
<tooltip-position-example></tooltip-position-example>

<h3>Tooltip with position at origin</h3>
<tooltip-position-at-origin-example></tooltip-position-at-origin-example>
82 changes: 82 additions & 0 deletions src/material/legacy-tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,77 @@ describe('MatTooltip', () => {
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
}));

it('should position center-bottom by default', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatLegacyTooltipModule, OverlayModule],
declarations: [WideTooltipDemo],
})
.compileComponents();

const wideFixture = TestBed.createComponent(WideTooltipDemo);
wideFixture.detectChanges();
tooltipDirective = wideFixture.debugElement
.query(By.css('button'))!
.injector.get<MatLegacyTooltip>(MatLegacyTooltip);
const button: HTMLButtonElement = wideFixture.nativeElement.querySelector('button');
const triggerRect = button.getBoundingClientRect();

dispatchMouseEvent(button, 'mouseenter', triggerRect.right - 100, triggerRect.top + 100);
wideFixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(true);

expect(tooltipDirective._overlayRef!.overlayElement.offsetLeft).toBeGreaterThan(
triggerRect.left + 200,
);
expect(tooltipDirective._overlayRef!.overlayElement.offsetLeft).toBeLessThan(
triggerRect.left + 300,
);
expect(tooltipDirective._overlayRef!.overlayElement.offsetTop).toBe(triggerRect.bottom);
}));

it('should be able to override the default positionAtOrigin', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatLegacyTooltipModule, OverlayModule],
declarations: [WideTooltipDemo],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {positionAtOrigin: true},
},
],
})
.compileComponents();

const wideFixture = TestBed.createComponent(WideTooltipDemo);
wideFixture.detectChanges();
tooltipDirective = wideFixture.debugElement
.query(By.css('button'))!
.injector.get<MatLegacyTooltip>(MatLegacyTooltip);
const button: HTMLButtonElement = wideFixture.nativeElement.querySelector('button');
const triggerRect = button.getBoundingClientRect();

dispatchMouseEvent(button, 'mouseenter', triggerRect.left + 50, triggerRect.bottom - 10);
wideFixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(true);

expect(tooltipDirective._overlayRef!.overlayElement.offsetLeft).toBe(triggerRect.left + 28);
expect(tooltipDirective._overlayRef!.overlayElement.offsetTop).toBe(triggerRect.bottom - 10);
}));

it('should be able to disable tooltip interactivity', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
Expand Down Expand Up @@ -1556,6 +1627,17 @@ class TooltipDemoWithoutPositionBinding {
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
}

@Component({
selector: 'app',
styles: [`button { width: 500px; height: 500px; }`],
template: `<button #button [matTooltip]="message">Button</button>`,
})
class WideTooltipDemo {
message = 'Test';
@ViewChild(MatLegacyTooltip) tooltip: MatLegacyTooltip;
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
}

/** Asserts whether a tooltip directive has a tooltip instance. */
function assertTooltipInstance(tooltip: MatLegacyTooltip, shouldExist: boolean): void {
// Note that we have to cast this to a boolean, because Jasmine will go into an infinite loop
Expand Down
6 changes: 6 additions & 0 deletions src/material/tooltip/tooltip.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ CSS class that can be used for style (e.g. to add an arrow). The possible classe

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

To display the tooltip relative to the mouse or touch that triggered it, use the
`matTooltipPositionAtOrigin` input.
With this setting turned on, the tooltip will display relative to the origin of the trigger rather
than the host element. In cases where the tooltip is not triggered by a touch event or mouse click,
it will display the same as if this setting was turned off.

### Showing and hiding

By default, the tooltip will be immediately shown when the user's mouse hovers over the tooltip's
Expand Down
83 changes: 83 additions & 0 deletions src/material/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,78 @@ describe('MDC-based MatTooltip', () => {
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
}));

it('should position on the bottom-left by default', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule],
declarations: [WideTooltipDemo],
})
.compileComponents();

const wideFixture = TestBed.createComponent(WideTooltipDemo);
wideFixture.detectChanges();
tooltipDirective = wideFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);
const button: HTMLButtonElement = wideFixture.nativeElement.querySelector('button');
const triggerRect = button.getBoundingClientRect();

dispatchMouseEvent(button, 'mouseenter', triggerRect.right - 100, triggerRect.top + 100);
wideFixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(true);

expect(tooltipDirective._overlayRef!.overlayElement.offsetLeft).toBeLessThan(
triggerRect.right - 250,
);
expect(tooltipDirective._overlayRef!.overlayElement.offsetTop).toBeGreaterThanOrEqual(
triggerRect.bottom,
);
}));

it('should be able to override the default positionAtOrigin', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}

TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule],
declarations: [WideTooltipDemo],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {positionAtOrigin: true},
},
],
})
.compileComponents();

const wideFixture = TestBed.createComponent(WideTooltipDemo);
wideFixture.detectChanges();
tooltipDirective = wideFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);
const button: HTMLButtonElement = wideFixture.nativeElement.querySelector('button');
const triggerRect = button.getBoundingClientRect();

dispatchMouseEvent(button, 'mouseenter', triggerRect.right - 100, triggerRect.top + 100);
wideFixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(true);

expect(tooltipDirective._overlayRef!.overlayElement.offsetLeft).toBe(
triggerRect.right - 100 - 20,
);
expect(tooltipDirective._overlayRef!.overlayElement.offsetTop).toBe(triggerRect.top + 100);
}));

it('should be able to disable tooltip interactivity', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
Expand Down Expand Up @@ -1588,6 +1660,17 @@ class TooltipDemoWithoutPositionBinding {
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
}

@Component({
selector: 'app',
styles: [`button { width: 500px; height: 500px; }`],
template: `<button #button [matTooltip]="message">Button</button>`,
})
class WideTooltipDemo {
message = 'Test';
@ViewChild(MatTooltip) tooltip: MatTooltip;
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
}

/** Asserts whether a tooltip directive has a tooltip instance. */
function assertTooltipInstance(tooltip: MatTooltip, shouldExist: boolean): void {
// Note that we have to cast this to a boolean, because Jasmine will go into an infinite loop
Expand Down
Loading