Theme UI

0.17.4 · active · verified Sun Apr 19

Theme UI is a robust library for constructing themeable user interfaces, leveraging constraint-based design principles to build cohesive design systems, component libraries, and web applications. It serves as the spiritual successor and next evolution of Styled System, offering a flexible API designed for developer ergonomics. The current stable version is 0.17.4. While specific release cadences for major versions aren't fixed, the project sees active development with frequent minor and develop releases, indicating an ongoing commitment to bug fixes and incremental improvements on the 0.x branch. Key differentiators include its powerful `sx` prop for direct, theme-based styling, broad compatibility with virtually any UI component library, built-in support for dark modes, primitive page layout components, and deep integration with popular tools like Gatsby and MDX for content styling. It is built on Emotion for efficient scoped styles and adheres to a standard Theme Specification, promoting interoperability and a consistent design language across projects, making it ideal for large-scale design system implementation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up a `ThemeProvider` with a custom theme and using basic components like `Box` and `Text` with the `sx` prop for styling, including responsive styles and dark mode support via theme configuration. This code is runnable in a React environment.

import React from 'react';
import { ThemeProvider, Box, Text } from 'theme-ui';

const customTheme = {
  colors: {
    text: '#000',
    background: '#fff',
    primary: '#07c',
    secondary: '#05a',
    modes: {
      dark: {
        text: '#fff',
        background: '#000',
        primary: '#0cf',
        secondary: '#09f',
      },
    },
  },
  fonts: {
    body: 'system-ui, sans-serif',
    heading: 'Georgia, serif',
  },
  text: {
    heading: {
      fontFamily: 'heading',
      lineHeight: 'heading',
      fontWeight: 'heading',
      fontSize: [4, 5, 6],
    },
  },
  buttons: {
    primary: {
      color: 'background',
      bg: 'primary',
      '&:hover': {
        bg: 'secondary',
      },
    },
  },
  styles: {
    root: {
      fontFamily: 'body',
      color: 'text',
      bg: 'background',
    },
  },
};

const App = () => (
  <ThemeProvider theme={customTheme}>
    <Box sx={{
      maxWidth: 960,
      mx: 'auto',
      px: [3, 4],
      py: 4,
      bg: 'background',
      color: 'text',
    }}>
      <Text as="h1" sx={{ variant: 'text.heading', color: 'primary' }}>
        Welcome to Theme UI!
      </Text>
      <Text sx={{ fontSize: [2, 3] }}>
        This is an example of a themed application using Theme UI's `sx` prop and `ThemeProvider`.
        The styles are derived from the `customTheme` object, enabling responsive and consistent design.
      </Text>
      <Box as="button" sx={{ variant: 'buttons.primary', mt: 4, p: 3, borderRadius: 4 }}>
        Click Me
      </Box>
    </Box>
  </ThemeProvider>
);

export default App;

view raw JSON →