Skip to content

Commit ef34b61

Browse files
authored
fix(emotion): add transient props support (#252)
Emotion allows its consumer apps to change styles based on props. See here. By default, emotion would prevent invalid props from being rendered to the DOM element but since xstyled is overriding the "shouldForwardProp" this functionality is being removed. Styled-components handle this by prefixing the prop name with a dollar sign $.
1 parent e620935 commit ef34b61

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/emotion/src/createX.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const createX: CreateX = <TProps extends object>(
3333
// @ts-ignore
3434
x[tag] = styled(tag, {
3535
shouldForwardProp: (prop: string) =>
36-
prop !== 'as' && !generator.meta.props.includes(prop),
36+
prop !== 'as' && !prop.startsWith('$') && !generator.meta.props.includes(prop),
3737
// @ts-ignore
3838
})<TProps>(() => [`&&{`, generator, `}`])
3939
})

packages/emotion/src/styled.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ describe('#styled', () => {
151151
const { container } = render(<Dummy color="lemonchiffon" />)
152152
expect(container.firstChild).not.toHaveAttribute('color', 'lemonchiffon')
153153
})
154+
155+
it('should not pass props that are invalid html attributes', () => {
156+
// https://emotion.sh/docs/styled#customizing-prop-forwarding
157+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(jest.fn())
158+
const Dummy = styled.box({})
159+
160+
// @ts-ignore
161+
const { container } = render(<Dummy $dark={false} />)
162+
163+
expect(container.firstChild).not.toHaveAttribute('$dark', false)
164+
expect(consoleErrorSpy).not.toHaveBeenCalled()
165+
166+
consoleErrorSpy.mockRestore()
167+
})
154168
})
155169

156170
describe('#styled.xxx', () => {

0 commit comments

Comments
 (0)