jsx function is Theme UI’s custom JSX pragma that enables the sx prop on all elements. It wraps Emotion’s JSX function and processes the sx and css props.
Import
Signature
The component type or HTML element name (e.g., ‘div’, ‘button’, or a React component)
Props object that may include
sx and css propsChild elements to render
How It Works
Thejsx function performs the following steps:
- Receives JSX elements from the compiler
- Parses the
sxprop using the Theme UI CSS function - Transforms the
sxprop into an Emotioncssprop - Passes the result to Emotion’s JSX function for rendering
sx prop on any element:
Usage
Classic Runtime (Manual Pragma)
For the classic JSX runtime, add the pragma comment at the top of your file:Automatic Runtime (Recommended)
Configure your build tool to use Theme UI’s JSX runtime automatically: TypeScript (tsconfig.json):
.babelrc):
Direct Usage
You can also call thejsx function directly:
Props Processing
Thejsx function processes props through parseProps, which:
- Leaves props untouched if no
sxorcssprop is present - Removes the
sxprop from the final props - Converts
sxto Emotion’scssprop using Theme UI’s CSS function - Preserves the existing
cssprop if present - Both
sxandcsscan be used together:
Internal Implementation
Frompackages/core/src/index.ts:39-43:
parseProps function (packages/core/src/parseProps.tsx:9-21):
TypeScript Support
Thejsx function includes full TypeScript definitions for JSX intrinsic elements and the sx prop.
The JSX namespace is augmented to include:
ElementElementClassIntrinsicElements(withsxprop)IntrinsicAttributes
Examples from Tests
Basic sx Prop
Raw CSS Prop
CSS Prop with Function
Combined sx and css
Related
- sx prop - Using the sx prop in components
- ThemeUIProvider - Setting up the theme context
- CSS Function - The underlying CSS transformation
