Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow interpolation return value to be a function. #346

Merged
merged 8 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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