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

balanced-text

v1.1.6

Published

A performance-optimized script for balancing text in browser.

Downloads

32

Readme

Balanced Text

npm npm jsdeliver

balanced text demo

A performance-optimized script for balancing text in browser. text-wrap: balance is in the CSS Text Module Level 4 draft. This JS script is a polyfill and is dependency-free.

Initial benchmarks are faster than both Adobe's and NY Time's polyfills.

Demo

Quick Install

npm i balanced-text

Import

After installation, you can import the JS file into your project using this snippet:

import { balanceText } from 'balanced-text'

Then run:

balanceText()

HTML

<element class='has-text-balanced'>Lorem…</element>

CSS (optional)

/* For when the CSS spec is available */
.has-text-balanced {
    text-wrap: balance;
}

The JS will only run if your browser does not support text-wrap: balance.

CDN (Alternative)

https://www.jsdelivr.com/package/npm/balanced-text

<script src='https://cdn.jsdelivr.net/npm/balanced-text@latest/balance-text.min.js'>
    balanceText()
</script>

Options

Options are passed as an optional object.

balanceText({
    elements: '.has-text-balanced',
    watch: false,
    debounce: 200,
    lazyBalance: false,
    disableWait: false
})

Elements

Change which elements are balanced.

  • Type: String
  • Default: '.has-text-balanced'
balanceText({ elements: '.balance-text' })

Any string that works with document.querySelectorAll() is valid.

Watch

If the window is resized, rebalance the text.

  • Type: Boolean
  • Default: false
balanceText({ watch: true })

Debounce

When watch: true, balanceText is debounced by default. That reduces jank whenever the window is resized. Use debounce to change the timing.

  • Type: Integer (milliseconds)
  • Default: 200
balanceText({ debounce: 200 })

Set debounce to 0 to eliminate it.

Lazy Balance

If you have many elements on your page that need balanced text, consider enabling lazy balancing.

When set to true, balanceText will only affect visible elements. Using IntersectionObserver, text will be automatically balanced when it enters the viewport. Because balanceText is fast, it should not introduce scroll jank.

  • Type: Boolean
  • Default: false
balanceText({ lazyBalance: true })

Disable Wait

By default, balanceText waits until the main thread is idle (see Timing). Enabling this option will make balanceText run as soon as possible. It may become render blocking. However, it can prevent the "flash" of unbalanced text.

  • Type: Boolean
  • Default: False
balanceText({ disableWait: true })

How it works

  1. Wraps every word in a span
  2. Gets the width of every word and space
  3. Calculates the average line length
  4. Places a br tag where the line breaks should go

This limits many performance drawbacks of other algorithms.

Limitations

This script does assume a few things about the HTML contents:

  • The HTML elements only contain text (no sub elements, including b, strong, a tags). Will be fixed in future versions.
  • The only line-break opportunities are spaces ' '

Timing

The script uses requestIdleCallback if available (~75% support). This reduces the likelihood that it interrupts important functions.

If not, it uses requestAnimationFrame to minimize the chances of dropping a frame.

requestAnimationFrame is always used during lazyBalancing to minimize scroll jank.

Report Bug / Feature Request

https://github.com/Nick-Mazuk/balanced-text/issues

Copyright and license Github

©2020 Nick Mazuk. Code released under the MIT license.