Skip to content

Commit 119520b

Browse files
committed
apply pr fixing height calculation
rikschennink#69
1 parent e251efe commit 119520b

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/fitty.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,29 @@ export default ((w) => {
7070
// get available width from parent node
7171
f.availableWidth = f.element.parentNode.clientWidth;
7272

73-
// the space our target element uses
73+
// get available height from parent node
74+
f.availableHeight = f.element.parentNode.clientHeight;
75+
76+
// the width our target element uses
7477
f.currentWidth = f.element.scrollWidth;
7578

79+
// the height our target element uses
80+
f.currentHeight = f.element.scrollHeight;
81+
7682
// remember current font size
7783
f.previousFontSize = f.currentFontSize;
7884

79-
// let's calculate the new font size
85+
// let's calculate the new font size taking into account the width and
86+
// the height
8087
f.currentFontSize = Math.min(
81-
Math.max(f.minSize, (f.availableWidth / f.currentWidth) * f.previousFontSize),
82-
f.maxSize
88+
Math.min(
89+
Math.max(f.minSize, (f.availableWidth / f.currentWidth) * f.previousFontSize),
90+
f.maxSize
91+
),
92+
Math.min(
93+
Math.max(f.minSize, (f.availableHeight / f.currentHeight) * f.previousFontSize),
94+
f.maxSize
95+
)
8396
);
8497

8598
// if allows wrapping, only wrap when at minimum font size (otherwise would break container)
@@ -90,7 +103,8 @@ export default ((w) => {
90103
const shouldRedraw = (f) =>
91104
f.dirty !== DrawState.DIRTY_LAYOUT ||
92105
(f.dirty === DrawState.DIRTY_LAYOUT &&
93-
f.element.parentNode.clientWidth !== f.availableWidth);
106+
(f.element.parentNode.clientWidth !== f.availableWidth ||
107+
f.element.parentNode.clientHeight !== f.availableHeight));
94108

95109
// every fitty element is tested for invalid styles
96110
const computeStyle = (f) => {

0 commit comments

Comments
 (0)