### Install react-responsive-masonry Source: https://context7.com/cedricdelpoux/react-responsive-masonry/llms.txt Provides commands for installing the library using npm, yarn, or via a CDN. ```bash # npm npm install react-responsive-masonry --save # yarn yarn add react-responsive-masonry # CDN (UMD build) # ``` -------------------------------- ### Start Gatsby Development Server Source: https://github.com/cedricdelpoux/react-responsive-masonry/blob/master/example/README.md Navigate into your new Gatsby project directory and start the development server. This command builds the site and makes it available locally for development. ```shell cd my-hello-world-starter/ gatsby develop ``` -------------------------------- ### Basic Usage Example Source: https://context7.com/cedricdelpoux/react-responsive-masonry/llms.txt Demonstrates how to use ResponsiveMasonry with columnsCountBreakPoints and gutterBreakPoints to create a responsive image gallery. ```APIDOC ## Basic Usage Example ### Description This example shows a basic implementation of `ResponsiveMasonry` where both the number of columns and the gutter size change based on predefined breakpoints. ### Code ```jsx import React from "react" import Masonry, {ResponsiveMasonry} from "react-responsive-masonry" const images = [ "https://picsum.photos/200/300", "https://picsum.photos/400/400", "https://picsum.photos/400/300", "https://picsum.photos/200/200", "https://picsum.photos/500/400", "https://picsum.photos/400/500", "https://picsum.photos/200/300", "https://picsum.photos/300/300", "https://picsum.photos/300/300", ] const GalleryPage = () => ( {images.map((src, i) => ( ))} ) ``` ``` -------------------------------- ### Usage Example Source: https://github.com/cedricdelpoux/react-responsive-masonry/blob/master/README.md Illustrates how to use the Masonry and ResponsiveMasonry components in a React application. ```APIDOC ## Usage Example ### Responsive Layout Wrap `Masonry` with `ResponsiveMasonry` for dynamic column and gutter changes. ```js import React from "react" import Masonry, {ResponsiveMasonry} from "react-responsive-masonry" class MyWrapper extends React.Component { render() { return ( {/* Children */} ) } } ``` ### Fixed Layout Use `Masonry` directly for a fixed number of columns. ```js import React from "react" import Masonry from "react-responsive-masonry" class MyWrapper extends Component { render() { return ( {/* Children */} ) } } ``` ``` -------------------------------- ### Responsive Masonry Layout Example Source: https://github.com/cedricdelpoux/react-responsive-masonry/blob/master/demo/src/Examples/Responsive/index.md This example shows how to use ResponsiveMasonry with breakpoint configurations for columns and gutters. Import Masonry and ResponsiveMasonry from 'react-responsive-masonry'. Define an array of image sources and map them to img elements within the Masonry component. Configure columnsCountBreakPoints and gutterBreakPoints props on ResponsiveMasonry to control layout changes. ```javascript import React from "react" import Masonry, {ResponsiveMasonry} from "react-responsive-masonry" const images = [ "https://picsum.photos/200/300?image=1050", //... "https://picsum.photos/300/300?image=206", ] class MyWrapper extends React.Component { render() { return ( {images.map((image, i) => ( ))} ) } } ``` -------------------------------- ### TypeScript Usage Example Source: https://context7.com/cedricdelpoux/react-responsive-masonry/llms.txt Provides a comprehensive example of using ResponsiveMasonry with TypeScript, including type definitions for props. ```APIDOC ## TypeScript Usage Example ### Description This example demonstrates how to use `ResponsiveMasonry` and `Masonry` components with TypeScript, showcasing proper type imports and usage for `MasonryProps` and `ResponsiveMasonryProps`. ### Code ```tsx import React from "react" import Masonry, {ResponsiveMasonry} from "react-responsive-masonry" import type {MasonryProps, ResponsiveMasonryProps} from "react-responsive-masonry" const cardStyle: React.CSSProperties = { background: "#fff", borderRadius: 8, boxShadow: "0 1px 4px rgba(0,0,0,.1)", padding: 16, } interface Card { id: number title: string body: string } const cards: Card[] = [ {id: 1, title: "Card A", body: "Short content."}, {id: 2, title: "Card B", body: "A bit more content here that makes this card taller."}, {id: 3, title: "Card C", body: "Medium length content for demonstration."}, {id: 4, title: "Card D", body: "Even more content that makes this card the tallest of all."}, ] const masonryProps: MasonryProps = { columnsCount: 3, gutter: "16px", } const responsiveProps: ResponsiveMasonryProps = { columnsCountBreakPoints: {480: 1, 768: 2, 1024: 3}, gutterBreakPoints: {480: "8px", 768: "12px", 1024: "16px"}, } const CardMasonry: React.FC = () => ( {cards.map((card) => (

{card.title}

{card.body}

))}
) export default CardMasonry ``` ``` -------------------------------- ### SSR Gallery Example with ResponsiveMasonry Source: https://context7.com/cedricdelpoux/react-responsive-masonry/llms.txt This example demonstrates how to use ResponsiveMasonry in a Next.js application for server-side rendering. It configures breakpoint-specific column counts and gutters that adapt to viewport size after hydration. ```tsx import React from "react" import Masonry, {ResponsiveMasonry} from "react-responsive-masonry" // On server: renders 1 column (350px breakpoint is first/smallest) // After hydration: updates to 2 or 3 columns based on actual viewport const Gallery: React.FC = () => ( {Array.from({length: 9}, (_, i) => ( ))} ) export default Gallery ``` -------------------------------- ### Install react-responsive-masonry with Yarn or npm Source: https://github.com/cedricdelpoux/react-responsive-masonry/blob/master/README.md Use either yarn or npm to add the react-responsive-masonry package to your project dependencies. ```shell yarn add react-responsive-masonry npm install react-responsive-masonry --save ``` -------------------------------- ### Basic Masonry Layout with Images Source: https://github.com/cedricdelpoux/react-responsive-masonry/blob/master/demo/src/Examples/Masonry/index.md This snippet shows a basic implementation of a masonry layout using react-responsive-masonry. It displays a list of images in a three-column layout with a 10px gutter. Ensure you have React and react-responsive-masonry installed. ```javascript import React from "react" import Masonry from "react-responsive-masonry" const images = [ "https://picsum.photos/200/300?image=1050", //... "https://picsum.photos/300/300?image=206", ] class MyWrapper extends React.Component { render() { return ( {images.map((image, i) => ( ))} ) } } ``` -------------------------------- ### Basic Masonry Grid with React Source: https://context7.com/cedricdelpoux/react-responsive-masonry/llms.txt Renders children into a fixed number of flexbox columns. Items are redistributed by height after the initial render to balance column heights. Requires `react-responsive-masonry` to be installed. ```jsx import React from "react" import Masonry from "react-responsive-masonry" const images = [ "https://picsum.photos/200/300", "https://picsum.photos/400/400", "https://picsum.photos/400/300", "https://picsum.photos/200/200", "https://picsum.photos/500/400", "https://picsum.photos/400/500", ] // Basic 3-column masonry with a 10px gutter const BasicMasonry = () => ( {images.map((src, i) => ( ))} ) ``` ```jsx // Custom container/item HTML tags (e.g. semantic