Skip to content

Commit

Permalink
Accept objects as className in jsx-created components (they are str…
Browse files Browse the repository at this point in the history
…ingified) to match React behavior (#1548)
  • Loading branch information
Andarist authored and emmatown committed Oct 21, 2019
1 parent 1bb3efe commit 2fc75f2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions .changeset/five-cycles-listen/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "releases": [{ "name": "@emotion/core", "type": "patch" }], "dependents": [] }
1 change: 1 addition & 0 deletions .changeset/five-cycles-listen/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Accept objects as `className` in jsx-created components (they are stringified) to match React behavior
21 changes: 21 additions & 0 deletions packages/core/__tests__/__snapshots__/legacy-class-name.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`object className 1`] = `
.emotion-0 {
color: hotpink;
}
<div
className="legacy__class emotion-0"
/>
`;

exports[`string className 1`] = `
.emotion-0 {
color: hotpink;
}
<div
className="legacy__class emotion-0"
/>
`;
35 changes: 35 additions & 0 deletions packages/core/__tests__/legacy-class-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// @flow
/** @jsx jsx */
import 'test-utils/next-env'
import { jsx, css } from '@emotion/core'
import renderer from 'react-test-renderer'

test('string className', () => {
const tree = renderer
.create(
<div
className="legacy__class"
css={css`
color: hotpink;
`}
/>
)
.toJSON()

expect(tree).toMatchSnapshot()
})

test('object className', () => {
const tree = renderer
.create(
<div
className={{ toString: () => 'legacy__class' }}
css={css`
color: hotpink;
`}
/>
)
.toJSON()

expect(tree).toMatchSnapshot()
})
4 changes: 3 additions & 1 deletion packages/core/src/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ let render = (cache, props, theme: null | Object, ref) => {
let registeredStyles = [cssProp]
let className = ''

if (props.className !== undefined) {
if (typeof props.className === 'string') {
className = getRegisteredStyles(
cache.registered,
registeredStyles,
props.className
)
} else if (props.className != null) {
className = `${props.className} `
}

let serialized = serializeStyles(registeredStyles)
Expand Down
4 changes: 2 additions & 2 deletions packages/styled-base/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
}

if (typeof props.className === 'string') {
className += getRegisteredStyles(
className = getRegisteredStyles(
context.registered,
classInterpolations,
props.className
)
} else if (props.className != null) {
className += `${props.className} `
className = `${props.className} `
}

const serialized = serializeStyles(
Expand Down

0 comments on commit 2fc75f2

Please sign in to comment.