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.
npm install react-wrap-balancerVerified import paths — ran on the pinned version, not inferred.
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.
Be aware that the `ratio` prop may not apply in modern browsers. If you need a specific balance ratio regardless of native support, consider setting `preferNative={false}` on the `<Balancer>` component (though this is generally not recommended for performance).Wrap your root component or the highest common ancestor of your `<Balancer>` components with `<Provider>`: `<Provider><App /></Provider>`.
Ensure target browsers meet the minimum version requirements (Chrome 64+, Edge 79+, Safari 13.1+, Firefox 69+). For unsupported browsers, consider adding a `ResizeObserver` polyfill if necessary.
Pass a `nonce` prop to your `<Balancer>` component: `<Balancer nonce="your-csp-nonce">`.
Try to place `<Balancer>` inside block-level elements. If it must be within a flex or grid container, ensure the container's styles do not aggressively constrain the text element, or consider using a wrapper `span` or `div` inside the flex/grid item.
Upgrade your browser to a version that supports `ResizeObserver` (e.g., Chrome 64+, Firefox 69+, Safari 13.1+) or include a `ResizeObserver` polyfill in your project.
Ensure you are using `react-wrap-balancer` version `0.2.4` or higher, which includes a fix for hydration warnings on script tags. If using CSP, provide a `nonce` prop.
Upgrade to `react-wrap-balancer@1.1.0` or newer. Ensure `<Provider>` is wrapping your application. If you explicitly want to force the JavaScript fallback, set `preferNative={false}` on the `<Balancer>` component, though this is generally less performant.