Commit ff5dcacc authored by First Kanisorn Sutham's avatar First Kanisorn Sutham Committed by Luis Alvarez D

Add the example of Tailwind CSS with emotion (#8931)

* Add the example of tailwindcss with emotion

* Add README.md

* Fix Unexpected trailing comma.

* Update the readme, and use latest Next.js version
parent 4374e7c5
{
"presets": ["next/babel"],
"plugins": [["emotion"], ["macros"]]
}
# 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;
}
```
module.exports = {
tailwind: {
styled: '@emotion/styled'
}
}
module.exports = {
webpack: (config, options) => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
}
return config
}
}
{
"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"
}
}
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
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment