

GitHub - itsrennyman/react-plock: The 1kB Masonry Grid for React.
source link: https://github.com/itsrennyman/react-plock
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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
npm install react-plock
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.
import { Masonry } from 'react-plock';
const ImagesMasonry = () => {
const items = [...imageUrls];
return (
<Masonry
items={items}
config={{
columns: [1, 2, 3],
gap: [24, 12, 6],
media: [640, 768, 1024],
}}
render={(item, idx) => (
<img key={idx} src={item} style={{ width: "100%", height: "auto" }} />
)}
/>
);
};
API Reference
Here's the TypeScript definition for the Masonry Component, below you can find a more detailed explanation.
export type MasonryProps<T> = React.ComponentPropsWithoutRef<"div"> & {
items: T[];
render: (item: T, idx: number) => React.ReactNode;
config: {
columns: number | number[];
gap: number | number[];
media?: number[];
};
};
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!
// Correct: This will be responsive with 3 breakpoints.
<Masonry
{...otherProps}
config={{
columns: [1, 2, 3],
gap: [12, 16, 20],
media: [640, 768, 1024],
}}
/>
// Correct: This will be responsive with 2 breakpoints.
<Masonry
{...otherProps}
config={{
columns: [1, 2],
gap: [2, 4],
media: [640, 1024],
}}
/>
// Correct: This will be fixed with 4 columns in every screen size.
<Masonry
{...otherProps}
config={{
columns: 4,
gap: 8
}}
/>
// NOT Correct: This will cause trouble in rendering.
<Masonry
{...otherProps}
config={{
columns: [4],
media: [640, 768],
}}
/>
</article
Recommend
-
90
前面写了一篇文章,介绍FlexLib这个布局库,见IOS新一代界面开发利器 —— FlexLib。然后有很多网友问相比于Masonry有什么优势,今天就简单对比一下两者优劣。 Masonry是一个非常优秀的开源库,可以说是Masonry的出现拯救了Autola
-
80
-
5
react-native-masonry-list Pinterest like listview made in React Native. It just behaves like the FlatList so it is easy to use. Installation yarn add @react-native-seoul/masonry-list
-
8
react-native-masonry-list Pinterest like listview made in React Native. It just behaves like the FlatList so it is easy to use. Installation yarn add @react-native-seoul/masonry-list
-
13
Nov 16th, 2020Hasty Treat - CSS Grid Masonry (Grid Level 3)? Download Show✏️ Edit...
-
8
Snippets Photo Shoot Create beautiful code snippets from your VSCode environment.
-
7
Would you like to support me? react-native-masonry-grid Customizable masonry Flatlist. it just behave like Flatlist but using
-
11
-
11
Clean Architecture React This project is an experiment in applying clean architecture with React. Important Inside a React context, Clean Architecture will always be some kind of adaption. The key is separation of concerns...
-
3
Repository files navigationPlock Use an LLM (or anything else that can stream to stdout) directly from literally anywhere you can type. Outputs in real time. Write a prompt, select it, and (by default) hit
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK