Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit b09d05f

Browse files
quicksnapKent C. Dodds
authored and
Kent C. Dodds
committed
feat: CSS function recursion (#391)
* CSS function recursion * Add test
1 parent 65c2376 commit b09d05f

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ yarn.lock
99
package-lock.json
1010
.vscode
1111
preact/
12+
.eslintcache

src/__tests__/__snapshots__/index.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ exports[`the css prop accepts "GlamorousStyles": css prop with a function 1`] =
260260
font-size: 10px;
261261
padding: 20px;
262262
margin: 30px;
263+
border: 1px solid pink;
263264
}
264265
265266
<section

src/__tests__/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,16 @@ test('the css prop accepts "GlamorousStyles"', () => {
8787

8888
const fn1 = jest.fn(() => ({padding: 20}))
8989
const fn2 = jest.fn(() => ({margin: 30}))
90-
const props = {css: [fn1, fn2], fontSize: 10, theme: {color: 'red'}}
90+
const nestedFn3 = jest.fn(() => ({border: '1px solid pink'}))
91+
const fn3 = () => [() => [() => nestedFn3]] // Deeply nested functions
92+
const props = {css: [fn1, fn2, fn3], fontSize: 10, theme: {color: 'red'}}
9193
expect(render(<glamorous.Section {...props} />)).toMatchSnapshot(
9294
'css prop with a function',
9395
)
9496
expect(fn1).toHaveBeenCalledTimes(1)
9597
expect(fn2).toHaveBeenCalledTimes(1)
98+
expect(nestedFn3).toHaveBeenCalledTimes(1)
99+
96100
const context = {__glamorous__: undefined}
97101
expect(fn1).toHaveBeenCalledWith(
98102
expect.objectContaining(props),
@@ -102,6 +106,10 @@ test('the css prop accepts "GlamorousStyles"', () => {
102106
expect.objectContaining(props),
103107
expect.objectContaining(context),
104108
)
109+
expect(nestedFn3).toHaveBeenCalledWith(
110+
expect.objectContaining(props),
111+
expect.objectContaining(context),
112+
)
105113
})
106114

107115
test('merges composed component styles for reasonable overrides', () => {

src/get-glamor-classname.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,10 @@ function handleStyles(styles, props, context) {
7676
const nonGlamorClassNames = []
7777
for (let i = 0; i < styles.length; i++) {
7878
current = styles[i]
79-
if (typeof current === 'function') {
80-
const result = current(props, context)
81-
if (typeof result === 'string') {
82-
const {glamorStyles, glamorlessClassName} = extractGlamorStyles(result)
83-
mappedArgs.push(...glamorStyles)
84-
nonGlamorClassNames.push(...glamorlessClassName)
85-
} else {
86-
mappedArgs.push(result)
87-
}
88-
} else if (typeof current === 'string') {
79+
while (typeof current === 'function') {
80+
current = current(props, context)
81+
}
82+
if (typeof current === 'string') {
8983
const {glamorStyles, glamorlessClassName} = extractGlamorStyles(current)
9084
mappedArgs.push(...glamorStyles)
9185
nonGlamorClassNames.push(...glamorlessClassName)

0 commit comments

Comments
 (0)