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

quartz-layout

v0.1.11

Published

Responsive pinterest-style layouts with balanced column heights.

Downloads

1

Readme

Build Status Coverage Status npm version

Quartz

Quartz is a javascript utility for producing responsive pinterest-style layouts. Quartz prioritizes balanced column heights over strict item ordering.

  • View text example
  • View image pre-loading example

Features

  • API for appending, prepending, and removing items
  • API for manual refresh of layout
  • Balanced column heights
  • All styling is controlled by your CSS
  • Adjusts column count by responding to MediaQueryList events
  • No dependencies for modern browsers

Installation

bower

bower install quartz

npm

npm install quartz-layout

Using Quartz

Define your HTML — a container element is required. The container can be empty, or pre-populated with items as show below. Use your own class names for the container and items.

<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

Include the javascript files. For IE 9 support, include the provided match-media.min.js polyfill, which is a concatenated and minified copy of matchMedia.js.

<!--[if lt IE 10]>
<script src="match-media.min.js"></script>
<![endif]-->
<script src="quartz.min.js"></script>

Setup your CSS grid as you normally would – for example:

@media screen {
  .column {
    width: 100%;
  }
}

@media screen and (min-width: 40em) {
  .column {
    width: 50%;
  }
}

@media screen and (min-width: 50em) {
  .column {
    width: 33.3333333333%;
  }
}

Initialize Quartz with your configuration settings.

var config = {
  container: '.container',
  items: '.item',
  columnClass: 'column',
  mediaQueries: [
    // max-width is required for smallest size
    {query: 'screen and (max-width: 39.99em)', columns: 1},
    // both min- and max-width are required for intermediate sizes
    {query: 'screen and (min-width: 40em) and (max-width: 49.99em)', columns: 2},
    {query: 'screen and (min-width: 50em)', columns: 3}
  ]
}

var quartz = new Quartz(config)

Using Quartz with images

In order for Quartz to maintain balanced column heights, images must be pre-loaded so that their heights can be included in the overall item height calculations.

One way to accomplish this is to use a utility like ImagesReady. For example:

$('.item')
    .imagesReady()
    .then(function(items){ // `items` is $('.item')
      // instantiate Quartz
      quartz = new Quartz({
        container: '.container',
        items: items,
        columnClass: 'items__column column',
        mediaQueries: [
          {query: 'screen and (max-width: 39.99em)', columns: 1},
          {query: 'screen and (min-width: 40em) and (max-width: 49.99em)', columns: 2},
          {query: 'screen and (min-width: 50em)', columns: 3}
        ]
      });
    });

Configuration Options

var config = {}

config.container

Required

// CSS selector
config.container = '.container'

// HTMLElement
config.container = document.querySelector('.container')

// jQuery
config.container = $('#container')[0]

config.items

Required

// CSS selector
config.items = '.item'

// Array of HTMLElement
config.items = [document.querySelector('#item-1'), document.querySelector('#item-2')]

// NodeList
config.items = document.querySelectorAll('.item')

// jQuery
config.items = $('.item')

config.columnClass

Required

// CSS class(es) to be applied to columns
config.columnClass = 'column foo'

config.mediaQueries

Required

  • List media queries in order, from smallest to largest.
  • max-width is required for smallest size
  • Both min-width and max-width are required for intermediate sizes

If your CSS looks like this:

@media screen {
  .column { width: 100%; } /* 1 column */
}

@media screen and (min-width: 40em) {
  .column { width: 50%; } /* 2 columns */
}

@media screen and (min-width: 50em) {
  .column { width: 33.3333333333%; } /* 3 columns */
}

Your configuration should look like this:

config.mediaQueries = [
  {query: 'screen and (max-width: 39.99em)', columns: 1},
  {query: 'screen and (min-width: 40em) and (max-width: 49.99em)', columns: 2},
  {query: 'screen and (min-width: 50em)', columns: 3}
]

API

quartz.append(items)

Add the provided item(s) to the end of the layout.

// `items` as a single HTMLElement
var item = document.querySelector('.item');
quartz.append(item);

// `items` as an array of HTMLElement
var items = [document.querySelector('#item-1'), document.querySelector('#item-2')];
quartz.append(items);

// `items` as a NodeList
var items = document.querySelectorAll('.item');
quartz.append(items);

// `items` as jQuery
quartz.append($('.item'));

quartz.prepend(items)

Add the provided item(s) to the beginning of the layout.

// `items` as a single HTMLElement
var item = document.querySelector('.item');
quartz.prepend(item);

// `items` as an array of HTMLElement
var items = [document.querySelector('#item-1'), document.querySelector('#item-2')];
quartz.prepend(items);

// `items` as a NodeList
var items = document.querySelectorAll('.item');
quartz.prepend(items);

// `items` as jQuery
quartz.prepend($('.item'));

quartz.remove(items)

Remove the provided item(s) from the layout.

// `items` as a single HTMLElement
var item = document.querySelector('#item-4');
quartz.remove(item);

// `items` as an array of HTMLElement
var items = [document.querySelector('#item-2'), document.querySelector('#item-6')];
quartz.remove(items);

// `items` as a NodeList
var items = document.querySelectorAll('.item--disabled');
quartz.remove(items);

// `items` as jQuery
quartz.remove($('.item--saved'));

quartz.removeAll()

Remove all items from the layout.

quartz.removeAll();

quartz.update([columnCount])

Force an update of the layout – all heights will be re-evaluated. Passing an optional columnCount will change the number of columns in the layout.

// manually refresh the current layout
quartz.update();

// change the current layout to a two column layout
quartz.update(2);

Browser Support

  • Chrome
  • Firefox
  • IE 9+
  • Safari

NOTE: IE 9 requires the provided matchMedia polyfill

Module Support

  • AMD
  • CommonJS
  • Browser global

License

Quartz is free to use under the open-source MIT license.