Skip to content

Commit 59818d1

Browse files
matskommalerba
authored andcommitted
fix(tooltip): render style values in ngOnInit instead of the constructor (#15469)
Due to changes in Angular it's not logical to change style values directly within a constructor. This should be done instead within the ngOnInit lifecycle hook. This patch changes this behavior for the `ToolTip` component.
1 parent a146499 commit 59818d1

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

src/lib/tooltip/tooltip.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@ import {Directionality} from '@angular/cdk/bidi';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1212
import {ESCAPE} from '@angular/cdk/keycodes';
1313
import {BreakpointObserver, Breakpoints, BreakpointState} from '@angular/cdk/layout';
14-
import {HammerLoader, HAMMER_LOADER} from '@angular/platform-browser';
1514
import {
1615
FlexibleConnectedPositionStrategy,
1716
HorizontalConnectionPos,
1817
OriginConnectionPosition,
1918
Overlay,
2019
OverlayConnectionPosition,
2120
OverlayRef,
22-
VerticalConnectionPos,
2321
ScrollStrategy,
22+
VerticalConnectionPos,
2423
} from '@angular/cdk/overlay';
25-
import {ScrollDispatcher} from '@angular/cdk/scrolling';
2624
import {Platform} from '@angular/cdk/platform';
2725
import {ComponentPortal} from '@angular/cdk/portal';
28-
import {take, takeUntil} from 'rxjs/operators';
26+
import {ScrollDispatcher} from '@angular/cdk/scrolling';
2927
import {
3028
ChangeDetectionStrategy,
3129
ChangeDetectorRef,
@@ -37,11 +35,15 @@ import {
3735
Input,
3836
NgZone,
3937
OnDestroy,
38+
OnInit,
4039
Optional,
4140
ViewContainerRef,
4241
ViewEncapsulation,
4342
} from '@angular/core';
44-
import {Subject, Observable} from 'rxjs';
43+
import {HAMMER_LOADER, HammerLoader} from '@angular/platform-browser';
44+
import {Observable, Subject} from 'rxjs';
45+
import {take, takeUntil} from 'rxjs/operators';
46+
4547
import {matTooltipAnimations} from './tooltip-animations';
4648

4749

@@ -116,7 +118,7 @@ export function MAT_TOOLTIP_DEFAULT_OPTIONS_FACTORY(): MatTooltipDefaultOptions
116118
'(touchend)': '_handleTouchend()',
117119
},
118120
})
119-
export class MatTooltip implements OnDestroy {
121+
export class MatTooltip implements OnDestroy, OnInit {
120122
_overlayRef: OverlayRef | null;
121123
_tooltipInstance: TooltipComponent | null;
122124

@@ -214,7 +216,6 @@ export class MatTooltip implements OnDestroy {
214216

215217
this._scrollStrategy = scrollStrategy;
216218
const element: HTMLElement = _elementRef.nativeElement;
217-
const elementStyle = element.style as CSSStyleDeclaration & {webkitUserDrag: string};
218219
const hasGestures = typeof window === 'undefined' || (window as any).Hammer || hammerLoader;
219220

220221
// The mouse events shouldn't be bound on mobile devices, because they can prevent the
@@ -231,6 +232,27 @@ export class MatTooltip implements OnDestroy {
231232

232233
this._manualListeners.forEach((listener, event) => element.addEventListener(event, listener));
233234

235+
_focusMonitor.monitor(_elementRef).pipe(takeUntil(this._destroyed)).subscribe(origin => {
236+
// Note that the focus monitor runs outside the Angular zone.
237+
if (!origin) {
238+
_ngZone.run(() => this.hide(0));
239+
} else if (origin === 'keyboard') {
240+
_ngZone.run(() => this.show());
241+
}
242+
});
243+
244+
if (_defaultOptions && _defaultOptions.position) {
245+
this.position = _defaultOptions.position;
246+
}
247+
}
248+
249+
/**
250+
* Setup styling-specific things
251+
*/
252+
ngOnInit() {
253+
const element = this._elementRef.nativeElement;
254+
const elementStyle = element.style as CSSStyleDeclaration & {webkitUserDrag: string};
255+
234256
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
235257
// When we bind a gesture event on an element (in this case `longpress`), HammerJS
236258
// will add some inline styles by default, including `user-select: none`. This is
@@ -246,19 +268,6 @@ export class MatTooltip implements OnDestroy {
246268
if (element.draggable && elementStyle.webkitUserDrag === 'none') {
247269
elementStyle.webkitUserDrag = '';
248270
}
249-
250-
_focusMonitor.monitor(_elementRef).pipe(takeUntil(this._destroyed)).subscribe(origin => {
251-
// Note that the focus monitor runs outside the Angular zone.
252-
if (!origin) {
253-
_ngZone.run(() => this.hide(0));
254-
} else if (origin === 'keyboard') {
255-
_ngZone.run(() => this.show());
256-
}
257-
});
258-
259-
if (_defaultOptions && _defaultOptions.position) {
260-
this.position = _defaultOptions.position;
261-
}
262271
}
263272

264273
/**

tools/public_api_guard/lib/tooltip.d.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,32 @@ export declare const MAT_TOOLTIP_SCROLL_STRATEGY_FACTORY_PROVIDER: {
1414
useFactory: typeof MAT_TOOLTIP_SCROLL_STRATEGY_FACTORY;
1515
};
1616

17-
export declare class MatTooltip implements OnDestroy {
18-
_overlayRef: OverlayRef | null;
19-
_tooltipInstance: TooltipComponent | null;
20-
disabled: boolean;
21-
hideDelay: number;
22-
message: string;
23-
position: TooltipPosition;
24-
showDelay: number;
25-
tooltipClass: string | string[] | Set<string> | {
26-
[key: string]: any;
27-
};
28-
constructor(_overlay: Overlay, _elementRef: ElementRef<HTMLElement>, _scrollDispatcher: ScrollDispatcher, _viewContainerRef: ViewContainerRef, _ngZone: NgZone, platform: Platform, _ariaDescriber: AriaDescriber, _focusMonitor: FocusMonitor, scrollStrategy: any, _dir: Directionality, _defaultOptions: MatTooltipDefaultOptions, hammerLoader?: HammerLoader);
29-
_getOrigin(): {
30-
main: OriginConnectionPosition;
31-
fallback: OriginConnectionPosition;
32-
};
33-
_getOverlayPosition(): {
34-
main: OverlayConnectionPosition;
35-
fallback: OverlayConnectionPosition;
36-
};
37-
_handleKeydown(e: KeyboardEvent): void;
38-
_handleTouchend(): void;
39-
_isTooltipVisible(): boolean;
40-
hide(delay?: number): void;
41-
ngOnDestroy(): void;
42-
show(delay?: number): void;
43-
toggle(): void;
17+
export declare class MatTooltip implements OnDestroy, OnInit {
18+
_overlayRef: OverlayRef|null;
19+
_tooltipInstance: TooltipComponent|null;
20+
disabled: boolean;
21+
hideDelay: number;
22+
message: string;
23+
position: TooltipPosition;
24+
showDelay: number;
25+
tooltipClass: string|string[]|Set<string>|{
26+
[key: string]: any;
27+
};
28+
constructor(
29+
_overlay: Overlay, _elementRef: ElementRef<HTMLElement>, _scrollDispatcher: ScrollDispatcher,
30+
_viewContainerRef: ViewContainerRef, _ngZone: NgZone, platform: Platform,
31+
_ariaDescriber: AriaDescriber, _focusMonitor: FocusMonitor, scrollStrategy: any,
32+
_dir: Directionality, _defaultOptions: MatTooltipDefaultOptions, hammerLoader?: HammerLoader);
33+
_getOrigin(): {main: OriginConnectionPosition; fallback: OriginConnectionPosition;};
34+
_getOverlayPosition(): {main: OverlayConnectionPosition; fallback: OverlayConnectionPosition;};
35+
_handleKeydown(e: KeyboardEvent): void;
36+
_handleTouchend(): void;
37+
_isTooltipVisible(): boolean;
38+
hide(delay?: number): void;
39+
ngOnDestroy(): void;
40+
ngOnInit(): void;
41+
show(delay?: number): void;
42+
toggle(): void;
4443
}
4544

4645
export declare const matTooltipAnimations: {

0 commit comments

Comments
 (0)