Skip to content

Commit 00d4061

Browse files
committed
Add mobile first support via an option, this will become the default in v3, Fixes #30
1 parent ad3ea77 commit 00d4061

File tree

8 files changed

+128
-99
lines changed

8 files changed

+128
-99
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ If set to true, Macy will wait for all images on the page to load before running
6666

6767
Set this to true if you would prefer to use a different image loaded library.
6868

69+
#### **mobileFirst**
70+
*Default: `false`*
71+
72+
Setting this value to true will alter how the breakAt options will work. Macy will now work in a mobile first way so the default `columns` will be the default then if for example you have `400: 2` in your breakAt object, if the document is greater or equal to 400px the column count will be 2.
73+
6974
#### **breakAt**
7075

7176
*Default: `None`*

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "macy",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"homepage": "http://macyjs.com/",
55
"author": {
66
"name": "Big Bite Creative",

demo/index.html

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,39 +103,22 @@ <h1 class="hero__title">Macy.js is a lightweight, dependency free, 2kb (gzipped)
103103

104104
<script src="../dist/macy.js"></script>
105105
<script>
106-
var masonry = new Macy({
107-
container: '#macy-container',
108-
trueOrder: false,
109-
waitForImages: false,
110-
useOwnImageLoader: false,
111-
debug: true,
112-
margin: {
113-
x: 10,
114-
y: 10
115-
},
116-
columns: 6,
117-
breakAt: {
118-
1200: {
119-
columns: 5,
120-
margin: {
121-
x: 23,
122-
y: 4
106+
var masonry = new Macy({
107+
container: '#macy-container',
108+
trueOrder: false,
109+
waitForImages: false,
110+
useOwnImageLoader: false,
111+
debug: true,
112+
mobileFirst: true,
113+
columns: 1,
114+
margin: 24,
115+
breakAt: {
116+
1200: 6,
117+
940: 5,
118+
520: 3,
119+
400: 2
123120
}
124-
},
125-
940: {
126-
margin: {
127-
y: 23
128-
}
129-
},
130-
520: {
131-
columns: 3,
132-
margin: 3,
133-
},
134-
400: {
135-
columns: 2
136-
}
137-
}
138-
});
121+
});
139122
</script>
140123
</body>
141124
</html>

dist/macy.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "macy",
33
"description": "Macy is a lightweight, dependency free, masonry layout library",
4-
"version": "2.2.0",
4+
"version": "2.3.0",
55
"author": {
66
"name": "Big Bite Creative",
77
"url": "http://bigbitecreative.com",

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let buildObj = {
77
entry: 'src/macy.js',
88
format: 'umd',
99
moduleName: 'Macy',
10-
banner: '/* Macy.js - v2.2.0 */',
10+
banner: '/* Macy.js - v2.3.0 */',
1111
plugins: [
1212
eslint(),
1313
babel(),

src/helpers/getResponsiveOptions.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import isObject from './isObject';
2+
3+
/**
4+
* Replaces responsiveOptions with temporary options where applicable.
5+
* @param tempOpts
6+
* @param responsiveOptions
7+
*/
8+
const replaceOptionsResponsively = (tempOpts, responsiveOptions) => {
9+
if (!isObject(tempOpts)) {
10+
responsiveOptions.columns = tempOpts;
11+
}
12+
13+
if (isObject(tempOpts) && tempOpts.columns) {
14+
responsiveOptions.columns = tempOpts.columns;
15+
}
16+
17+
if (isObject(tempOpts) && tempOpts.margin && !isObject(tempOpts.margin)) {
18+
responsiveOptions.margin = {
19+
x: tempOpts.margin,
20+
y: tempOpts.margin
21+
}
22+
}
23+
24+
if (isObject(tempOpts) && tempOpts.margin && isObject(tempOpts.margin) && tempOpts.margin.x) {
25+
responsiveOptions.margin.x = tempOpts.margin.x;
26+
}
27+
28+
if (isObject(tempOpts) && tempOpts.margin && isObject(tempOpts.margin) && tempOpts.margin.y) {
29+
responsiveOptions.margin.y = tempOpts.margin.y;
30+
}
31+
};
32+
33+
/**
34+
* Return the current spacing options based on document size, in a mobile first manner.
35+
* @param {Object} args - This passes the macy instance options, responsiveOptions object, the width keys and document width.
36+
* @return {Object} - Object containing the current spacing options
37+
*/
38+
export function getOptionsAsMobileFirst ({ options, responsiveOptions, keys, docWidth }) {
39+
let tempOpts;
40+
41+
for (let i = 0; i < keys.length; i++) {
42+
let widths = parseInt(keys[i], 10);
43+
44+
if (docWidth >= widths) {
45+
tempOpts = options.breakAt[widths];
46+
replaceOptionsResponsively(tempOpts, responsiveOptions);
47+
}
48+
}
49+
50+
return responsiveOptions;
51+
}
52+
53+
/**
54+
* Return the current spacing options based on document size, in a desktop first manner.
55+
* @param {Object} args - This passes the macy instance options, responsiveOptions object, the width keys and document width.
56+
* @return {Object} - Object containing the current spacing options
57+
*/
58+
export function getOptionsAsDesktopFirst ({ options, responsiveOptions, keys, docWidth }) {
59+
let tempOpts;
60+
61+
for (let i = keys.length - 1; i >= 0; i--) {
62+
let widths = parseInt(keys[i], 10);
63+
64+
if (docWidth <= widths) {
65+
tempOpts = options.breakAt[widths];
66+
replaceOptionsResponsively(tempOpts, responsiveOptions);
67+
}
68+
}
69+
70+
return responsiveOptions;
71+
}
72+
73+
/**
74+
* Return the current spacing options based on document size.
75+
* @param {Object} options - Macy instance's options
76+
* @return {Object} - Object containing the current spacing options
77+
*/
78+
export function getResponsiveOptions (options) {
79+
let docWidth = document.body.clientWidth;
80+
let responsiveOptions = {
81+
columns: options.columns,
82+
};
83+
84+
if (!isObject(options.margin)) {
85+
responsiveOptions.margin = {
86+
x: options.margin,
87+
y: options.margin
88+
}
89+
} else {
90+
responsiveOptions.margin = {
91+
x: options.margin.x,
92+
y: options.margin.y
93+
}
94+
}
95+
96+
let keys = Object.keys(options.breakAt);
97+
98+
if (options.mobileFirst) {
99+
return getOptionsAsMobileFirst({ options, responsiveOptions, keys, docWidth });
100+
}
101+
102+
return getOptionsAsDesktopFirst({ options, responsiveOptions, keys, docWidth });
103+
}

src/modules/calculations.js

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,5 @@
1-
import isObject from '../helpers/isObject';
21
import foreach from '../helpers/foreach';
3-
4-
/**
5-
* Return the current spacing options based on document size.
6-
* @param {Object} options - Macy instance's options
7-
* @return {Object} - Object containing the current spacing options
8-
*/
9-
export function getResponsiveOptions (options) {
10-
let docWidth = document.body.clientWidth;
11-
let responsiveOptions = {
12-
columns: options.columns,
13-
};
14-
15-
let tempOpts;
16-
17-
if (!isObject(options.margin)) {
18-
responsiveOptions.margin = {
19-
x: options.margin,
20-
y: options.margin
21-
}
22-
} else {
23-
responsiveOptions.margin = {
24-
x: options.margin.x,
25-
y: options.margin.y
26-
}
27-
}
28-
29-
let keys = Object.keys(options.breakAt);
30-
31-
for (let i = keys.length - 1; i >= 0; i--) {
32-
let widths = parseInt(keys[i], 10);
33-
34-
if (docWidth <= widths) {
35-
tempOpts = options.breakAt[widths];
36-
37-
if (!isObject(tempOpts)) {
38-
responsiveOptions.columns = tempOpts;
39-
}
40-
41-
if (isObject(tempOpts) && tempOpts.columns) {
42-
responsiveOptions.columns = tempOpts.columns;
43-
}
44-
45-
if (isObject(tempOpts) && tempOpts.margin && !isObject(tempOpts.margin)) {
46-
responsiveOptions.margin = {
47-
x: tempOpts.margin,
48-
y: tempOpts.margin
49-
}
50-
}
51-
52-
if (isObject(tempOpts) && tempOpts.margin && isObject(tempOpts.margin) && tempOpts.margin.x) {
53-
responsiveOptions.margin.x = tempOpts.margin.x;
54-
}
55-
56-
if (isObject(tempOpts) && tempOpts.margin && isObject(tempOpts.margin) && tempOpts.margin.y) {
57-
responsiveOptions.margin.y = tempOpts.margin.y;
58-
}
59-
60-
}
61-
}
62-
63-
return responsiveOptions;
64-
}
2+
import { getResponsiveOptions } from '../helpers/getResponsiveOptions';
653

664
/**
675
* Return the current number of columns macy should be

0 commit comments

Comments
 (0)