-
Notifications
You must be signed in to change notification settings - Fork 5
/
_cssgrid.scss
451 lines (408 loc) · 11.8 KB
/
_cssgrid.scss
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// ===================================================
// CSS Grid Layout Helper
//
// To convert some of the CSS Grid Spec 2 features to Spec 1
//
// Author: Sascha Fuchs
//
// ===================================================
@mixin grid-display() {
display: -ms-grid;
display: grid;
}
@mixin grid-display-fallback() {
.new-grid-spec & {
display: grid;
}
.old-grid-spec & {
display: flex;
flex-wrap: wrap;
}
}
/// Add Gap between the boxes
///
/// @group core - cssgrid
///
/// @param {list} $boxes - List with box sizes
/// @param {string} $gap - Optional column gap
@function box-gap($boxes, $gap) {
$box: ();
@for $i from 1 through length($boxes) {
$box: append($box, nth($boxes, $i), space);
// Adding Gap Between
@if $gap != false and $gap > 0 {
// Not last Loop
@if $i != length($boxes) {
$box: append($box, $gap, space);
}
}
}
@return $box;
}
/// Add Gap between the boxes with Repeating Mode
///
/// @group core - cssgrid
///
/// @param {number} $boxes - Number of used boxes
/// @param {string} $size - Box Size
/// @param {string} $gap - Optional column gap
@function box-gap-repeat($boxes, $size, $gap) {
$box: ();
@for $i from 1 through $boxes {
$box: append($box, $size, space);
@if $i != $boxes {
$box: append($box, $gap, space);
}
}
@return $box;
}
/// To build Grid Template Columns with optional gap
///
/// @group core - cssgrid
///
/// @param {string} $gap - Optional column gap
/// @param {list} $columns - Columns sizes
///
/// @require {function} box-gap
///
/// @example scss - scss
/// .test {
/// @include grid-columns(10px 250px 1fr 50px 100px);
/// }
@mixin grid-columns($gap, $columns) {
grid-template-columns: $columns;
@if $gap != false and $gap > 0 {
grid-column-gap: $gap;
}
.old-grid-spec & {
-ms-grid-columns: box-gap($columns, $gap);
}
}
/// For builing repeating columns
///
/// @group core - cssgrid
///
/// @param {string} $gap - Optional column gap
/// @param {number} $columns - Number of Columns
/// @param {number} $size - Columns sizes
///
/// @require {function} box-gap
///
/// @example scss - scss
/// .test {
/// @include grid-column-repeat(20px, 10, 1fr);
/// }
@mixin grid-column-repeat($gap, $columns, $size) {
@if $gap != false and $gap > 0 {
grid-column-gap: $gap;
}
grid-template-columns: repeat($columns, $size);
.old-grid-spec & {
-ms-grid-columns: box-gap-repeat($columns, $size, $gap);
}
}
/// To build Grid Template Rows with optional gap
///
/// @group core - cssgrid
///
/// @param {string} $gap - Optional row gap
/// @param {list} $rows - Rows sizes
///
/// @require {function} box-gap
///
/// @example scss - scss
/// .test {
/// @include grid-rows(10px 1fr);
/// }
@mixin grid-rows($gap, $rows) {
grid-template-rows: $rows;
@if $gap != false and $gap > 0 {
grid-row-gap: $gap;
}
.old-grid-spec & {
-ms-grid-rows: box-gap($rows, $gap);
}
}
/// For builing repeating rows
///
/// @group core - cssgrid
///
/// @param {string} $gap - Optional column gap
/// @param {number} $columns - Number of Columns
/// @param {number} $size - Columns sizes
///
/// @require {function} box-gap
///
/// @example scss - scss
/// .test {
/// @include grid-row-repeat(40px, 4, 1fr);
/// }
@mixin grid-row-repeat($gap, $rows, $size) {
@if $gap != false and $gap > 0 {
grid-row-gap: $gap;
}
grid-template-rows: repeat($rows, $size);
.old-grid-spec & {
-ms-grid-rows: box-gap-repeat($rows, $size, $gap);
}
}
/// Generate a Grid Template Area from a Grid Map
///
/// @group core - cssgrid
///
/// @param {string} $area - Name of the Area
/// @param {map} $map - Map of your grid area matrix
///
/// @example scss - scss
/// $gridAreaMap: (
/// row1: ('logo', 'nav', 'nav', 'nav'),
/// row2: ('logo', 'empty', 'search', 'search')
/// );
/// @include grid-matrix($gridAreaMap);
@mixin grid-matrix($map) {
$matrix: ();
@each $index, $row in $map {
$list: null;
@for $i from 1 through length($row) {
$list: append($list, unquote(nth(($row), $i)), space);
}
$matrix: append($matrix, quote($list), space);
}
grid-template-areas: $matrix;
}
/// Generate Coordinates based on Grid Matrix and the selected area
///
/// @group core - cssgrid
///
/// @param {string} $area - Name of the Area
/// @param {map} $map - Map of your grid area matrix
/// @param {bool} $columnGap - If you use Gap for Columns
/// @param {bool} $rowGap - If you use Gap for Rows
///
/// @example scss - scss
/// $gridAreaMap: (
/// row1: ('logo', 'nav', 'nav', 'nav'),
/// row2: ('logo', 'empty', 'search', 'search')
/// );
/// @include grid-area(logo, $gridAreaMap, true);
@mixin grid-area($area, $map, $columnGap: false, $rowGap: false, $areapost: true) {
// Init Vars
$gridColumnStart: '';
$gridColumnEnd: '';
$gridRows: ();
$gridRowHeight: '';
$areaExists: 0;
@if $areapost {
// Output regular Area
grid-area: $area;
}
// Generate Old Spec Coordinates based on Area
.old-grid-spec & {
// Get Width element
@for $i from 1 through length($map) {
@if sl-contain(map-get($map,row#{$i}), $area) {
$areaExists: append($areaExists, 1);
$gridRowHeight: append($gridRowHeight, $i);
$gridColumnStart: index(map-get($map,row#{$i}), $area);
$gridColumnEnd: sl-last-index(map-get($map,row#{$i}), $area);
$gridRows: append($gridRows, $i);
}
}
// If Area exists in the Map
@if length($areaExists) > 1 {
// Convert Coordinate if Column Gap is active
@if $columnGap and $columnGap > 0 {
-ms-grid-column: $gridColumnStart + ($gridColumnStart - 1);
-ms-grid-column-span: $gridColumnEnd - $gridColumnStart + 1 + ($gridColumnEnd - $gridColumnStart);
} @else {
-ms-grid-column: $gridColumnStart;
-ms-grid-column-span: $gridColumnEnd - $gridColumnStart + 1;
}
// Add Row Coordinates
@if $rowGap and $rowGap > 0 {
-ms-grid-row: nth($gridRows,1) + (nth($gridRows,1) - 1);
-ms-grid-row-span: length($gridRowHeight) - 1 + (length($gridRowHeight) - 2);
} @else {
-ms-grid-row: nth($gridRows,1);
-ms-grid-row-span: length($gridRowHeight) - 1;
}
}
}
}
// To use the three Generators you have to write down the Grid in the Component Map like this...
//grid: (
// associated: (
// info: '__mail' false,
// social: '.c-socialNetworks' true,
// adress: '.c-officeAdresses' true,
// copyright: '.c-copyright' true,
// nav: '.c-footernav' true
// ),
// sets: (
// set1: (
// breakpoint: false,
// column-gap: 0,
// row-gap: 40px,
// columns: 1fr,
// rows: 1fr 1fr 1fr 1fr 1fr,
// areas: (
// row1: ('info'),
// row2: ('social'),
// row3: ('adress'),
// row4: ('copyright'),
// row5: ('nav')
// )
// ),
// set2: (
// breakpoint: "m",
// column-gap: 0,
// row-gap: 0,
// columns: 1fr 1fr 1fr,
// rows: 1fr 1fr 1fr 1fr,
// areas: (
// row1: ('info', 'info', 'info'),
// row2: ('social', 'social', 'social'),
// row3: ('adress', 'adress', 'adress'),
// row4: ('copyright', 'nav', 'nav')
// )
// )
// ),
//)
/// Associate area-names with classes and generate the styling based on the breakpoints
///
/// @group core - cssgrid
///
/// @param {string} $area - Name of the Area
/// @param {map} $map - Map of your grid area matrix
@mixin _grid-area-connection($map, $root, $setup, $areapost: true) {
@each $key, $assoc in $map {
@if nth($assoc, 2) {
#{nth($assoc, 1)} {
@include grid-area($key, map-deep-get($setup, areas), map-get($setup, column-gap), map-get($setup, row-gap), $areapost);
}
} @else {
// Internal Classes
@at-root #{$root}#{nth($assoc, 1)} {
@include grid-area($key, map-deep-get($setup, areas), map-get($setup, column-gap), map-get($setup, row-gap), $areapost);
}
}
}
}
@mixin _grid-connect($setup, $map, $root, $areapost) {
@include grid-matrix(map-deep-get($setup, areas));
@include grid-columns(map-get($setup, column-gap), map-get($setup, columns));
@include grid-rows(map-get($setup, row-gap), map-get($setup, rows));
// Base Areas Connections
@include _grid-area-connection(map-get($map, associated), $root, $setup, $areapost);
}
// To use the three Generators you have to write down the Grid in the Component Map like this...
// grid: (
// associated: (
// box0: '__box0' false,
// box1: '.c-box1' true,
// box2: '.c-box2' true,
// box3: '.c-box3' true,
// box3: '.c-box4' true
// ),
// sets: (
// set1: (
// breakpoint: false,
// column-gap: false,
// row-gap: 30px,
// columns: 1fr,
// rows: auto auto auto auto auto,
// areas: (
// row1: (box1),
// row2: (box2),
// row3: (box3),
// row4: (box4),
// row5: (box0)
// )
// ),
// set2: (
// breakpoint: "m",
// column-gap: false,
// row-gap: 60px,
// columns: 1fr 1fr 1fr,
// rows: auto auto auto auto,
// areas: (
// row1: (box1 box1 box1),
// row2: (box2 box2 box2),
// row3: (box3 box3 box3),
// row4: (box4 box0 box0)
// )
// )
// )
// )...
/// Associate area-names with classes and generate the styling based on the breakpoints
///
/// @group core - cssgrid
///
/// @param {string} $area - Name of the Area
/// @param {map} $map - Map of your grid area matrix
@mixin cssgrid-generator($map, $root, $cq: false) {
@include grid-display();
// Build Styling outside Breakpoint
@each $key, $setup in map-get($map, sets) {
@if map-get($setup, breakpoint) == false {
// Base Wrapper Style
@include _grid-connect($setup, $map, $root, true);
} @else {
// Generate Styles inside Breakpoints
@if $cq {
@at-root {
#{$root}#{cq(map-get($setup, breakpoint))} {
@include _grid-connect($setup, $map, $root, false);
}
}
} @else {
// Generate Styles inside Breakpoints
@include break(map-get($setup, breakpoint)) {
@include _grid-connect($setup, $map, $root, false);
}
}
}
}
}
/// A simple CSS Repeating Grid with a Flexbox Fallback
///
/// @group core - cssgrid
///
/// @param {number} $xGap - Horizontal Gap
/// @param {number} $yGap - Vertical Gap
/// @param {number} $columns - Number of Columns in a row
/// @param {number} $width - Column size [default: 1fr]
/// @param {number} $height - Columns sizes [default: false]
/// @param {string} $child - Childname or type [default: '.box']
///
/// @require {function} box-gap
///
/// @example scss - scss
/// .test {
/// @include cssgrid-simple(20px, 40px, 4, 1fr, $height: false, $child: 'div');
/// }
@mixin cssgrid-simple($xGap, $yGap, $columns, $width: 1fr, $height: false, $child: '.box') {
.new-grid-spec & {
grid-row-gap: if($yGap != false and $yGap > 0, $yGap, null);
grid-column-gap: if($xGap != false and $xGap > 0, $xGap, null);
grid-template-columns: repeat($columns, $width);
grid-template-rows: if($height, repeat(1, $height), null);
}
.old-grid-spec & {
margin-bottom: if($yGap != false and $yGap > 0, 0 - $yGap, null);
@if $xGap != false and $xGap > 0 {
@include overlap($xGap / 2);
}
#{$child} {
@if $xGap != false and $xGap > 0 {
margin-left: $xGap / 2;
margin-right: $xGap / 2;
margin-bottom: if($yGap != false and $yGap > 0, $yGap, null);
flex-grow: 1;
flex-shrink: 1;
flex-basis: calc(#{100 / $columns + 0%} - #{$xGap});
height: if($height, $height, null);
}
}
}
}