Skip to content

Commit 50e9d4f

Browse files
authored
Improve render performance (codesandbox#4018)
1 parent aeab2e6 commit 50e9d4f

File tree

7 files changed

+940
-516
lines changed

7 files changed

+940
-516
lines changed

.circleci/config.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ commands:
214214
JEST_JUNIT_OUTPUT: '/tmp/test-results/js-test-results.xml'
215215
- store_test_results: *store_test_results
216216
- store_artifacts: *store_artifacts
217-
# test_visual_regressions:
218-
# description: "Test Chromatic with 'yarn chromatic'"
219-
# steps:
220-
# - restore_cache: *restore_deps_cache
221-
# - attach_workspace: *attach_deps_workspace
222-
# - run:
223-
# command: yarn chromatic
217+
test_visual_regressions:
218+
description: "Test Chromatic with 'yarn chromatic'"
219+
steps:
220+
- restore_cache: *restore_deps_cache
221+
- attach_workspace: *attach_deps_workspace
222+
- run:
223+
command: yarn chromatic
224224
test_jest:
225225
description: 'Test with `yarn test`'
226226
steps:
@@ -298,11 +298,11 @@ jobs:
298298
steps:
299299
- checkout_with_cache
300300
- test_integrations
301-
# test-visual-regressions:
302-
# executor: node
303-
# steps:
304-
# - checkout_with_cache
305-
# - test_visual_regressions
301+
test-visual-regressions:
302+
executor: node
303+
steps:
304+
- checkout_with_cache
305+
- test_visual_regressions
306306
test-jest:
307307
executor: node
308308
steps:
@@ -363,9 +363,9 @@ workflows:
363363
- test-integrations:
364364
requires:
365365
- build-deps
366-
# - test-visual-regressions:
367-
# requires:
368-
# - build-deps
366+
- test-visual-regressions:
367+
requires:
368+
- build-deps
369369
- typecheck:
370370
requires:
371371
- build-deps
@@ -388,7 +388,7 @@ workflows:
388388
- typecheck
389389
- test-integrations
390390
- test-jest
391-
# - test-visual-regressions
391+
- test-visual-regressions
392392
filters:
393393
branches:
394394
only:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"license": "GPL-3.0",
1010
"workspaces": {
1111
"packages": [
12+
"packages/@styled-system/css",
1213
"packages/@types/ot",
1314
"packages/app",
1415
"packages/homepage",
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/* eslint-disable */
2+
3+
// based on https://github.com/developit/dlv
4+
export var get = function get(obj, key, def, p, undef) {
5+
key = key && key.split ? key.split('.') : [key];
6+
7+
for (p = 0; p < key.length; p++) {
8+
obj = obj ? obj[key[p]] : undef;
9+
}
10+
11+
return obj === undef ? def : obj;
12+
};
13+
const defaultBreakpoints = [40, 52, 64].map(function(n) {
14+
return n + 'em';
15+
});
16+
const aliases = {
17+
bg: 'backgroundColor',
18+
m: 'margin',
19+
mt: 'marginTop',
20+
mr: 'marginRight',
21+
mb: 'marginBottom',
22+
ml: 'marginLeft',
23+
mx: 'marginX',
24+
my: 'marginY',
25+
p: 'padding',
26+
pt: 'paddingTop',
27+
pr: 'paddingRight',
28+
pb: 'paddingBottom',
29+
pl: 'paddingLeft',
30+
px: 'paddingX',
31+
py: 'paddingY',
32+
};
33+
const multiples = {
34+
marginX: ['marginLeft', 'marginRight'],
35+
marginY: ['marginTop', 'marginBottom'],
36+
paddingX: ['paddingLeft', 'paddingRight'],
37+
paddingY: ['paddingTop', 'paddingBottom'],
38+
size: ['width', 'height'],
39+
};
40+
const scales = {
41+
color: 'colors',
42+
backgroundColor: 'colors',
43+
borderColor: 'colors',
44+
margin: 'space',
45+
marginTop: 'space',
46+
marginRight: 'space',
47+
marginBottom: 'space',
48+
marginLeft: 'space',
49+
marginX: 'space',
50+
marginY: 'space',
51+
padding: 'space',
52+
paddingTop: 'space',
53+
paddingRight: 'space',
54+
paddingBottom: 'space',
55+
paddingLeft: 'space',
56+
paddingX: 'space',
57+
paddingY: 'space',
58+
top: 'space',
59+
right: 'space',
60+
bottom: 'space',
61+
left: 'space',
62+
gridGap: 'space',
63+
gridColumnGap: 'space',
64+
gridRowGap: 'space',
65+
gap: 'space',
66+
columnGap: 'space',
67+
rowGap: 'space',
68+
fontFamily: 'fonts',
69+
fontSize: 'fontSizes',
70+
fontWeight: 'fontWeights',
71+
lineHeight: 'lineHeights',
72+
letterSpacing: 'letterSpacings',
73+
border: 'borders',
74+
borderTop: 'borders',
75+
borderRight: 'borders',
76+
borderBottom: 'borders',
77+
borderLeft: 'borders',
78+
borderWidth: 'borderWidths',
79+
borderStyle: 'borderStyles',
80+
borderRadius: 'radii',
81+
borderTopRightRadius: 'radii',
82+
borderTopLeftRadius: 'radii',
83+
borderBottomRightRadius: 'radii',
84+
borderBottomLeftRadius: 'radii',
85+
borderTopWidth: 'borderWidths',
86+
borderTopColor: 'colors',
87+
borderTopStyle: 'borderStyles',
88+
borderBottomWidth: 'borderWidths',
89+
borderBottomColor: 'colors',
90+
borderBottomStyle: 'borderStyles',
91+
borderLeftWidth: 'borderWidths',
92+
borderLeftColor: 'colors',
93+
borderLeftStyle: 'borderStyles',
94+
borderRightWidth: 'borderWidths',
95+
borderRightColor: 'colors',
96+
borderRightStyle: 'borderStyles',
97+
outlineColor: 'colors',
98+
boxShadow: 'shadows',
99+
textShadow: 'shadows',
100+
zIndex: 'zIndices',
101+
width: 'sizes',
102+
minWidth: 'sizes',
103+
maxWidth: 'sizes',
104+
height: 'sizes',
105+
minHeight: 'sizes',
106+
maxHeight: 'sizes',
107+
flexBasis: 'sizes',
108+
size: 'sizes',
109+
// svg
110+
fill: 'colors',
111+
stroke: 'colors',
112+
};
113+
114+
const positiveOrNegative = function positiveOrNegative(scale, value) {
115+
if (typeof value !== 'number' || value >= 0) {
116+
return get(scale, value, value);
117+
}
118+
119+
const absolute = Math.abs(value);
120+
const n = get(scale, absolute, absolute);
121+
if (typeof n === 'string') return '-' + n;
122+
return n * -1;
123+
};
124+
125+
const transforms = [
126+
'margin',
127+
'marginTop',
128+
'marginRight',
129+
'marginBottom',
130+
'marginLeft',
131+
'marginX',
132+
'marginY',
133+
'top',
134+
'bottom',
135+
'left',
136+
'right',
137+
].reduce(function(acc, curr) {
138+
let _extends2;
139+
140+
return {
141+
...acc,
142+
...((_extends2 = {}), (_extends2[curr] = positiveOrNegative), _extends2),
143+
};
144+
}, {});
145+
export var responsive = function responsive(styles) {
146+
return function(theme) {
147+
const next = {};
148+
const breakpoints = get(theme, 'breakpoints', defaultBreakpoints);
149+
const mediaQueries = [null].concat(
150+
breakpoints.map(function(n) {
151+
return '@media screen and (min-width: ' + n + ')';
152+
})
153+
);
154+
155+
for (const key in styles) {
156+
const value =
157+
typeof styles[key] === 'function' ? styles[key](theme) : styles[key];
158+
if (value == null) continue;
159+
160+
if (!Array.isArray(value)) {
161+
next[key] = value;
162+
continue;
163+
}
164+
165+
for (let i = 0; i < value.slice(0, mediaQueries.length).length; i++) {
166+
const media = mediaQueries[i];
167+
168+
if (!media) {
169+
next[key] = value[i];
170+
continue;
171+
}
172+
173+
next[media] = next[media] || {};
174+
if (value[i] == null) continue;
175+
next[media][key] = value[i];
176+
}
177+
}
178+
179+
return next;
180+
};
181+
};
182+
export var css = function css(args) {
183+
return function(props) {
184+
if (props === void 0) {
185+
props = {};
186+
}
187+
188+
const theme = props.theme || props;
189+
190+
let result = {};
191+
const obj = typeof args === 'function' ? args(theme) : args;
192+
const styles = responsive(obj)(theme);
193+
194+
for (const key in styles) {
195+
const x = styles[key];
196+
const val = typeof x === 'function' ? x(theme) : x;
197+
198+
if (key === 'variant') {
199+
const variant = css(get(theme, val))(theme);
200+
result = { ...result, ...variant };
201+
continue;
202+
}
203+
204+
if (val && typeof val === 'object') {
205+
result[key] = css(val)(theme);
206+
continue;
207+
}
208+
209+
const prop = get(aliases, key, key);
210+
const scaleName = get(scales, prop);
211+
const scale = get(theme, scaleName, get(theme, prop, {}));
212+
const transform = get(transforms, prop, get);
213+
const value = transform(scale, val, val);
214+
215+
if (multiples[prop]) {
216+
const dirs = multiples[prop];
217+
218+
for (let i = 0; i < dirs.length; i++) {
219+
result[dirs[i]] = value;
220+
}
221+
} else {
222+
result[prop] = value;
223+
}
224+
}
225+
226+
return result;
227+
};
228+
};
229+
export default css;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@styled-system/css",
3+
"version": "5.1.5",
4+
"description": "Styled System for the `css` prop",
5+
"main": "dist/index.esm.js",
6+
"module": "dist/index.esm.js",
7+
"sideEffects": false,
8+
"keywords": [
9+
"css",
10+
"css-in-js",
11+
"emotion",
12+
"styled-components",
13+
"style",
14+
"react"
15+
],
16+
"author": "Brent Jackson <[email protected]>",
17+
"license": "MIT",
18+
"gitHead": "629fa1a080fb71c0f966c79539aa4c5fd92f2f74",
19+
"publishConfig": {
20+
"access": "public"
21+
}
22+
}

packages/components/.storybook/config.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import isChromatic from 'storybook-chromatic/isChromatic';
2+
import isChromatic from 'chromatic/isChromatic';
33
import { withKnobs } from '@storybook/addon-knobs';
44
import { withA11y } from '@storybook/addon-a11y';
55
import { addDecorator, addParameters, configure } from '@storybook/react';

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@reach/tooltip": "^0.8.6",
3434
"@reach/visually-hidden": "^0.7.0",
3535
"@styled-system/css": "^5.1.4",
36+
"chromatic": "^4.0.2",
3637
"codesandbox-api": "0.0.24",
3738
"color": "3.1.2",
3839
"date-fns": "^2.8.1",
@@ -60,7 +61,6 @@
6061
"np": "^5.2.1",
6162
"rimraf": "^3.0.0",
6263
"storybook-addon-styled-component-theme": "^1.3.0",
63-
"storybook-chromatic": "^3.5.3",
6464
"typescript": "3.7.4"
6565
},
6666
"peerDependencies": {

0 commit comments

Comments
 (0)