Skip to content

Commit

Permalink
Fix tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
akariv committed Feb 24, 2025
1 parent e8b647a commit 7691bcd
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
[style.left.px]='tooltip.left()'
>
<app-tooltip [align]='tooltip.align()'
(mouseenter)='tooltip.keep()'
(mouseleave)='tooltip.unkeep()'
>
<div [innerHTML]='tooltip.content()'></div>
<div [class.inert]='tooltip.inert()'
(mouseenter)='tooltip.keep()'
(mouseleave)='tooltip.unkeep()'
[innerHTML]='tooltip.content()'></div>
</app-tooltip>
</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@
display: flex;
flex-flow: column;
align-items: flex-end;

app-tooltip {
pointer-events: none;
div {
pointer-events: all;
&.inert {
pointer-events: none;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<span class='anchor' #anchor (mouseover)='show()'>
<span class='anchor' #anchor (mouseover)='show($event)' (mousemove)='show($event)'>
<ng-content select='[anchor]'></ng-content>
</span>
<span class='content' #content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TooltipAlignments, TooltipService } from '../tooltip.service';
})
export class TooltipWrapperComponent {
@Input() align: TooltipAlignments = 'bottom-left';
@Input() inert = false;

content = '';
@ViewChild('anchor') anchorEl: ElementRef;
Expand All @@ -20,8 +21,9 @@ export class TooltipWrapperComponent {
this.content = this.contentEl.nativeElement.innerHTML;
}

show() {
console.log('showing tooltip');
this.tooltip.show(this.anchorEl.nativeElement, this.content, this.align);
show(event: MouseEvent) {
// console.log('showing tooltip', event, this.anchorEl.nativeElement);
this.tooltip.show(this.anchorEl.nativeElement, this.content, this.align, this.inert);
return true;
}
}
15 changes: 11 additions & 4 deletions projects/treecat/src/app/tooltip.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ export class TooltipService {
keeping = false;

visible = signal(false);
inert = signal(false);

hider = new Subject<void>();

constructor() {
this.hider.pipe(
debounceTime(1000),
debounceTime(100),
filter(() => !this.keeping)
).subscribe(() => {
this.hide();
});
}

show(el: Element, content: string, align: TooltipAlignments = 'bottom-left') {
show(el: Element, content: string, align: TooltipAlignments = 'bottom-left', inert=false) {
if (this.visible() && this.lastEl === el) {
console.log('already showing this tooltip');
// console.log('already showing this tooltip');
return;
}
const rect = el.getBoundingClientRect();
Expand All @@ -44,26 +45,32 @@ export class TooltipService {
this.left.set(rect.left + rect.width / 2);
this.align.set(align);
this.content.set(content);
this.visible.set(true);
this.visible.set(true);
this.inert.set(inert);
this.lastEl = el;
fromEvent(el, 'mouseout').pipe(
take(1)
).subscribe(() => {
// console.log('mouse out', el);
this.hider.next();
});

}

keep() {
// console.log('keeping');
this.keeping = true;
}

unkeep() {
// console.log('unkeeping');
this.keeping = false;
this.lastEl = null;
this.hider.next();
}

hide() {
// console.log('hiding');
this.visible.set(false);
this.lastEl = null;
this.keeping = false;
Expand Down
29 changes: 25 additions & 4 deletions projects/treecat/src/app/tooltip/tooltip.component.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

:host {
width: fit-content;
height: 0;
overflow: visible;
position: relative;
&.align-bottom-left {
top: -8px;
left: -40px;
}
&.align-bottom-right {
top: -8px;
left: 40px;
}

&.align-top-left {
top: 8px;
left: -40px;
}

&.align-top-right {
top: 8px;
left: 40px;
}
}
.tooltip {
border-radius: 5px;
Expand All @@ -26,15 +47,15 @@
}

&.align-bottom-left {
transform: translateY(-8px)translateX(-40px)translateY(-100%);
transform: translateY(-100%);
.tip {
bottom: 0;
transform: translateY(100%);
left: 32px;
}
}
&.align-bottom-right {
transform: translateY(-8px)translateX(-100%)translateX(40px)translateY(-100%);
transform: translateX(-100%)translateY(-100%);
.tip {
bottom: 0;
transform: translateY(100%);
Expand All @@ -43,7 +64,7 @@
}

&.align-top-left {
transform: translateY(8px)translateX(-40px);
// transform: translateY(8px)translateX(-40px);
.tip {
top: 0;
left: 32px;
Expand All @@ -52,7 +73,7 @@
}

&.align-top-right {
transform: translateY(8px)translateX(-100%)translateX(40px);
transform: translateX(-100%);
.tip {
top: 0;
right: 32px;
Expand Down
9 changes: 7 additions & 2 deletions projects/treecat/src/app/tooltip/tooltip.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import { TooltipAlignments } from '../tooltip.service';
selector: 'app-tooltip',
imports: [],
templateUrl: './tooltip.component.html',
styleUrl: './tooltip.component.less'
styleUrl: './tooltip.component.less',
host: {
'[class.align-bottom-left]': 'align === "bottom-left"',
'[class.align-bottom-right]': 'align === "bottom-right"',
'[class.align-top-left]': 'align === "top-left"',
'[class.align-top-right]': 'align === "top-right"',
},
})
export class TooltipComponent {

@Input() align: TooltipAlignments = 'bottom-left';

}
8 changes: 5 additions & 3 deletions projects/treecat/src/app/tree-card/tree-card.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div class='card'>
<img [src]='tree.mainPhoto?.url || data.DEFAULT_IMAGE_URL' (load)='markAsLoaded()'>
<a class='overlay' (activated)='open()' clickOnReturn>
<!-- [class.visible]='showOverlay()'>
(mouseover)='showOverlay.set(true)' (mouseout)='showOverlay.set(false)'> -->
<div class='content'>
<div class='header'>
<div class='name'>
Expand Down Expand Up @@ -30,13 +32,13 @@ <h3>{{tree.botanicalName}}</h3>
</div>
</div>
</a>
@if (state.isInCart(tree)) {
<app-tooltip-wrapper align='bottom-left' class='remove-from-cart'>
@if (isInCart()) {
<app-tooltip-wrapper align='bottom-left' class='remove-from-cart' [inert]='true'>
<a anchor class='icon' (activated)='state.removeFromCart(tree)' clickOnReturn aria-label="הסרה מהמריצה"></a>
<div tooltip>הסרה מהמריצה</div>
</app-tooltip-wrapper>
} @else {
<app-tooltip-wrapper align='bottom-left' class='add-to-cart'>
<app-tooltip-wrapper align='bottom-left' class='add-to-cart' [inert]='true'>
<a anchor class='icon' (activated)='state.addToCart(tree)' clickOnReturn aria-label="הוספה למריצה"></a>
<div tooltip>הוספה למריצה</div>
</app-tooltip-wrapper>
Expand Down
4 changes: 0 additions & 4 deletions projects/treecat/src/app/tree-card/tree-card.component.less
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
}
}
.add-to-cart {
display: none;
.icon {
background-image: url(../../../public/img/icon-star-empty-black.svg);
&:hover {
Expand All @@ -147,9 +146,6 @@
}
}
}
.add-to-cart {
display: block;
}
}
}
}
11 changes: 9 additions & 2 deletions projects/treecat/src/app/tree-card/tree-card.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Component, effect, EventEmitter, Input, Output, signal } from '@angular/core';
import { DataService, Tree } from '../data.service';
import { environment } from '../../environments/environment';
import { Router, RouterModule } from '@angular/router';
Expand All @@ -22,7 +22,14 @@ export class TreeCardComponent {
@Input() catalog = false;
@Output() loaded = new EventEmitter<void>();

constructor(public state: StateService, public data: DataService, private router: Router) {}
isInCart = signal(false);
showOverlay = signal(false);

constructor(public state: StateService, public data: DataService, private router: Router) {
effect(() => {
this.isInCart.set(this.state.isInCart(this.tree));
});
}

markAsLoaded() {
this.loaded.emit();
Expand Down

0 comments on commit 7691bcd

Please sign in to comment.