Giter VIP home page Giter VIP logo

gatsby-tailwind-demo's Introduction

Gatsby + Tailwind w/Emotion

This demo is a quick example of combining Tailwind CSS with Emotion in a Gatsby site.

Check the inspector to see loaded CSS in the demo.

Quickstart

# Clone the repo
git clone [email protected]:jlengstorf/gatsby-tailwind-demo.git
cd gatsby-tailwind-demo/

# Install dependencies
yarn

# Start the app in development mode
yarn develop

Make sure to use the right build command in your build settings e.g. if you use Netlify! Per default Netlify sets it to gatsby build but you have to use yarn run build to run postcss before building the Gatsby site.

Why does this exist?

CSS frameworks tend to generate large files, and it’s rare that a given page uses all of them. With CSS-in-JS solutions like Emotion, only the styles actually being used are attached to each component, which decreases the amount of style data sent for each page in a given app.

Thanks to @bradlc’s work on babel-plugin-tailwind-components, we can easily get the power of Tailwind without the stylesheet bloat by passing Tailwind classes into Emotion.

This repo contains two distinct approaches: styled components and the css() function.

Styled Components

Live demo

With styled components, we can add Tailwind classes directly to a React component like so:

/* global tw */
import React from 'react';
import styled from 'react-emotion';

const Heading = styled('h1')`
  ${tw`my-0 text-xl leading-tight`};
`;

export default () => <Heading>This is some text!</Heading>;

To see the CSS approach in this demo, start the app with yarn develop, then navigate to http://localhost:8000.

NOTE: The Babel plugin will automatically process the tw() function, which means we don’t actually need to import it. ESLint will complain about this, however, so we need to add tw as a global variable.

CSS Function

Live Demo

Using Emotion’s css function, we would do something like this:

/* global tw */
import React from 'react';
import { css } from 'emotion';

const heading = css(tw`my-0 text-xl leading-tight`);

export default () => <h1 className={heading}>This is some text!</h1>;

To see the CSS approach in this demo, start the app with yarn develop, then navigate to http://localhost:8000/css.

Both approaches produce the same output — ultimately it comes down to your preference.

Only the Styles We Need Are Included

If we inspect the page, we can see that only the styles for each given Tailwind class used by our component have been included. This means that we're able to use Tailwind without any concerns about stylesheet bloat over time.

gatsby-tailwind-demo's People

Contributors

jlengstorf avatar lekoarts avatar tompichaud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gatsby-tailwind-demo's Issues

does this still work?

I tried to import this to my project to use tailwind css and it doesnt work
is it still being maintained?

Simple README.md typo

I think under the styled components section you meant to say To see the **styled components** approach in this demo, start the app with yarn develop, then navigate to http://localhost:8000.

Both the styled components and the CSS components say To see the CSS approach...

BTW: Thanks for this repository!

Tailwind Fraction values are not being compiled correctly

Hi, I not sure if I'm missing something, but when I try to use the fraction tailwind classes like w-1/2, w-1/3, the compiled values are only for w-1

For example:
${tww-full md:w-1/2 bg-green} the rendered css is calculating the width to be 0.25rem instead of '50%'

I've also tried using an escape character like this:
${tww-full md:w-1/2 bg-green}

Broken install?

Freshly cloned the master, npm install throws error: No matching version found for gatsby-plugin-emotion@next

Custom CSS - Develop Failing / Build is Fine

Hello,

I ran into a strange issue, on development the custom css is not taking into account, so default tailwind css is being use.

On building / serve, the custom css is being use.

Is it Gatsby related ? or a config mistake ?

Update to twin.macro

Hey Jason!

I understand this repo is an absolute grandpa in developer years but I found it on Google and thought I could lend a hand with updating it to use my updated fork of tailwind.macro - twin.macro.

Let me know if you're keen and I'll submit a PR.

Cheers!

emotion 10 support?

When following readme to use your setup in my own gatsby 2 project, I get error:

Error: The package "react-emotion" has been replaced by "@emotion/styled" in version 10. See https://emotion.sh/docs/migrating-to-emotion-10 for more information on migrating.

When replacing import styled from 'react-emotion' with import styled from '@emotion/styled' I get eeror:

ReferenceError: tw is not defined

How to make it work with emotion 10?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.