Skip to content

Commit 8268c97

Browse files
author
Andrea Barbasso
committed
[UXP-126] move tooltip on scroll
1 parent d78b68b commit 8268c97

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

src/app/directives/text-select/text-selection-tooltip/text-selection-tooltip.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
<i *ngIf="this.utterance && isPaused" class="button fa fa-play" (click)="resumeTextToSpeech()"></i>
66
</div>
77
</div>
8-
<div class="pointer-down">&nbsp;</div>
8+
<div class="tooltip-arrow">&nbsp;</div>

src/app/directives/text-select/text-selection-tooltip/text-selection-tooltip.component.scss

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
z-index: 9999;
44
background-color: var(--bs-gray-900);
55
transform: translate(-50%, -100%);
6+
7+
&.bottom-placement {
8+
transform: translate(-50%, 0);
9+
div.tooltip-arrow {
10+
transform: translateX(-50%) translateY(-50%) rotate(45deg);
11+
bottom: initial;
12+
top: 0;
13+
}
14+
}
615
}
716

817
i.button {
918
cursor: pointer;
1019
}
1120

12-
div.pointer-down {
21+
div.tooltip-arrow {
1322
position: absolute;
1423
bottom: 0;
1524
left: 50%;

src/app/directives/text-select/text-selection-tooltip/text-selection-tooltip.component.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import {
22
ChangeDetectionStrategy,
33
ChangeDetectorRef,
4-
Component,
4+
Component, ElementRef,
55
HostBinding,
6-
HostListener,
6+
HostListener, Inject,
77
Input,
8+
NgZone,
89
OnDestroy,
910
OnInit
1011
} from '@angular/core';
1112
import { NgIf } from '@angular/common';
13+
import { NativeWindowRef, NativeWindowService } from '../../../core/services/window.service';
1214

1315
@Component({
1416
selector: 'ds-text-selection-tooltip',
@@ -46,19 +48,40 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
4648
@HostBinding('style.right.px')
4749
right: number;
4850

49-
@HostBinding('style.bottom.px')
50-
bottom: number;
51+
@HostBinding('class.bottom-placement')
52+
bottomPlacement = false;
5153

5254
utterance: SpeechSynthesisUtterance;
5355
isPaused = false;
5456

55-
constructor(private changeDetectorRef: ChangeDetectorRef) {
57+
bindedCheckPosition = this.checkPosition.bind(this);
58+
59+
constructor(
60+
@Inject(NativeWindowService) protected _window: NativeWindowRef,
61+
private changeDetectorRef: ChangeDetectorRef,
62+
private ngZone: NgZone,
63+
private element: ElementRef) {
5664

5765
}
5866

5967
ngOnInit(): void {
60-
this.top = this.rectangleTop - 6;
6168
this.left = this.rectangleLeft + this.rectangleWidth / 2;
69+
this.checkPosition();
70+
this.ngZone.runOutsideAngular(() => {
71+
// listen to scroll event to update position
72+
window.addEventListener('scroll', this.bindedCheckPosition);
73+
});
74+
}
75+
76+
checkPosition() {
77+
if (this.rectangleTop < this._window.nativeWindow.scrollY) {
78+
this.top = this.rectangleTop + this.rectangleHeight + 6;
79+
this.bottomPlacement = true;
80+
} else {
81+
this.top = this.rectangleTop - 6;
82+
this.bottomPlacement = false;
83+
}
84+
this.changeDetectorRef.detectChanges();
6285
}
6386

6487
// listen to mousedown on host element to avoid clearing selection
@@ -83,6 +106,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
83106
if (this.utterance) {
84107
speechSynthesis.cancel();
85108
}
109+
window.removeEventListener('scroll', this.bindedCheckPosition);
86110
}
87111

88112
pauseTextToSpeech() {

0 commit comments

Comments
 (0)