Skip to content

Commit 7cad481

Browse files
Improve lineheight calc in measureDimensions (#2615)
* Improve lineheight calc in measureDimensions * format * Add changeset
1 parent b6b7d1a commit 7cad481

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"victory-core": patch
3+
---
4+
5+
Fix text size regression when using line-height

packages/victory-core/src/victory-util/textsize.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ const _approximateDimensionsInternal = (
267267
const _getMeasurementContainer = memoize(() => {
268268
const element = document.createElementNS("http://www.w3.org/2000/svg", "svg");
269269
element.setAttribute("xlink", "http://www.w3.org/1999/xlink");
270+
element.setAttribute("width", "300");
271+
element.setAttribute("height", "300");
272+
element.setAttribute("viewBox", "0 0 300 300");
270273

271274
const containerElement = document.createElementNS(
272275
"http://www.w3.org/2000/svg",
@@ -304,24 +307,31 @@ const _measureDimensionsInternal = memoize(
304307
);
305308
const params = _prepareParams(style, i);
306309
textElement.style.fontFamily = params.fontFamily;
307-
textElement.style.transform = `rotate(${params.angle})`;
308310
textElement.style.fontSize = `${params.fontSize}px`;
309311
textElement.style.lineHeight = params.lineHeight;
310312
textElement.style.fontFamily = params.fontFamily;
311313
textElement.style.letterSpacing = params.letterSpacing;
312314
textElement.textContent = line;
313315
textElement.setAttribute("x", "0");
314316
textElement.setAttribute("y", `${heightAcc}`);
315-
heightAcc += params.lineHeight * params.fontSize;
316-
317317
containerElement.appendChild(textElement);
318+
319+
heightAcc +=
320+
params.lineHeight * textElement.getBoundingClientRect().height;
318321
}
319322

320-
const { width, height } = containerElement.getBoundingClientRect();
323+
const { width } = containerElement.getBoundingClientRect();
321324

322325
containerElement.innerHTML = "";
323326

324-
return { width, height };
327+
return {
328+
width: style?.angle
329+
? _getSizeWithRotate(width, heightAcc, style?.angle)
330+
: width,
331+
height: style?.angle
332+
? _getSizeWithRotate(heightAcc, width, style?.angle)
333+
: heightAcc,
334+
};
325335
},
326336
(text, style) => {
327337
const totalText = Array.isArray(text) ? text.join() : text;

stories/victory-legend.stories.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,33 @@ export const DefaultRendering = () => {
7070
);
7171
};
7272

73+
export const LineHeight = () => {
74+
return (
75+
<div style={containerStyle}>
76+
<Wrapper>
77+
<VictoryLegend
78+
orientation="vertical"
79+
rowGutter={0}
80+
style={{
81+
labels: { lineHeight: 0.275 },
82+
}}
83+
data={[{ name: "One" }, { name: "Two" }, { name: "Three" }]}
84+
/>
85+
</Wrapper>
86+
<Wrapper>
87+
<VictoryLegend
88+
orientation="vertical"
89+
rowGutter={0}
90+
style={{
91+
labels: { lineHeight: 0.75 },
92+
}}
93+
data={[{ name: "One" }, { name: "Two" }, { name: "Three" }]}
94+
/>
95+
</Wrapper>
96+
</div>
97+
);
98+
};
99+
73100
export const Title = () => {
74101
return (
75102
<div style={containerStyle}>

0 commit comments

Comments
 (0)