Skip to content
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

fix: size text with computed styles even when hidden #8572

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions core/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,14 @@ export function getTextWidth(textElement: SVGTextElement): number {
}
}

// Attempt to compute fetch the width of the SVG text element.
try {
width = textElement.getComputedTextLength();
} catch (e) {
// In other cases where we fail to get the computed text. Instead, use an
// approximation and do not cache the result. At some later point in time
// when the block is inserted into the visible DOM, this method will be
// called again and, at that point in time, will not throw an exception.
return textElement.textContent!.length * 8;
}
// Compute the width of the SVG text element.
const style = window.getComputedStyle(textElement);
width = getFastTextWidthWithSizeString(
textElement,
style.fontSize,
style.fontWeight,
style.fontFamily,
);

// Cache the computed width and return.
if (cacheWidths) {
Expand Down
Loading