Top Related Projects
⬢ Style props for rapid UI development
Quick Overview
The react-wrap-balancer
library is a React component that helps balance the text content within a container, ensuring that the lines of text are evenly distributed and visually appealing. It is particularly useful for creating responsive and well-balanced layouts in web applications.
Pros
- Automatic Text Balancing: The library automatically adjusts the text content to ensure that the lines are evenly distributed, creating a visually appealing layout.
- Responsive Design: The text balancing feature works seamlessly across different screen sizes, making it suitable for responsive web design.
- Easy Integration: The library can be easily integrated into existing React projects, with a simple and straightforward API.
- Customizable Behavior: The library provides various configuration options to fine-tune the text balancing behavior, allowing developers to tailor it to their specific needs.
Cons
- Dependency on React: The library is designed specifically for React, which may limit its usage in non-React projects.
- Performance Considerations: Depending on the complexity of the text content and the number of elements being balanced, the library may have a slight performance impact on the application.
- Limited Functionality: The library is focused solely on text balancing and does not provide additional features or functionality beyond that.
- Potential Compatibility Issues: As with any third-party library, there may be potential compatibility issues with certain versions of React or other dependencies.
Code Examples
Here are a few examples of how to use the react-wrap-balancer
library:
import React from 'react';
import Balancer from 'react-wrap-balancer';
const MyComponent = () => {
return (
<div>
<Balancer>
This is a long sentence that will be balanced across multiple lines.
</Balancer>
</div>
);
};
In this example, the Balancer
component is used to wrap the text content, and the library will automatically balance the lines of text.
import React from 'react';
import Balancer from 'react-wrap-balancer';
const MyComponent = () => {
return (
<div>
<Balancer
style={{
fontSize: '2rem',
fontWeight: 'bold',
color: '#333',
}}
>
Customized Text Balancing
</Balancer>
</div>
);
};
In this example, the Balancer
component is used with custom styles, allowing you to control the appearance of the balanced text.
import React from 'react';
import Balancer from 'react-wrap-balancer';
const MyComponent = () => {
return (
<div>
<Balancer
maxLines={2}
wrapperProps={{
className: 'my-custom-class',
}}
>
This text will be balanced, but limited to a maximum of 2 lines.
</Balancer>
</div>
);
};
In this example, the Balancer
component is used with additional props to limit the maximum number of lines and apply a custom class to the wrapper element.
Getting Started
To get started with the react-wrap-balancer
library, follow these steps:
- Install the library using npm or yarn:
npm install react-wrap-balancer
- Import the
Balancer
component in your React component:
import Balancer from 'react-wrap-balancer';
- Use the
Balancer
component to wrap the text content you want to balance:
<Balancer>
This is a long sentence that will be balanced across multiple lines.
</Balancer>
- (Optional) Customize the behavior of the
Balancer
component by using the available props:
<Balancer
maxLines={2}
wrapperProps={{
className: 'my-custom-class',
}}
>
This text will be balanced, but limited to a maximum of 2 lines.
</Balancer>
For more detailed information and advanced usage, please refer to the [project's documentation](https://github.com
Competitor Comparisons
⬢ Style props for rapid UI development
Pros of styled-system/styled-system
- Provides a comprehensive set of utility functions for applying styles based on the theme and responsive design.
- Supports a wide range of CSS properties, making it a versatile tool for building complex UI components.
- Integrates well with other popular CSS-in-JS libraries like Emotion and Styled Components.
Cons of styled-system/styled-system
- The learning curve can be steeper compared to react-wrap-balancer, as it requires understanding the concept of "style props" and the library's API.
- The library can add more overhead to the final bundle size, depending on the number of style props used.
Code Comparison
react-wrap-balancer
import WrapBalancer from 'react-wrap-balancer';
const MyComponent = () => (
<WrapBalancer>
<h1>This is a long heading that needs to be balanced.</h1>
</WrapBalancer>
);
styled-system/styled-system
import styled from 'styled-components';
import { typography, color, space } from 'styled-system';
const Box = styled.div`
${typography}
${color}
${space}
`;
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Introduction
React Wrap Balancer is a simple React Component that makes your titles more readable in different viewport sizes. It improves the wrapping to avoid situations like single word in the last line, makes the content more âbalancedâ:
Usage
To start using the library, install it to your project:
npm i react-wrap-balancer
And wrap text content with it:
import Balancer from 'react-wrap-balancer'
// ...
function Title() {
return (
<h1>
<Balancer>My Awesome Title</Balancer>
</h1>
)
}
<Balancer>
<Balancer>
is the main component of the library. It will automatically balance the text content inside it. It accepts the following props:
as
(optional): The HTML tag to be used to wrap the text content. Default tospan
.ratio
(optional): The ratio of âbalance-nessâ, 0 <= ratio <= 1. Default to1
.preferNative
(optional): An option to skip the re-balance logic and use the native CSS text-balancing if supported. Default totrue
.nonce
(optional): The nonce attribute to allowlist inline script injection by the component.
<Provider>
If you have multiple <Balancer>
components used, itâs recommended (but optional) to also use
<Provider>
to wrap the entire app. This will make them share the re-balance logic and reduce the HTML size:
import { Provider } from 'react-wrap-balancer'
// ...
function App() {
return (
<Provider>
<MyApp/>
</Provider>
)
}
For full documentation and use cases, please visit react-wrap-balancer.vercel.app.
Browser Support Information
Desktop:
Browser | Min Version |
---|---|
Chrome | 64 |
Edge | 79 |
Safari | 13.1 |
FireFox | 69 |
Opera | 51 |
IE | No Support |
Mobile:
Browser | Min Version |
---|---|
Chrome | 64 |
Safari | 13.4 |
Firefox | 69 |
Opera | 47 |
WebView Android | 64 |
Cross-browser compatibility issues are mainly due to the fact that lib uses the ResizeObserver API. More information about this API can be found at this link.
If you are using a browser which version is lower than the versions in the table, please consider adding polyfill for this API or upgrade your browser.
About
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.
Special thanks to Emil Kowalski for testing and feedback.
Created by Shu Ding in 2022, released under the MIT license.
Top Related Projects
⬢ Style props for rapid UI development
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot