Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use flush effects to custom apply css-in-js solution to app. Re-introduce flush effects to app-render, and remove default support of styled-jsx in `app/`. So that users will choose their own css-in-js solution if they need any customization. styled-jsx won't appear in client bundle if you didn't use it. For now we have to inject the initial styles before `</head>` to avoid hydration errors. Later on we can remove this once react can handle it. - [x] inject styles before end of head element - [x] add tests
- Loading branch information
Showing
18 changed files
with
188 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { createContext, useContext } from 'react' | ||
|
||
export type FlushEffectsHook = (callbacks: () => React.ReactNode) => void | ||
|
||
export const FlushEffectsContext = createContext<FlushEffectsHook | null>( | ||
null as any | ||
) | ||
|
||
export function useFlushEffects(callbacks: () => React.ReactNode): void { | ||
const flushEffectsImpl = useContext(FlushEffectsContext) | ||
// Should have no effects on client where there's no flush effects provider | ||
if (!flushEffectsImpl) return | ||
return flushEffectsImpl(callbacks) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Comp from './styled-jsx.client' | ||
import StyledComp from './styled-components.client' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<Comp /> | ||
<StyledComp /> | ||
</div> | ||
) | ||
} |
30 changes: 30 additions & 0 deletions
30
test/e2e/app-dir/rsc-basic/app/css-in-js/styled-components.client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import styled from 'styled-components' | ||
|
||
const Button = styled.button` | ||
display: inline-block; | ||
border-radius: 3px; | ||
padding: 0.5rem 0; | ||
margin: 0.5rem 1rem; | ||
width: 11rem; | ||
color: blue; | ||
border: 2px solid blue; | ||
` | ||
|
||
const Box = styled.div` | ||
border: 1px solid blue; | ||
padding: 8px; | ||
margin: 8px 0; | ||
` | ||
|
||
const Title = styled.h3` | ||
color: blue; | ||
` | ||
|
||
export default () => { | ||
return ( | ||
<Box> | ||
<Title>styled-components</Title> | ||
<Button>{`💅 This area belongs to styled-components`}</Button> | ||
</Box> | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
test/e2e/app-dir/rsc-basic/app/css-in-js/styled-jsx.client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default function Comp() { | ||
return ( | ||
<div> | ||
<style jsx>{` | ||
h3 { | ||
color: purple; | ||
} | ||
.box { | ||
padding: 8px; | ||
border: 2px solid purple; | ||
} | ||
`}</style> | ||
<div className="box"> | ||
<h3>styled-jsx</h3> | ||
<p>This area is rendered by styled-jsx</p> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import React from 'react' | ||
import RootStyleRegistry from './root-style-registry.client' | ||
|
||
export default function AppLayout({ children }) { | ||
return ( | ||
<html> | ||
<head> | ||
<title>RSC</title> | ||
</head> | ||
<body>{children}</body> | ||
<body> | ||
<RootStyleRegistry>{children}</RootStyleRegistry> | ||
</body> | ||
</html> | ||
) | ||
} |
43 changes: 43 additions & 0 deletions
43
test/e2e/app-dir/rsc-basic/app/root-style-registry.client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react' | ||
import { StyleRegistry, createStyleRegistry } from 'styled-jsx' | ||
import { ServerStyleSheet, StyleSheetManager } from 'styled-components' | ||
import { useFlushEffects } from 'next/dist/client/components/hooks-client' | ||
import { useState } from 'react' | ||
|
||
export default function RootStyleRegistry({ children }) { | ||
const [jsxStyleRegistry] = useState(() => createStyleRegistry()) | ||
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet()) | ||
const styledJsxFlushEffect = () => { | ||
const styles = jsxStyleRegistry.styles() | ||
jsxStyleRegistry.flush() | ||
return <>{styles}</> | ||
} | ||
const styledComponentsFlushEffect = () => { | ||
const styles = styledComponentsStyleSheet.getStyleElement() | ||
styledComponentsStyleSheet.seal() | ||
|
||
return <>{styles}</> | ||
} | ||
|
||
useFlushEffects(() => { | ||
const effects = styledComponentsFlushEffect() | ||
|
||
return ( | ||
<> | ||
{styledJsxFlushEffect()} | ||
{effects} | ||
</> | ||
) | ||
}) | ||
|
||
// Only include style registry on server side for SSR | ||
if (typeof window === 'undefined') { | ||
return ( | ||
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}> | ||
<StyleRegistry registry={jsxStyleRegistry}>{children}</StyleRegistry> | ||
</StyleSheetManager> | ||
) | ||
} | ||
|
||
return children | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.