Skip to content

Commit 0bc1284

Browse files
authored
Merge pull request #174 from etripier/add-basic-grid-prefixes
Add basic support for CSS grid prefixing
2 parents 38057dd + ee494d6 commit 0bc1284

File tree

6 files changed

+219
-20
lines changed

6 files changed

+219
-20
lines changed

modules/__tests__/createPrefixer-test.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,5 +431,120 @@ describe('Static Prefixer', () => {
431431
expect(prefix(input)).toEqual(output)
432432
})
433433
})
434+
435+
describe('grid', () => {
436+
it('should prefix display values', () => {
437+
const input = { display: 'grid' }
438+
const inlineInput = { display: 'inline-grid' }
439+
const output = { display: ['-ms-grid', 'grid'] }
440+
const inlineOutput = { display: ['-ms-inline-grid', 'inline-grid'] }
441+
expect(prefix(input)).toEqual(output)
442+
expect(prefix(inlineInput)).toEqual(inlineOutput)
443+
})
444+
445+
it('should prefix basic alignment values', () => {
446+
const input = { alignSelf: 'stretch', justifySelf: 'end' }
447+
const output = {
448+
WebkitAlignSelf: 'stretch',
449+
alignSelf: 'stretch',
450+
justifySelf: 'end',
451+
msFlexItemAlign: 'stretch',
452+
msGridColumnAlign: 'end',
453+
msGridRowAlign: 'stretch',
454+
}
455+
expect(prefix(input)).toEqual(output)
456+
})
457+
458+
describe('template', () => {
459+
it('should prefix column templating', () => {
460+
const input = { gridTemplateColumns: '1fr auto' }
461+
const output = {
462+
gridTemplateColumns: '1fr auto',
463+
msGridColumns: '1fr auto',
464+
}
465+
expect(prefix(input)).toEqual(output)
466+
})
467+
468+
it('should prefix row templating', () => {
469+
const input = { gridTemplateRows: '1fr auto' }
470+
const output = {
471+
gridTemplateRows: '1fr auto',
472+
msGridRows: '1fr auto',
473+
}
474+
expect(prefix(input)).toEqual(output)
475+
})
476+
477+
it('should prefix templating even with named grid lines', () => {
478+
const input = { gridTemplateColumns: '1fr [header content] auto' }
479+
const output = {
480+
gridTemplateColumns: '1fr [header content] auto',
481+
msGridColumns: '1fr [header content] auto',
482+
}
483+
expect(prefix(input)).toEqual(output)
484+
})
485+
})
486+
487+
describe('positions', () => {
488+
it('should prefix a cell with numerical start and end column values', () => {
489+
const input = { gridColumnStart: 2, gridColumnEnd: 5 }
490+
const output = {
491+
gridColumnEnd: 5,
492+
gridColumnStart: 2,
493+
msGridColumn: 2,
494+
msGridColumnSpan: 3,
495+
}
496+
expect(prefix(input)).toEqual(output)
497+
})
498+
499+
it('should prefix a cell with numerical start and end row values', () => {
500+
const input = { gridRowStart: 3, gridRowEnd: 7 }
501+
const output = {
502+
gridRowEnd: 7,
503+
gridRowStart: 3,
504+
msGridRow: 3,
505+
msGridRowSpan: 4,
506+
}
507+
expect(prefix(input)).toEqual(output)
508+
})
509+
510+
it('should not prefix a cell with non-numerical position values', () => {
511+
const input = { gridRowStart: 'span 3', gridRowEnd: 5 }
512+
const output = {
513+
gridRowEnd: 5,
514+
gridRowStart: 'span 3',
515+
}
516+
expect(prefix(input)).toEqual(output)
517+
})
518+
519+
it('should expand the shorthand column syntax', () => {
520+
const input = { gridColumn: '3 / 5' }
521+
const output = {
522+
gridColumn: '3 / 5',
523+
msGridColumn: 3,
524+
msGridColumnSpan: 2,
525+
}
526+
expect(prefix(input)).toEqual(output)
527+
})
528+
529+
it('should expand the shorthand row syntax', () => {
530+
const input = { gridRow: '2 / 7' }
531+
const output = {
532+
gridRow: '2 / 7',
533+
msGridRow: 2,
534+
msGridRowSpan: 5,
535+
}
536+
expect(prefix(input)).toEqual(output)
537+
})
538+
539+
it('should not expand non-numerical values', () => {
540+
const input = { gridRow: '2 / span 3' }
541+
const output = {
542+
gridRow: '2 / span 3',
543+
msGridRow: 2,
544+
}
545+
expect(prefix(input)).toEqual(output)
546+
})
547+
})
548+
})
434549
})
435550
})

modules/generator/maps/pluginMap.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export default {
6363
android: 4.4,
6464
and_uc: maximumVersion,
6565
},
66+
grid: {
67+
edge: 16,
68+
ie: 11,
69+
ie_mob: 11,
70+
},
6671
imageSet: {
6772
chrome: maximumVersion,
6873
safari: maximumVersion,

modules/generator/maps/propertyMap.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,6 @@ export default {
123123
'breakInside',
124124
'regionFragment',
125125
],
126-
'css-grid': [
127-
'gridTemplateColumns',
128-
'gridTemplateRows',
129-
'gridTemplateAreas',
130-
'gridTemplate',
131-
'gridAutoColumns',
132-
'gridAutoRows',
133-
'gridAutoFlow',
134-
'grid',
135-
'gridRowStart',
136-
'gridColumnStart',
137-
'gridRowEnd',
138-
'gridRow',
139-
'gridColumn',
140-
'gridColumnEnd',
141-
'gridColumnGap',
142-
'gridRowGap',
143-
'gridArea',
144-
'gridGap',
145-
],
146126
'object-fit': ['objectFit', 'objectPosition'],
147127
'text-overflow': 'textOverflow',
148128
'background-img-opts': [

modules/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import filter from './plugins/filter'
99
import flex from './plugins/flex'
1010
import flexboxOld from './plugins/flexboxOld'
1111
import gradient from './plugins/gradient'
12+
import grid from './plugins/grid'
1213
import imageSet from './plugins/imageSet'
1314
import logical from './plugins/logical'
1415
import position from './plugins/position'
@@ -22,6 +23,7 @@ const plugins = [
2223
filter,
2324
flexboxOld,
2425
gradient,
26+
grid,
2527
imageSet,
2628
logical,
2729
position,

modules/plugins/grid.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* @flow */
2+
3+
function isSimplePositionValue(value: any) {
4+
return typeof value === 'number' && !isNaN(value)
5+
}
6+
7+
const alignmentValues = ['center', 'end', 'start', 'stretch']
8+
9+
const displayValues = {
10+
'inline-grid': ['-ms-inline-grid', 'inline-grid'],
11+
grid: ['-ms-grid', 'grid'],
12+
}
13+
14+
const propertyConverters = {
15+
alignSelf: (value: any, style: Object) => {
16+
if (alignmentValues.indexOf(value) > -1) {
17+
style.msGridRowAlign = value
18+
}
19+
},
20+
21+
gridColumn: (value: any, style: Object) => {
22+
if (isSimplePositionValue(value)) {
23+
style.msGridColumn = value
24+
} else {
25+
const [start, end] = value.split('/').map(position => +position)
26+
propertyConverters.gridColumnStart(start, style)
27+
propertyConverters.gridColumnEnd(end, style)
28+
}
29+
},
30+
31+
gridColumnEnd: (value: any, style: Object) => {
32+
const { msGridColumn } = style
33+
if (isSimplePositionValue(value) && isSimplePositionValue(msGridColumn)) {
34+
style.msGridColumnSpan = value - msGridColumn
35+
}
36+
},
37+
38+
gridColumnStart: (value: any, style: Object) => {
39+
if (isSimplePositionValue(value)) {
40+
style.msGridColumn = value
41+
}
42+
},
43+
44+
gridRow: (value: any, style: Object) => {
45+
if (isSimplePositionValue(value)) {
46+
style.msGridRow = value
47+
} else {
48+
const [start, end] = value.split('/').map(position => +position)
49+
propertyConverters.gridRowStart(start, style)
50+
propertyConverters.gridRowEnd(end, style)
51+
}
52+
},
53+
54+
gridRowEnd: (value: any, style: Object) => {
55+
const { msGridRow } = style
56+
if (isSimplePositionValue(value) && isSimplePositionValue(msGridRow)) {
57+
style.msGridRowSpan = value - msGridRow
58+
}
59+
},
60+
61+
gridRowStart: (value: any, style: Object) => {
62+
if (isSimplePositionValue(value)) {
63+
style.msGridRow = value
64+
}
65+
},
66+
67+
gridTemplateColumns: (value: any, style: Object) => {
68+
style.msGridColumns = value
69+
},
70+
71+
gridTemplateRows: (value: any, style: Object) => {
72+
style.msGridRows = value
73+
},
74+
75+
justifySelf: (value: any, style: Object) => {
76+
if (alignmentValues.indexOf(value) > -1) {
77+
style.msGridColumnAlign = value
78+
}
79+
},
80+
}
81+
82+
export default function grid(
83+
property: string,
84+
value: any,
85+
style: Object
86+
): ?Array<string> {
87+
if (property === 'display' && value in displayValues) {
88+
return displayValues[value]
89+
}
90+
91+
if (property in propertyConverters) {
92+
const propertyConverter = propertyConverters[property]
93+
propertyConverter(value, style)
94+
}
95+
}

modules/plugins/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import flex from './flex'
77
import flexboxIE from './flexboxIE'
88
import flexboxOld from './flexboxOld'
99
import gradient from './gradient'
10+
import grid from './grid'
1011
import imageSet from './imageSet'
1112
import logical from './logical'
1213
import position from './position'
@@ -23,6 +24,7 @@ export default [
2324
flexboxIE,
2425
flexboxOld,
2526
gradient,
27+
grid,
2628
imageSet,
2729
logical,
2830
position,

0 commit comments

Comments
 (0)