Skip to content

Commit

Permalink
feat(module:tooltip,popover,popconfirm): support "nzMouseEnterDelay" …
Browse files Browse the repository at this point in the history
…and "nzMouseLeaveDelay" (#758)

close #757
  • Loading branch information
wilsoncook authored and vthinkxie committed Dec 15, 2017
1 parent 4a50d0d commit 3a894f0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/components/tooltip/nz-tooltip.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class NzToolTipComponent {
@Input() nzTitle: string;
@Input() nzOverlayClassName = '';
@Input() nzOverlayStyle = {};
@Input() nzMouseEnterDelay = 0; // Unit: second
@Input() nzMouseLeaveDelay = 0.1; // Unit: second
@Output() nzVisibleChange: EventEmitter<boolean> = new EventEmitter();
@ContentChild('nzTemplate') nzTemplate: TemplateRef<void>;
@ViewChild('overlay') overlay: ConnectedOverlayDirective;
Expand Down
31 changes: 15 additions & 16 deletions src/components/tooltip/nz-tooltip.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NzTooltipDirective implements AfterViewInit {

private tooltip: NzToolTipComponent;
private isDynamicTooltip = false; // Indicate whether current tooltip is dynamic created
private smoothTimer; // Timer for hiding smoothly
private delayTimer; // Timer for delay enter/leave

constructor(
public elementRef: ElementRef,
Expand All @@ -52,13 +52,13 @@ export class NzTooltipDirective implements AfterViewInit {
ngAfterViewInit(): void {
if (this.tooltip.nzTrigger === 'hover') {
let overlayElement;
this.renderer.listen(this.elementRef.nativeElement, 'mouseenter', () => this.showSmoothly());
this.renderer.listen(this.elementRef.nativeElement, 'mouseenter', () => this.delayEnterLeave(true, true, this.tooltip.nzMouseEnterDelay));
this.renderer.listen(this.elementRef.nativeElement, 'mouseleave', () => {
this.hideSmoothly();
this.delayEnterLeave(true, false, this.tooltip.nzMouseLeaveDelay);
if (this.tooltip.overlay.overlayRef && !overlayElement) { // NOTE: we bind events under "mouseleave" due to the overlayRef is only created after the overlay was completely shown up
overlayElement = this.tooltip.overlay.overlayRef.overlayElement;
this.renderer.listen(overlayElement, 'mouseenter', () => this.showSmoothly());
this.renderer.listen(overlayElement, 'mouseleave', () => this.hideSmoothly());
this.renderer.listen(overlayElement, 'mouseenter', () => this.delayEnterLeave(false, true));
this.renderer.listen(overlayElement, 'mouseleave', () => this.delayEnterLeave(false, false));
}
});
} else if (this.tooltip.nzTrigger === 'focus') {
Expand All @@ -82,18 +82,17 @@ export class NzTooltipDirective implements AfterViewInit {
this.isTooltipOpen = false;
}

private showSmoothly(): void {
if (this.smoothTimer) {
clearTimeout(this.smoothTimer);
private delayEnterLeave(isOrigin: boolean, isEnter: boolean, delay: number = -1): void {
if (this.delayTimer) { // Clear timer during the delay time
window.clearTimeout(this.delayTimer);
this.delayTimer = null;
} else if (delay > 0) {
this.delayTimer = window.setTimeout(() => {
this.delayTimer = null;
isEnter ? this.show() : this.hide();
}, delay * 1000);
} else {
this.show();
isEnter && isOrigin ? this.show() : this.hide(); // [Compatible] The "isOrigin" is used due to the tooltip will not hide immediately (may caused by the fade-out animation)
}
}

private hideSmoothly(): void {
this.smoothTimer = setTimeout(() => {
this.smoothTimer = null;
this.hide();
}, 50);
}
}
12 changes: 12 additions & 0 deletions src/showcase/nz-demo-popconfirm/nz-demo-popconfirm.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ <h3 id="Rate"><span>nz-popconfirm</span><!-- <a class="anchor">#</a> --></h3>
<td>object</td>
<td></td>
</tr>
<tr>
<td>nzMouseEnterDelay</td>
<td>鼠标移入后延时多少才显示 Tooltip,单位:秒(nzTrigger='hover'时有效)</td>
<td>number</td>
<td>0</td>
</tr>
<tr>
<td>nzMouseLeaveDelay</td>
<td>鼠标移出后延时多少才隐藏 Tooltip,单位:秒(nzTrigger='hover'时有效)</td>
<td>number</td>
<td>0.1</td>
</tr>
</tbody>
</table>

Expand Down
12 changes: 12 additions & 0 deletions src/showcase/nz-demo-popover/nz-demo-popover.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ <h3 id="Rate"><span>nz-popover</span><!-- <a class="anchor">#</a> --></h3>
<td>object</td>
<td></td>
</tr>
<tr>
<td>nzMouseEnterDelay</td>
<td>鼠标移入后延时多少才显示 Tooltip,单位:秒(nzTrigger='hover'时有效)</td>
<td>number</td>
<td>0</td>
</tr>
<tr>
<td>nzMouseLeaveDelay</td>
<td>鼠标移出后延时多少才隐藏 Tooltip,单位:秒(nzTrigger='hover'时有效)</td>
<td>number</td>
<td>0.1</td>
</tr>
</tbody>
</table>
</section>
Expand Down
12 changes: 12 additions & 0 deletions src/showcase/nz-demo-tooltip/nz-demo-tooltip.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ <h2 id="API"><span>API</span>
<td>object</td>
<td></td>
</tr>
<tr>
<td>nzMouseEnterDelay</td>
<td>鼠标移入后延时多少才显示 Tooltip,单位:秒(nzTrigger='hover'时有效)</td>
<td>number</td>
<td>0</td>
</tr>
<tr>
<td>nzMouseLeaveDelay</td>
<td>鼠标移出后延时多少才隐藏 Tooltip,单位:秒(nzTrigger='hover'时有效)</td>
<td>number</td>
<td>0.1</td>
</tr>
</tbody>
</table>
<h2 id="注意"><span>注意</span>
Expand Down

0 comments on commit 3a894f0

Please sign in to comment.