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

font unit, overflow updates and fixed examples #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samueljim
Copy link

  • Added stopOverflow. If there is too much text then the text will still overflow outside of the div at the smallest font size. Setting stopOverflow to true will add a .overflow class to the selected div if the text is overflowing. It also adds CSS which hides the text that is overflowing in order to keep the design of the page.
  • Added maxLines. Similar to stopOverflow but instead it adds the class if theres too many lines of text. This setting takes an input of false or a number of lines to apply the limit at. This needs to be improved but it's still useful to have now.
  • Added fontUnit. Changes the unit used in the minFontSize and maxFontSize. This allows units such as cm, mm, in, pt, pc, em, vw, vh, % and rem to be used instead of px.
  • Added fontChangeSize. This changes the amount of the font is changed by when trying to find the final font size. The default is 1 but changing this to a decimal is sometimes needed. When using rem or em font units 0.1 or 0.01 is a recommended change size.

Please let me know if you have any questions :)

* Added `stopOverflow`. If there is too much text then the text will still overflow outside of the div at the smallest font size. Setting stopOverflow to true will add a `.overflow` class to the seleted div if the text is overflowing. It also adds CSS which hides the text that is overflowing in order to keep the design of the page.
* Added `maxLines`. Similar to stopOverflow but instead it adds the class if there is too many lines of text. This setting takes an input of `false` or a number of lines to apply the limit at. This needs to be improved but it's still useful to have now.
* Added `fontUnit`. Changes the unit used in the minFontSize and maxFontSize. This allows units such as cm, mm, in, pt, pc, em, vw, vh, % and rem to be used instead of px.
* Added `fontChangeSize`. This changes the amount of the font is changed by when trying to find the final font size. The default is 1 but changing this to a decimal is sometimes needed. When using rem or em font units 0.1 or 0.01 is a recommended change size.
@samueljim samueljim changed the title 4 new settings and fixed examples font unit, overflow updates and fixed examples Jan 21, 2021
Copy link
Owner

@STRML STRML left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A few minor bits here. This is a pretty large change without a ton of code - bravo for that!

Please exclude the example files. I need to properly .gitignore them but they're only meant for gh-pages publishing.


if (settings.maxLines) {
if (Number.isInteger(settings.maxLines)) {
const computedStyle = window.getComputedStyle(innerSpan);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to avoid es6 for compatibility's sake

const fontSize = parseFloat(computedStyle.getPropertyValue('font-size'));
// If line-height is normal, we multiply the element's font-size with 1.14
if ( isNormal ) innerSpan.style.lineHeight = (fontSize * 1.14) + 'px';
const lineHeight = parseFloat(computedStyle.getPropertyValue('line-height'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const lineHeight = parseFloat(computedStyle.getPropertyValue('line-height'));
const lineHeightFloat = parseFloat(computedStyle.getPropertyValue('line-height'));
const lineHeight = Number.isFinite(lineHeightFloat) ? lineHeightFloat : parseFloat(computedStyle.getPropertyValue('font-size') * 1.14;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically can remove the double-read here and simplify the logic a bit by simply assuming fontSize * 1.14 if lineHeight is unreadable

// Setting the element's max-height
const limitHeight = (lineHeight * settings.maxLines);

innerSpan.style.maxHeight = limitHeight + 'px';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible try to batch all reads before all writes to avoid layout thrashing

@JacksonWolff
Copy link

JacksonWolff commented Jul 14, 2022

Really interested on this PR @STRML, any plans for bring the fontUnit options soon? Can I help you on that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants