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

flexmasonry

v0.2.3

Published

A lightweight masonry (cascading grid layout) library powered by flexbox

Downloads

4,783

Readme

npm

FlexMasonry

FlexMasonry is a lightweight, zero-dependency, masonry (cascading grid layout) library powered by CSS flexbox. The library itself is inspired by this article by Tobias Ahlin on using flex, :nth-child(), and order to create a pure CSS masonry layout (as opposed to the hugely popular Masonry library by David DeSandro that is powered by Javascript). I've taken this concept and sprinkled in some Javascript to tie it all together and make it easy to use.

Features

  • Lightweight - Just 6KB of JS and CSS
  • Fast - Uses CSS flexbox for layout
  • Responsive - Show different number of columns at different breakpoints
  • Simple - Just 3 options

Install

npm install flexmasonry
yarn add flexmasonry

Then, include the flexmasonry.js and flexmasonry.css files from the dist folder in your HTML. Or you can use the files directly from a CDN:

<link rel="stylesheet" href="https://unpkg.com/flexmasonry/dist/flexmasonry.css">
<script src="https://unpkg.com/flexmasonry/dist/flexmasonry.js"></script>

Usage

Set up your HTML. For example:

<div class="grid">
    <div><img src="https://source.unsplash.com/t3DHojIo-08" alt=""></div>
    <div><img src="https://source.unsplash.com/Imc-IoZDMXc" alt=""></div>
    <div><img src="https://source.unsplash.com/SOZWHqeXcPQ" alt=""></div>
    <div><img src="https://source.unsplash.com/bkdzvgBB7rQ" alt=""></div>
    <div><img src="https://source.unsplash.com/Aruugw_rJCM" alt=""></div>
</div>

Then hook up the script, passing in the selector target:

FlexMasonry.init('.grid');

FlexMasonry will then convert all .grid elements to masonry grids will multiple columns.

Options

The second, optional, parameter of the init method is an object containing options. The default options are as follows:

{
    /*
     * If `responsive` is `true`, `breakpointCols` will be used to determine
     * how many columns a grid should have at a given responsive breakpoint.
     */
    responsive: true,
    /*
     * A list of how many columns should be shown at different responsive
     * breakpoints, defined by media queries.
     */
    breakpointCols: {
        'min-width: 1500px': 6,
        'min-width: 1200px': 5,
        'min-width: 992px': 4,
        'min-width: 768px': 3,
        'min-width: 576px': 2,
    },
    /*
     * If `responsive` is `false`, this number of columns will always be shown,
     * no matter the width of the screen.
     */
    numCols: 4,
}

For example, to always shown 6 columns in your grid:

FlexMasonry.init('.grid', {
    responsive: false,
    numCols: 6
});

Methods

The FlexMasonry variable has several methods:

init(targets, options = {})

Initialises the FlexMasonry library and sets up the targets as masonry grids.

  • targets can be a string, an array of elements or a Node​List.
  • options see above.

refresh(target, options = {})

Refreshes the target grid layout.

  • targets can be a string, an array of elements or a Node​List.
  • options see above.

refreshAll(options = {})

Refreshes the grid layouts of all targets passed to init().

destroyAll()

Removes the event listeners for all targets passed to init().

Development

Run yarn to install the dependencies and use demo/index.html to test things. To watch/build the library:

yarn watch
yarn build

FAQ

Why not just use pure CSS?

A good question! You can use pure CSS to achieve the same outcome. However, there are several aspects of this setup that require a bit of "dynamic" updating to make it flexible and easy to use (hence the use of Javascript). The main one being that the masonry container requires a fixed height (which FlexMasonry calculates on the fly). Also the masonry container needs a certain number of "break" elements to work properly depending on the number of columns. To enable this, and to support having a different number of columns at different responsive breakpoints, we need Javascript.

Credits

FlexMasonry was created by Gilbert Pellegrom from Dev7studios. Released under the MIT license.