Skip to content

Commit 531c927

Browse files
committed
Implement currying interface, resolve #18
1 parent e80e66a commit 531c927

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ const Button = styled('button', {
3030
color: (props) => props.theme.textColor
3131
})
3232

33+
// Curried interface.
34+
const Button = styled('button')({
35+
fontSize: 12,
36+
color: (props) => props.theme.textColor
37+
})
38+
39+
const div = styled('div')
40+
41+
const Container = div({
42+
padding: 20
43+
})
44+
3345
// Composition.
3446
const PrimaryButton = styled(Button, {
3547
color: 'red'

src/createStyled.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const getStyledArgs = (
2020
return {tagName, style}
2121
}
2222

23+
const curry1 = (fn: Function) => (a: *, b?: *) => (b !== undefined ? fn(a, b) : b2 => fn(a, b2))
24+
2325
const createStyled = (jss: Function) => (baseStyles: BaseStylesType = {}): StyledType => {
2426
let staticSheet
2527
let dynamicSheet
@@ -39,15 +41,15 @@ const createStyled = (jss: Function) => (baseStyles: BaseStylesType = {}): Style
3941
return {staticSheet, dynamicSheet}
4042
}
4143

42-
return Object.assign((
44+
return Object.assign(curry1((
4345
tagNameOrStyledElement: TagNameOrStyledElementType,
4446
ownStyle: ComponentStyleType
4547
): StyledElementType => {
4648
const {tagName, style} = getStyledArgs(tagNameOrStyledElement)
4749
const elementStyle = {...style, ...ownStyle}
4850

4951
return styled({tagName, baseStyles, elementStyle, mountSheets})
50-
}, {mountSheets, styles: baseStyles})
52+
}), {mountSheets, styles: baseStyles})
5153
}
5254

5355
export default createStyled

src/tests/App.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@ export default (styled: StyledType) => {
66
margin: 50,
77
})
88

9-
const Header = styled('header', {
9+
const Header = styled('header')({
1010
padding: 10,
1111
})
1212

13-
const Section = styled('section', {
13+
// curried
14+
const section = styled('section')
15+
16+
const Section = section({
1417
color: 'red',
1518
})
1619

17-
const AnotherSection = styled(Section, {
20+
// composition
21+
const AnotherSection = styled(Section)({
1822
color: 'yellow',
1923
})
2024

2125
const Title = styled('h1', {
2226
color: 'red',
2327
})
2428

29+
// function value
2530
const Button = styled('button', {
2631
margin: ({margin = 0}) => margin,
2732
})

0 commit comments

Comments
 (0)