Skip to content
Theme UI
GitHub

API

theme-ui

The core theme-ui package exports the following modules.

jsx

The jsx export is a React create element function intended for use with a custom pragma comment. It adds support for the sx prop, which uses Emotion's create element function internally and parses style objects with the Theme UI css utility.

Usage:

/** @jsx jsx */
import { jsx } from 'theme-ui'
export default (props) => (
<div
{...props}
sx={{
color: 'primary',
}}
/>
)

ThemeProvider

The ThemeProvider component is a wrapper around Emotion's ThemeProvider and MDX's MDXProvider. It provides default styled components to MDX content that pick up values from theme.styles. It also handles color mode state.

PropTypeDescription
themeObjectTheming context object
componentsObjectOptional custom MDX components
childrenNodeReact children

useThemeUI

The useThemeUI hook returns the full Theme UI context object, which includes theme, and color mode.

const context = useThemeUI()

Context

The React context used in Theme UI.

useColorMode

The useColorMode hook returns an array with two values: the current color mode state and an updater function. The color mode state can be any string and should match the name of a color mode defined in theme.colors.modes or be a name for the default color mode.

const [colorMode, setColorMode] = useColorMode()

Styled

The Styled export is an object of Emotion styled components that map to keys in the theme.styles object. Each component will pick up styles from its corresponding theme object. These are primarily meant to be used as a mechanism to use styles defined in a theme object outside of MDX.

// picks up styles from `theme.styles.h1`
<Styled.h1 />
PropTypeDescription
asString or ComponentChanges the underlying rendered element

BaseStyles

The BaseStyles component can be used to style child content that isn't generated from MDX.

<BaseStyles>
<h1>
Styled based on <code>theme.styles</code>
</h1>
</BaseStyles>

css

As an alternative to using the sx prop, the css utility is intended for use with the Emotion css prop. It will attempt to use values from the theme object for many common CSS properties, and will fall back to raw values when there is no corresponding theme value.

To see a list of supported CSS properties, see the sx prop docs.

css(styleObject)(theme)

Usage:

<div css={css(styles)} />
ArgumentTypeDescription
styleObjectObjecta theme-aware style object with support for responsive array values and Styled System prop shortcuts
themeObjectthe Emotion theming context object

get

An existential getter function used internally.

merge

A function to deeply merge themes.

Usage

import dark from '@theme-ui/preset-dark'
const theme = merge(dark, {
fontWeights: {
body: 500,
heading: 700,
bold: 900,
},
})

InitializeColorMode

Used internally by gatsby-plugin-theme-ui, this component can be rendered in HTML before the main application to help prevent the flash of colors that can occur during rehydration.

Usage

<InitializeColorMode />

components

An object of styled MDX components for use with MDXProvider.

Components

For component API docs, see Components

Edit the page on GitHub
Previous:
useThemeUI
Next:
Theme Specification