React Broken Layouter
A lightweight React utility for creating responsive masonry-style layouts with automatic height estimation and column distribution.
Features
Simple and intuitive API
Easy to integrate and use with a straightforward API design
Responsive column-based layout
Adapts to different screen sizes with customizable breakpoints
Automatic height estimation
Intelligently calculates optimal item placement
Customizable gap between items
Control spacing between elements for the perfect layout
Support for dynamic content
Handles changing content gracefully
Zero dependencies
Lightweight with no external dependencies except React
TypeScript support
Full TypeScript type definitions included
Utility functions
Helpful utilities for common layout tasks
Environment Compatibility
This component is primarily designed for client-side use in React applications. While it doesn't use React hooks, it relies on client-side calculations for optimal layout and height estimation.
Recommended Usage
- β Client-side React applications
- β Browser environments
- β React 17+ and React 18+
- β Next.js client components (with 'use client' directive)
- β Client-side rendered pages
Server Component Usage
To use this component with server components in frameworks like Next.js, follow these guidelines:
Provide explicit heights via getHeight
or estimateHeight
props to ensure consistent layout:
Not Recommended
- βServer Components without 'use client' directive
- βServer-side rendering without proper client-side hydration
- βStatic site generation without client-side JavaScript
Considerations
- β οΈHeight estimation is most accurate in client environments
- β οΈFor server-side rendering, consider providing explicit heights via `getHeight` or `estimateHeight` props
- β οΈDynamic content may require client-side re-rendering for optimal layout
Installation
Usage
Basic Example
A simple setup with a fixed number of columns:
Custom Render
You can provide a custom render function that returns a component to display each item:
render
prop expects a component that accepts an item
prop. Using a function allows dynamic rendering while maintaining compatibility.Responsive Layout with breakpoints
Use the breakpoints
prop to override the default cols
value based on screen width, ideal for responsive layouts:
This dynamically sets:
- 1 column below 768px (using
cols
) - 2 columns from 768px to 1023px
- 3 columns from 1024px to 1439px
- 4 columns at 1440px and above
Responsive Visualization
Props
Prop | Type | Required | Default | Description |
---|---|---|---|---|
cols | number | β | - | Default number of columns (overridden by breakpoints if provided) |
breakpoints | { [width: number]: { cols: number } } | - | - | Map of viewport widths to column counts for responsive behavior |
items | any[] | β | - | List of items to display |
render | React.FC<{ item: any }> | β | - | Component to render each item (can be a function returning a component) |
gap | number | - | 16 | Gap between items (px) |
getId | (item: any) => string | number | - | - | Optional function to get unique ID for each item |
getHeight | (item: any) => number | - | - | Exact item height (for layout optimization) |
estimateHeight | (item: any) => number | - | - | Estimate item height if exact value isn't available |
mediaHeight | number | - | - | Additional height buffer (e.g., for images or media content) |
Utility Functions
These utility functions simplify common tasks like height estimation and ID generation:
heightEstimator
Estimates the total height based on an array of strings or numbers. Useful for calculating content heights.
getRandomId
Generates a random string ID. Useful for creating unique keys when none are provided.
isObject
Checks if a value is a plain object (not an array or null).
Experimental Features
Note: These features are experimental and may not work perfectly in all scenarios. Test thoroughly with your use case.
Height Estimation
The height estimation features (getHeight
and estimateHeight
) are currently experimental and may be subject to change in future versions. These features provide ways to control the height of items in the layout:
getHeight
: Allows you to provide exact heights for itemsestimateHeight
: Lets you implement custom height estimation logic
How It Works
The Layouter component:
- Estimates the height of each item based on content length
- Distributes items across columns to maintain balanced heights
- Renders items in a responsive grid layout
1. Height Estimation
2. Column Distribution
3. Responsive Rendering
Development
License & Contributing
MIT Β© Hassan Mohamed
Contributing
We'd love to improve React Broken Layouter with your help! Contributions are welcomeβplease feel free to submit a Pull Request or open an issue to discuss your ideas.