Convert Figma logo to code with AI

metafizzy logopackery

:bento: Gapless, draggable grid layouts

4,128
315
4,128
54

Top Related Projects

16,381

:love_hotel: Cascading grid layout plugin

10,773

Infinite responsive, sortable, filterable and draggable layouts

39,523

Most modern mobile touch slider with hardware accelerated transitions

17,875

The JavaScript Drag & Drop library your grandparents warned you about.

21,948

:ok_hand: Drag and drop so simple it hurts

Moveable! Draggable! Resizable! Scalable! Rotatable! Warpable! Pinchable! Groupable! Snappable!

Quick Overview

Packery is a JavaScript library for creating bin-packing layouts. It can be used to create grid-like layouts that fit elements into available spaces, similar to masonry but with the ability to fill gaps. Packery is particularly useful for creating responsive, gap-free layouts with elements of varying sizes.

Pros

  • Flexible layout options with support for various element sizes and shapes
  • Smooth animations for item movements and transitions
  • Drag-and-drop functionality for interactive layouts
  • Works well with responsive designs

Cons

  • Learning curve for advanced usage and customization
  • Performance may degrade with a large number of elements
  • Limited built-in styling options, requiring additional CSS work
  • Dependency on other libraries for some features (e.g., draggability)

Code Examples

  1. Basic initialization:
const elem = document.querySelector('.grid');
const pckry = new Packery(elem, {
  itemSelector: '.grid-item',
  gutter: 10
});
  1. Adding and removing items:
// Add a new item
const elem = getItemElement();
pckry.appended(elem);

// Remove an item
pckry.remove(elem);
pckry.layout();
  1. Enabling drag-and-drop:
const dragElems = pckry.getItemElements();
for (const dragElem of dragElems) {
  const draggie = new Draggabilly(dragElem);
  pckry.bindDraggabillyEvents(draggie);
}

Getting Started

  1. Install Packery via npm:
npm install packery
  1. Include Packery in your project:
import Packery from 'packery';
  1. Create your HTML structure:
<div class="grid">
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
</div>
  1. Initialize Packery:
document.addEventListener('DOMContentLoaded', () => {
  const elem = document.querySelector('.grid');
  const pckry = new Packery(elem, {
    itemSelector: '.grid-item',
    gutter: 10
  });
});
  1. Style your elements as needed:
.grid-item {
  width: 200px;
  height: 200px;
  background: #e6e6e6;
  margin-bottom: 10px;
}

Competitor Comparisons

16,381

:love_hotel: Cascading grid layout plugin

Pros of Masonry

  • Lighter weight and faster performance for simple grid layouts
  • More widely adopted and battle-tested in production environments
  • Simpler API and easier to implement for basic use cases

Cons of Masonry

  • Less flexible for complex layouts and item rearrangement
  • Lacks built-in drag-and-drop functionality
  • Limited options for handling different sized items

Code Comparison

Masonry:

var grid = document.querySelector('.grid');
var msnry = new Masonry(grid, {
  itemSelector: '.grid-item',
  columnWidth: 200
});

Packery:

var elem = document.querySelector('.grid');
var pckry = new Packery(elem, {
  itemSelector: '.grid-item',
  gutter: 10
});

Both libraries use similar initialization patterns, but Packery offers more advanced layout options and item manipulation capabilities out of the box. Masonry is focused on creating cascading grid layouts, while Packery provides additional features like drag-and-drop functionality and more complex item positioning algorithms.

Masonry is generally better suited for simpler, image-heavy layouts where items have a consistent width. Packery excels in scenarios requiring more dynamic layouts, item rearrangement, or when dealing with elements of varying sizes.

10,773

Infinite responsive, sortable, filterable and draggable layouts

Pros of Muuri

  • More flexible layout options, including vertical, horizontal, and mixed layouts
  • Built-in support for drag-and-drop functionality
  • Smaller file size and potentially better performance

Cons of Muuri

  • Less mature project with fewer long-term users and contributors
  • Documentation may not be as comprehensive as Packery's

Code Comparison

Packery:

var elem = document.querySelector('.grid');
var pckry = new Packery(elem, {
  itemSelector: '.grid-item',
  gutter: 10
});

Muuri:

var grid = new Muuri('.grid', {
  items: '.grid-item',
  layout: {
    fillGaps: true,
    horizontal: false,
    alignRight: false,
    alignBottom: false,
    rounding: false
  }
});

Both libraries offer similar basic functionality for creating grid layouts, but Muuri provides more granular control over the layout options. Packery's API is simpler and more straightforward, while Muuri offers more advanced features like drag-and-drop support out of the box.

Packery is a well-established project with a longer history and larger user base, which may translate to better long-term support and stability. However, Muuri's more flexible layout options and built-in drag-and-drop functionality make it an attractive choice for developers who need these features without additional plugins.

39,523

Most modern mobile touch slider with hardware accelerated transitions

Pros of Swiper

  • More versatile, supporting various types of sliders and carousels
  • Larger community and more frequent updates
  • Better mobile touch support and responsive design

Cons of Swiper

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to more complex API and options

Code Comparison

Swiper initialization:

const swiper = new Swiper('.swiper-container', {
  slidesPerView: 3,
  spaceBetween: 30,
  pagination: {
    el: '.swiper-pagination',
    clickable: true,
  },
});

Packery initialization:

const pckry = new Packery('.grid', {
  itemSelector: '.grid-item',
  gutter: 10,
  percentPosition: true
});

Swiper focuses on creating smooth, touch-enabled sliders and carousels, while Packery specializes in creating masonry-style layouts. Swiper offers more flexibility for creating various types of sliding content, whereas Packery excels at organizing elements in a grid-like structure with a bin-packing algorithm.

Swiper's code is more oriented towards slide configuration and navigation, while Packery's code deals with grid layout and item positioning. Both libraries have their unique strengths and are best suited for different use cases in web development.

17,875

The JavaScript Drag & Drop library your grandparents warned you about.

Pros of Draggable

  • More versatile, supporting various drag-and-drop scenarios beyond grid layouts
  • Actively maintained with regular updates and improvements
  • Extensive documentation and examples for easier implementation

Cons of Draggable

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to more complex API and configuration options

Code Comparison

Packery:

var elem = document.querySelector('.grid');
var pckry = new Packery(elem, {
  itemSelector: '.grid-item',
  gutter: 10
});

Draggable:

const draggable = new Draggable.Sortable(document.querySelectorAll('.container'), {
  draggable: '.item',
  mirror: {
    appendTo: 'body',
    constrainDimensions: true
  }
});

Key Differences

Packery focuses on creating efficient, gap-free grid layouts with draggable items, while Draggable offers a more general-purpose drag-and-drop solution. Packery excels in creating masonry-style layouts, whereas Draggable provides greater flexibility for various drag-and-drop interactions across different UI elements.

Packery's API is simpler and more focused on grid layouts, making it easier to implement for specific use cases. Draggable, on the other hand, offers more customization options and events, allowing developers to create complex drag-and-drop interactions beyond grid layouts.

21,948

:ok_hand: Drag and drop so simple it hurts

Pros of Dragula

  • Lightweight and dependency-free, making it easier to integrate into projects
  • Supports touch events, enabling drag-and-drop on mobile devices
  • Provides a simple API with customizable options for drag-and-drop behavior

Cons of Dragula

  • Lacks built-in layout management features, requiring additional code for complex layouts
  • Does not offer automatic bin-packing or grid-based positioning like Packery
  • Limited animation options compared to Packery's extensive animation capabilities

Code Comparison

Dragula:

dragula([document.querySelector('#left'), document.querySelector('#right')])
  .on('drag', function(el) {
    el.className += ' is-moving';
  })
  .on('drop', function(el) {
    el.className = el.className.replace('is-moving', '');
  });

Packery:

var pckry = new Packery('#grid', {
  itemSelector: '.grid-item',
  gutter: 10,
  percentPosition: true
});

pckry.on('layoutComplete', function(items) {
  console.log(items.length + ' items laid out');
});

Both libraries offer different approaches to element manipulation. Dragula focuses on drag-and-drop functionality, while Packery provides advanced layout management with bin-packing algorithms. The choice between them depends on specific project requirements and desired features.

Moveable! Draggable! Resizable! Scalable! Rotatable! Warpable! Pinchable! Groupable! Snappable!

Pros of Moveable

  • More versatile: Supports dragging, resizing, rotating, and other transformations
  • Lightweight: Smaller bundle size compared to Packery
  • Active development: More frequent updates and bug fixes

Cons of Moveable

  • Steeper learning curve: More complex API due to additional features
  • Less focused: Not specialized for grid layouts like Packery

Code Comparison

Packery:

var elem = document.querySelector('.grid');
var pckry = new Packery(elem, {
  itemSelector: '.grid-item',
  gutter: 10
});

Moveable:

import Moveable from "moveable";

const moveable = new Moveable(document.body, {
  target: document.querySelector(".target"),
  draggable: true,
  resizable: true
});

Summary

Packery is focused on creating grid layouts with a straightforward API, while Moveable offers a wider range of element manipulation options. Packery is better suited for projects requiring specific grid layouts, whereas Moveable is more appropriate for general-purpose element transformation needs. The choice between the two depends on the specific requirements of your project and the level of control you need over element positioning and manipulation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Packery

Bin-packing layout library

See packery.metafizzy.co for complete docs and demos

Install

Download

CDN

Link directly to Packery files on unpkg.

<script src="https://unpkg.com/packery@2.1/dist/packery.pkgd.js"></script>
<!-- or -->
<script src="https://unpkg.com/packery@2.1/dist/packery.pkgd.min.js"></script>

Package managers

Bower: bower install packery --save

npm: npm install packery --save

License

Commercial license

If you want to use Packery to develop commercial sites, themes, projects, and applications, the Commercial license is the appropriate license. With this option, your source code is kept proprietary. Purchase a Packery Commercial License at packery.metafizzy.co

Open source license

If you are creating an open source application under a license compatible with the GNU GPL license v3, you may use Packery under the terms of the GPLv3.

Read more about Packery's license.

Initialize

With jQuery

$('.grid').packery({
  // options...
  itemSelector: '.grid-item'
});

With vanilla JavaScript

// vanilla JS
var grid = document.querySelector('.grid');
// initialize with element
var pckry = new Packery( grid, {
  // options...
  itemSelector: '.grid-item'
});

// initialize with selector string
var pckry = new Packery('.grid', {
  // options...
});

With HTML

Add a data-packery attribute to your element. Options can be set in JSON in the value.

<div class="grid" data-packery='{ "itemSelector": ".grid-item" }'>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  ...
</div>

By Metafizzy

NPM DownloadsLast 30 Days