Skip to content

Commit dbe0aae

Browse files
quantizorclaude
andcommitted
fix: make OverrideStyle compatible with React 19 and return undefined theme in RSC
Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent ca61aca commit dbe0aae

4 files changed

Lines changed: 26 additions & 18 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'styled-components': patch
3+
---
4+
5+
In RSC environments, `theme` is now `undefined` instead of `{}` for styled components, matching the existing behavior of `withTheme` and `createGlobalStyle`. This ensures accessing theme properties without a ThemeProvider correctly throws rather than silently returning `undefined`.

‎packages/styled-components/src/test/theme.test.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ describe('theming', () => {
153153

154154
it('should only inject props.theme into styled components within its child component tree', () => {
155155
const Comp1 = styled.div`
156-
color: ${props => props.theme.color || 'red'};
156+
color: ${props => props.theme?.color || 'red'};
157157
`;
158158
const Comp2 = styled.div`
159-
color: ${props => props.theme.color || 'red'};
159+
color: ${props => props.theme?.color || 'red'};
160160
`;
161161

162162
const theme = { color: 'black' };
@@ -531,7 +531,7 @@ describe('theming', () => {
531531
}
532532

533533
const CompWithTheme = withTheme(Comp);
534-
const ref = React.createRef<typeof Comp>();
534+
const ref = React.createRef<InstanceType<typeof Comp>>();
535535

536536
render(
537537
<ThemeProvider theme={{}}>

‎packages/styled-components/src/test/types.tsx‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ const Component2 = () => {
5555
const Example = styled.div.attrs({
5656
title: 'test',
5757
})`
58-
margin-top: ${props => props.theme.spacing};
58+
margin-top: ${props => props.theme?.spacing};
5959
`;
6060

6161
const Example2 = styled.div.attrs({
6262
// title: "test" // This works for some reason
6363
})`
64-
margin-top: ${props => props.theme.spacing};
64+
margin-top: ${props => props.theme?.spacing};
6565
`;
6666

6767
/**
@@ -326,16 +326,16 @@ const UnstyledComponentVeryLargeUnion = (_props: VeryLargeUnionProps) => {
326326

327327
const StyledComponentVeryLargeUnion = styled(UnstyledComponentVeryLargeUnion)`
328328
// Multiple template strings helps illustrate possible perf issue
329-
color: ${props => props.theme.waz};
330-
color: ${props => props.theme.waz};
331-
color: ${props => props.theme.waz};
332-
color: ${props => props.theme.waz};
333-
color: ${props => props.theme.waz};
334-
color: ${props => props.theme.waz};
335-
color: ${props => props.theme.waz};
336-
color: ${props => props.theme.waz};
337-
color: ${props => props.theme.waz};
338-
color: ${props => props.theme.waz};
329+
color: ${props => props.theme?.waz || 'black'};
330+
color: ${props => props.theme?.waz || 'black'};
331+
color: ${props => props.theme?.waz || 'black'};
332+
color: ${props => props.theme?.waz || 'black'};
333+
color: ${props => props.theme?.waz || 'black'};
334+
color: ${props => props.theme?.waz || 'black'};
335+
color: ${props => props.theme?.waz || 'black'};
336+
color: ${props => props.theme?.waz || 'black'};
337+
color: ${props => props.theme?.waz || 'black'};
338+
color: ${props => props.theme?.waz || 'black'};
339339
`;
340340

341341
// Can provide div props into attrs

‎packages/styled-components/src/types.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ export type ExecutionProps = {
8686
};
8787

8888
/**
89-
* ExecutionProps but with `theme` required.
89+
* ExecutionProps but with `theme` narrowed from optional to required.
90+
*
91+
* Note: in RSC environments where ThemeProvider is a no-op,
92+
* `theme` will be `undefined` at runtime.
9093
*/
9194
export interface ExecutionContext extends ExecutionProps {
9295
theme: DefaultTheme;
@@ -287,8 +290,8 @@ export type CSSPropertiesWithVars = CSSProperties & {
287290
[key: `--${string}`]: string | number | undefined;
288291
};
289292

290-
type OverrideStyle<P> = P extends { style?: any }
291-
? Omit<P, 'style'> & { style?: CSSPropertiesWithVars }
293+
type OverrideStyle<P> = P extends { style?: infer S }
294+
? Omit<P, 'style'> & { style?: CSSPropertiesWithVars | (S & {}) }
292295
: P;
293296

294297
export type CSSPseudos = { [K in CSS.Pseudos]?: CSSObject };

0 commit comments

Comments
 (0)