Skip to content

Commit

Permalink
Use reduce-css-calc to avoid inspecting value to negate
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Aug 6, 2019
1 parent 4695f53 commit 25b3aba
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion __tests__/resolveConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ test('custom properties are multiplied by -1 for negative values', () => {
'-4': '-4px',
'-foo': 'calc(var(--foo) * -1)',
'-bar': 'calc(var(--bar, 500px) * -1)',
'-baz': 'calc(calc(50% - 10px) * -1)',
'-baz': 'calc(-50% - -10px)',
},
},
variants: {},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"postcss-js": "^2.0.0",
"postcss-nested": "^4.1.1",
"postcss-selector-parser": "^6.0.0",
"pretty-hrtime": "^1.0.3"
"pretty-hrtime": "^1.0.3",
"reduce-css-calc": "^2.1.6"
},
"browserslist": [
"> 1%"
Expand Down
6 changes: 2 additions & 4 deletions src/util/resolveConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isFunction from 'lodash/isFunction'
import defaults from 'lodash/defaults'
import map from 'lodash/map'
import toPath from 'lodash/toPath'
import startsWith from 'lodash/startsWith'
import reduceCalc from 'reduce-css-calc'

const configUtils = {
negative(scale) {
Expand All @@ -12,9 +12,7 @@ const configUtils = {
.reduce(
(negativeScale, key) => ({
...negativeScale,
[`-${key}`]: ['var(', 'calc('].some(prefix => startsWith(scale[key], prefix))
? `calc(${scale[key]} * -1)`
: `-${scale[key]}`,
[`-${key}`]: reduceCalc(`calc(${scale[key]} * -1)`),
}),
{}
)
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,11 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
shebang-command "^1.2.0"
which "^1.2.9"

css-unit-converter@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/css-unit-converter/-/css-unit-converter-1.1.1.tgz#d9b9281adcfd8ced935bdbaba83786897f64e996"
integrity sha1-2bkoGtz9jO2TW9urqDeGiX9k6ZY=

cssesc@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-2.0.0.tgz#3b13bd1bb1cb36e1bcb5a4dcd27f54c5dcb35703"
Expand Down Expand Up @@ -4006,6 +4011,14 @@ realpath-native@^1.1.0:
dependencies:
util.promisify "^1.0.0"

reduce-css-calc@^2.1.6:
version "2.1.6"
resolved "https://registry.yarnpkg.com/reduce-css-calc/-/reduce-css-calc-2.1.6.tgz#050fe6ee7d98a1d70775d2e93ce0b713cee394d2"
integrity sha512-+l5/qlQmdsbM9h6JerJ/y5vR5Ci0k93aszLNpCmbadC3nBcbRGmIBm0s9Nj59i22LvCjTGftWzdQRwdknayxhw==
dependencies:
css-unit-converter "^1.1.1"
postcss-value-parser "^3.3.0"

regenerate-unicode-properties@^8.0.2:
version "8.1.0"
resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-8.1.0.tgz#ef51e0f0ea4ad424b77bf7cb41f3e015c70a3f0e"
Expand Down

0 comments on commit 25b3aba

Please sign in to comment.