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

flextype

v1.0.6

Published

Scale text against its container width using CSS.

Downloads

7

Readme

flextype

Build Status Build Status Codecov License: MIT

Scale the font size of an element in proportion to its width... in style. 😎

Basic example

Full demo here.

Features

  • Simple syntax for scaling at different amounts for different widths
  • Define your scaling ratios in CSS, allowing you to:
    • easily override within media queries
    • leverage whatever CSS-preprocessor variables you might be using for your site's layout and typography
  • No dependencies
  • Supports all modern browsers and IE9+[see caveat for IE/Edge]

Install

NPM

npm install flextype --save

Download

CDN

<!-- Minified: -->
<script src="https://unpkg.com/flextype/dist/flextype.min.js"></script>

<!-- Un-minified: -->
<script src="https://unpkg.com/flextype/dist/flextype.js"></script>

Basic usage

  1. Add the class js-flextype to the element(s) you want to scale.

    <div class="MyElement js-flextype">Hello</div>
  2. In CSS set the percentage of the .js-flextype element's width you want the font size to be on the CSS custom property --flextype. For instance, if you want the font size to be 18px when the element is 500px wide and to scale up and down from there at a 1:1 ratio, you would use the percentage 3.6%.

    .MyElement {
      width: 30%;
      float: left;
      /* ... */
      --flextype: 3.6%;
    }

Advanced usage

The ratio passed to flextype can also be expressed as a key-value pair written in JSON, where the key is an element width (in pixels) and the value is the desired corresponding font size (in pixels).

.MyElement {
  /* Use valid JSON wrapped in single quotes */
  --flextype: '{ "500": 18 }';
}

This syntax gives you access to two additional features: "Tweening" and "Locking".

Tweening

The font size can be made to scale at different ratios for different width-ranges, flextype will adjust the font size depending on which rules the element's width currently falls between.

.MyElement {
  --flextype: '{ "500": 18, "1000": 22 }';
}

Given those rules we can expect that, for example, when 250px wide .MyElement will have a font size of 9px and when 750px wide .MyElement will have a font size of 20px.

Locking

You can lock the font size in for particular width-ranges by using + and - modifiers on the width keys.

.MyElement {
  --flextype: '{ "250-": 10, "500+": 18, "1000": "22" "1500+": 36 }';
}

The +/- modifiers will prevent the font size from scaling until the next/previous rule becomes active, respectively.

API

Flextype also has a simple javascript interface, which for the most part you shouldn't have to interact with unless you want to do particularly custom stuff.

  • Manually force elements to scale outside of the window resize event

    myElement.style.width = '300px';
    flextype.flex();
  • Use a custom selector instead of .js-flextype

    const flexer = flextype(document.getElementsByClassName('💪'));
    // Use flexer.flex() to manually scale should you need to
    // Use flexer.destroy() to remove the resize listener and reset the font
  • Execute code on an element whenever flextype finishes scaling it

    myElement.addEventListener('flextype:changed', function () {
      // Cycle the hue every 12px change in font size
      const hue = ((parseFloat(this.style.fontSize) % 12) * 360) / 12;
      this.style.color = `hsl(${hue}, 100%, 50%)`;
    });
  • Get the suggested font size of an element based on its CSS rules and width

    flextype.getElSize(myElement);
  • Set the font size based on CSS rules and width

    flextype.setElSize(myElement);
  • Parse a set of rules against a width programmatically

    flextype.size({ '500-': 10, 1500: 20 }, 1000); // = 15

Caveats

1. IE/Edge support

CSS custom properties are supported in the latest versions of all the major browsers except IE and Edge. As a workaround flextype also accepts rules embedded in the ::before pseudo element's content property.

.MyElement::before {
  content: '3.6%';
  display: none;
}

You can (and probably should) use the flextype PostCSS plugin to convert your --flextype declarations into this format for you while Microsoft catches up.

2. Trigger flextype manually after javascript manipulations

The font size of any .js-flextype elements will be scaled immediately when flextype.js is loaded and then again whenever the window is resized. However, if you insert a new .js-flextype element into the DOM after flextype has already initialized, or if you resize a flextype container outside of the normal window resize event, you'll need to manually trigger the resize with:

flextype.flex();

3. Set a width

Flextype bases the font size off the width of the js-flextype element. If the width of the js-flextype element is itself based off the font size of its content (as it is with inline elements, for example) it can't work.