Plock

The 1kB Masonry Grid for React.

README

react-plock


React Plock is a tree-shakeable ultra small npm package (less than 1kB gzipped) that allows you to create amazing masonry layouts with an amazing developer experience. With React Plock, you can easily create responsive and customizable layouts that adapt to different screen sizes and devices.

Features


- Masonry Layout: Create beautiful masonry layouts with ease.
- Responsive: Automatically adapts to different screen sizes and devices.
- Customizable: Customize the layout to match your needs.
- TypeScript Ready: Get the strength of type-safe languages.
- Amazing DX: Easy to use and well-documented.

Installation


  1. ```bash
  2. npm install react-plock
  3. ```

Usage


Using Plock with the new v3 APIs it's a piece of cake. Here's an example of how can you create an Unsplash-Like masonry grid. You can even see a demo of this example by clicking here.

  1. ```tsx
  2. import { Masonry } from 'react-plock';

  3. const ImagesMasonry = () => {
  4.   const items = [...imageUrls];

  5.   return (
  6.     <Masonry
  7.       items={items}
  8.       config={{
  9.         columns: [1, 2, 3],
  10.         gap: [24, 12, 6],
  11.         media: [640, 768, 1024],
  12.       }}
  13.       render={(item, idx) => (
  14.         <img key={idx} src={item} style={{ width: "100%", height: "auto" }} />
  15.       )}
  16.     />
  17.   );
  18. };
  19. ```

API Reference


Here's the TypeScript definition for the Masonry Component, below you can find a more detailed explanation.

  1. ```ts
  2. export type MasonryProps<T> = React.ComponentPropsWithoutRef<"div"> & {
  3.   items: T[];
  4.   render: (item: T, idx: number) => React.ReactNode;
  5.   config: {
  6.     columns: number | number[];
  7.     gap: number | number[];
  8.     media?: number[];
  9.   };
  10. };
  11. ```

Items


This prop accepts a generic array of elements, each one will be passed to the render property.

Render


The masonry render prop. Here's where you define the styles of every tile of the grid, the function takes the current looping item and the relative index.

Config


A configuration object that is used to define the number of columns, media queries and gaps between items.

Other Props


As you can see, by using `React.ComponentPropsWithoutRef<"div">` you can simply pass every available property to the div, some examples are **id** and **className**. The only one property that will be overwritten will be the `style` because is used internally for the masonry generation.

Important Note


Please, note that in case you are passing an array to the columns attribute of the config property, the number of elements MUST be equal to the number of media AND gap breakpoints provided!

  1. ```tsx
  2. // Correct: This will be responsive with 3 breakpoints.
  3. <Masonry
  4.   {...otherProps}
  5.   config={{
  6.     columns: [1, 2, 3],
  7.     gap: [12, 16, 20],
  8.     media: [640, 768, 1024],
  9.   }}
  10. />

  11. // Correct: This will be responsive with 2 breakpoints.
  12. <Masonry
  13.   {...otherProps}
  14.   config={{
  15.     columns: [1, 2],
  16.     gap: [2, 4],
  17.     media: [640, 1024],
  18.   }}
  19. />

  20. // Correct: This will be fixed with 4 columns in every screen size.
  21. <Masonry
  22.   {...otherProps}
  23.   config={{
  24.     columns: 4,
  25.     gap: 8
  26.   }}
  27. />

  28. // NOT Correct: This will cause trouble in rendering.
  29. <Masonry
  30.   {...otherProps}
  31.   config={{
  32.     columns: [4],
  33.     media: [640, 768],
  34.   }}
  35. />
  36. ```