Skip to main content
The gatsby-plugin-theme-ui provides seamless Theme UI integration with Gatsby, including automatic color mode support and component shadowing.

Installation

Install Theme UI and the Gatsby plugin:

Basic Setup

Add the plugin to your gatsby-config.js:
That’s it! The plugin automatically:
  • Wraps your app with ThemeUIProvider
  • Integrates MDX support
  • Prevents color mode flash on page load
  • Sets up the theme context

Creating Your Theme

Create a theme file using Gatsby’s component shadowing:
The theme must be the default export from src/gatsby-plugin-theme-ui/index.js.

Plugin Options

Configure the plugin with options:

Available Options

string
default:"null"
The name of the preset for styling code blocks in markdown files. Available presets: 'dracula', 'duotone-dark', 'duotone-light', 'github', 'night-owl', 'nightOwl', 'oceanicNext', 'okaidia', 'prism', 'shadesOfPurple', 'ultramin', 'vsDark', 'vsLight'.
string | object
default:"null"
A theme preset to use as a base. Can be a package name (string) or a theme object. The preset is merged with your shadowed theme.
boolean
default:"true"
Injects a script to prevent color mode flashing on page load. Set to false for AMP pages.

Using Presets

Install a Preset Package

Configure with Preset

Extend a Preset

Combine a preset with custom styles:

Code Syntax Highlighting

The plugin integrates with @theme-ui/prism for syntax highlighting.

Configure Prism Preset

Available Prism Presets

  • 'dracula'
  • 'night-owl' or 'nightOwl'
  • 'okaidia'
  • 'duotone-dark'
  • 'vsDark'
  • 'shadesOfPurple'

Custom Prism Styles

Override Prism styles in your theme:

Color Modes

Add color modes to your theme:

Color Mode Toggle

Create a color mode toggle component:

Prevent Color Flash

The plugin automatically injects a script to prevent color mode flashing. This script runs before React hydrates and sets the correct color mode. To disable this (e.g., for AMP pages):

Custom MDX Components

Shadow the components file to customize MDX rendering:

Component Shadowing

Gatsby’s component shadowing allows you to override plugin files.

Shadow Files

You can shadow these files:

Example: Complete Override

Extending Gatsby Themes

Extend a Gatsby theme that uses Theme UI:

Load Theme from Custom Path

If you prefer a different location for your theme:
gatsby-config.js doesn’t support ES6 modules. Use module.exports instead of export default.

Plugin Implementation

Here’s how the plugin works internally:

gatsby-node.js

The plugin processes theme presets and creates a Gatsby node:

Provider Component

The plugin wraps your app with providers:

Complete Example

Here’s a complete Gatsby site setup:

Troubleshooting

Theme Not Applying

Make sure your theme file exports a default:

Color Flash on Load

Ensure injectColorFlashScript is true (default):

Preset Not Loading

Verify the preset package is installed:

Resources

Gatsby Shadowing

Learn about component shadowing in Gatsby

Theme UI Presets

Browse available Theme UI presets

MDX in Gatsby

Using MDX with Gatsby

Prism Themes

Code syntax highlighting themes