Skip to content

Commit

Permalink
Function return values (#346)
Browse files Browse the repository at this point in the history
* Add emotion system to the repo

* Revert "Add emotion system to the repo"

This reverts commit efd73e0

* Support functions as return values in interpolations

* Revert "Support functions as return values in interpolations"

This reverts commit 7e5d8db

* Support functions as return values in interpolations

(cherry picked from commit 7e5d8db)

* Recursively handle a function interpolation value.

* Fucking linter
  • Loading branch information
Kye Hohenberger committed Sep 26, 2017
1 parent ec3012e commit 719358b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ function handleInterpolation(
return ''
}

if (typeof interpolation === 'function') {
return handleInterpolation(interpolation())
}

if (typeof interpolation === 'object') {
return createStringFromObject(interpolation)
}
Expand Down
10 changes: 10 additions & 0 deletions packages/emotion/test/__snapshots__/css.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ exports[`css registered styles as nested selector value in object 1`] = `
/>
`;

exports[`css return function in interpolation 1`] = `
.glamor-0 {
color: blue;
}
<div
className="glamor-0"
/>
`;

exports[`css weakmap 1`] = `
.glamor-0 {
display: -webkit-box;
Expand Down
7 changes: 7 additions & 0 deletions packages/emotion/test/css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,11 @@ describe('css', () => {
const tree2 = renderer.create(<div className={cls2} />).toJSON()
expect(tree2).toMatchSnapshot()
})

test('return function in interpolation', () => {
const cls1 = css`color: ${() => 'blue'};`

const tree = renderer.create(<div className={cls1} />).toJSON()
expect(tree).toMatchSnapshot()
})
})
12 changes: 12 additions & 0 deletions packages/react-emotion/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ exports[`styled higher order component 1`] = `
/>
`;

exports[`styled inline function return value is a function 1`] = `
.glamor-0 {
font-size: 20px;
}
<h1
className="glamor-0"
>
hello world
</h1>
`;

exports[`styled innerRef 1`] = `
.glamor-0 {
font-size: 12px;
Expand Down
9 changes: 9 additions & 0 deletions packages/react-emotion/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ describe('styled', () => {
expect(tree).toMatchSnapshot()
})

test('inline function return value is a function', () => {
const fontSize = () => 20
const H1 = styled('h1')`font-size: ${() => fontSize}px;`

const tree = renderer.create(<H1>hello world</H1>).toJSON()

expect(tree).toMatchSnapshot()
})

test('call expression', () => {
const fontSize = 20
const H1 = styled('h1')`font-size: ${fontSize}px;`
Expand Down

0 comments on commit 719358b

Please sign in to comment.