Full CSS support for JSX without compromises, providing scoped component-friendly CSS with server-side rendering support
npx @tessl/cli install tessl/npm-styled-jsx@5.1.0styled-jsx provides full CSS support for JSX without compromises, enabling developers to write scoped, component-friendly CSS directly within JSX components. It offers complete CSS capabilities with automatic scoping, server-side rendering support, and a compact 3kb gzipped runtime.
npm install styled-jsximport { StyleRegistry, createStyleRegistry, useStyleRegistry } from "styled-jsx";For CSS utilities:
import css from "styled-jsx/css";For style component (usually auto-imported by Babel):
import JSXStyle from "styled-jsx/style";For babel macro support:
import { resolve } from "styled-jsx/macro";// Basic scoped styles
function Button({ children }) {
return (
<button>
{children}
<style jsx>{`
button {
padding: 20px;
background: #eee;
color: #999;
border: none;
border-radius: 4px;
}
`}</style>
</button>
);
}
// Global styles
function App() {
return (
<div>
<Button>Click me</Button>
<style jsx global>{`
body {
margin: 0;
font-family: Arial, sans-serif;
}
`}</style>
</div>
);
}styled-jsx is built around several key concepts:
Core CSS-in-JS functionality for writing scoped styles directly in JSX components. Perfect for component-level styling with full CSS support.
// JSX Style Tag (transformed by Babel)
<style jsx>{`/* CSS */`}</style>
<style jsx global>{`/* Global CSS */`}</style>Server-side rendering and style management system for handling style injection, deduplication, and extraction.
function StyleRegistry({
children,
registry
}: {
children: JSX.Element | import('react').ReactNode;
registry?: StyledJsxStyleRegistry;
}): JSX.Element;
function createStyleRegistry(): StyledJsxStyleRegistry;
function useStyleRegistry(): StyledJsxStyleRegistry;
interface StyledJsxStyleRegistry {
styles(options?: { nonce?: string }): JSX.Element[];
flush(): void;
add(props: any): void;
remove(props: any): void;
}CSS utilities for creating styles outside of JSX components, including scoped class generation and global style definition.
function css(chunks: TemplateStringsArray, ...args: any[]): JSX.Element;
namespace css {
function global(
chunks: TemplateStringsArray,
...args: any[]
): JSX.Element;
function resolve(
chunks: TemplateStringsArray,
...args: any[]
): { className: string; styles: JSX.Element };
}Babel macro functionality for using css.resolve without babel configuration, supporting the babel-plugin-macros ecosystem.
function resolve(
chunks: TemplateStringsArray,
...args: any[]
): {
className: string;
styles: JSX.Element;
}Babel plugin and webpack loader for transforming styled-jsx syntax and processing external CSS files.
// Babel plugin (styled-jsx/babel)
module.exports = function styledJsxBabelPlugin(options?: {
optimizeForSpeed?: boolean;
sourceMaps?: boolean;
styleModule?: string;
vendorPrefixes?: boolean;
plugins?: Array<any>;
}): any;
// Webpack loader (styled-jsx/webpack)
module.exports = {
loader: string; // Path to webpack loader
}interface StyledJsxStyleRegistry {
/** Get array of style elements for rendering */
styles(options?: { nonce?: string }): JSX.Element[];
/** Clear all styles from registry */
flush(): void;
/** Add style to registry */
add(props: any): void;
/** Remove style from registry */
remove(props: any): void;
}
// React module augmentation for jsx and global props
declare module 'react' {
interface StyleHTMLAttributes<T> extends HTMLAttributes<T> {
jsx?: boolean;
global?: boolean;
}
}