• Stars
    star
    4,657
  • Rank 8,833 (Top 0.2 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A blazing fast masonry layout generator for fixed width elements.

Bricks.js

Bricks.js on NPM Bricks.js Downloads on NPM Standard JavaScript Style

Momma said, "Stay patient." - Bricks, DJ Carnage

But you don't need to, because Bricks is a blazing fast masonry layout generator for fixed width elements.

Getting Started

Follow these steps:

  1. Install
  2. Instantiate
  3. Review Parameters
  4. Review API / Events
  5. Review Example Code

Install

Bricks was developed with a modern JavaScript workflow in mind. To use it, it's recommended you have a build system in place that can transpile ES6, and bundle modules. For a minimal boilerplate that does so, check out outset.

Using NPM, install Bricks.js, and add it to your package.json dependencies.

$ npm install bricks.js --save

Refer to the releases page for version specific information.

Instantiate

Simply import Bricks, then instantiate it.

It's recommended that you assign your Bricks instance to a variable. Using your instance, you can:

  • enable and disable the resize handler
  • add and remove event handlers
  • accommodate dynamically added elements
// import Bricks
import Bricks from 'bricks.js'

// create an instance
const instance = Bricks({
  // ...
})

Parameters passed to the constructor are detailed below.

Parameters

Note that all parameters, except position, are required:

container

A node, or CSS selector, that represents the grid wrapper. The direct children of this element must be the grid items.

// passing a node

const instance = Bricks({
  container: node
})

// passing a selector (document.querySelector is used to get the node)

const instance = Bricks({
  container: '.selector'
})

packed

An attribute added to the grid items after they're positioned within the grid. If the attribute is not prefixed with data-, it will be added.

// prefixed

const instance = Bricks({
  packed: 'data-packed'
})

// unprefixed

const instance = Bricks({
  // becomes: 'data-packed'
  packed: 'packed'
})

Note that Bricks uses this attribute internally to avoiding unnecessarily repositioning grid items already in place. It's best to avoid manipulating it.

sizes

An array of objects describing the grid's properties at different breakpoints.

When defining your sizes, note the following:

  • Sizes must be listed smallest to largest
  • Sizes must use min-width media queries (any unit)
  • Width of the grid items at each breakpoint should be set in your CSS (in px)
  • The size without the mq property is assumed to be your smallest breakpoint, and must appear first
// mq      - the minimum viewport width (String CSS unit: em, px, rem)
// columns - the number of vertical columns
// gutter  - the space (in px) between the columns and grid items

const sizes = [
  { columns: 2, gutter: 10 },
  { mq: '768px', columns: 3, gutter: 25 },
  { mq: '1024px', columns: 4, gutter: 50 }
]

const instance = Bricks({
  sizes: sizes
})

position

A boolean, defaulting to true, indicating that the grid items should be positioned using the top and left CSS properties.

If set to false, the grid items will be positioned using the transform CSS property.

// default ('true')
// grid items are positioned via the 'top' and 'left' properties

const instance = Bricks({
  position: true
})

// explicitly 'false' (not any falsy value!)
// grid items are positioned via the 'transform' property

const instance = Bricks({
  position: false
})

Positioning using transform is done via translate3d for optimal performance. Coupled with a CSS transition, this option allows for smoothly animating the grid items into place.

API / Events

Bricks instances are extended with Knot.js, a browser-based event emitter. Use the event emitter syntax to add and remove handlers for the events emitted by the API methods. Review the emitter syntax here.

Bricks exposes the following methods, and corresponding events:

Note that all methods, including those from the event emitter, are chainable.

.pack()

Used to pack all elements within the container.

// pack ALL grid items
instance.pack()

// 'pack' is emitted when ALL items have been packed
instance.on('pack', () => {
  // ...
})

Note that it should be called when creating your instance, to pack the initial items.

.update()

Used to pack elements without the packed attribute within the container.

// pack NEW grid items
instance.update()

// 'update' is emitted when NEW items have been packed
instance.on('update', () => {
  // ...
})

Note that this is the preferred method for handling dynamically added items, because it will only operate on items that have not yet been packed (i.e. don't have the packed attribute).

.resize(flag)

Used to add or remove the resize event handler. It's recommended that you add the resize handler when you create your instance.

// add or remove the resize handler
instance
  .resize(true)       // 'true' adds it
  .resize(false)      // 'false' removes it

// 'resize' is emitted when resizing has resulted in a new matching 'size' object
instance.on('resize', size => {
  // 'size' is the newly matching size object
  // ...
})

Note that the resize handler fires the pack method if the resulting screen size matches a size parameter other than the current one. In this case, the pack event will be fired immediately before the resize event. Use the resize event only for breakpoint specific code, not code meant for when the grid has been packed.

Browser Support

Bricks depends on the following browser APIs:

Consequently, it supports the following natively:

  • Chrome 24+
  • Firefox 23+
  • Safari 6.1+
  • Opera 15+
  • IE 10+
  • iOS Safari 7.1+
  • Android Browser 4.4+

To support older browsers, consider including polyfills/shims for the APIs listed above. There are no plans to include any in the library, in the interest of file size.

Colophon

License

MIT. Β© 2017 Michael Cavalea

Built With Love

More Repositories

1

layzr.js

A modern lazy loading library for images.
JavaScript
5,575
star
2

jump.js

A modern smooth scrolling library.
JavaScript
3,472
star
3

gotem

Copy to clipboard for modern browsers in less than 1kb.
JavaScript
429
star
4

outset

A minimal frontend boilerplate, emphasizing workflow.
SCSS
170
star
5

knot.js

A browser-based event emitter, for tying things together.
JavaScript
105
star
6

heading-links.js

A small, dependency-free library for deep linking headings - GitHub style.
JavaScript
66
star
7

text-split

Text wrapping for type animations.
JavaScript
46
star
8

nuclear.css

An experiment in generating atomic, immutable, class-based CSS.
CSS
23
star
9

stockpile.js

A tiny localStorage wrapper providing namespacing and typed values.
JavaScript
18
star
10

hsx

Static HTML sites with JSX and webpack (no React).
JavaScript
16
star
11

string-dom

Create HTML strings using JSX (or functions).
JavaScript
13
star
12

maximize.js

A tiny wrapper for cross-browser requestFullscreen.
JavaScript
13
star
13

obzerv

A convenient wrapper around IntersectionObserver for tracking element position relative to the viewport.
JavaScript
11
star
14

string-css

Create CSS-in-JS via tagged template strings.
JavaScript
10
star
15

tail-end

Promise-wrapped CSS animations and transitions (async/await friendly).
HTML
8
star
16

bandito

Boilerplate for mostly-static React frontends.
JavaScript
8
star
17

jax.js

A tiny Promise wrapper for GET requests.
JavaScript
8
star
18

xec

An async script loader, for the browser, built on Promises.
JavaScript
8
star
19

evented-viewport

Viewport width and height utility for JavaScript modules.
JavaScript
7
star
20

ajax-component

A Custom Element that AJAXs its content, style, and scripts.
JavaScript
7
star
21

ique

A queue that runs tasks when the browser is idle.
JavaScript
7
star
22

sublime

My current Sublime Text 3 settings.
6
star
23

next-thing

Frame, tick, and idle functions for the browser.
JavaScript
6
star
24

handmade

DIY build systems, tooling, and more.
JavaScript
6
star
25

selly

Convenient and optimized DOM element selection.
JavaScript
6
star
26

one-ready

A Promise wrapper for DOMContentLoaded that prevents event duplication.
JavaScript
5
star
27

understated

Render stateless components and JSX to real DOM.
JavaScript
5
star
28

outset-lib

A boilerplate for vanilla JavaScript libraries.
JavaScript
5
star
29

dotnot

Access object properties using dot notation.
JavaScript
5
star
30

array-intersect

Array intersection, simplified.
JavaScript
5
star
31

js-trending

GitHub's trending JavaScript repositories as a microservice.
JavaScript
4
star
32

hurry.js

An optimized curry function, for enabling full and partial function application.
JavaScript
3
star
33

outset-react

A boilerplate for React libraries, built with webpack.
JavaScript
3
star
34

reread

Recursively read paths to all files within a folder and its subfolders.
JavaScript
3
star
35

callmecavs.github.io

My personal website.
CSS
3
star
36

handmade-fs

File system tasks for handmade.
JavaScript
2
star
37

ama

You know the drill...
1
star
38

understated-cli

Create understated sites with no configuration.
JavaScript
1
star