Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1.4.0] Add typings and an option to disable the responsive behaviour #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Minimalist dependency free Masonry layout library

MiniMasonry is a **lightweight** dependency free Masonry layout. It will compute elements position in JavaScript and update their positions using CSS's **transform attribute**. This means positioning does not trigger browser layout and **use** the device's **GPU**. This also allows CSS animation during element positioning.

MiniMasonry is **responsive**, you give it a target width and it will adjust columns number and elements width. MiniMasonry will increase element width (until another column can fit in the layout) but will never reduce the target width.
MiniMasonry is **responsive** by default, you give it a target width and it will adjust columns number and elements width. MiniMasonry will increase element width (until another column can fit in the layout) but will never reduce the target width.

## Installation

Expand All @@ -27,7 +27,7 @@ import MiniMasonry from "minimasonry";

## Usage

To use MiniMasonry you should have a container **relatively positioned** with your elements as children. Those **children** elements must be **absolutely positioned**.
⚠️ To use MiniMasonry you should have a container **relatively positioned** with your elements as children. Those **children** elements must be **absolutely positioned**.

Then you can initialise MiniMasonry :

Expand All @@ -52,6 +52,7 @@ surroundingGutter (boolean)|true|Set left gutter on first columns and right gutt
ultimateGutter (int)|5|Gutter applied when only 1 column can be displayed.
direction (string)|"ltr"|Sorting direction, "ltr" or "rtl".
wedge (boolean)|false|False will start to sort from center, true will start from left or right according to direction parameter.
responsive (boolean)|true|True will add a "resize" event listener on the window to redraw the masonry on window resize. This listener is throttled to 66ms (15fps).

## API

Expand All @@ -60,9 +61,7 @@ Here is the list of available APIs :
Name|Description
----|-----------
layout()|If list has changed, trigger a relayout of the masonry.
destroy()|Remove the resize listener and set back container as it was before initialization.

MiniMasonry will add a "resize" event listener on the window to redraw the masonry on window resize. This listener is throttled to 66ms (15fps).
destroy()|Remove the resize listener if any and set back container as it was before initialization.

## Example

Expand Down
22 changes: 11 additions & 11 deletions build/minimasonry.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var MiniMasonry = function(conf) {
ultimateGutter: 5,
surroundingGutter: true,
direction: 'ltr',
wedge: false
wedge: false,
responsive: true,
};

this.init(conf);
Expand All @@ -38,12 +39,9 @@ MiniMasonry.prototype.init = function(conf) {
if (this.conf.gutterX == null || this.conf.gutterY == null) {
this.conf.gutterX = this.conf.gutterY = this.conf.gutter;
}

this._currentGutterX = this.conf.gutterX;
this._currentGutterY = this.conf.gutterY;

console.log(this._currentGutterX);

this._container = typeof this.conf.container == 'object' && this.conf.container.nodeName ?
this.conf.container :
document.querySelector(this.conf.container);
Expand All @@ -52,11 +50,13 @@ MiniMasonry.prototype.init = function(conf) {
throw new Error('Container not found or missing');
}

var onResize = this.resizeThrottler.bind(this);
window.addEventListener("resize", onResize);
this._removeListener = function() {
window.removeEventListener("resize", onResize);
};
if(this.conf.responsive) {
var onResize = this.resizeThrottler.bind(this);
window.addEventListener("resize", onResize);
this._removeListener = function() {
window.removeEventListener("resize", onResize);
};
}

this.layout();
};
Expand Down Expand Up @@ -214,8 +214,8 @@ MiniMasonry.prototype.resizeThrottler = function() {
if (this._container.clientWidth != this._width) {
this.layout();
}
// The actualResizeHandler will execute at a rate of 30fps
}.bind(this), 33);
// The actualResizeHandler will execute at a rate of 15fps
}.bind(this), 66);
}
};

Expand Down
2 changes: 1 addition & 1 deletion build/minimasonry.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
declare module 'minimasonry' {
export interface MiniMasonryOptions {
container: string | ReturnType<typeof document.querySelector>;
baseWidth?: number;
gutter?: number;
gutterX?: number;
gutterY?: number;
minify?: boolean;
surroundingGutter?: boolean;
ultimateGutter?: number;
direction?: 'ltr' | 'rtl';
wedge?: boolean;
responsive?: boolean;
}

export default class MiniMasonry {
constructor(opts: MiniMasonryOptions);

layout(): void;
destroy(): void;
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "minimasonry",
"description": "Minimalist dependancy free Masonry layout library",
"version": "1.3.1",
"version": "1.4.0",
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"rollup": "^2.58.3",
Expand All @@ -13,6 +13,7 @@
},
"main": "build/minimasonry.min.js",
"module": "build/minimasonry.esm.js",
"types": "index.d.ts",
"dependencies": {},
"repository": {
"type": "git",
Expand Down
17 changes: 10 additions & 7 deletions src/minimasonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var MiniMasonry = function(conf) {
ultimateGutter: 5,
surroundingGutter: true,
direction: 'ltr',
wedge: false
wedge: false,
responsive: true,
};

this.init(conf);
Expand Down Expand Up @@ -49,10 +50,12 @@ MiniMasonry.prototype.init = function(conf) {
throw new Error('Container not found or missing');
}

var onResize = this.resizeThrottler.bind(this)
window.addEventListener("resize", onResize);
this._removeListener = function() {
window.removeEventListener("resize", onResize);
if(this.conf.responsive) {
var onResize = this.resizeThrottler.bind(this)
window.addEventListener("resize", onResize);
this._removeListener = function() {
window.removeEventListener("resize", onResize);
}
}

this.layout();
Expand Down Expand Up @@ -213,8 +216,8 @@ MiniMasonry.prototype.resizeThrottler = function() {
if (this._container.clientWidth != this._width) {
this.layout();
}
// The actualResizeHandler will execute at a rate of 30fps
}.bind(this), 33);
// The actualResizeHandler will execute at a rate of 15fps
}.bind(this), 66);
}
}

Expand Down