Skip to content

Commit 6860497

Browse files
committed
IBX-6160: Fixed ellipsis dynamic tooltips
1 parent 7d638d5 commit 6860497

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/bundle/Resources/public/js/scripts/helpers/tooltips.helper.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,42 @@
7575

7676
return defaultBsPopperConfig;
7777
};
78+
const getTextHeight = (text, styles) => {
79+
const tag = doc.createElement('div');
80+
81+
tag.innerHTML = text;
82+
83+
for (const key in styles) {
84+
tag.style[key] = styles[key];
85+
}
86+
87+
doc.body.appendChild(tag);
88+
89+
const { height: texHeight } = tag.getBoundingClientRect();
90+
91+
doc.body.removeChild(tag);
92+
93+
return texHeight;
94+
};
95+
const isTitleEllipsized = (node) => {
96+
const title = node.title || node.dataset.originalTitle;
97+
const { width: nodeWidth, height: nodeHeight } = node.getBoundingClientRect();
98+
const computedNodeStyles = getComputedStyle(node);
99+
const styles = {
100+
width: `${nodeWidth}px`,
101+
padding: computedNodeStyles.getPropertyValue('padding'),
102+
'font-size': computedNodeStyles.getPropertyValue('font-size'),
103+
'font-family': computedNodeStyles.getPropertyValue('font-family'),
104+
'font-weight': computedNodeStyles.getPropertyValue('font-weight'),
105+
'font-style': computedNodeStyles.getPropertyValue('font-style'),
106+
'font-variant': computedNodeStyles.getPropertyValue('font-variant'),
107+
'line-height': computedNodeStyles.getPropertyValue('line-height'),
108+
};
109+
110+
const textHeight = getTextHeight(title, styles);
111+
112+
return textHeight > nodeHeight;
113+
};
78114
const parse = (baseElement = doc) => {
79115
if (!baseElement) {
80116
return;
@@ -93,7 +129,7 @@
93129
if (hasEllipsisStyle) {
94130
resizeEllipsisObserver.observe(tooltipNode);
95131

96-
const isEllipsized = tooltipNode.scrollWidth > tooltipNode.offsetWidth;
132+
const isEllipsized = isTitleEllipsized(tooltipNode);
97133
const tooltipInstance = bootstrap.Tooltip.getInstance(tooltipNode);
98134

99135
if (tooltipInstance) {
@@ -109,6 +145,11 @@
109145
tooltipNode.title = tooltipNode.dataset.title;
110146
}
111147
} else {
148+
if (tooltipNode.title) {
149+
tooltipNode.dataset.title = tooltipNode.title;
150+
tooltipNode.title = '';
151+
}
152+
112153
continue;
113154
}
114155
}

0 commit comments

Comments
 (0)