-
Notifications
You must be signed in to change notification settings - Fork 14
refactor(tooltip): move all logic into the service #1535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
spike-rabbit
wants to merge
1
commit into
main
Choose a base branch
from
refactor/tooltip/move-all-logic-into-the-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ import { Overlay, OverlayRef } from '@angular/cdk/overlay'; | |
| import { ComponentPortal } from '@angular/cdk/portal'; | ||
| import { ComponentRef, ElementRef, inject, Injectable, Injector } from '@angular/core'; | ||
| import { getOverlay, getPositionStrategy, positions } from '@siemens/element-ng/common'; | ||
| import { Subscription } from 'rxjs'; | ||
| import { fromEvent, Subject, Subscription, timer } from 'rxjs'; | ||
| import { delayWhen, takeUntil } from 'rxjs/operators'; | ||
|
|
||
| import { TooltipComponent } from './si-tooltip.component'; | ||
| import { SI_TOOLTIP_CONFIG, SiTooltipContent } from './si-tooltip.model'; | ||
|
|
@@ -18,15 +19,64 @@ import { SI_TOOLTIP_CONFIG, SiTooltipContent } from './si-tooltip.model'; | |
| * @internal | ||
| */ | ||
| class TooltipRef { | ||
| private readonly destroy$ = new Subject<void>(); | ||
| private isFocused = false; | ||
| private isHovered = false; | ||
|
|
||
| constructor( | ||
| private overlayRef: OverlayRef, | ||
| private element: ElementRef, | ||
| private injector?: Injector | ||
| ) {} | ||
| ) { | ||
| const nativeElement = this.element.nativeElement; | ||
|
|
||
| fromEvent(nativeElement, 'focus') | ||
| .pipe(takeUntil(this.destroy$)) | ||
| .subscribe(event => this.onFocus(event)); | ||
|
|
||
| fromEvent(nativeElement, 'mouseenter') | ||
| .pipe( | ||
| takeUntil(this.destroy$), | ||
| delayWhen(() => timer(500).pipe(takeUntil(fromEvent(nativeElement, 'mouseleave')))) | ||
| ) | ||
| .subscribe(() => this.onMouseEnter()); | ||
|
|
||
| private subscription?: Subscription; | ||
| fromEvent(nativeElement, 'focusout') | ||
| .pipe(takeUntil(this.destroy$)) | ||
| .subscribe(() => this.onFocusOut()); | ||
|
|
||
| fromEvent(nativeElement, 'mouseleave') | ||
| .pipe(takeUntil(this.destroy$)) | ||
| .subscribe(() => this.onMouseLeave()); | ||
| } | ||
|
|
||
| private positionSubscription?: Subscription; | ||
|
|
||
| private onFocus(event: unknown): void { | ||
| if (event instanceof FocusEvent && event.target instanceof Element) { | ||
| if ((event.target as Element).matches(':focus-visible')) { | ||
| this.isFocused = true; | ||
| this.show(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| show(): void { | ||
| private onFocusOut(): void { | ||
| this.isFocused = false; | ||
| this.hide(); | ||
| } | ||
|
|
||
| private onMouseEnter(): void { | ||
| this.isHovered = true; | ||
| this.show(); | ||
| } | ||
|
|
||
| private onMouseLeave(): void { | ||
| this.isHovered = false; | ||
| this.hide(); | ||
| } | ||
|
|
||
| private show(): void { | ||
| if (this.overlayRef.hasAttached()) { | ||
| return; | ||
| } | ||
|
|
@@ -35,20 +85,25 @@ class TooltipRef { | |
| const tooltipRef: ComponentRef<TooltipComponent> = this.overlayRef.attach(toolTipPortal); | ||
|
|
||
| const positionStrategy = getPositionStrategy(this.overlayRef); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| this.subscription?.unsubscribe(); | ||
| this.subscription = positionStrategy?.positionChanges.subscribe(change => | ||
| this.positionSubscription?.unsubscribe(); | ||
| this.positionSubscription = positionStrategy?.positionChanges.subscribe(change => | ||
| tooltipRef.instance.updateTooltipPosition(change, this.element) | ||
| ); | ||
| } | ||
|
|
||
| hide(): void { | ||
| private hide(): void { | ||
| if (this.isFocused || this.isHovered) { | ||
| return; | ||
| } | ||
| this.overlayRef.detach(); | ||
| this.subscription?.unsubscribe(); | ||
| this.positionSubscription?.unsubscribe(); | ||
| } | ||
|
|
||
| destroy(): void { | ||
| this.destroy$.next(); | ||
| this.destroy$.complete(); | ||
| this.overlayRef.dispose(); | ||
| this.subscription?.unsubscribe(); | ||
| this.positionSubscription?.unsubscribe(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TooltipRefconstructor directly subscribes to DOM events usingfromEvent. This will fail during Server-Side Rendering (SSR) as these events andnativeElementare browser-specific. This change removes a previousisPlatformBrowsercheck, introducing a regression.This violates a general rule for our repository: 'When using browser-dependent UI features like Angular CDK Overlay, ensure they are only executed in a browser environment. Use a check like
isPlatformBrowserto prevent errors during Server-Side Rendering (SSR).'To fix this, you should ensure this logic only runs in a browser environment. The cleanest way would be to inject
PLATFORM_IDinSiTooltipServiceand pass it down toTooltipRef.Here is an example of how you could implement this:
Modify
SiTooltipServiceto injectPLATFORM_IDand pass it toTooltipRef:Update
TooltipRefto acceptplatformIdand guard the browser-specific code:You will also need to add the necessary imports for
PLATFORM_IDandisPlatformBrowser.References
isPlatformBrowserto prevent errors during Server-Side Rendering (SSR).