React Wrap Balancer

1.1.1 · active · verified Sun Apr 19

React Wrap Balancer is a React component designed to improve the readability of text, particularly titles and headings, by intelligently balancing line wraps to prevent awkward single-word lines or uneven text blocks. Its current stable version is 1.1.1, with an active release cadence that frequently introduces bug fixes and minor enhancements. A key differentiator is its ability to automatically detect and prioritize the native CSS `text-wrap: balance` property for optimal performance, falling back to a JavaScript-based solution when native browser support is unavailable. For applications utilizing multiple `<Balancer>` instances, the optional `<Provider>` component is recommended to centralize and share the re-balancing logic, which reduces HTML size and improves overall efficiency, especially when the native CSS feature is not available. The library ships with TypeScript types, ensuring a robust developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates wrapping a React application with `<Provider>` for shared re-balance logic and using `<Balancer>` to balance the text content of titles and subtitles, improving readability.

import { Provider } from 'react-wrap-balancer';
import Balancer from 'react-wrap-balancer';
import React from 'react';
import ReactDOM from 'react-dom/client';

function App() {
  return (
    <Provider>
      <div style={{ maxWidth: '400px', margin: '50px auto', fontFamily: 'sans-serif' }}>
        <h1>
          <Balancer>
            This is a very long title that needs to be perfectly balanced for optimal readability across various screen sizes.
          </Balancer>
        </h1>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        </p>
        <h2>
          <Balancer ratio={0.8}>
            Another balanced subtitle for improved typography.
          </Balancer>
        </h2>
      </div>
    </Provider>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root') ?? document.createElement('div'));
root.render(<App />);

view raw JSON →