npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-masonify

v0.0.2

Published

Lightweight React component for creating responsive Masonry layouts

Downloads

164

Readme

React Masonify

A lightweight React component library for creating responsive Masonry layouts.

Installation

npm install react-masonify
# or
yarn add react-masonify

Features

  • 🎯 Responsive grid layouts
  • 🪶 Lightweight implementation
  • ⚡ Performance optimized
  • 🔄 Dynamic column redistribution
  • 📱 Breakpoint-based responsiveness
  • 🎨 Customizable gaps and styling

Components

ResponsiveMasonry

A wrapper component that handles responsive behavior based on screen width breakpoints.

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | columnsCountBreakPoints | { [key: number]: number } | { 350: 1, 750: 2, 900: 3 } | Object defining breakpoints and their corresponding column counts | | children | React.ReactNode | Required | Should be a Masonry component | | className | string \| null | null | Optional CSS class name | | style | CSSProperties \| null | null | Optional inline styles |

Masonry

The core component that creates the masonry layout.

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | children | React.ReactNode | Required | Items to be arranged in the masonry layout | | columnsCount | number | 3 | Number of columns (automatically set when used with ResponsiveMasonry) | | gap | string | "0" | Gap between items (CSS value) | | className | string \| null | null | Optional CSS class name | | style | React.CSSProperties | {} | Optional inline styles | | containerTag | keyof JSX.IntrinsicElements | "div" | HTML tag for the container element | | itemTag | keyof JSX.IntrinsicElements | "div" | HTML tag for the items | | itemStyle | React.CSSProperties | {} | Styles applied to each item container | | sequential | boolean | false | If true, items are arranged sequentially instead of by height |

Basic Usage

import { Masonry, ResponsiveMasonry } from 'react-masonify';

const Gallery = () => {
  return (
    <ResponsiveMasonry
      columnsCountBreakPoints={{ 350: 1, 750: 2, 900: 3 }}
    >
      <Masonry gap="1rem">
        {images.map((src) => (
          <img src={src} key={src} alt="" />
        ))}
      </Masonry>
    </ResponsiveMasonry>
  );
};

Advanced Usage

Custom Breakpoints

const CustomGallery = () => {
  return (
    <ResponsiveMasonry
      columnsCountBreakPoints={{
        320: 1,  // 1 column for viewport width >= 320px
        480: 2,  // 2 columns for viewport width >= 480px
        720: 3,  // 3 columns for viewport width >= 720px
        1024: 4, // 4 columns for viewport width >= 1024px
      }}
    >
      <Masonry gap="1.5rem" itemTag="section">
        {items.map((item) => (
          <div key={item.id}>
            {/* Your content */}
          </div>
        ))}
      </Masonry>
    </ResponsiveMasonry>
  );
};

Custom Styling

const StyledGallery = () => {
  return (
    <ResponsiveMasonry
      style={{ padding: '20px' }}
      className="gallery-container"
    >
      <Masonry
        gap="2rem"
        itemStyle={{
          backgroundColor: '#f5f5f5',
          borderRadius: '8px',
          padding: '10px'
        }}
        style={{
          maxWidth: '1200px',
          margin: '0 auto'
        }}
      >
        {items.map((item) => (
          <div key={item.id}>
            {/* Your content */}
          </div>
        ))}
      </Masonry>
    </ResponsiveMasonry>
  );
};

How It Works

  1. ResponsiveMasonry monitors the window width and determines the appropriate number of columns based on the provided breakpoints.

  2. Masonry distributes children across columns using one of two strategies:

    • Default (sequential=false): Distributes items based on their heights to maintain balanced columns
    • Sequential (sequential=true): Distributes items sequentially from left to right
  3. The layout automatically updates when:

    • The window is resized
    • Children are added or removed
    • The columns count changes

Browser Support

React Masonify works in all modern browsers that support CSS Flexbox:

  • Chrome/Edge (latest)
  • Firefox (latest)
  • Safari (latest)
  • Opera (latest)

TypeScript Support

React Masonify is written in TypeScript and includes built-in type definitions.

Performance Considerations

  • Use the key prop with unique values for each child to ensure proper updates
  • Avoid frequent changes to columnsCount or gap props
  • Consider using sequential={true} for simpler layouts or when performance is crucial
  • Use appropriate image sizes to prevent layout shifts

License

MIT License