Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit a1b4c59

Browse files
authored
fix(styles): expand css shorthands in order for them to be applied properly (#869)
* wip * -added fela expand css shorthands plugin * -update changelog with PR number * -added json plugin for rollup * -fixed import in felaExpandCssShorthandPlugin * -reverted import * -added typings * -changed typings * -changed declaration file * -changed import * -fixed segment styles * -updated package json and yarn lock * -addressed comments -added test * -added list of handled prop in the css shorthands plugin * -fixed condition -refactorings * -updated yarn lock * -added memoize * -improved declaration * -added comment for the reason for memoizeing camel case * -temporary remove dependencies * -installed back the required dependencies * -updated changelog * -fix lint error
1 parent 06f785a commit a1b4c59

File tree

9 files changed

+207
-1
lines changed

9 files changed

+207
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2222
- Fix Teams theme styles for `Alert` [redlines] @codepretty ([#1226](https://github.com/stardust-ui/react/pull/1226))
2323
- Fix selected status of `Dropdown` when focus is on the `List` @silviuavram ([#1258](https://github.com/stardust-ui/react/pull/1258))
2424
- Fix `propTypes` warning in `ListItem` @layershifter ([#1266](https://github.com/stardust-ui/react/pull/1266))
25+
- Expand css shorthands for correct merging of the styles @mnajdova ([#869](https://github.com/stardust-ui/react/pull/869))
2526

2627
<!--------------------------------[ v0.29.0 ]------------------------------- -->
2728
## [v0.29.0](https://github.com/stardust-ui/react/tree/v0.29.0) (2019-04-24)

build/gulp/tasks/test-projects.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ task('test:projects:rollup', async () => {
179179
'rollup-plugin-replace',
180180
'rollup-plugin-commonjs',
181181
'rollup-plugin-node-resolve',
182+
'rollup-plugin-json',
182183
'react',
183184
'react-dom',
184185
].join(' ')

build/gulp/tasks/test-projects/rollup/rollup.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import replace from 'rollup-plugin-replace'
22
import resolve from 'rollup-plugin-node-resolve'
33
import commonjs from 'rollup-plugin-commonjs'
4+
import json from 'rollup-plugin-json'
45

56
const warningWhitelist = [
67
'THIS_IS_UNDEFINED', // comes from TS transforms
@@ -57,5 +58,10 @@ export default {
5758
],
5859
},
5960
}),
61+
json({
62+
include: 'node_modules/**',
63+
preferConst: true,
64+
compact: true,
65+
}),
6066
],
6167
}

packages/react/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"@stardust-ui/react-component-nesting-registry": "^0.29.0",
1010
"@stardust-ui/react-proptypes": "^0.29.0",
1111
"classnames": "^2.2.5",
12+
"css-shorthand-expand": "^1.2.0",
1213
"downshift": "^3.2.6",
14+
"fast-memoize": "^2.5.1",
1315
"fela": "^10.2.0",
1416
"fela-plugin-fallback-value": "^10.2.0",
1517
"fela-plugin-placeholder-prefixer": "^10.2.0",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as _ from 'lodash'
2+
import * as _expand from 'css-shorthand-expand'
3+
import * as _memoize from 'fast-memoize'
4+
5+
// `fast-memoize` is a CJS library, there are known issues with them:
6+
// https://github.com/rollup/rollup/issues/1267#issuecomment-446681320
7+
const memoize = (_memoize as any).default || _memoize
8+
9+
// `css-shorthand-expand` is a CJS library, there are known issues with them:
10+
// https://github.com/rollup/rollup/issues/1267#issuecomment-446681320
11+
const expand = memoize((_expand as any).default || _expand)
12+
13+
// _.camelCase is quite fast, but we are running it for the same values many times
14+
const camelCase = memoize(_.camelCase)
15+
16+
const handledCssPropsMap = {
17+
font: 'font',
18+
padding: 'padding',
19+
margin: 'margin',
20+
border: 'border',
21+
borderWidth: 'border-width',
22+
borderStyle: 'border-style',
23+
borderColor: 'border-color',
24+
borderTop: 'border-top',
25+
borderRight: 'border-right',
26+
borderBottom: 'border-bottom',
27+
borderLeft: 'border-left',
28+
borderRadius: 'border-radius',
29+
background: 'background',
30+
outline: 'outline',
31+
}
32+
33+
export default () => {
34+
const expandCssShorthands = (styles: Object) => {
35+
return Object.keys(styles).reduce((acc, cssPropertyName) => {
36+
const cssPropertyValue = styles[cssPropertyName]
37+
38+
if (typeof cssPropertyValue === 'object') {
39+
return { ...acc, [cssPropertyName]: expandCssShorthands(cssPropertyValue) }
40+
}
41+
42+
if (handledCssPropsMap[cssPropertyName]) {
43+
const expandedProps = expand(handledCssPropsMap[cssPropertyName], `${cssPropertyValue}`)
44+
if (expandedProps) {
45+
return { ...acc, ...convertKeysToCamelCase(expandedProps) }
46+
}
47+
}
48+
49+
return { ...acc, [cssPropertyName]: cssPropertyValue }
50+
}, {})
51+
}
52+
53+
return expandCssShorthands
54+
}
55+
56+
const convertKeysToCamelCase = obj => _.mapKeys(obj, (value, key) => camelCase(key))

packages/react/src/lib/felaRenderer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createRenderer } from 'fela'
22
import felaSanitizeCss from './felaSanitizeCssPlugin'
3+
import felaExpandCssShorthandsPlugin from './felaExpandCssShorthandsPlugin'
34
import felaPluginFallbackValue from 'fela-plugin-fallback-value'
45
import felaPluginPlaceholderPrefixer from 'fela-plugin-placeholder-prefixer'
56
import felaPluginPrefixer from 'fela-plugin-prefixer'
@@ -50,6 +51,7 @@ const createRendererConfig = (options: any = {}) => ({
5051
skip: ['content'],
5152
}),
5253

54+
felaExpandCssShorthandsPlugin(),
5355
felaPluginPlaceholderPrefixer(),
5456
felaPluginPrefixer(),
5557

packages/react/src/themes/teams/components/Segment/segmentStyles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const segmentStyles: ComponentSlotStylesInput<SegmentProps, SegmentVariables> =
1515
boxShadow: `0 1px 1px 1px ${v.boxShadowColor}`,
1616
color: v.color,
1717
backgroundColor: v.backgroundColor,
18-
borderColor: segmentColor,
18+
...(p.color && { borderColor: segmentColor }),
1919
...(p.inverted && {
2020
color: v.backgroundColor,
2121
backgroundColor: segmentColor || v.color,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import felaExpandCssShorthandsPlugin from 'src/lib/felaExpandCssShorthandsPlugin'
2+
3+
const expandCssShorthands = felaExpandCssShorthandsPlugin()
4+
5+
describe('felaExpandCssShorthandsPlugin', () => {
6+
test('should expand margin prop', () => {
7+
const style = {
8+
display: 'block',
9+
margin: '0px 10px',
10+
}
11+
12+
expect(expandCssShorthands(style)).toMatchObject({
13+
display: 'block',
14+
marginTop: '0px',
15+
marginRight: '10px',
16+
marginBottom: '0px',
17+
marginLeft: '10px',
18+
})
19+
})
20+
21+
test('should expand pseudo object', () => {
22+
const style = {
23+
display: 'block',
24+
'::before': {
25+
margin: '0px',
26+
},
27+
}
28+
29+
expect(expandCssShorthands(style)).toMatchObject({
30+
display: 'block',
31+
'::before': {
32+
marginTop: '0px',
33+
marginRight: '0px',
34+
marginBottom: '0px',
35+
marginLeft: '0px',
36+
},
37+
})
38+
})
39+
40+
test('should expand nested pseudo object', () => {
41+
const style = {
42+
display: 'block',
43+
'::before': {
44+
margin: '0px',
45+
':hover': {
46+
padding: '10px',
47+
},
48+
},
49+
}
50+
51+
expect(expandCssShorthands(style)).toMatchObject({
52+
display: 'block',
53+
'::before': {
54+
marginTop: '0px',
55+
marginRight: '0px',
56+
marginBottom: '0px',
57+
marginLeft: '0px',
58+
':hover': {
59+
paddingTop: '10px',
60+
paddingRight: '10px',
61+
paddingBottom: '10px',
62+
paddingLeft: '10px',
63+
},
64+
},
65+
})
66+
})
67+
68+
test('should merge expanded prop with its shorthand', () => {
69+
const style = {
70+
marginTop: '3px',
71+
margin: '10px',
72+
marginRight: '15px',
73+
}
74+
75+
expect(expandCssShorthands(style)).toMatchObject({
76+
marginTop: '10px', // overridden by margin: '10px'
77+
marginRight: '15px', // overridden by marginRight: '15px'
78+
marginBottom: '10px',
79+
marginLeft: '10px',
80+
})
81+
})
82+
})

yarn.lock

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,6 +4099,11 @@ crypto-random-string@^1.0.0:
40994099
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
41004100
integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=
41014101

4102+
css-color-names@0.0.1:
4103+
version "0.0.1"
4104+
resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-0.0.1.tgz#5d0548fa256456ede4a9a0c2ac7ab19d3eb1ad81"
4105+
integrity sha1-XQVI+iVkVu3kqaDCrHqxnT6xrYE=
4106+
41024107
css-in-js-utils@^2.0.0:
41034108
version "2.0.1"
41044109
resolved "https://registry.yarnpkg.com/css-in-js-utils/-/css-in-js-utils-2.0.1.tgz#3b472b398787291b47cfe3e44fecfdd9e914ba99"
@@ -4124,6 +4129,27 @@ css-select@^1.1.0, css-select@~1.2.0:
41244129
domutils "1.5.1"
41254130
nth-check "~1.0.1"
41264131

4132+
css-shorthand-expand@^1.2.0:
4133+
version "1.2.0"
4134+
resolved "https://registry.yarnpkg.com/css-shorthand-expand/-/css-shorthand-expand-1.2.0.tgz#bd6ac8d79f99928581eaca9fe05a03d316d17fe5"
4135+
integrity sha512-L3RS1VNYuXgMOfVGX4WzP9AFK6KL0JuioSoO8661egEac2eHX9/s4yFO8mgK6QEtm8UmU8IvuKzPgdQpU0DhpQ==
4136+
dependencies:
4137+
css-color-names "0.0.1"
4138+
css-url-regex "0.0.1"
4139+
hex-color-regex "^1.0.1"
4140+
hsl-regex "^1.0.0"
4141+
hsla-regex "^1.0.0"
4142+
map-obj "^1.0.0"
4143+
repeat-element "^1.1.0"
4144+
rgb-regex "^1.0.1"
4145+
rgba-regex "^1.0.0"
4146+
xtend "^4.0.0"
4147+
4148+
css-url-regex@0.0.1:
4149+
version "0.0.1"
4150+
resolved "https://registry.yarnpkg.com/css-url-regex/-/css-url-regex-0.0.1.tgz#e05af8c6c290d451ef1632b455ea5c81b4b1395c"
4151+
integrity sha1-4Fr4xsKQ1FHvFjK0VepcgbSxOVw=
4152+
41274153
css-what@2.1:
41284154
version "2.1.0"
41294155
resolved "https://registry.yarnpkg.com/css-what/-/css-what-2.1.0.tgz#9467d032c38cfaefb9f2d79501253062f87fa1bd"
@@ -6934,6 +6960,11 @@ he@1.1.x:
69346960
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
69356961
integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0=
69366962

6963+
hex-color-regex@^1.0.1:
6964+
version "1.1.0"
6965+
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
6966+
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==
6967+
69376968
history@^4.7.2:
69386969
version "4.7.2"
69396970
resolved "https://registry.yarnpkg.com/history/-/history-4.7.2.tgz#22b5c7f31633c5b8021c7f4a8a954ac139ee8d5b"
@@ -6983,6 +7014,16 @@ hosted-git-info@^2.1.4, hosted-git-info@^2.6.0, hosted-git-info@^2.7.1:
69837014
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.7.1.tgz#97f236977bd6e125408930ff6de3eec6281ec047"
69847015
integrity sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==
69857016

7017+
hsl-regex@^1.0.0:
7018+
version "1.0.0"
7019+
resolved "https://registry.yarnpkg.com/hsl-regex/-/hsl-regex-1.0.0.tgz#d49330c789ed819e276a4c0d272dffa30b18fe6e"
7020+
integrity sha1-1JMwx4ntgZ4nakwNJy3/owsY/m4=
7021+
7022+
hsla-regex@^1.0.0:
7023+
version "1.0.0"
7024+
resolved "https://registry.yarnpkg.com/hsla-regex/-/hsla-regex-1.0.0.tgz#c1ce7a3168c8c6614033a4b5f7877f3b225f9c38"
7025+
integrity sha1-wc56MWjIxmFAM6S194d/OyJfnDg=
7026+
69867027
html-element-map@^1.0.0:
69877028
version "1.0.0"
69887029
resolved "https://registry.yarnpkg.com/html-element-map/-/html-element-map-1.0.0.tgz#19a41940225153ecdfead74f8509154ff1cdc18b"
@@ -12101,6 +12142,11 @@ renderkid@^2.0.1:
1210112142
strip-ansi "^3.0.0"
1210212143
utila "~0.3"
1210312144

12145+
repeat-element@^1.1.0:
12146+
version "1.1.3"
12147+
resolved "https://registry.yarnpkg.com/repeat-element/-/repeat-element-1.1.3.tgz#782e0d825c0c5a3bb39731f84efee6b742e6b1ce"
12148+
integrity sha512-ahGq0ZnV5m5XtZLMb+vP76kcAM5nkLqk0lpqAuojSKGgQtn4eRi4ZZGm2olo2zKFH+sMsWaqOCW1dqAnOru72g==
12149+
1210412150
repeat-element@^1.1.2:
1210512151
version "1.1.2"
1210612152
resolved "https://registry.yarnpkg.com/repeat-element/-/repeat-element-1.1.2.tgz#ef089a178d1483baae4d93eb98b4f9e4e11d990a"
@@ -12339,6 +12385,16 @@ rfc6902@^3.0.1:
1233912385
resolved "https://registry.yarnpkg.com/rfc6902/-/rfc6902-3.0.1.tgz#03a3d38329dbc266fbc92aa7fc14546d7839e89f"
1234012386
integrity sha512-a4t5OlaOgAejBg48/lkyQMcV6EWpljjSjmXAtSXLhw83x1OhlcVGLMLf//GoUSpHsWt8x/7oxaf5FEGM9QH/iQ==
1234112387

12388+
rgb-regex@^1.0.1:
12389+
version "1.0.1"
12390+
resolved "https://registry.yarnpkg.com/rgb-regex/-/rgb-regex-1.0.1.tgz#c0e0d6882df0e23be254a475e8edd41915feaeb1"
12391+
integrity sha1-wODWiC3w4jviVKR16O3UGRX+rrE=
12392+
12393+
rgba-regex@^1.0.0:
12394+
version "1.0.0"
12395+
resolved "https://registry.yarnpkg.com/rgba-regex/-/rgba-regex-1.0.0.tgz#43374e2e2ca0968b0ef1523460b7d730ff22eeb3"
12396+
integrity sha1-QzdOLiyglosO8VI0YLfXMP8i7rM=
12397+
1234212398
rimraf@2, rimraf@2.6.3:
1234312399
version "2.6.3"
1234412400
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"

0 commit comments

Comments
 (0)