-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
635 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["next/babel"], | ||
"plugins": [["emotion"], ["macros"]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
5 changes: 5 additions & 0 deletions
5
examples/with-tailwindcss-emotion/babel-plugin-macros.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
tailwind: { | ||
styled: '@emotion/styled' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.