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

Add container height as part of the calculation #69

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/fitty.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,38 @@ export default ((w) => {
// get available width from parent node
f.availableWidth = f.element.parentNode.clientWidth;

// the space our target element uses
// get available height from parent node
f.availableHeight = f.element.parentNode.clientHeight;

// the width our target element uses
f.currentWidth = f.element.scrollWidth;

// the height our target element uses
f.currentHeight = f.element.scrollHeight;

// remember current font size
f.previousFontSize = f.currentFontSize;

// let's calculate the new font size
// let's calculate the new font size taking into account the width and
// the height
f.currentFontSize = Math.min(
Math.max(
f.minSize,
(f.availableWidth / f.currentWidth) * f.previousFontSize
Math.min(
Math.max(
f.minSize,
(f.availableWidth / f.currentWidth) * f.previousFontSize
),
f.maxSize
),
f.maxSize
Math.min(
Math.max(
f.minSize,
(f.availableHeight / f.currentHeight) * f.previousFontSize
),
f.maxSize
)
);


// if allows wrapping, only wrap when at minimum font size (otherwise would break container)
f.whiteSpace = f.multiLine && f.currentFontSize === f.minSize
? 'normal'
Expand All @@ -93,7 +110,7 @@ export default ((w) => {
};

// should always redraw if is not dirty layout, if is dirty layout, only redraw if size has changed
const shouldRedraw = f => f.dirty !== DrawState.DIRTY_LAYOUT || (f.dirty === DrawState.DIRTY_LAYOUT && f.element.parentNode.clientWidth !== f.availableWidth);
const shouldRedraw = f => f.dirty !== DrawState.DIRTY_LAYOUT || (f.dirty === DrawState.DIRTY_LAYOUT && (f.element.parentNode.clientWidth !== f.availableWidth || f.element.parentNode.clientHeight !== f.availableHeight));

// every fitty element is tested for invalid styles
const computeStyle = f => {
Expand Down