Simple Solid.js Component That Makes Titles More Readable
SolidJS: Simple and performant reactivity for building user interfaces.
SolidJS: Simple and performant reactivity for building user interfaces.
Solid Wrap Balancer avoids single hanging word on the last line
Getting Started
This is a port of react-wrap-balancer made by Shu Ding.
npm install solid-wrap-balancer
The simplest way is to wrap the text content with <Balancer>:
import { Balancer } from 'solid-wrap-balancer'
// ...
<h1>
<Balancer>My Title</Balancer>
</h1>
If you have multiple <Balancer> components used, it’s recommended (but optional) to use <BalancerProvider> to wrap the entire app. This will make them share the re-balance logic and reduce the HTML size:import { BalancerProvider } from 'solid-wrap-balancer'
// ...
<BalancerProvider>
<App/>
</BalancerProvider>
- Small bundle size
- Fast O(log n) algorithm
- Doesn’t cause layout shifts
- Works perfectly with web fonts
- SSR and streaming SSR supported
- SolidStart compatible
This library requires Solid.js, and IE 11 is not supported.
Custom Balance Ratio
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
Adjust the balance ratio to a custom value between 0 (loose) and 1 (compact, the default)
How Does It Work?
Solid Wrap 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
Useful in tooltips and other UI components
第六個沉思:論物質性東西的存在;論人的靈魂和肉體之間的實在區別
第六個沉思:論物質性東西的存在;論人的靈魂和肉體之間的實在區別
Left aligned, non-latin content
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
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):
It shows that when there are less than 100 elements with Solid 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 Solid Wrap Balancer
This is a port of react-wrap-balancer made by Shu Ding. This project was 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.