Skip to content

Commit

Permalink
fix some things
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Dec 9, 2024
1 parent b691cf1 commit eafffcc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 76 deletions.
8 changes: 3 additions & 5 deletions packages/react/__tests__/with-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ test(`withTheme(Comp) hoists non-react static class properties`, () => {
})

test('should forward the ref', () => {
class SomeComponent extends React.Component {
render() {
return this.props.theme.color
}
function SomeComponent(props) {
return <div ref={props.ref}>{props.theme.color}</div>
}

const ComponentWithTheme = withTheme(SomeComponent)
Expand All @@ -48,5 +46,5 @@ test('should forward the ref', () => {
<ComponentWithTheme ref={ref} />
</ThemeProvider>
)
expect(ref.current).toBeInstanceOf(SomeComponent)
expect(ref.current).toBeInstanceOf(HTMLDivElement)
})
126 changes: 59 additions & 67 deletions packages/react/src/emotion-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,82 +109,74 @@ const Insertion = ({
return null
}

let Emotion = /* #__PURE__ */ withEmotionCache<EmotionProps>(
(props, cache, ref) => {
let cssProp = props.css as EmotionProps['css']

// so that using `css` from `emotion` and passing the result to the css prop works
// not passing the registered cache to serializeStyles because it would
// make certain babel optimisations not possible
if (
typeof cssProp === 'string' &&
cache.registered[cssProp] !== undefined
) {
cssProp = cache.registered[cssProp]
}
let Emotion = /* #__PURE__ */ withEmotionCache<EmotionProps>((props, cache) => {
let cssProp = props.css as EmotionProps['css']

// so that using `css` from `emotion` and passing the result to the css prop works
// not passing the registered cache to serializeStyles because it would
// make certain babel optimisations not possible
if (typeof cssProp === 'string' && cache.registered[cssProp] !== undefined) {
cssProp = cache.registered[cssProp]
}

let WrappedComponent = props[
typePropName
] as EmotionProps[typeof typePropName]
let registeredStyles = [cssProp]
let className = ''

if (typeof props.className === 'string') {
className = getRegisteredStyles(
cache.registered,
registeredStyles,
props.className
)
} else if (props.className != null) {
className = `${props.className} `
}
let WrappedComponent = props[
typePropName
] as EmotionProps[typeof typePropName]
let registeredStyles = [cssProp]
let className = ''

let serialized = serializeStyles(
if (typeof props.className === 'string') {
className = getRegisteredStyles(
cache.registered,
registeredStyles,
undefined,
React.useContext(ThemeContext)
props.className
)
} else if (props.className != null) {
className = `${props.className} `
}

if (isDevelopment && serialized.name.indexOf('-') === -1) {
let labelFromStack = props[labelPropName]
if (labelFromStack) {
serialized = serializeStyles([
serialized,
'label:' + labelFromStack + ';'
])
}
}
let serialized = serializeStyles(
registeredStyles,
undefined,
React.useContext(ThemeContext)
)

className += `${cache.key}-${serialized.name}`

const newProps: Record<string, unknown> = {}
for (let key in props) {
if (
hasOwn.call(props, key) &&
key !== 'css' &&
key !== typePropName &&
(!isDevelopment || key !== labelPropName)
) {
newProps[key] = props[key]
}
}
newProps.className = className
if (ref) {
newProps.ref = ref
if (isDevelopment && serialized.name.indexOf('-') === -1) {
let labelFromStack = props[labelPropName]
if (labelFromStack) {
serialized = serializeStyles([
serialized,
'label:' + labelFromStack + ';'
])
}
}

return (
<>
<Insertion
cache={cache}
serialized={serialized}
isStringTag={typeof WrappedComponent === 'string'}
/>
<WrappedComponent {...newProps} />
</>
)
className += `${cache.key}-${serialized.name}`

const newProps: Record<string, unknown> = {}
for (let key in props) {
if (
hasOwn.call(props, key) &&
key !== 'css' &&
key !== typePropName &&
(!isDevelopment || key !== labelPropName)
) {
newProps[key] = props[key]
}
}
)
newProps.className = className

return (
<>
<Insertion
cache={cache}
serialized={serialized}
isStringTag={typeof WrappedComponent === 'string'}
/>
<WrappedComponent {...newProps} />
</>
)
})

if (isDevelopment) {
Emotion.displayName = 'EmotionCssPropInternal'
Expand Down
5 changes: 1 addition & 4 deletions packages/styled/src/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const createStyled = (tag: ElementType, options?: StyledOptions) => {
}

const Styled: ElementType = withEmotionCache(
(props: Record<string, unknown>, cache, ref) => {
(props: Record<string, unknown>, cache) => {
const FinalTag =
(shouldUseAs && (props.as as React.ElementType)) || baseTag

Expand Down Expand Up @@ -170,9 +170,6 @@ const createStyled = (tag: ElementType, options?: StyledOptions) => {
}
}
newProps.className = className
if (ref) {
newProps.ref = ref
}

return (
<>
Expand Down

0 comments on commit eafffcc

Please sign in to comment.