-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_grid.js
215 lines (188 loc) · 4.78 KB
/
generate_grid.js
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
"use strict"
/**
* Naive grid generator - creates rows with cells that can be pushed
* to different start positions.
* Based on lost: https://github.com/peterramsing/lost#readme
* TODO: Refactor to combine classes that does the same
* E.g. These 3 declaration blocks are identical:
* ```css
.f-row_6 .f-row__cell_3 {
lost-cell: 3/6 flex;
}
.f-row_12 .f-row__cell_6 {
lost-cell: 6/12 flex;
}
```
*/
/** nth take a selector as list and a declaration as list
* and combine them while inserting n in null positions
* [a] -> [a] -> b -> c
* @param {string[]} css
* @param {number} n
* @returns {string}
*/
const nth = (css, n) =>
css.map(a => a === placeholders.cellNumber ? n : a).join("")
const replace = (list, predicate, r) =>
list.map(a => a === predicate ? r : a)
/**
* @param {string[]} selector
* @param [string[]] declaration
* @param {number} cells
*/
const generator = (selector, declaration, cells, f = nth) => {
const output = []
const Rselector = replace(selector, placeholders.rowNumber, cells)
const Rdeclaration = replace(declaration, placeholders.rowNumber, cells)
for (let n = 1; n < cells; n++) {
output.push( f([...Rselector, ...Rdeclaration], n) )
}
return output.join(" ")
}
// const types = {
// rowNumber: "★",
// cellNumber: "Ⓐ"
// }
const placeholders = {
rowNumber: Symbol("rowNumber"),
cellNumber: Symbol("cellNumber")
}
const startBracket = " {\r\n"
const endBracket = "\r\n}\r\n"
const cellSelector = [
".f-row_",
placeholders.rowNumber,
" .f-row__cell-",
placeholders.cellNumber
]
const pushSelector = [
".f-row_",
placeholders.rowNumber,
" .f-row__cell_push_",
placeholders.cellNumber
]
const mobileCellSelector = [
".f-row_",
placeholders.rowNumber,
" .f-row__cell-mobile-",
placeholders.cellNumber
]
const mobilePushSelector = [
".f-row_",
placeholders.rowNumber,
" .f-row__cell-mobile_push_",
placeholders.cellNumber
]
const widthDeclaration = [
startBracket,
"\tlost-column: ",
placeholders.cellNumber,
"/",
placeholders.rowNumber,
" flex;",
endBracket
]
const pushDeclaration = [
startBracket,
"\tlost-offset: -",
placeholders.cellNumber,
"/",
placeholders.rowNumber,
";",
endBracket
]
const columnSelector = [
".f-column_",
placeholders.cellNumber,
" > *"
]
const mobileColumnSelector = [
".f-column_mobile_",
placeholders.cellNumber,
" > *"
]
const columnDeclaration = [
startBracket,
"\tlost-column: 1/",
placeholders.cellNumber,
" ",
placeholders.cellNumber,
" flex;",
endBracket
]
const grid_6 = generator(cellSelector, widthDeclaration, 6)
const pushGrid_6 = generator(pushSelector, pushDeclaration, 6)
const gridMobile_6 = generator(mobileCellSelector, widthDeclaration, 6)
const pushGridMobile_6 = generator(mobilePushSelector, pushDeclaration, 6)
const grid_12 = generator(cellSelector, widthDeclaration, 12)
const pushGrid_12 = generator(pushSelector, pushDeclaration, 12)
const gridMobile_12 = generator(mobileCellSelector, widthDeclaration, 12)
const pushGridMobile_12 = generator(mobilePushSelector, pushDeclaration, 12)
const columnGridNth = (css, n) =>
css.map(a => a === placeholders.cellNumber ? n+1 : a).join("")
const columnGrid_6 = generator(columnSelector, columnDeclaration, 6, columnGridNth)
const columnMobileGrid_6 = generator(mobileColumnSelector, columnDeclaration, 6, columnGridNth)
console.log(`
@lost gutter 3.4rem;
.f-row {
lost-flex-container: row;
width: 100%;
}
/* f-row_reverse is implemented extremely hacky to override *lost* margins */
.f-row_reverse {
flex-direction: row-reverse;
}
.f-row_reverse [class^="f-row__cell"],
.f-row_reverse [class*="f-row__cell"] {
margin-left: var(--gutter-width, 3.4rem)!important;
margin-right: 0!important;
}
.f-row_reverse [class^="f-row__cell"]:last-child,
.f-row_reverse [class*="f-row__cell"]:last-child {
margin-left: 0!important;
}
.f-row_reverse [class^="f-row__cell"]:first-child,
.f-row_reverse [class*="f-row__cell"]:first-child {
margin-right: 0!important;
}
.f-row::after {
lost-utility: clearfix;
}
.f-row_debug, .f-row_debug > * {
border: 1px dashed cyan;
}
.f-row__cell {
lost-column: 1 flex;
}
.f-column {
lost-flex-container: row;
width: 100%;
justify-content: space-between;
}
/*FIXME: serious hack to get flexbox working with lost - investigate 13-05-2016
[class^="f-column_"] > *,
[class*=" f-column_"] > * {
margin-right: 0!important;
margin-left: 0!important;
}*/
${columnGrid_6}
@media screen and (--viewport-desktop) {
.f-row_not-desktop, .f-row__not-desktop { display: none !important; }
${pushGrid_12}
${pushGrid_6}
}
${grid_12}
${grid_6}
/*
## mobile
*/
@media screen and (--viewport-mobile) {
.f-row_not-mobile, .f-row__not-mobile { display: none !important; }
.f-row__cell-mobile { lost-column: 1 flex!important; }
${gridMobile_12}
${pushGridMobile_12}
${gridMobile_6}
${pushGridMobile_6}
${columnMobileGrid_6}
}
`)