Skip to content

Commit a4bfcbd

Browse files
committed
fix some things
1 parent f15e754 commit a4bfcbd

File tree

3 files changed

+63
-76
lines changed

3 files changed

+63
-76
lines changed

packages/react/__tests__/with-theme.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ test(`withTheme(Comp) hoists non-react static class properties`, () => {
3535
})
3636

3737
test('should forward the ref', () => {
38-
class SomeComponent extends React.Component {
39-
render() {
40-
return this.props.theme.color
41-
}
38+
function SomeComponent(props) {
39+
return <div ref={props.ref}>{props.theme.color}</div>
4240
}
4341

4442
const ComponentWithTheme = withTheme(SomeComponent)
@@ -48,5 +46,5 @@ test('should forward the ref', () => {
4846
<ComponentWithTheme ref={ref} />
4947
</ThemeProvider>
5048
)
51-
expect(ref.current).toBeInstanceOf(SomeComponent)
49+
expect(ref.current).toBeInstanceOf(HTMLDivElement)
5250
})

packages/react/src/emotion-element.tsx

+59-67
Original file line numberDiff line numberDiff line change
@@ -109,82 +109,74 @@ const Insertion = ({
109109
return null
110110
}
111111

112-
let Emotion = /* #__PURE__ */ withEmotionCache<EmotionProps>(
113-
(props, cache, ref) => {
114-
let cssProp = props.css as EmotionProps['css']
115-
116-
// so that using `css` from `emotion` and passing the result to the css prop works
117-
// not passing the registered cache to serializeStyles because it would
118-
// make certain babel optimisations not possible
119-
if (
120-
typeof cssProp === 'string' &&
121-
cache.registered[cssProp] !== undefined
122-
) {
123-
cssProp = cache.registered[cssProp]
124-
}
112+
let Emotion = /* #__PURE__ */ withEmotionCache<EmotionProps>((props, cache) => {
113+
let cssProp = props.css as EmotionProps['css']
114+
115+
// so that using `css` from `emotion` and passing the result to the css prop works
116+
// not passing the registered cache to serializeStyles because it would
117+
// make certain babel optimisations not possible
118+
if (typeof cssProp === 'string' && cache.registered[cssProp] !== undefined) {
119+
cssProp = cache.registered[cssProp]
120+
}
125121

126-
let WrappedComponent = props[
127-
typePropName
128-
] as EmotionProps[typeof typePropName]
129-
let registeredStyles = [cssProp]
130-
let className = ''
131-
132-
if (typeof props.className === 'string') {
133-
className = getRegisteredStyles(
134-
cache.registered,
135-
registeredStyles,
136-
props.className
137-
)
138-
} else if (props.className != null) {
139-
className = `${props.className} `
140-
}
122+
let WrappedComponent = props[
123+
typePropName
124+
] as EmotionProps[typeof typePropName]
125+
let registeredStyles = [cssProp]
126+
let className = ''
141127

142-
let serialized = serializeStyles(
128+
if (typeof props.className === 'string') {
129+
className = getRegisteredStyles(
130+
cache.registered,
143131
registeredStyles,
144-
undefined,
145-
React.useContext(ThemeContext)
132+
props.className
146133
)
134+
} else if (props.className != null) {
135+
className = `${props.className} `
136+
}
147137

148-
if (isDevelopment && serialized.name.indexOf('-') === -1) {
149-
let labelFromStack = props[labelPropName]
150-
if (labelFromStack) {
151-
serialized = serializeStyles([
152-
serialized,
153-
'label:' + labelFromStack + ';'
154-
])
155-
}
156-
}
138+
let serialized = serializeStyles(
139+
registeredStyles,
140+
undefined,
141+
React.useContext(ThemeContext)
142+
)
157143

158-
className += `${cache.key}-${serialized.name}`
159-
160-
const newProps: Record<string, unknown> = {}
161-
for (let key in props) {
162-
if (
163-
hasOwn.call(props, key) &&
164-
key !== 'css' &&
165-
key !== typePropName &&
166-
(!isDevelopment || key !== labelPropName)
167-
) {
168-
newProps[key] = props[key]
169-
}
170-
}
171-
newProps.className = className
172-
if (ref) {
173-
newProps.ref = ref
144+
if (isDevelopment && serialized.name.indexOf('-') === -1) {
145+
let labelFromStack = props[labelPropName]
146+
if (labelFromStack) {
147+
serialized = serializeStyles([
148+
serialized,
149+
'label:' + labelFromStack + ';'
150+
])
174151
}
152+
}
175153

176-
return (
177-
<>
178-
<Insertion
179-
cache={cache}
180-
serialized={serialized}
181-
isStringTag={typeof WrappedComponent === 'string'}
182-
/>
183-
<WrappedComponent {...newProps} />
184-
</>
185-
)
154+
className += `${cache.key}-${serialized.name}`
155+
156+
const newProps: Record<string, unknown> = {}
157+
for (let key in props) {
158+
if (
159+
hasOwn.call(props, key) &&
160+
key !== 'css' &&
161+
key !== typePropName &&
162+
(!isDevelopment || key !== labelPropName)
163+
) {
164+
newProps[key] = props[key]
165+
}
186166
}
187-
)
167+
newProps.className = className
168+
169+
return (
170+
<>
171+
<Insertion
172+
cache={cache}
173+
serialized={serialized}
174+
isStringTag={typeof WrappedComponent === 'string'}
175+
/>
176+
<WrappedComponent {...newProps} />
177+
</>
178+
)
179+
})
188180

189181
if (isDevelopment) {
190182
Emotion.displayName = 'EmotionCssPropInternal'

packages/styled/src/base.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const createStyled = (tag: ElementType, options?: StyledOptions) => {
120120
}
121121

122122
const Styled: ElementType = withEmotionCache(
123-
(props: Record<string, unknown>, cache, ref) => {
123+
(props: Record<string, unknown>, cache) => {
124124
const FinalTag =
125125
(shouldUseAs && (props.as as React.ElementType)) || baseTag
126126

@@ -170,9 +170,6 @@ const createStyled = (tag: ElementType, options?: StyledOptions) => {
170170
}
171171
}
172172
newProps.className = className
173-
if (ref) {
174-
newProps.ref = ref
175-
}
176173

177174
return (
178175
<>

0 commit comments

Comments
 (0)