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

auto-fit

v0.1.3

Published

Automatically fit text into its container, preventing overflow and underflow.

Downloads

6

Readme

AutoFit

Automatically fit text into its container, preventing overflow and underflow.

The algorithm uses computed width and height and therefore works for all font types and variations. It adjusts the font size of a given element so that it precisely fills its container. Not only text, it works for any elements with dimensions defined relative to font size (eg. width: 1em).

Live demo

React component AutoFit

The AutoFit component automatically re-runs when children changes and when the viewport resizes.

import { AutoFit } from 'auto-fit'

export const Title = ({ text }) => {
  return (
    <div style={{ maxWidth: '60%', margin: '0 auto' }}>
      <AutoFit>{text}</AutoFit>
    </div>
  )
}

AutoFit Props

| Name | Type | Default | Description | | --- | --- | --- | --- | | children | ReactNode | | The content to be auto fitted. | | multiline | boolean | false | Allow text to wrap and fit into both container width and height. | | minFontSizePx | number | 8 | The smallest font size the algorithm will use. | | maxFontSizePx | number | 160 | The largest font size the algorithm will use. | | as | string \| ReactComponent | 'div' | The underlying component that AutoFit will use. |

Low-level autoFit function

For advanced use cases, the autoFit function may be useful. It is used by the AutoFit component and has no dependencies.

import { autoFit } from 'auto-fit'

autoFit(options)

If you have many elements or re-run autoFit frequently, throttle is good for performance:

import { autoFit, throttleAnimationFrame } from 'auto-fit'

const throttledAutoFit = throttleAnimationFrame(() => autoFit(options))

throttledAutoFit() // Subsequent calls are reasonably throttled

autoFit options

| Name | Type | Default | Description | | --- | --- | --- | --- | | innerEl | HTMLElement | | The inner element to be auto fitted. | | containerEl | HTMLElement | | The container element is used as bounding box for the inner element. | | minFontSizePx | number | 8 | The smallest font size the algorithm will use. | | maxFontSizePx | number | 160 | The largest font size the algorithm will use. |

Details

  • The single-line algorithm predicts how the browser will render text in a different font size and iterates until converging with an error of <= 0.1px (usually 1-2 iterations).
  • The multi-line algorithm performs a binary search among the possible font sizes until converging with an error of <= 0.1px (usually ~10 iterations). There is no reliable way of predicting how the browser will render text in a different font size when multi-line text wrap is at play.
  • Performance Each iteration has a performance hit. AutoFit uses requestAnimationFrame to throttle repeated calls, ensuring that we render as often as possible without excessively blocking the UI.
  • No overflow. After usual iteration, the algorithms runs a second loop to ensure that no overflow occurs. Underflow is preferred since it doesn't look visually broken like overflow does.
  • Font size is used rather than transform scale since the latter wouldn't support multi-line text wrap. Transform scale would work for singleline but we prefer to keep the two modes similar. Furthermore, transform scale tends to make text blurry in some browsers.

Developing

When developing one typically wants to see the output in the example application without having to publish and install. We achieve this by linking the local package into the example app.

Because of issues with yarn link, we use Yalc. A linking approach is preferred over yarn workspaces since we want to run the package as it would appear in the real world.

npm i yalc -g
yarn
yarn watch

# Other terminal
cd example
yarn
yalc add --link auto-fit
yarn dev

Notes on Yalc

Using --link makes it so that Next.js can HMR the updates instantly. It creates a link: rather than a file: dependency.

Publishing

# Bump version number
yarn clean && yarn build
npm publish