-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (56 loc) · 1.51 KB
/
Copy pathindex.js
File metadata and controls
69 lines (56 loc) · 1.51 KB
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
const postcss = require('postcss');
const DEFAULTS = {
ieRows: 5
};
function createIERow(rule, n) {
const row = postcss.rule({
selector: `${rule.selector} > :nth-child(${n})`
});
row.append({
prop: '-ms-grid-row',
value: n
});
return row;
}
module.exports = postcss.plugin('postcss-subgrid', ({ ieHack }) => {
const PROPS = [
{ prop: 'grid-column', value: ieHack ? '1 / 99' : '1 / -1' },
{ prop: 'grid', value: 'inherit' },
{ prop: 'grid-gap', value: 'inherit' }
];
ieHack &&
PROPS.push(
{ prop: 'grid-template-columns', value: 'inherit' },
{ prop: 'grid-template-areas', value: '' }
);
return root => {
root.walkDecls('display', decl => {
const { important, parent, value } = decl;
if (value !== 'subgrid') {
return;
}
decl.value = 'grid';
PROPS.reverse().forEach(prop =>
decl.after(Object.assign(prop, { important }))
);
if (ieHack) {
let ieRows = DEFAULTS.ieRows;
parent.walkDecls('-ms-subgrid-rows', decl => {
ieRows = decl.value;
decl.remove();
});
for (let i = ieRows; i > 0; i--) {
root.insertAfter(parent, createIERow(parent, i));
}
}
});
// Apply MS rows to other -ms-subgrid-rows props
root.walkDecls('-ms-subgrid-rows', decl => {
const { value, parent } = decl;
decl.remove();
for (let i = value; i > 0; i--) {
root.insertAfter(parent, createIERow(parent, i));
}
});
};
});