⚠️ This is a blatant copy of react-wrap-balancer.vercel.app

A Simple Svelte Action That Makes Titles More Readable

Default

Svelte: A cooler JavaScript library for building user interfaces

With Balancer

Svelte: A cooler JavaScript library for building user interfaces

Svelte Action Balancer avoids single hanging word on the last line

Getting Started

npm install svelte-action-balancer

You can just import and use the action:<script>
import { balancer } from 'svelte-action-balancer'
</script>

// ...

<h1
use:balancer>
My Title
</h1>

  • 158 B Gzipped
  • Fast O(log n) algorithm
  • Works perfectly with web fonts

View project on GitHub

Custom Balance Ratio

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog

use:balancer={{ ratio: 0.55 }}

Adjust the balance ratio to a custom value between 0 (loose) and 1 (compact, the default)

How Does It Work?

Svelte Action Balancer reduces the width of the content wrapper as much as it could, before causing an extra line break. When reaching the minimum width, each line will approximately have the same width, and look more compact and balanced.

Check out the GitHub Repository to learn more.

Use Cases

Default

第六個沉思:論物質性東西的存在;論人的靈魂和肉體之間的實在區別

With Balancer

第六個沉思:論物質性東西的存在;論人的靈魂和肉體之間的實在區別

Left aligned, non-latin content

Default
You have wakened not out of sleep, but into a prior dream, and that dream lies within another, and so on, to infinity, which is the number of grains of sand. The path that you are to take is endless, and you will die before you have truly awakened.
- Jorge Luis Borges
With Balancer
You have wakened not out of sleep, but into a prior dream, and that dream lies within another, and so on, to infinity, which is the number of grains of sand. The path that you are to take is endless, and you will die before you have truly awakened.
- Jorge Luis Borges

Makes multi-line content more compact with fewer visual changes when resizing

Performance Impact

It is worth to mention that this project is a workaround for the lack of native support for balanced text wrapping in CSS. It is not perfect as it adds some performance overhead. However, the performance impact is usually very trivial and can be ignored in most cases.

The following benchmark (source) is done by measuring the script execution time of X balanced titles when loading the webpage (raw data):

Benchmark result

It shows that when there are less than 100 elements with React Wrap Balancer in the initial HTML, the per-element impact to the page load time is less than 0.25 ms. When there are 1,000 elements, that number increases to ~1 ms. When there are 5,000 elements, the per-element script execution time becomes ~7 ms.

These numbers don’t scale linearly because re-layouts usually have an impact to other elements on the page. Hence the best practice is to only use this library for title elements when necessary, or use it for content that is behind user interactions (e.g. tooltips), to avoid negative impacts to the page performance.

About Svelte Action Balancer

This project was inspired by Shu Ding react-wrap-balancer which was in turn inspired by Adobe’s balance-text project, NYT’s text-balancer project, and Daniel Aleksandersen’s Improving the New York Times’ line wrap balancer. If you want to learn more, you can also take a look at the text-wrap: balance proposal.