Skip to content

pauloelias/gatsby-tailwind-emotion-starter

Repository files navigation

Netlify Status

Gatsby Gatsby Gatsby

Tailwind CSS βž• Emotion Starter for Gatsby

Kick off your project with this bare-bones Tailwind CSS + Emotion starter for Gatsby. This starter ships with the packages and configuration files you need to get hit the ground running on your next Tailwind CSS project.

πŸ“ Notes

Andrew Welch wrote a terrific article about this setup over on his blog, "Using Tailwind CSS with Gatsby, React & Emotion Styled Components". The only difference between this starter and Andrew's writeup is I have not included stylelint as of this release.

If you prefer to use a Gatsby Theme instead of a Gatsby Starter, Jordi Talens has an excellent theme that you can layer on with other Gatsby themes.

πŸ’„ Demo

You can see a demo of the frontend over yonder.

πŸš€ Quick start

  1. Create a Gatsby site.

    Use the Gatsby CLI to create a new site, specifying the gatsby-tailwind-emotion-starter starter.

    # create a new Gatsby site using the gatsby-tailwind-emotion-starter
    gatsby new my-gatsby-tailwind-emotion-starter https://github.com/pauloelias/gatsby-tailwind-emotion-starter
  2. Start developing.

    Navigate into your new site’s directory and start it up.

    cd my-gatsby-tailwind-emotion-starter/
    gatsby develop
  3. Open the source code and start editing!

    Your site is now running at http://localhost:8000!

    Open the my-gatsby-tailwind-emotion-starter directory in your code editor of choice and edit src/pages/index.js. Save your changes and the browser will update in real time!

✨ Features?

This starter contains has the following features enabled by default:

  • Tailwind CSS: The full power of Tailwind is at your fingertips. Style your components using twin.macro to add Tailwind classes to your project.
  • Emotion: Best-in-class CSS-in-JS support with Emotion. Write your own custom styled components with Emotion or use twin.macro inside your styled components to add Tailwind CSS classes alongside your custom styling.
  • PostCSS: Use the flexibility of PostCSS to extend Tailwind's CSS or write your own CSS. Postcss-Preset-Env is enabled out-of-the box allowing you to write tomorrow's CSS today!

πŸ“¦ Example components

To use Tailwind CSS classes inside of your components you use the twin.macro package. You can also create richer styled components using a combination of both Tailwind's classes and your own custom CSS with Emotion. Laslty, if needed, you can use PostCSS to write your own custom CSS as well.

Standalone Tailwind Classes

import tw from "twin.macro"
import React from "react"

const Heading = tw.h1`
  text-2xl text-gray-500 uppercase
`

export default () => (
  <div>
    <Heading>Hello, world!</Heading>
  </div>
)

Styled Components with Emotion

import tw, { styled } from "twin.macro"
import React from "react"

import pattern from "../images/pattern.png"

const Container = styled.div`
    ${tw`bg-gray-100 w-full`}
    background-image: url(${background});
    padding: 15px;
`

export default () => (
  <Container>
    <h1>Hello, world!</h1>
  </Container>
)

Combined Standalone + Styled Components Example

import tw, { styled } from "twin.macro"
import React from "react"

import pattern from "../images/pattern.png"

const Container = styled.div`
    ${tw`bg-gray-100 w-full`}
    background-image: url(${background});
    padding: 15px;
`

const Heading = tw.h1`
  text-2xl text-gray-500 uppercase
`

export default () => (
  <Container>
    <Heading>Hello, world!</Heading>
  </Container>
)

CSS Prop to inline Tailwind Classes

import tw, { css } from "twin.macro"
import React from "react"

export default () => (
  <div
    css={css`
      ${tw`flex items-center justify-between px-4 py-3`}
    `}
  >
    <h1>Hello, world!</h1>
    <h2>I'm a flex item too!</h2>
  </div>
)

🧐 What's inside?

A quick look at the top-level files and directories you'll see in a Gatsby project.

.
β”œβ”€β”€ src
β”œβ”€β”€ .babelrc
β”œβ”€β”€ .eslintignore
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .nvmrc
β”œβ”€β”€ .prettierignore
β”œβ”€β”€ .prettierrc
β”œβ”€β”€ .stylelint.config.js
β”œβ”€β”€ babel-plugin-macros.config.js
β”œβ”€β”€ gatsby-browser.js
β”œβ”€β”€ gatsby-config.js
β”œβ”€β”€ gatsby-node.js
β”œβ”€β”€ gatsby-ssr.js
β”œβ”€β”€ LICENSE
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ package.json
β”œβ”€β”€ postcss.config.js
β”œβ”€β”€ README.md
└── tailwind.config.js
  1. /src: This directory will contain all of the code related to what you will see on the front-end of your site (what you see in the browser) such as your site header or a page template. src is a convention for β€œsource code”.

  2. .babelrc: This configuration file allows us to fine-tune Babel's configuration settings. In this starter we are adding the babel-preset-gatsby preset to allow us to customize Babel as needed.

  3. .eslintignore: This file specifies files and folders that we want to exclude from linting with Eslint.

  4. .gitignore: This file tells git which files it should not track / not maintain a version history for.

  5. .nvmrc: The .nvmrc allows you to lock down a project's node version when using nvm.

  6. .prettierignore: This file allows us to specifiy files that we want to exclude from formatting with Prettier.

  7. .prettierrc: This is a configuration file for Prettier. Prettier is a tool to help keep the formatting of your code consistent.

  8. .stylelint.config.js: Stylelint configuration file to customize stylelint rules in the project. Styleline is enabled to tame CSS errors displayed in some editors due to TailwindCSS's syntaxt inside CSS files.

  9. babel-plugin-macros.config.js: This file helps us configure Tailwind CSS macros to be used with Emotion, our CSS-in-JS tool of choice.

  10. gatsby-browser.js: This file is where Gatsby expects to find any usage of the Gatsby browser APIs (if any). These allow customization/extension of default Gatsby settings affecting the browser. By default we are injecting Tailwind's base styles into the browser.

  11. gatsby-config.js: This is the main configuration file for a Gatsby site. This is where you can specify information about your site (metadata) like the site title and description, which Gatsby plugins you’d like to include, etc. (Check out the config docs for more detail).

  12. gatsby-node.js: This file is where Gatsby expects to find any usage of the Gatsby Node APIs (if any). These allow customization/extension of default Gatsby settings affecting pieces of the site build process.

  13. gatsby-ssr.js: This file is where Gatsby expects to find any usage of the Gatsby server-side rendering APIs (if any). These allow customization of default Gatsby settings affecting server-side rendering.

  14. LICENSE: Gatsby is licensed under the MIT license.

  15. package-lock.json: (See package.json below, first). This is an automatically generated file based on the exact versions of your npm dependencies that were installed for your project. (You won’t change this file directly).

  16. package.json: A manifest file for Node.js projects, which includes things like metadata (the project’s name, author, etc). This manifest is how npm knows which packages to install for your project.

  17. postcss.config.js: This configuration file allows us to customize our PostCSS settings. PostCSS is used to compile the custom css we write outside of Emotion.

  18. README.md: A text file containing useful reference information about your project.

  19. tailwind.config.js: This is the default Tailwind CSS configuration file.

πŸ’« Deploy

Deploy to Netlify

πŸ“š Learn More About Gatsby

To learn more about Gatsby, use the following resources:

You can check out the Gatsby GitHub repository - your feedback and contributions are welcome!