Skip to content

Commit 15c165a

Browse files
authored
Merge pull request #20 from styled-system/css-variants
Add variants support to `css` package
2 parents 7fa97f1 + 97da5a8 commit 15c165a

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

packages/css/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ export const responsive = styles => theme => {
8181

8282
for (const key in styles) {
8383
const value = styles[key]
84-
if (value && !Array.isArray(value) && typeof value === 'object') {
85-
next[key] = responsive(value)(theme)
86-
continue
87-
}
8884
if (!Array.isArray(value)) {
8985
next[key] = value
9086
continue
@@ -105,7 +101,7 @@ export const responsive = styles => theme => {
105101

106102
export const css = args => (props = {}) => {
107103
const theme = { ...defaultTheme, ...(props.theme || props) }
108-
const result = {}
104+
let result = {}
109105
const obj = typeof args === 'function' ? args(theme) : args
110106
const styles = responsive(obj)(theme)
111107

@@ -115,6 +111,11 @@ export const css = args => (props = {}) => {
115111
const scale = get(theme, scaleName, get(theme, prop, {}))
116112
const x = styles[key]
117113
const val = typeof x === 'function' ? x(theme) : x
114+
if (key === 'variant') {
115+
const variant = css(get(theme, val))(theme)
116+
result = { ...result, ...variant }
117+
continue
118+
}
118119
if (val && typeof val === 'object') {
119120
result[prop] = css(val)(theme)
120121
continue

packages/css/test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ const theme = {
1818
fontWeights: {
1919
bold: 600,
2020
},
21+
buttons: {
22+
primary: {
23+
p: 3,
24+
fontWeight: 'bold',
25+
color: 'white',
26+
bg: 'primary',
27+
borderRadius: 2,
28+
},
29+
},
30+
text: {
31+
caps: {
32+
fontSize: [ 1, 2 ],
33+
letterSpacing: '0.1em',
34+
textTransform: 'uppercase',
35+
},
36+
title: {
37+
fontSize: [ 3, 4 ],
38+
letterSpacing: [ '-0.01em', '-0.02em']
39+
},
40+
}
2141
}
2242

2343
test('returns a function', t => {
@@ -156,3 +176,44 @@ test('supports functional values', t => {
156176
color: 'tomato',
157177
})
158178
})
179+
180+
test('returns variants from theme', t => {
181+
const res = css({
182+
variant: 'buttons.primary',
183+
})(theme)
184+
t.deepEqual(res, {
185+
padding: 16,
186+
fontWeight: 600,
187+
color: 'white',
188+
backgroundColor: 'tomato',
189+
borderRadius: 2,
190+
})
191+
})
192+
193+
test('handles variants with responsive values', t => {
194+
const res = css({
195+
variant: 'text.caps',
196+
})(theme)
197+
t.deepEqual(res, {
198+
fontSize: 14,
199+
letterSpacing: '0.1em',
200+
textTransform: 'uppercase',
201+
'@media screen and (min-width: 40em)': {
202+
fontSize: 16,
203+
}
204+
})
205+
})
206+
207+
test('handles responsive variants', t => {
208+
const res = css({
209+
variant: 'text.title',
210+
})(theme)
211+
t.deepEqual(res, {
212+
fontSize: 24,
213+
letterSpacing: '-0.01em',
214+
'@media screen and (min-width: 40em)': {
215+
fontSize: 36,
216+
letterSpacing: '-0.02em',
217+
}
218+
})
219+
})

0 commit comments

Comments
 (0)