show Tooltip only when the ellipsis is active
Overflow of an element can be detected in JavaScript with this helper, using Angular ElementRef
and a formula from this answer:
function isTextTruncated(element: ElementRef): boolean {
const e = element.nativeElement;
return e.scrollWidth > e.clientWidth;
}
Then, in your component, use it referencing the element with a "@ViewChild
" property:
@ViewChild('hospitalName') hospitalNameElement: ElementRef;
isHospitalNameTruncated(): boolean {
return isTextTruncated(this.hospitalNameElement);
}
Finally, in the template, add the identifier #hospitalName
and call isHospitalNameTruncated()
to customize the tooltip text:
<td mat-cell [attr.id]="hospital.organizational_id + '_hospitalname'"
*matCellDef="let hospital">
<div id="hospital_name" #hospitalName class="truncate"
[matTooltip]="isHospitalNameTruncated() ? hospital.organization_name : null ">
{{hospital.organization_name}}
</div>
</td>
How to create a tooltip only when ellipsis is active ? I want the solution only in CSS
Use title attribute not hover style.
.truncate {
max-width: 160px;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
white-space: nowrap;
}
<span class="truncate" title="HERE is my sample text HERE is my sample text">
HERE is my sample text HERE is my sample text
</span>
How to activate tooltip when ellipsis is activated using a directive in angular 5?
You can create this directive:
import { AfterViewInit, Directive, ElementRef, EventEmitter, Output } from
'@angular/core';
@Directive({
selector: '[isEllipsisActive]'
})
export class IsEllipsisActiveDirective implements AfterViewInit {
constructor(private elementRef: ElementRef) {}
ngAfterViewInit(): void {
setTimeout(() => {
const element = this.elementRef.nativeElement;
if(element.offsetWidth < element.scrollWidth){
element.title = element.innerHTML;
}
}, 500);
}
}
take a look on this https://stackblitz.com/edit/angular-qjmg7m?file=src%2Fapp%2Fis-ellipsis-active.directive.ts
React show Material-UI Tooltip only for text that has ellipsis
To go off of @benjamin.keen answer. Here is a standalone functional component which is just an extension of his answer using hooks to perform the comparison functions.
import React, { useRef, useEffect, useState } from 'react';
import Tooltip from '@material-ui/core/Tooltip';
const OverflowTip = props => {
// Create Ref
const textElementRef = useRef();
const compareSize = () => {
const compare =
textElementRef.current.scrollWidth > textElementRef.current.clientWidth;
console.log('compare: ', compare);
setHover(compare);
};
// compare once and add resize listener on "componentDidMount"
useEffect(() => {
compareSize();
window.addEventListener('resize', compareSize);
}, []);
// remove resize listener again on "componentWillUnmount"
useEffect(() => () => {
window.removeEventListener('resize', compareSize);
}, []);
// Define state and function to update the value
const [hoverStatus, setHover] = useState(false);
return (
<Tooltip
title={props.value}
interactive
disableHoverListener={!hoverStatus}
style={{fontSize: '2em'}}
>
<div
ref={textElementRef}
style={{
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis'
}}
>
{props.someLongText}
</div>
</Tooltip>
);
};
export default OverflowTip;
Related Topics
Need to Find Height of Hidden Div on Page (Set to Display:None)
How to Separate Web Components to Individual Files and Load Them
Mui Createtheme Is Not Properly Passing Theme to Mui Components
Restart a Gif Animation Without Reloading the File
Simplest Way to Set Cursor to Wait, Then Have All Elements Revert Back
Multiline Typewriter Effect with Two Consistent Words
Cross-Browser Window Resize Event - JavaScript/Jquery
How to View Events Fired on an Element in Chrome Devtools
Javascript: Get Deep Value from Object by Passing Path to It as String
Event When User Stops Scrolling
Jquery: How to Listen for Dom Changes
How Does Github Change the Url But Not the Reload
Safari: Absolutely Positioned Divs Not Moving When Updated via Dom
How to Reliably Get Screen Width with the Scrollbar
Jquery - How to Disable the Entire Page
Changed Id Not Being Found by Jquery