Skip to content

Commit b30d51a

Browse files
committed
Add support for em and rem units
1 parent 0fbc7d9 commit b30d51a

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

main.coffee

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ setAutoprefixer = (prefixOptions = '> 1%, last 2 versions, Firefox ESR, Opera 12
5454
catch e
5555
'Parse error – try to check the syntax'
5656

57+
setNumberValue = (number) ->
58+
converted = parseInt(number, 10)
59+
if not number.match(/^\d+(\.\d+)?$/)
60+
return 'Please enter numeric value'
61+
else
62+
return converted
63+
5764

5865
_prefixed = ($, property, value) ->
5966
setAutoprefixer() unless prefixer
@@ -72,7 +79,13 @@ class CSS
7279
prefixed = _.partial(_prefixed, $$)
7380
declaration = _.partial(_declaration, $$, @options.vendorPrefixes, prefixed)
7481
comment = _.partial(_comment, $, @options.showComments)
75-
unit = _.partial(css.unit, @options.unit)
82+
83+
rootValue = switch @options.unit
84+
when 'px' then 0
85+
when 'em' then @options.emValue
86+
when 'rem' then @options.remValue
87+
unit = _.partial(css.unit, @options.unit, rootValue)
88+
7689
convertColor = _.partial(_convertColor, @options)
7790
fontStyles = _.partial(css.fontStyles, declaration, convertColor, unit, @options.quoteType)
7891

@@ -149,4 +162,4 @@ class CSS
149162
endSelector()
150163

151164

152-
module.exports = {defineVariable, renderVariable, setAutoprefixer, renderClass: CSS}
165+
module.exports = {defineVariable, renderVariable, setAutoprefixer, setNumberValue, renderClass: CSS}

package.json

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"license": "MIT",
2828
"dependencies": {
2929
"autoprefixer-core": "^4.0.2",
30-
"octopus-helpers": "^1.0.0"
30+
"octopus-helpers": "^1.5.0"
3131
},
3232
"settings": {
3333
"inheritFontStyles": {
@@ -55,6 +55,31 @@
5555
"type": "boolean",
5656
"default": true
5757
},
58+
"unit": {
59+
"type": "select",
60+
"options": [
61+
"px",
62+
"em",
63+
"rem"
64+
],
65+
"default": "px"
66+
},
67+
"emValue": {
68+
"description": "1 em = x px — Ammount of pixels that correspond to one em. Em value is **not** relative to parent element.",
69+
"type": "text",
70+
"default": 16,
71+
"validate": "setNumberValue",
72+
"showFor": "unit",
73+
"showWhen": "em"
74+
},
75+
"remValue": {
76+
"description": "1 rem = x px — Ammount of pixels that correspond to one rem",
77+
"type": "text",
78+
"default": 16,
79+
"validate": "setNumberValue",
80+
"showFor": "unit",
81+
"showWhen": "rem"
82+
},
5883
"autoprefixer": {
5984
"description": "Defines which vendor prefixes will be generated. Read more on [Github](https://github.com/postcss/autoprefixer).",
6085
"type": "text",
@@ -100,15 +125,6 @@
100125
"\""
101126
],
102127
"default": "\""
103-
},
104-
"unit": {
105-
"type": "hidden",
106-
"options": [
107-
"px",
108-
"em",
109-
"rem"
110-
],
111-
"default": "px"
112128
}
113129
},
114130
"bundledDependencies": [

0 commit comments

Comments
 (0)