forked from ozwaldorf/iron-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iron-grid.html
400 lines (305 loc) · 11.2 KB
/
iron-grid.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/utils/flattened-nodes-observer.html">
<script src="../resize-observer-polyfill/dist/ResizeObserver.js"></script>
<!--
# Iron Container
`iron-container` is not strictly part of iron-grid but is important in laying out content. It allows you to center your page content. `iron-container`'s default is set to ~90% of the window width. It helps you center and contain your page content. We use the container to contain our body content.
### Styling
The following custom proporties are available for styling:
| Custom property | Description | Default |
| ------------------------ | ---------------------- | ------- |
| `--iron-container-width` | Width of the Container | `90%` |
| `--iron-grid-element-style` | Permit to override grid style element | `null` |
# Iron Grid
`iron-grid` helps you layout polymer elements in an ordered, easy fashion. We are using a standard 12 column fluid responsive grid system.
`iron-grid` is now designed to be used in each element you want to be responsive, not only the complete page. So if you want differents behaviors following screen size for each element, you can.
Now each compoment can manage its own responsiveness.
Please note that all the examples pictured below have the class 'example' on them. The class *is* included in the component. This class gives each grid column a 1px white border so that you can see the seperation.
### 12 Columns
Our standard grid has 12 columns. No matter the size of the browser, each of these columns will always have an equal width.
![1](https://raw.githubusercontent.com/The5heepDev/iron-grid/master/img/1.png)
To get a feel of how the grid is used in HTML. Take a look at this code below which will produce a similar result as the one above.
```html
<iron-grid>
<div class="s1">1</div>
<div class="s1">2</div>
<div class="s1">3</div>
<div class="s1">4</div>
<div class="s1">5</div>
<div class="s1">6</div>
<div class="s1">7</div>
<div class="s1">8</div>
<div class="s1">9</div>
<div class="s1">10</div>
<div class="s1">11</div>
<div class="s1">12</div>
</iron-grid>
```
### Offsets
To offset, simply add `offset-s2` to the class where `s` signifies the screen class-prefix (s = small, m = medium, l = large) and the number after is the number of columns you want to offset by.
![2](https://raw.githubusercontent.com/The5heepDev/iron-grid/master/img/2.png)
```html
<iron-grid>
<div class="s12">
<span>This div is 12-columns wide on all screen sizes</span>
</div>
<div class="s6 offset-s6">
<span>6-columns (offset-by-6)</span>
</div>
</iron-grid>
```
### Orders
You can change the normal element's order of appearance on the screen. To do this, simply add `order-s1` to the class where `s` signifies the screen class-prefix (s = small, m = medium, l = large) and the number is the appearance order (from 1 to 12). The default element order is 6, so the page will load in order of number (ie. order-s1 order-s2 order-s3 etc.), and if there are multiple element's with the same order number, then in consecutive order, as shown below.
![3](https://raw.githubusercontent.com/The5heepDev/iron-grid/master/img/4.png)
```html
<iron-grid>
<div class="s1">1</div>
<div class="s1">2</div> // Without the order tag, the `div` will default to order-s6.
<div class="s1">3</div>
<div class="s1">4</div>
<div class="s1">5</div>
<div class="s1">6</div>
<div class="s1">7</div>
<div class="s1">8</div>
<div class="s1">9</div>
<div class="s1">10</div>
<div class="s1">11</div>
<div class="s1 order-s1">12</div>
</iron-grid>
```
### Hiding elements
You can create hidden elements by using `s0`, `m0`, or `l0`.
```html
<iron-grid>
<div class="s0 m12">
<span>This div is hidden on small screen sizes and 12-columns wide on medium and large screen sizes.</span>
</div>
</iron-grid>
```
### Creating Responsive Layouts
Above we showed you how to layout elements using our grid system. Now we'll show you how to design your layouts so that they look great on all screen sizes.
| | Mobile Devices <= 480px | Mobile Devices <= 960px | Tablet Devices <= 1280px | Desktop Devices <= 1600px | Desktop Devices > 1600px |
|-----------------------|----------------------------|----------------------------|----------------------------|-----------------------------|-----------------------------|
| **Class Prefix** | .xs | .s | .m | .l | .xl |
| **Number of Columns** | 12 | 12 | 12 | 12 | 12 |
### Adding Responsiveness
In the previous examples, we only defined the size for small screens using `s12`. This is fine if we want a fixed layout since the rules propogate upwards. By just saying s12, we are essentially saying `s12 m12 l12`. But by explicitly defining the size we can make our website more responsive.
![4](https://raw.githubusercontent.com/The5heepDev/iron-grid/master/img/3.png)
```html
<iron-grid>
<div class="s12">
<span>I am always full-width (s12)</span>
</div>
<div class="s12 m6">
<span>I am full-width on mobile (s12 m6)</span>
</div>
</iron-grid>
```
### Custom screen size format
For each usage of iron-grid you can change the default screen sizes of screen format management (xs, s, m, l, xl)
Each iron-grid instance will have its own screen size. Because of this, each seprate component can have it's own breakpoint.
```html
<iron-grid xs-max-width="400" s-max-width="600" m-max-width="900" l-max-width="1150">
<div class="s12">
<span>I am always full-width (s12)</span>
</div>
<div class="s12 m6">
<span>I am full-width on mobile (s12 m6)</span>
</div>
</iron-grid>
```
Note that xl is not set because it is the size above 'l' max width.
### Logging
You can log an iron-grid during you development adding `log` on iron-grid tag.
```html
<iron-grid log>
<div class="s12">
<span>I am always full-width (s12)</span>
</div>
<div class="s12 m6">
<span>I am full-width on mobile (s12 m6)</span>
</div>
</iron-grid>
```
Open your dev console and you will see during zomm/dezoom :
width : 375
screenFormat = s
It is recomended to only apply logging on iron-grid one at a time. If not, you will be disturb because each iron-grid can manage its screen formats. Because of the ability for an element to have it's own breakpoints, the console may become cluttered and confusing with multiple logs.
@group Iron Element
@customElement iron-grid
@polymer
@demo demo/index.html
@homepage http://the5heep.tk/
-->
<script>
(() => {
'use strict';
class IronGrid extends Polymer.Element {
constructor() {
super();
}
static get properties() {
return {
log: {
type: Boolean,
value: false
},
xsMaxWidth: {
type: Number,
value: 480,
reflectToAttribute: true
},
sMaxWidth: {
type: Number,
value: 960,
reflectToAttribute: true
},
mMaxWidth: {
type: Number,
value: 1280,
reflectToAttribute: true
},
lMaxWidth: {
type: Number,
value: 1600,
reflectToAttribute: true
},
ratios: {
type: Array,
value: function () {
return [0, 8.33333, 16.66667, 25, 33.33333, 41.66667, 50, 58.33333, 66.66667, 75, 83.33333, 91.66667, 100];
}
},
screenFormats: {
type: Array,
value: function () {
return ["xs", "s", "m", "l", "xl"];
}
},
sizes: {
type: Array,
value: function () {
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
}
},
prefixs: {
type: Array,
value: function () {
return ["", "offset-", "order-"];
}
},
currentScreenformat: {
type: String,
notify: true
},
ironGridDefaultStyle: {
type: String,
value: "margin-left: 0;order: 6;display : flex;flex-wrap: wrap;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;"
}
}
}
static get template() {
return `
<style>
:host {
width: 100%;
display: flex;
flex-wrap: wrap;
}
:host:after {
content: "";
display: table;
clear: both;
}
</style>
<slot id="slot"></slot>`;
}
ready() {
super.ready();
this.onResize();
}
connectedCallback() {
super.connectedCallback();
const resizeObserver = new ResizeObserver(this.onResize.bind(this));
resizeObserver.observe(this);
}
constructClass(screen, size, prefix, suffix) {
return prefix + screen + size + suffix;
}
onResize() {
var newWidth = this.offsetWidth;
if (newWidth == 0) {
newWidth = this.parentNode.clientWidth;
}
var screenFormat = this.getScreenFormat(newWidth);
if (screenFormat !== this.currentScreenformat) {
var oldScreenformat = this.currentScreenformat;
this.currentScreenformat = screenFormat;
var d;
var time1;
if (this.log) {
console.log('width : ' + newWidth);
console.log("screenFormat = " + screenFormat);
d = new Date();
var time1 = d.getTime();
}
var screenFormatsSorted = this.screenFormats.slice();
screenFormatsSorted.splice(this.screenFormats.indexOf(this.currentScreenformat), 1);
screenFormatsSorted.push(this.currentScreenformat);
for (var i = 0; i < screenFormatsSorted.length; i++) {
var format = screenFormatsSorted[i];
var importantSuffix = "";
if (format == screenFormat) {
importantSuffix = " !important";
}
for (var j = 0; j < this.sizes.length; j++) {
var size = this.sizes[j];
for (var k = 0; k < this.prefixs.length; k++) {
var prefix = this.prefixs[k];
var myClass = this.constructClass(format, size, prefix, "");
var selector = "." + myClass;
var elements = this.querySelectorAll(selector);
for (var l = 0; l < elements.length; l++) {
var style;
if (prefix == "") {
style = this.ironGridDefaultStyle;
if (this.ratios[j] != 0) {
style = style + "width:" + this.ratios[j] + "%" + importantSuffix + ";";
} else {
style = style + "display:none" + importantSuffix + ";";
}
} else if (prefix == "offset-") {
style = elements[l].style.cssText + "margin-left:" + this.ratios[j] + "%" + importantSuffix + ";";
} else if (prefix == "order-") {
style = elements[l].style.cssText + "order:" + j + importantSuffix + ";";
}
elements[l].setAttribute('style', style);
}
}
}
}
if (this.log) {
d = new Date();
var time2 = d.getTime();
var totalTime = time2 - time1;
console.log("Time to apply responsiveness = " + totalTime + "ms");
}
}
}
getScreenFormat(newWidth) {
if (newWidth <= this.xsMaxWidth) {
return "xs";
} else if (newWidth <= this.sMaxWidth) {
return "s";
} else if (newWidth <= this.mMaxWidth) {
return "m";
} else if (newWidth <= this.lMaxWidth) {
return "l";
} else {
return "xl";
}
}
}
customElements.define('iron-grid', IronGrid);
})();
</script>