Skip to content

Commit

Permalink
Accept objects as className in styled components (they are stringif…
Browse files Browse the repository at this point in the history
…ied) to match React behavior

* Accept objects as className in styled components

* PR feedback - change to else if

* PR Feedback - change condition

* PR feedback - fix other test

* Update .changeset/afraid-dolls-speak.md

Co-Authored-By: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Update .changeset/afraid-dolls-speak.md

Co-Authored-By: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Update .changeset/afraid-dolls-speak.md

Co-Authored-By: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
jgroszko and Andarist committed Oct 11, 2019
1 parent a07f8a7 commit c3f0bc1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/afraid-dolls-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@emotion/styled-base': patch
'@emotion/styled': patch
---

Accept objects as `className` in styled components (they are stringified) to match React behavior
3 changes: 3 additions & 0 deletions packages/styled-base/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
classInterpolations,
props.className
)
} else if (props.className != null) {
className += `${props.className} `
}

const serialized = serializeStyles(
styles.concat(classInterpolations),
context.registered,
Expand Down
6 changes: 6 additions & 0 deletions packages/styled/__tests__/__snapshots__/styled.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ exports[`styled no prop filtering on string tags started with upper case 1`] = `
</SomeCustomLink>
`;

exports[`styled object as className 1`] = `
<div
className="myclass emotion-0"
/>
`;

exports[`styled object as style 1`] = `
.emotion-0 {
font-size: 20px;
Expand Down
11 changes: 10 additions & 1 deletion packages/styled/__tests__/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ describe('styled', () => {
expect(tree).toMatchSnapshot()
})

test('object as className', () => {
const myclass = { toString: () => 'myclass' }
const Comp = styled.div``

const tree = renderer.create(<Comp className={myclass} />).toJSON()

expect(tree).toMatchSnapshot()
})

test('glamorous style api & composition', () => {
const H1 = styled.h1(props => ({ fontSize: props.fontSize }))
const H2 = styled(H1)(props => ({ flex: props.flex }), { display: 'flex' })
Expand Down Expand Up @@ -392,7 +401,7 @@ describe('styled', () => {
const tree = renderer
.create(
<Button
className={css({
css={css({
'&:hover': {
color: 'pink',
'&:active': {
Expand Down

0 comments on commit c3f0bc1

Please sign in to comment.