CSS-in-JS
Warning: CSS-in-JS libraries which require runtime JavaScript are not currently supported in Server Components. Using CSS-in-JS with newer React features like Server Components and Streaming requires library authors to support the latest version of React, including concurrent rendering.
Weโre working with the React team on upstream APIs to handle CSS and JavaScript assets with support for React Server Components and streaming architecture.
The following libraries are supported in Client Components in the app directory (alphabetical):
chakra-uikuma-ui@mui/materialpandacssstyled-jsxstyled-componentsstyle9tamaguitss-reactvanilla-extract
The following are currently working on support:
Good to know: Weโre testing out different CSS-in-JS libraries and weโll be adding more examples for libraries that support React 18 features and/or the
appdirectory.
If you want to style Server Components, we recommend using CSS Modules or other solutions that output CSS files, like PostCSS or Tailwind CSS.
Configuring CSS-in-JS in app
Configuring CSS-in-JS is a three-step opt-in process that involves:
- A style registry to collect all CSS rules in a render.
- The new
useServerInsertedHTMLhook to inject rules before any content that might use them. - A Client Component that wraps your app with the style registry during initial server-side rendering.
styled-jsx
Using styled-jsx in Client Components requires using v5.1.0. First, create a new registry:
1'use client'23import React, { useState } from 'react'4import { useServerInsertedHTML } from 'next/navigation'5import { StyleRegistry, createStyleRegistry } from 'styled-jsx'67export default function StyledJsxRegistry({ children }: { children: React.ReactNode }) {8// Only create stylesheet once with lazy initial state9// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state10const [jsxStyleRegistry] = useState(() => createStyleRegistry())1112useServerInsertedHTML(() => {13const styles = jsxStyleRegistry.styles()14jsxStyleRegistry.flush()15return <>{styles}</>16})1718return <StyleRegistry registry={jsxStyleRegistry}>{children}</StyleRegistry>19}
Then, wrap your root layout with the registry:
1import StyledJsxRegistry from './registry'23export default function RootLayout({ children }: { children: React.ReactNode }) {4return (5<html>6<body>7<StyledJsxRegistry>{children}</StyledJsxRegistry>8</body>9</html>10)11}
Styled Components
Below is an example of how to configure styled-components@6 or newer:
First, enable styled-components in next.config.js.
1module.exports = {2compiler: {3styledComponents: true,4},5},6}
Then, use the styled-components API to create a global registry component to collect all CSS style rules generated during a render, and a function to return those rules. Then use the useServerInsertedHTML hook to inject the styles collected in the registry into the <head> HTML tag in the root layout.
1'use client'23import React, { useState } from 'react'4import { useServerInsertedHTML } from 'next/navigation'5import { ServerStyleSheet, StyleSheetManager } from 'styled-components'67export default function StyledComponentsRegistry({ children }: { children: React.ReactNode }) {8// Only create stylesheet once with lazy initial state9// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state10const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet())1112useServerInsertedHTML(() => {13const styles = styledComponentsStyleSheet.getStyleElement()14styledComponentsStyleSheet.instance.clearTag()15return <>{styles}</>16})1718if (typeof window !== 'undefined') return <>{children}</>1920return <StyleSheetManager sheet={styledComponentsStyleSheet.instance}>{children}</StyleSheetManager>21}
Wrap the children of the root layout with the style registry component:
1import StyledComponentsRegistry from './lib/registry'23export default function RootLayout({ children }: { children: React.ReactNode }) {4return (5<html>6<body>7<StyledComponentsRegistry>{children}</StyledComponentsRegistry>8</body>9</html>10)11}
Good to know:
- During server rendering, styles will be extracted to a global registry and flushed to the
<head>of your HTML. This ensures the style rules are placed before any content that might use them. In the future, we may use an upcoming React feature to determine where to inject the styles.- During streaming, styles from each chunk will be collected and appended to existing styles. After client-side hydration is complete,
styled-componentswill take over as usual and inject any further dynamic styles.- We specifically use a Client Component at the top level of the tree for the style registry because itโs more efficient to extract CSS rules this way. It avoids re-generating styles on subsequent server renders, and prevents them from being sent in the Server Component payload.
- For advanced use cases where you need to configure individual properties of styled-components compilation, you can read our Next.js styled-components API reference to learn more.