Skip to content

Commit ca0f9b0

Browse files
committed
feat: add unitless addon that add 'px' automatical
1 parent 34ad5f5 commit ca0f9b0

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

addon/unitless.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
var UNITLESS_NUMBER_PROPS = [
4+
'animation-iteration-count',
5+
'border-image-outset',
6+
'border-image-slice',
7+
'border-image-width',
8+
'box-flex',
9+
'box-flex-group',
10+
'box-ordinal-group',
11+
'column-count',
12+
'columns',
13+
'flex',
14+
'flex-grow',
15+
'flex-positive',
16+
'flex-shrink',
17+
'flex-negative',
18+
'flex-order',
19+
'grid-row',
20+
'grid-row-end',
21+
'grid-row-span',
22+
'grid-row-start',
23+
'grid-column',
24+
'grid-column-end',
25+
'grid-column-span',
26+
'grid-column-start',
27+
'font-weight',
28+
'line-clamp',
29+
'line-height',
30+
'opacity',
31+
'order',
32+
'orphans',
33+
'tabSize',
34+
'widows',
35+
'z-index',
36+
'zoom',
37+
38+
// SVG-related properties
39+
'fill-opacity',
40+
'flood-opacity',
41+
'stop-opacity',
42+
'stroke-dasharray',
43+
'stroke-dashoffset',
44+
'stroke-miterlimit',
45+
'stroke-opacity',
46+
'stroke-width',
47+
];
48+
49+
var unitlessCssProperties = {};
50+
51+
for (var i = 0; i < UNITLESS_NUMBER_PROPS.length; i++) {
52+
var prop = UNITLESS_NUMBER_PROPS[i];
53+
54+
unitlessCssProperties[prop] = 1;
55+
unitlessCssProperties['-webkit-' + prop] = 1;
56+
unitlessCssProperties['-ms-' + prop] = 1;
57+
unitlessCssProperties['-moz-' + prop] = 1;
58+
unitlessCssProperties['-o-' + prop] = 1;
59+
}
60+
61+
exports.addon = function (renderer) {
62+
var originalDecl = renderer.decl;
63+
64+
renderer.decl = function (prop, value) {
65+
var str = originalDecl(prop, value);
66+
67+
if (typeof value === 'number') {
68+
var pos = str.indexOf(':');
69+
var propKebab = str.substr(0, pos);
70+
71+
if (unitlessCssProperties[propKebab]) {
72+
return str;
73+
}
74+
75+
return originalDecl(prop, value + 'px');
76+
}
77+
78+
return str;
79+
};
80+
};

0 commit comments

Comments
 (0)