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

columns.js

v1.1.4

Published

Lightweight masonry grid js

Downloads

100

Readme

columns.js

npm CircleCI Maintainability Test Coverage

A minimalist masonry layout is written in vanilla JS, with no dependencies.

columns.js creates a masonry grid by wrapping each column of items in an element. Masonry grid is then created without the need for absolute positioning nor css transforms.

Demo

https://columnsjs.com/

Pros

  • Blazing fast, vanilla js
  • Written as ES6 module, no dependencies
  • No inline styles, no absolute positioning, no transforms
  • Responsive support, minimal configuration
  • Multiple partitioning algorithms

Dependencies

No dependencies! Written in plain javascript.

Install

$ npm install columns.js

or

yarn add columns.js

Example

Following example demonstrates creating a masonry grid with 3 columns:

import Columns from 'columns.js';

let grid = new Columns(document.getElementById('columns'), {
  columns: 3
});
<div id="columns">
  <img src="https://picsum.photos/400/200/?random">
  <img src="https://picsum.photos/300/500/?random">
  ...
</div>

Dynamically append elements to the grid:

let item = new Image();
item.src = 'https://picsum.photos/400/200/?random';

grid.append(item);

Options

new Columns(document.getElementById('columns'), {
 /**
  * Number of columns (Mandatory)
  */
  columns: 1,

 /**
  * Responsive settings (Optional)
  *
  * Each key represents a screen size and each value
  * is corresponding number of columns
  *
  * Example:
  * < 480px             -> 1 column
  * > 480px and < 839px -> 3 columns
  * > 840px             -> 4 columns
  */
  breakpoints: {
    480: 3,
    840: 4
  },

 /**
  * Html class added to grid column elements (Optional)
  */
  column_class: 'column-js',

 /**
  * Choose algorithm used for partitioning elements into columns (Optional)
  *
  * 'greedy' (default) or 'chronological'
  */
  algorithm: 'greedy'
});

Partitioning

This section provides information on algorithms implemented by columns.js for partitioning elements into columns.

It currently supports two algorithms – greedy and chronological

Greedy algorithm - greedy (default)

The algorithm iterates through the elements in descending order, assigning each of them to whichever column has the smaller total height.

This approach reduces the difference between column heights while sacrificing the chronological order of items.

As an improvement, column.js implementation preserves chronological order of items within one column.

Chronological algorithm - chronological

The algorithm iterates through the elements, assigning them to columns in chronological order.

There is no optimization of column heights.

Methods

grid.count(); // Get current number of columns
grid.append(element); // Append new html element
grid.render(); // Re-flow the grid
grid.setOptions(options)

Styling

columns.js does not apply any inline styles. No absolute positioning. No transformations.

Instead, columns.js maintains columns data attribute on the wrapper element with a current column count value, which can be used for styling:

[data-columns] {
  display: flex;
  flex-wrap: wrap;
}

[data-columns="3"] > * {
  flex-basis: calc(100% / 3);
}

[data-columns="4"] > * {
  flex-basis: calc(100% / 4);
}

Here's a handy mixin:

@mixin columnsjs {
  display: flex;
  flex-wrap: wrap;

  @for $i from 1 through 9 {
    &[data-columns="#{$i}"] {

      & > * {
        flex-basis: calc(100% / #{$i});
      }
    }
  }
}

#columns {
  @include columnsjs;
}

IE Support

columns.js relies on modern ES6 features, some of which are not supported by IE. Polyfills for following methods are needed:

  • NodeList.prototype.forEach
  • Array.from
  • Object.entries
  • Element.prototype.append

Thanks @hybridvision for composing the list. :)

To Do

  1. Removing an element from the grid
  2. Event system