Official Plugin
View plugin on GitHub
See starters using this

gatsby-plugin-emotion

Provide support for using the css-in-js library Emotion including server side rendering.

This plugin supports Emotion v10+

Older versions should use versions of this plugin which support Emotion 8 and 9. Check out the Emotion 10 migration guide for more information on how to upgrade.

Install

npm install gatsby-plugin-emotion @emotion/core @emotion/styled

How to use

Add the plugin to your gatsby-config.js.

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-emotion`,
      options: {
        // Accepts the following options, all of which are defined by `babel-plugin-emotion` plugin.
        // The values for each key in this example are the defaults the plugin uses.
        sourceMap: true,
        autoLabel: process.env.NODE_ENV !== "production",
        labelFormat: `[local]`,
        cssPropOptimization: true,
      },
    },
  ],
}

Options

The plugin supports the same options that you can pass into babel-plugin-emotion.

Option Type Description Default Required
sourceMap boolean Tells the plugin to inject source maps for use in browser dev tools in development. true
autoLabel boolean Automatically adds the label property to styles so that class names generated by css or styled include the name of the variable the result is assigned to. You can read more about this option in babel-plugin-emotion’s docs process.env.NODE_ENV !== 'production'
labelFormat string Only works when autoLabel is set to true. It allows you to define the format of the resulting label. The format is defined via string where variable parts are enclosed in square brackets []. For example labelFormat: "my-classname--[local]", where [local] will be replaced with the name of the variable the result is assigned to. ”[local]”
cssPropOptimization boolean Assumes that you are using something to make @emotion/core’s jsx function work for all jsx. If you are not doing so and you do not want such optimizations to occur, disable this option. true