sx prop on all HTML elements. This guide covers how to configure your project to use the sx prop.
JSX Automatic Runtime (Recommended)
Theme UI supports React’s automatic JSX runtime, which is the recommended approach for modern React applications. With automatic runtime, you don’t need to import React in every file.Configuration
Add thejsxImportSource comment at the top of your file:
The
@jsxImportSource pragma must be at the very top of your file, before any imports.TypeScript Configuration
For TypeScript projects, update yourtsconfig.json:
@jsxImportSource comment.
Next.js Example
How It Works
Theme UI provides its own JSX runtime that wraps Emotion’s JSX functions. When you use the@jsxImportSource pragma, your JSX is transformed using Theme UI’s custom runtime.
Implementation Details
The Theme UI JSX runtime processes thesx prop before passing elements to Emotion:
parseProps function converts the sx prop into Emotion’s css prop:
Using @theme-ui/core
For minimal bundle size, you can use@theme-ui/core which provides the essential JSX runtime without additional components:
@theme-ui/core doesn’t add global or root styles. Use the full theme-ui package if you need those features.Classic JSX Runtime (Legacy)
If you’re using an older React version (< 17), you can use the classic JSX pragma:Troubleshooting
TypeScript Errors
If you’re getting TypeScript errors with thesx prop, make sure you’re using TypeScript 5.1.2 or newer:
Theme UI v0.16+ requires TypeScript 5.1.2+ and
@types/react published after June 1, 2023.Mixing Runtimes
You can mix both the automatic runtime and explicitcss prop usage:
CSS Prop vs SX Prop
Thesx prop is transformed into the css prop at build time. You can use both:
sx- Theme-aware, has access to theme valuescss- Raw Emotion styles, useful for complex CSS
Migration Guide
Moving from the classic runtime to automatic runtime:- Change
@jsx jsxto@jsxImportSource theme-ui - Remove the
import { jsx } from 'theme-ui'statement - No other code changes needed!
