Skip to content

Commit bdb09c4

Browse files
committed
Handle nested functions
1 parent c79ba7c commit bdb09c4

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/__tests__/css.test.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createStyled } from '../styled'
22
import { buildDynamicStyles } from '../buildDynamicStyles'
3+
import { UnknownProps } from '../types'
34

45
const props = { theme: {} }
56

@@ -322,4 +323,17 @@ describe('styled props', () => {
322323
backgroundColor: 'yellow',
323324
})
324325
})
325-
})
326+
327+
test('Should handle nested functions', () => {
328+
const cfs = (value: number) => (_props: UnknownProps) => value
329+
const props = { scale: 2 }
330+
const { css } = createStyled()
331+
const styles = css<typeof props>`
332+
font-size: ${({ scale }) => cfs(2 * scale)}px;
333+
`
334+
335+
expect(buildDynamicStyles(props, styles)).toStrictEqual({
336+
fontSize: 4,
337+
})
338+
})
339+
})

src/buildDynamicStyles.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export function buildDynamicStyles(
1313
if (isParser(value)) {
1414
value(props, dynamicStyles)
1515
} else {
16-
dynamicStyles[key] = value(props)
16+
let resultOrFn = value(props)
17+
while(isFunction(resultOrFn)) {
18+
resultOrFn = resultOrFn(props)
19+
}
20+
dynamicStyles[key] = resultOrFn
1721
}
1822
} else {
1923
dynamicStyles[key] = value

0 commit comments

Comments
 (0)