Skip to content

Commit 5009ec0

Browse files
fix(module:tooltip,popover,popconfirm): fix hydration error (#8512)
1 parent ee46760 commit 5009ec0

File tree

4 files changed

+34
-66
lines changed

4 files changed

+34
-66
lines changed

components/popconfirm/popconfirm.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
ChangeDetectionStrategy,
1212
ChangeDetectorRef,
1313
Component,
14-
ComponentRef,
1514
Directive,
1615
ElementRef,
1716
EventEmitter,
@@ -22,18 +21,16 @@ import {
2221
Optional,
2322
Output,
2423
QueryList,
25-
Renderer2,
2624
TemplateRef,
2725
ViewChildren,
28-
ViewContainerRef,
2926
ViewEncapsulation
3027
} from '@angular/core';
3128
import { Observable, Subject } from 'rxjs';
3229
import { finalize, first, takeUntil } from 'rxjs/operators';
3330

3431
import { NzButtonModule, NzButtonType } from 'ng-zorro-antd/button';
3532
import { zoomBigMotion } from 'ng-zorro-antd/core/animation';
36-
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
33+
import { NzConfigKey, WithConfig } from 'ng-zorro-antd/core/config';
3734
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
3835
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
3936
import { NzOverlayModule } from 'ng-zorro-antd/core/overlay';
@@ -89,9 +86,6 @@ export class NzPopconfirmDirective extends NzTooltipBaseDirective {
8986
@Output() readonly nzOnCancel = new EventEmitter<void>();
9087
@Output() readonly nzOnConfirm = new EventEmitter<void>();
9188

92-
protected override readonly componentRef: ComponentRef<NzPopconfirmComponent> =
93-
this.hostView.createComponent(NzPopconfirmComponent);
94-
9589
protected override getProxyPropertyMap(): PropertyMapping {
9690
return {
9791
nzOkText: ['nzOkText', () => this.nzOkText],
@@ -108,14 +102,8 @@ export class NzPopconfirmDirective extends NzTooltipBaseDirective {
108102
};
109103
}
110104

111-
constructor(
112-
elementRef: ElementRef,
113-
hostView: ViewContainerRef,
114-
renderer: Renderer2,
115-
@Host() @Optional() noAnimation?: NzNoAnimationDirective,
116-
nzConfigService?: NzConfigService
117-
) {
118-
super(elementRef, hostView, renderer, noAnimation, nzConfigService);
105+
constructor() {
106+
super(NzPopconfirmComponent);
119107
}
120108

121109
/**

components/popover/popover.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,29 @@ import {
1010
ChangeDetectionStrategy,
1111
ChangeDetectorRef,
1212
Component,
13-
ComponentRef,
1413
Directive,
1514
ElementRef,
1615
EventEmitter,
1716
Host,
1817
Input,
1918
Optional,
2019
Output,
21-
Renderer2,
22-
ViewContainerRef,
2320
ViewEncapsulation
2421
} from '@angular/core';
2522

2623
import { zoomBigMotion } from 'ng-zorro-antd/core/animation';
27-
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
24+
import { NzConfigKey, WithConfig } from 'ng-zorro-antd/core/config';
2825
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
2926
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
3027
import { NzOverlayModule } from 'ng-zorro-antd/core/overlay';
3128
import { BooleanInput, NgStyleInterface, NzTSType } from 'ng-zorro-antd/core/types';
3229
import { InputBoolean } from 'ng-zorro-antd/core/util';
3330
import {
34-
isTooltipEmpty,
35-
NzTooltipBaseDirective,
3631
NzToolTipComponent,
32+
NzTooltipBaseDirective,
3733
NzTooltipTrigger,
38-
PropertyMapping
34+
PropertyMapping,
35+
isTooltipEmpty
3936
} from 'ng-zorro-antd/tooltip';
4037

4138
const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'popover';
@@ -71,23 +68,15 @@ export class NzPopoverDirective extends NzTooltipBaseDirective {
7168
// eslint-disable-next-line @angular-eslint/no-output-rename
7269
@Output('nzPopoverVisibleChange') override readonly visibleChange = new EventEmitter<boolean>();
7370

74-
override componentRef: ComponentRef<NzPopoverComponent> = this.hostView.createComponent(NzPopoverComponent);
75-
7671
protected override getProxyPropertyMap(): PropertyMapping {
7772
return {
7873
nzPopoverBackdrop: ['nzBackdrop', () => this.nzPopoverBackdrop],
7974
...super.getProxyPropertyMap()
8075
};
8176
}
8277

83-
constructor(
84-
elementRef: ElementRef,
85-
hostView: ViewContainerRef,
86-
renderer: Renderer2,
87-
@Host() @Optional() noAnimation?: NzNoAnimationDirective,
88-
nzConfigService?: NzConfigService
89-
) {
90-
super(elementRef, hostView, renderer, noAnimation, nzConfigService);
78+
constructor() {
79+
super(NzPopoverComponent);
9180
}
9281
}
9382

components/tooltip/base.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55

66
import { Direction, Directionality } from '@angular/cdk/bidi';
77
import { CdkConnectedOverlay, ConnectedOverlayPositionChange, ConnectionPositionPair } from '@angular/cdk/overlay';
8+
import { isPlatformBrowser } from '@angular/common';
89
import {
910
AfterViewInit,
1011
ChangeDetectorRef,
11-
ComponentRef,
1212
Directive,
1313
ElementRef,
1414
EventEmitter,
1515
OnChanges,
1616
OnDestroy,
1717
OnInit,
1818
Optional,
19+
PLATFORM_ID,
1920
Renderer2,
2021
SimpleChanges,
2122
TemplateRef,
23+
Type,
2224
ViewChild,
23-
ViewContainerRef
25+
ViewContainerRef,
26+
inject
2427
} from '@angular/core';
2528
import { Subject, asapScheduler } from 'rxjs';
2629
import { delay, distinctUntilChanged, filter, takeUntil } from 'rxjs/operators';
@@ -38,7 +41,7 @@ export interface PropertyMapping {
3841
export type NzTooltipTrigger = 'click' | 'focus' | 'hover' | null;
3942

4043
@Directive()
41-
export abstract class NzTooltipBaseDirective implements OnChanges, OnDestroy, AfterViewInit {
44+
export abstract class NzTooltipBaseDirective implements AfterViewInit, OnChanges, OnDestroy {
4245
arrowPointAtCenter?: boolean;
4346
config?: Required<PopoverConfig | PopConfirmConfig>;
4447
directiveTitle?: NzTSType | null;
@@ -56,11 +59,6 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnDestroy, Af
5659
cdkConnectedOverlayPush?: boolean;
5760
visibleChange = new EventEmitter<boolean>();
5861

59-
/**
60-
* For create tooltip dynamically. This should be override for each different component.
61-
*/
62-
protected componentRef!: ComponentRef<NzTooltipBaseComponent>;
63-
6462
/**
6563
* This true title that would be used in other parts on this component.
6664
*/
@@ -116,13 +114,21 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnDestroy, Af
116114

117115
private delayTimer?: ReturnType<typeof setTimeout>;
118116

119-
constructor(
120-
public elementRef: ElementRef,
121-
protected hostView: ViewContainerRef,
122-
protected renderer: Renderer2,
123-
protected noAnimation?: NzNoAnimationDirective,
124-
protected nzConfigService?: NzConfigService
125-
) {}
117+
elementRef = inject(ElementRef);
118+
protected hostView = inject(ViewContainerRef);
119+
protected renderer = inject(Renderer2);
120+
protected noAnimation = inject(NzNoAnimationDirective, { host: true, optional: true });
121+
protected nzConfigService = inject(NzConfigService);
122+
protected platformId = inject(PLATFORM_ID);
123+
124+
constructor(protected componentType: Type<NzTooltipBaseComponent>) {}
125+
126+
ngAfterViewInit(): void {
127+
if (isPlatformBrowser(this.platformId)) {
128+
this.createComponent();
129+
this.registerTriggers();
130+
}
131+
}
126132

127133
ngOnChanges(changes: SimpleChanges): void {
128134
const { trigger } = changes;
@@ -136,11 +142,6 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnDestroy, Af
136142
}
137143
}
138144

139-
ngAfterViewInit(): void {
140-
this.createComponent();
141-
this.registerTriggers();
142-
}
143-
144145
ngOnDestroy(): void {
145146
this.destroy$.next();
146147
this.destroy$.complete();
@@ -171,7 +172,8 @@ export abstract class NzTooltipBaseDirective implements OnChanges, OnDestroy, Af
171172
* Create a dynamic tooltip component. This method can be override.
172173
*/
173174
protected createComponent(): void {
174-
const componentRef = this.componentRef;
175+
const componentRef = this.hostView.createComponent(this.componentType);
176+
175177
this.component = componentRef.instance as NzTooltipBaseComponent;
176178

177179
// Remove the component's DOM because it should be in the overlay container.

components/tooltip/tooltip.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ import {
1010
ChangeDetectionStrategy,
1111
ChangeDetectorRef,
1212
Component,
13-
ComponentRef,
1413
Directive,
1514
ElementRef,
1615
EventEmitter,
1716
Host,
1817
Input,
1918
Optional,
2019
Output,
21-
Renderer2,
22-
ViewContainerRef,
2320
ViewEncapsulation
2421
} from '@angular/core';
2522

@@ -68,16 +65,8 @@ export class NzTooltipDirective extends NzTooltipBaseDirective {
6865
// eslint-disable-next-line @angular-eslint/no-output-rename
6966
@Output('nzTooltipVisibleChange') override readonly visibleChange = new EventEmitter<boolean>();
7067

71-
override componentRef: ComponentRef<NzToolTipComponent> = this.hostView.createComponent(NzToolTipComponent);
72-
73-
constructor(
74-
elementRef: ElementRef,
75-
hostView: ViewContainerRef,
76-
77-
renderer: Renderer2,
78-
@Host() @Optional() noAnimation?: NzNoAnimationDirective
79-
) {
80-
super(elementRef, hostView, renderer, noAnimation);
68+
constructor() {
69+
super(NzToolTipComponent);
8170
}
8271

8372
protected override getProxyPropertyMap(): PropertyMapping {

0 commit comments

Comments
 (0)