The MatToolTip from Angular Material will only appear if the element is cut off

(Update: Urgent assistance needed! I am facing a critical issue. This problem persists, please check the comments for further details)

I am seeking a solution to make the matTooltip appear only when the labels exceed their designated space limit. For instance: when text overflows, and if they do not overflow, no tooltips should be displayed.

Below is the code snippet where I attempted to utilize matTooltipDisabled:

HTML (I have omitted some code as I believe it's irrelevant to the issue. Essentially, in the HTML, certain labels remain hidden until the dropdown is clicked, which also poses a concern for tooltips):

<div *ngFor="...; let i = index">
            <div class="panel-container-label">
              <div *ngIf="i < 5" fxLayout="row" fxLayoutAlign="space-between center">
                <div>
                  <span matTooltip="..." [matTooltipPosition]="'after'" [matTooltipDisabled]="!ifOverflow(toolTip)" #toolTip>
                    <mat-checkbox class="search-facet-checkboxes" ...>
                      ...
                    </mat-checkbox>
                  </span>
                </div>
              </div>
              <div *ngIf="i >= 5 &&..." fxLayout="row" fxLayoutAlign="space-between center">
                <div class="container-name-oflw">
                  <span matTooltip="..." [matTooltipPosition]="'after'" [matTooltipDisabled]="!ifOverflow(toolTip)" #toolTip>
                    <mat-checkbox class="search-facet-checkboxes" ...>
                      ...
                    </mat-checkbox>
                  </span>
                </div>
              </div>
            </div>
          </div>

CSS:

.search-facet-checkboxes .mat-checkbox-layout .mat-checkbox-label {
  font-weight: normal !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
}

div.panel-container-label {
  background-color: #ffffff;
  color: #487980;
  padding: 0 10px;
  clear: both;
  overflow: hidden;
  .container-name-oflw {
    width: 170px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

TypeScript:

ifOverflow(e){
    console.log(e.scrollWidth + " and " + e.clientWidth)
    return e.scrollWidth > e.clientWidth;
  }

The console is displaying 0 for both scrollWidth and ClientWidth, and I am unable to determine the reason behind this discrepancy.

I also experimented with ViewChild using a local reference #toolTip, but encountered an error stating

Cannot read property 'native-element' of undefined
.

Employing a directive without Angular Material like the one mentioned here: https://medium.com/@anup.bangale/showing-tooltip-only-when-text-overflow-ellipsis-active-angular8-bd5e9d7667a9 wasn't preferred as it involves creating a new file, and I specifically need to utilize Angular Material.

I have exhaustively browsed through numerous Stack Overflow threads related to this issue. Your guidance and support would be greatly appreciated. Thank you!

Answer №1

It seems like your concept is on the right track. However, consider using a div instead of span, or adjusting the display property to either block or inline-block in the CSS. This is because inline elements have a width of 0. I have personally implemented this approach before, so I have created a simple example that may be helpful:

https://stackblitz.com/edit/angular-material-tooltip-overflow

More information about inline element width can be found here: clientWidth and clientHeight report zero while getBoundingClientRect is correct

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Errors encountered while running `npm install discord.js`

I am currently facing an issue while trying to install discord.js. Unfortunately, I keep encountering the following errors: npm ERR! cb() never called! npm ERR! This is an error with npm itself. Please report this error at: npm ERR! <https://npm.co ...

Steps for Disabling Autocomplete in a JSP Page

How can I prevent the browser from remembering the username and password on my JSP page after submitting the form? I've already tried using autocomplete=off in the JSP code. <form name="indexFrm" id="indexFrm" autocomplete="off" method="post"> ...

just half of the four $_POST variables are actually assigned

Here is a simple form structure: <table class='table table-striped table-bordered table-hover' id='dataTables-example'> <thead> <tr> <th>1</th> <th>2</th> ...

Developing a Liferay 7.0 portlet using Angular and TypeScript

Is it possible to develop a Liferay 7.0 portlet using Angular (2, Angular Material v1.1.1) and TypeScript? I am aware that it is possible to incorporate Angular 1.4 library and create a portlet using plain JavaScript/Angular code, as mentioned in this sou ...

Image not being sent to the back-end server

I've successfully set up the back-end to retrieve files using the post method in Postman. However, I'm facing an issue with the Angular2 front-end as the service code seems to be incorrect. data1 contains the image data. addData(postData: Imag ...

Selenium was unable to scroll a sidebar or container

I'm just starting out with selenium and python. I'm trying to scrape the content from a website that has a sidebar (container), and I need to copy its content multiple times while scrolling until I reach the end of the container. However, I&apos ...

Adjust the height of the container to accommodate responsive images automatically

Hello, I am trying to make the container height adjust automatically based on the responsive image it contains. The container's display is set to flex and the image is positioned between two divs with the class "text-block". These divs have a width of ...

Tips for exporting 3D objects from 3ds Max Studio for optimal use in Three.js

I am facing an issue with loading a 3D object that I created in 3D Studio Max! When I export it as a .obj file (which generates two files, .obj and .mtl), I have tried using OBJMTLLOADET(), MTLLOADER(), and OBJLOADER() but none of them seem to work. Other ...

Jersey/Jaxb is providing a result of strings rather than numbers

This is the structure that my jersey service returns: @XmlRootElement(name="chart-data") public class ChartDataDto { private List<Series> series = new ArrayList<>(); public ChartDataDto() { } public void putSeries(St ...

Adding a new value to a string variable using TypeScript

Just starting out with Angular and I'm looking to capture user input from a text box with each keystroke and add it to a string variable like in Java. In my text box, I have the following setup for capturing key events: <input type="text" ...

The combination of MUI CardContent and flexBox seems to have issues when used in conjunction with typography and chips

Take a look at this React code using MUI. It's designed to create a Card that showcases Character Information. const CharacterPreview = ({ characterKey }: CharacterPreviewProps) => { return ( <Card sx={{ maxWidth: 256, borderRadius: 4 }}&g ...

Tips for transferring a reference to a variable, rather than its value, to a method in VueJS

Within my template, there is this code snippet: <input @input="myMethod(myVariableName)" /> Following that, I have the myMethod function: myMethod(variablePassed) { console.log(variablePassed) } Upon execution, I receive the value of th ...

What is the best way to display a loader when utilizing AJAX with jQuery?

I am having trouble implementing a loader in my ajax jQuery call. My goal is to display a loader while the ajax request is fetching data from an API and hide it once the request is completed. I have split this functionality into 2 separate JavaScript fil ...

Issue with Promise.all not waiting for Promise to resolve

After making a request to the server, I receive the data as a promise, which contains the correct information. However, for some reason, the program fails to execute properly. Prior to uploading it on Zeit, this program was functioning correctly. Fetch R ...

The error form is not responding even after attempting to locate the issue

I have created a form and it was working fine, but now when I click on the submit or reset buttons, nothing happens. I've checked my code thoroughly line by line, but can't seem to find the issue. I even tried restoring previous versions, but no ...

Add a style to every div except for the final one

I've attempted to add a unique style to all divs with the same class within a parent div, except for the last one. Strangely, my code doesn't seem to work as expected for this particular case. Can anyone spot what I might be overlooking? Right no ...

Is Clarity Design compatible with Angular 15?

After updating my project to Angular version 15, I found that the clarity version I was using was as follows: "@cds/core": "5.0.0", "@clr/angular": "5.0.0", "@clr/icons": "5.0.0", "@clr/ui&qu ...

When defining a class property in TypeScript, you can make it optional by not providing

Is there a way to make a property on a Class optional without it being undefined? In the following example, note that the Class constructor takes a type of itself (this is intentional) class Test { foo: number; bar: string; baz?: string; construc ...

Issue with Angular 2 Observable not triggering the complete function

I've been experimenting with the hero app tutorial for Angular 2 and currently have this Component set up: import { Component, OnInit } from '@angular/core' import { Subject } from 'rxjs/Subject'; import { Hero } from "./hero"; im ...

Ensure that this regular expression is able to accurately match all numbers, even those without decimal points

Seeking help to create a script that can extract the negative percentage value between two numbers. One of the numbers is dynamic and includes different currencies, decimals, etc., so I believe a regex is needed for this task. The current script works, but ...