Gatsby is a static site generator in React. It uses React components, then serves static using GrapQL. Themes were recently introducted in Gatsby. Let us understand themes in Gatsby.

Why are Gatsby themes useful?

When you build your site in Gatsby, the content of the site and the layout/theme of the site are all in one place. You cannot reuse your layout and theme in another Gatsby site directly. Also, if you are using some Gatsby starter application, and built your site on it, then it is difficult to update the Gatsby starter application to newer version.

So Gatsby themes are kind of plugins in Gatsby, which allow you to separate out the look and feel of Gatsby site and the content. This allows you to develop your theme independently, across many Gatsby sites.

In order to show the benefit of Gatsby site truely, we are going to download two starter applications in Gatsby. One will be the Gatsby starter application, and another will be Gatsby theme starter application. Both of these application do the same thing, except that in the Gatsby theme starter application, the look and feel of the application is separated out.

How do you create a Gatsby theme?

There is nothing special about creating a Gatsby theme. You can just initialize an npm package, add Gatsby as dependency, and you have your theme ready. Its just wont do anything. There are no special steps to create a Gatsby theme.

Normally, you would want to test your theme before pubishing to npm repository. It would be helpful to create a setup, which you enable you to test the theme and develop the theme parallely.

So you would find many tutorials, and even Gatsby theme workspace starter, which use yarn workspaces. Yarn workspaces allow you to add a local dependency, and keep it synched. You will create two workspaces, one workspace to test your theme (workspace1), and another workspace(workspace2) to develop your theme. The workspace2 is added as dependency to workspace1.

Is Yarn workspace really required to create Gatsby theme?

No, yarn workspaces are not required. You can create your theme without it, although the process will be difficult and tiresome.

What are the steps to create Gatsby theme?

Simplest step is to clone the gatsby theme workspace starter. This starter will have the yarn workspace initialized.

gatsby new my-theme https://github.com/gatsbyjs/gatsby-starter-theme-workspace

How do you publish Gatsby theme?

Gatsby themes are created as npm packages. These npm packages can be published to npm repository.

How do you consume an Gatsby theme?

Add the Gatsby theme npm package as dependency to your Gatsby project. Then you need to add it in the plugin array in the gatsby-config.js file. In the below code, the gatsby-theme-minimal is the theme name.

gatsby-config.js file

module.exports = {
  plugins: [{ 
      resolve: `gatsby-theme-minimal`, 
      options: {} 
  }],
}

package.json file

"dependencies": {
    "gatsby": "^2.13.45",
    "gatsby-theme-minimal": "^1.0.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6"
  }