npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

textfit-web-component

v1.1.0

Published

Framework agnostic text fitting web component that can be used to ensure that a number of lines of text is rendered on screen by reducing or increasing font size and also help users detect and handle text with overflows. This also is a very functional lin

Downloads

32

Readme

textfitting example

textfit

npm version

The worlds only library for text fitting that can limit by number of lines as well as by container with support for dynamic font size and dynamic line counting. This package uses an overly complicated algorithm to determine the font size and line count of the text but it worked for any font and framework. Most of the issues you will run into are related to a parent block type elements not allowing the text to grow in size after it has shrunk the font size, You can avoid this by making the parent element have a width of 100%. There also will be issues with putting this around elements that use rem or px units for font sizing as they set the font size based on the root font size. You can fix this by putting textfit inside of the element that sets the font size or using em units. There are some issues with webpack not understanding web components but you can ignore the warnings as this JS logic exists outside of react/angular/vue etc. This component just extends the HTML object to add a new tag that can be used just like a div or h1 tag.

usage examples

<script type="module">import "./node_modules/textfit-web-component/textfit.js"</script>
<!-- or -->
<script type="module" src="https://cdn.jsdelivr.net/npm/[email protected]/textfit.js"></script>

<body>
    <div>
        <text-fit max-lines="1">
            hello world
        </text-fit>
    </div>
</body>

you can also use this in a framework like react as long as it's imported before it's used since it's just adding a new html tag to the dom. This can be in the html file, top level index.js entry point or even in the same file.

import 'textfit-web-component'

const ReactComponent = () => {
    return (
        <div>
            <text-fit max-lines="1">
                hello react world
            </text-fit>
        </div>
    )
}

Props

| Props | Description | Default value | |----------|-----|-----| | max-lines | The maximum number of lines that should be allowed to be rendered. The font size will reduce until it hits the minimum font size. | undefined | | max-height | The maximum that the element should be allowed to be before the font size gets reduced. The possible values are parent, outerbox, innerbox, css, self, or a integer in pixels or % of viewport. The font size will reduce until it fits or the min font size is hit | undefined | | max-font-size | The maximum allowed font size that the resizing can set to | 100 | | min-font-size | The minimum allowed font size that the resizing can set to | 20 | | font-unit | The unit that the font size should be set to. This can be any valid css unit | % | | classname | The class name that should be applied to the element | undefined | | log-overflow-error | If the text is too large to fit in the element then it will log an error message for debugging | false | | debounce-time | The amount of time in milliseconds that the resize event should be debounced by | 0 | | dont-grow-in-height | Prevent the element from getting larger than its initial size. This can create problems as if you resize the initial size will become the current size meaning it'll never grow in size | false | | disable-dynamic-font-size | Disable the dynamic font size and only use this for line counting | false | | debug-overflow-error | If the text is too large it will show a red box over the element to help visualize when overflow is happening | false |

Advanced examples 🫶

if you want a element to be based of the hight of something else then you can use the max-height props. There is a range of different max height limiters that have been provided in order to allow their system to work in all use cases.

`parent` It sets the max height based on if this element is larger than its direct parent. 
<div style="height: 20px;">
    <text-fit max-height="parent">
        max height based on parent
    </text-fit>
</div>

outerbox lets you check if this element is larger than its parent using a bounding box.

<div style="height: 30px;">
    <text-fit max-height="outerbox">
        max height based on parent's bounding box
    </text-fit>
</div>

innerbox lets you check if any child element is larger than the textfit element. This is useful in situations where you have elements other than text also inside the component.

<text-fit max-height="innerbox">
    <i class='icon'/>
    max height based on parent's bounding box
</text-fit>

self Checks if the element's scroll height is larger than its height. This is a classic overflow where scroll bars would appear.

<text-fit max-height="self">
    max height based on bounding box of the element inside 
</text-fit>

css This is the default value. It will use the css height value that has been set on the element. This is useful if you want to set a max height in css and then use that value.

<style>
    .textbox {
        max-height: 200px;
    }
</style>
<text-fit max-height="css" classname="textbox">
    max height based on css max-height value
</text-fit>

% is a special value that will set the max height to be a % of the viewport height. this is most useful for doing header text

<text-fit max-height="30%" max-font-size="2000">
    text that will be at most 30% of the viewport height or 2000% larger than initial font size
</text-fit>

You can also pass in a integer in order to set a manual max height

<div style="height: 200px;">
    <text-fit max-height="300">
        max height is 300px 
    </text-fit>
</div>

you can use both max-height and max-lines together. This will allow you to set a max height and then have the text fit into that max height by reducing the font size. This is useful if you want to have a title that is a maximum of 3 lines but also stays withing the bounds of the parent element.

  <text-fit max-lines="3" max-font-size="2000" max-height="30%" >
   text that will be at most 3 lines and 30% of the viewport height or 2000% larger than initial font size
  </text-fit>

if you just want to use textfit for line counting you can disable the font resizing with the disable-dynamic-font-size prop

<text-fit disable-dynamic-font-size id="target">
    hello world
</text-fit>

<script>
    document.querySelector('#id').lineCount; // 1
</script>

Thanks for reading. Please let me know if you find this useful or if you need any changes done ❤️

i also made a URL shorter that just uses emojis https://bne.sh/🇨🇻🥩