Question T21170
Visible to All Users

Need to adjust tooltip perfectly for smaller fields

created 3 months ago (modified 3 months ago)

Hi,
we are using tool tip for the smaller fields and we are adjusting this tool tip position manually, we want to adjust the tool tip position dynamically, tool tip position is not proper when there is long text

To adjust the position of the tool tip, we are using this below approach

Code
const hoverElement = options.htmlElement;     hoverElement.addEventListener("mouseover", (event: MouseEvent) => {       const target = event.target as HTMLElement;       console.log('target:',target);       if(target.classList.contains('sd-input') || target.classList.contains('sd-dropdown')){         const currentText = target.innerText || target.getAttribute('value');         if (!currentText) return         this.popupText = currentText && currentText.trim();             const { left , top , width } = event.target['getBoundingClientRect']();             const tooltipWidth = 1036;             const tooltipLeft = left + width / 2 - tooltipWidth / 2;             const tooltipTop = top - 42.00;             this.popupPosition = {               top: `${tooltipTop}px`,               left: `${tooltipLeft}px`             }             this.toolShowPopup = true;             this.cdRef.detectChanges();       }     });
Code
<ng-container  *ngIf="toolShowPopup">   <div class="onHoverTooltip" [ngStyle]="{ 'left': popupPosition.left, 'top': popupPosition.top }">     <span class="tooltip-text">{{ popupText }}</span>     <span class="beak beak_top"></span>     </div> </ng-container>

Clipboard-File-1.png
Please tell me the best approach to achieve this

Thanks,
Surya

Comments (1)

    Hello Surya,
    SurveyJS forms do not have a dedicated API for applying custom components as form element tooltips. However, you can use the survey.onAfterRenderQuestionInput event to attach custom tooltips to SurveyJS elements. This function allows you to access the corresponding HTML form element (e.g., a question input or a cell) and modify it as needed.

    Regarding your specific question: please note that tooltip functionality is not directly tied to SurveyJS components. Once you obtain the underlying HTML element, you can retrieve its properties and calculate the tooltip's position and dimensions as required. However, by looking at your code, I noticed that the tooltipWidth is set to a fixed value of 1036. This may lead to incorrect positioning if the tooltip's actual width changes, such as when displaying long text. To ensure proper alignment, consider dynamically calculating the tooltip's width using tooltipElement.offsetWidth or a similar method after rendering.

    Thank you