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

simple-masonry-layout

v1.4.0

Published

Calculating masonry layouts based on rectangles, without being tied to the DOM.

Downloads

188

Readme

simple-masonry-layout Build Status

Calculating masonry layouts based on rectangles, without being tied to the DOM.

Installation

npm install simple-masonry-layout

Getting started

Importing

es6 import SimpleMasonry from 'simple-masonry-layout'

es5 var SimpleMasonry = require('simple-masonry-layout')

Browser global

When including simple-masonry.js inside of a <script> tag, SimpleMasonry will be attached to the window object.

Generating rectangles

SimpleMasonry only has a single public method: generateRectangles(options)

The only thing this method does is generate an array of rectangle objects which look like this:

  [{
    width: <number>,
    height: <number>,
    x: <number>,
    y: <number>
  }]

These rectangles will form your layout, it is up to you to render them on screen.

To do this generateRectangles will need you to give it an object with the following properties:

dimensions - Array

An array consisting of 'dimension' objects, which looks like this:

[{
  width: <number>,
  height: <number>
}]

The dimensions will be scaled proportionately so they'll fit inside of the columns.

columns - number (default: 3)

The number of columns you'd like to have.

width - number (default: 800)

The total width the layout should fill up.

gutter - number (default: 0)

The size of the gutters you'd like to have between elements.

gutterX - number (default: value of gutter)

The vertical gutter between elements.

gutterY - number (default: value of gutter)

The horizontal gutter between elements.

maxHeight - number (default: 0 - inactive)

The maximum height of each element.

collapsing - boolean (default: true)

If the rectangles should collapse into each other.

centering - boolean (default: false)
customize - function (default: (r) => r)

A function to customize the dimensions before they get positioned, simpleMasonry will map over this function. It should return the rectangle.

It receives the following arguments:

  • rectangle the current rectangle
  • i the current index
  • rectangles all rectangles
  • options the original options object passed

If number of columns is greater than number of rectangles, center the rectangles horizontally.

Code example

import SimpleMasonry from 'simple-masonry-layout'

// These values could come from anywhere, an api, measuring images in the browser etc.
const dimensions = [{
  width: 500,
  height: 800
}, {
  width: 900,
  height: 1000
},
...
]

const rectangles = SimpleMasonry.generateRectangles({
  dimensions,
  columns: 5,
  width: 1920,
  gutter: 20
})

// Now do something with the rectangles to render the layout

For a more detailed example please check out the examples directory.

React component

If you'd like to use it with react, please check out react-simple-masonry.