Magic Grid
A simple, lightweight Javascript library for dynamic grid layouts.
README

A simple, lightweight Javascript library for dynamic grid layouts.
Why not CSS Grid?
Implementing a grid layout can quickly turn into a tricky task. If you have grid items that are always the same height, then you can probably make do with a Flexbox grid or some other CSS grid implementation. However, if you’re dealing with user-generated content, chances are, you don’t have the luxury of equal height components. One longer or shorter component would either stretch the other components in its row, or leave some unpleasant whitespace at the bottom of the row. All of a sudden, our beloved CSS grid has become insufficient.
That's not something grid is designed for. Grid is two dimensional so you are always working in both rows and columns at the same time. You can't use grid to do a "masonry" style layout like that. You could place items in that way if you had a lot of rows and managed how many each spanned, but you can't use auto-placement to get that kind of layout.
Ports
Getting Started
Step 1
- ```
- npm install magic-grid
- ```
- ``` html
- <script src="https://unpkg.com/magic-grid/dist/magic-grid.cjs.js"></script>
- <script src="https://unpkg.com/magic-grid/dist/magic-grid.min.js"></script>
- ```
Step 2 (skip if using CDN)
- ``` js
- import MagicGrid from "magic-grid"
- // or
- let MagicGrid = require("magic-grid");
- ```
- ``` html
- <script src="node_modules/magic-grid/dist/magic-grid.cjs.js"></script>
- <script src="node_modules/magic-grid/dist/magic-grid.min.js"></script>
- ```
Step 3
- ``` js
- let magicGrid = new MagicGrid(...);
- magicGrid.listen();
- ```
Usage
Static content:
- ``` js
- let magicGrid = new MagicGrid({
- container: "#container", // Required. Can be a class, id, or an HTMLElement.
- static: true, // Required for static content.
- animate: true, // Optional.
- });
- magicGrid.listen();
- ```
Dynamic content:
- ``` js
- let magicGrid = new MagicGrid({
- container: "#container", // Required. Can be a class, id, or an HTMLElement.
- items: 20, // For a grid with 20 items. Required for dynamic content.
- animate: true, // Optional.
- });
- magicGrid.listen();
- ```
API
MagicGrid(config)
- ``` js
- let magicGrid = new MagicGrid({
- container: "#container", // Required. Can be a class, id, or an HTMLElement
- static: false, // Required for static content. Default: false.
- items: 30, // Required for dynamic content. Initial number of items in the container.
- gutter: 30, // Optional. Space between items. Default: 25(px).
- maxColumns: 5, // Optional. Maximum number of columns. Default: Infinite.
- useMin: true, // Optional. Prioritize shorter columns when positioning items? Default: false.
- useTransform: true, // Optional. Position items using CSS transform? Default: True.
- animate: true, // Optional. Animate item positioning? Default: false.
- center: true, //Optional. Center the grid items? Default: true.
- });
- ```
.listen()
- ``` js
- let magicGrid = new MagicGrid({
- container: "#container", // Required. Can be a class, id, or an HTMLElement
- static: true, // Required for static content.
- animate: true, // Optional.
- });
- magicGrid.listen();
- ```
.positionItems()
- ``` js
- let magicGrid = new MagicGrid({
- container: "#container", // Required. Can be a class, id, or an HTMLElement
- items: 30, // Required for dynamic content.
- animate: true, // Optional
- });
- magicGrid.listen();
- // get data from api
- // append new element to DOM
- // reposition items
- magicGrid.positionItems();
- ```