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

adapttext.js

v1.0.1

Published

adapttext.js is a dependency free and simple javascript solution capable to fit your text inside the parent element.

Downloads

5

Readme

:raised_hands: adapttext.js v1.0

adapttext.js is a dependency free and simple javascript solution capable to fit your text inside the parent element.

It calculates the maximum font-size possibile in order to keep the text's width and height inside the parent.

It's very useful in responsive situation and where we don't know in advance the text's length (text pulled from a CMS?). It is also very handy when we need to display text along an image and we want to avoid overlaps.

demo

Installation

adapttext.js supports AMD, CommonJS and Browser global (it uses UMD)

You can install it using bower: bower install adapttext.js --save-dev

You can install it using npm: npm install adapttext.js --save-dev

or just including the script in your page:

<script src="adapttext.js" type="text/javascript"></script>

Usage

new AdaptText(element, options);

Example:

<div id="mybox">
    <span>This is an example test...</span>
</div>
<style>
    #mybox {
        width: 50%;
        height: 30%;
    }
</style>
<script>
    var el = document.getElementById('mybox');
    var adapt = new AdaptText(el);
</script>

Using jQuery

If you're using jQuery in your project you can use

$('.mybox').adaptText(options);

If you need the adapttext.js instance later:

var instance = $('.mybox').data('adaptText');
instance.destroy();

Options

adapttext.js also accept some optional options:

new AdaptText(el, {
    minFontSize: 10, //define in px the minimum font size possible
    maxFontSize: 200, //define in px the maximum font size possible
    tollerance: 10, //define a number of px of allowed exceed
    callback: function(newpx) {
        //this callback is called everytime a new font size is set
        //the first argument is the new font-size px value
    }
});

CSS stuff to consider

  • Your parent element need to have both width and height.
  • The child will be automatically set as display: inline-block and margin: 0

Methods

.destroy()

Removes the event listener attached to the window resize.

.elaborate()

Force the recalculation of the correct font-size.

Why?

Most solutions (flowtype.js, fitText.js...) uses jquery as dependency and just fit the text based on parent's width, without the height constraint.

TODO

  • Debounce on resize event
  • Meke tests on various browsers

Other

adapttext.js is released under the MIT License and it's made by Luigi De Rosa.

The photo used in the demo is courtesy of https://unsplash.com/danielacuevas

Issue & Pull Requests are more than welcome!!