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

blocks.ts

v1.0.4

Published

Performant fixed-width masonry layout system with typescript support.

Downloads

13

Readme

blocks.ts

blocks.ts is a library for creating super performant fixed width masonry style layouts. It was created using typescript, and compiled into production ready es5 javascript, with the additional support of .d.ts file, bringing you the benefits of type checking.

Demo

Install

Using NPM, install blocks.ts and add it to your package.json dependencies.

$ npm install blocks.ts --save

Instantiate

All you need to do is import Blocks and instantiate it, assigning it to a variable so you can call the appropriate functions.

// import Blocks
import Blocks from 'Blocks.ts'

// create an instance
const blocks = Blocks({
  // ...BlockOptions
})

Block options

  • (required) container (node or CSS selector)
  • (required) sizes (array)
  • position boolean (defaulting to true)
  • animationEndClass string

container

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

// passing an element

const containerElement =  document.querySelector('.container');
const blocks = Blocks({
  container: containerElement
})

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

const blocks = Blocks({
  container: '.container'
})

sizes

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

When defining your sizes, note the following:

  • Sizes must use min-width media queries (px)
  • The minWidth property must not be added for your smallest default size
// minWidth- the minimum viewport width (px)
// columns - the number of vertical columns
// gutter  - the space (px) between the columns and grid items

const sizes: [
{columns: 1, gutter: 15},
{minWidth: 800, columns: 2, gutter: 20},
{minWidth: 1200, columns: 3, gutter: 20},
{minWidth: 1600, columns: 4, gutter: 20}
]

const blocks = Blocks({
  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.

// grid items positioned via the 'transform' property

const blocks = Blocks({
  position: false
})

note: the block container element needs to be positioned relative/absolute

animationEndClass

If supplied, places the class on each block after the animation has completed. This is primarily used for transitioning element positions, as using a transition of transform or top/left directly on the block class would cause the elements to come in from position 0,0 when they are first loaded. By using an animation, such as a fade-in, then adding a transition to the animationEndClass, you can avoid this problem.

// animationEndClass supplied in javascript

const blocks = Blocks({
  animationEndClass: "placed"
})

//stlyes applied to a block

.block {
	width: 300px;
	background: black;
	animation: fadeIn 0.5s  ease-in;
}

//styles to be applied after block has been positioned

.block.placed {
	transition: all  0.4s; (allows block postion to transition when moved)
}

API

Blocks exposes the following methods:

  • rePack()
  • update()
  • destroy()

.rePack()

Used to pack all elements within the container.

// pack ALL grid items
	blocks.rePack()

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, primarily for when new elements are appended.

// pack NEW grid items
	blocks.update()

.destroy()

Used to remove the window resize event listener.

// remove the resize listener
	blocks.destroy()
})

License

MIT. © 2018 Brett Shepherd