Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the example of Tailwind CSS with emotion #8931

Merged
merged 7 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/with-tailwindcss-emotion/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": [["emotion"], ["macros"]]
}
65 changes: 65 additions & 0 deletions examples/with-tailwindcss-emotion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Tailwind CSS with Emotion.js example

This is an example of how you can add the tailwind CSS with Emotion.js in your web app.

## How to use

### Using `create-next-app`

Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:

```bash
npx create-next-app --example with-tailwindcss-emotion with-tailwindcss-emotion-app
# or
yarn create next-app --example with-tailwindcss-emotion with-tailwindcss-emotion-app
```

### Download manually

Download the example:

```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-tailwindcss-emotion
cd with-tailwindcss-emotion
```

Install it and run:

```bash
npm install
npm run dev
# or
yarn
yarn dev
```

Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)):

```bash
now
```

## The idea behind the example

This setup has inspiration from [examples/with-tailwindcss](https://github.com/zeit/next.js/blob/canary/examples/with-tailwindcss/README.md). This example will show you how to integrate [Emotion](https://emotion.sh/docs/introduction) with [tailwind](https://tailwindcss.com/).

`tailwindcss.macros` is used to add tailwind classes inside Emotion by injecting the tailwind CSS into the styled component. No need to use CSS files, autoprefix, minifier, etc. You will get the full benefits of Emotion.

The CSS classes generated by Emotion will include the tailwind styles but not the name of the classes. For example the following component:

```jsx
const Header = styled.div`
${tw`font-mono text-sm text-gray-800`}
`
```

Will be transformed into:

```css
.css-25og8s-Header {
font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New',
monospace;
font-size: 0.875rem;
color: #2d3748;
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
tailwind: {
styled: '@emotion/styled'
}
}
10 changes: 10 additions & 0 deletions examples/with-tailwindcss-emotion/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
webpack: (config, options) => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
}

return config
}
}
23 changes: 23 additions & 0 deletions examples/with-tailwindcss-emotion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "with-tailwindcss-emotion",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@emotion/core": "10.0.17",
"@emotion/styled": "10.0.17",
"next": "latest",
"react": "16.10.1",
"react-dom": "16.10.1"
},
"devDependencies": {
"babel-plugin-emotion": "10.0.19",
"babel-plugin-macros": "2.6.1",
"tailwind.macro": "1.0.0-alpha.10",
"tailwindcss": "1.1.2"
}
}
38 changes: 38 additions & 0 deletions examples/with-tailwindcss-emotion/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'
import { css } from '@emotion/core'
import styled from '@emotion/styled'
import tw from 'tailwind.macro'

/**
* We can use macros in `styled`.
*/
const Header = styled.div`
${tw`font-mono text-sm text-gray-800 hover:text-red-500`}
`

const Button = styled.button`
${tw`bg-blue-500 text-white font-mono px-4 py-2 rounded`}
:hover {
${tw`bg-blue-700`}
}
`

/**
* Also, we can use `css`.
*/
const CardStyle = css`
${tw`p-4 border-solid border border-gray-300 rounded p-4 shadow-xl`}
`

const Card = styled.div`
${CardStyle}
`

const Example = () => (
<Card>
<Header>Hello</Header>
<Button>Emotion.js</Button>
</Card>
)

export default Example
Loading