Unverified Commit e97cf15d authored by Arthur Petrie's avatar Arthur Petrie Committed by GitHub

[Example] update with-tailwindcss-emotion to tailwind 1.3.3 and emotion 11 +…

[Example] update with-tailwindcss-emotion to tailwind 1.3.3 and emotion 11 + tailwind-ui plugin (#11611)

* [Example] update to tailwind 1.2.0 and emotion 11

* fix lint error

* Used different ways to style components

* update @tailwindcssinjs/macro package
remove clear cache script
update component comments

* update tailwindcss package to 1.3.3

* update dependencies

* update dependencies

* Updated readme, package.json, and removed unrequired imports
Co-authored-by: 's avatarLuis Alvarez <luis@zeit.co>
parent 2a3e0772
{ {
"presets": ["next/babel"], "presets": [
"plugins": [["emotion"], ["macros"]] "next/babel"
],
"plugins": [
"macros",
"@emotion/babel-plugin"
]
} }
\ No newline at end of file
# Tailwind CSS with Emotion.js example # 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. This is an example of how you can add [tailwind CSS](https://tailwindcss.com/) with [Emotion.js](https://emotion.sh/docs/introduction) in your web app. It takes inspiration from [examples/with-tailwindcss](https://github.com/zeit/next.js/blob/canary/examples/with-tailwindcss/README.md).
`@tailwindcssinjs/macro` 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.
## Deploy your own ## Deploy your own
...@@ -43,10 +45,6 @@ Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&ut ...@@ -43,10 +45,6 @@ Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&ut
## Notes ## Notes
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: The CSS classes generated by Emotion will include the tailwind styles but not the name of the classes. For example the following component:
```jsx ```jsx
...@@ -65,3 +63,13 @@ Will be transformed into: ...@@ -65,3 +63,13 @@ Will be transformed into:
color: #2d3748; color: #2d3748;
} }
``` ```
### tailwind CSS config
Use the following command when you add a tailwind plugin that adds to tailwind's base css:
```bash
npm run build:base-css
# or
yarn run build:base-css
```
module.exports = {
tailwind: {
styled: '@emotion/styled',
},
}
/*
Example with @emotion/css
Note: configure "extractCritical" from "@emotion/server" in _document.js.
Required packages for this component:
"@emotion/css"
"@emotion/babel-plugin"
"@emotion/server"
These packages can be removed if you plan on only using @emotion/css API:
"@emotion/react"
"@emotion/styled"
*/
import { css, cx } from '@emotion/css'
import tw from '@tailwindcssinjs/macro'
//"react native style"
const styles = {
button: css(tw`
relative
w-64 min-w-full
flex justify-center
py-2 px-4
border border-transparent
text-sm leading-5 font-medium
rounded-md
text-white
bg-gray-600
hover:bg-gray-500
focus[outline-none border-gray-700 shadow-outline-gray]
active:bg-gray-700
transition duration-150 ease-in-out
`),
}
const ButtonCss = ({ className, children, ...props }) => (
<button {...props} className={cx(styles.button, 'group', className)}>
{/* inline style */}
<span className={css(tw`absolute left-0 inset-y-0 flex items-center pl-3`)}>
<svg
className={css(
tw`h-5 w-5 text-gray-500 group-hover:text-gray-400 transition ease-in-out duration-150`
)}
fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z"
clipRule="evenodd"
/>
</svg>
</span>
{children}
</button>
)
export default ButtonCss
/*
Example with @emotion/react
Required packages for this component:
"@emotion/react"
"@emotion/babel-plugin"
These packages can be removed if you plan on only using @emotion/react API:
"@emotion/css"
"@emotion/styled"
"@emotion/server"
*/
/** @jsx jsx */
import { jsx } from '@emotion/react'
import tw from '@tailwindcssinjs/macro'
//"react native style"
const styles = {
button: tw`
relative
w-64 min-w-full
flex justify-center
py-2 px-4
border border-transparent
text-sm leading-5 font-medium
rounded-md
text-white
bg-teal-600
hover:bg-teal-500
focus[outline-none border-teal-700 shadow-outline-teal]
active:bg-teal-700
transition duration-150 ease-in-out
`,
}
const ButtonReact = ({ className, children, ...props }) => (
<button
{...props}
css={styles.button}
className={['group', className].join(' ')}
>
{/* inline style */}
<span css={tw`absolute left-0 inset-y-0 flex items-center pl-3`}>
<svg
css={tw`h-5 w-5 text-teal-500 group-hover:text-teal-400 transition ease-in-out duration-150`}
fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z"
clipRule="evenodd"
/>
</svg>
</span>
{children}
</button>
)
export default ButtonReact
/*
Example with @emotion/styled
Required packages for this component:
"@emotion/react"
"@emotion/styled"
"@emotion/babel-plugin"
These packages can be removed if you plan on only using @emotion/styled API:
"@emotion/css"
"@emotion/server"
*/
import styled from '@emotion/styled'
import tw from '@tailwindcssinjs/macro'
const Button = styled.button(tw`
relative
w-64 min-w-full
flex justify-center
py-2 px-4
border border-transparent
text-sm leading-5 font-medium
rounded-md
text-white
bg-indigo-600
hover:bg-indigo-500
focus[outline-none border-indigo-700 shadow-outline-indigo]
active:bg-indigo-700
transition duration-150 ease-in-out
`)
const IconWrapper = styled.span(tw`
absolute left-0 inset-y-0
flex items-center
pl-3
`)
const Icon = styled.svg(tw`
h-5 w-5
text-indigo-500
group-hover:text-indigo-400
transition ease-in-out duration-150
`)
const ButtonStyled = ({ className, children, ...props }) => (
<Button {...props} className={['group', className].join(' ')}>
<IconWrapper>
<Icon fill="currentColor" viewBox="0 0 20 20">
<path
fillRule="evenodd"
d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z"
clipRule="evenodd"
/>
</Icon>
</IconWrapper>
{children}
</Button>
)
export default ButtonStyled
module.exports = {
webpack: (config, options) => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty',
}
return config
},
}
{ {
"name": "with-tailwindcss-emotion", "name": "with-tailwindcss-emotion",
"version": "0.1.0", "version": "1.0.0",
"private": true,
"scripts": { "scripts": {
"dev": "next dev", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start" "start": "next start",
"build:base-css": "tailwindcss build ./styles/tailwind.base.css -o ./styles/base.css"
}, },
"dependencies": { "dependencies": {
"@emotion/core": "10.0.17", "@emotion/css": "^11.0.0-next.12",
"@emotion/styled": "10.0.17", "@emotion/react": "^11.0.0-next.12",
"@emotion/styled": "^11.0.0-next.12",
"next": "latest", "next": "latest",
"react": "16.10.1", "react": "16.13.1",
"react-dom": "16.10.1" "react-dom": "16.13.1"
}, },
"devDependencies": { "devDependencies": {
"babel-plugin-emotion": "10.0.19", "@babel/core": "^7.9.0",
"babel-plugin-macros": "2.6.1", "@emotion/babel-plugin": "^11.0.0-next.12",
"tailwind.macro": "1.0.0-alpha.10", "@emotion/server": "^11.0.0-next.12",
"tailwindcss": "1.1.2" "@tailwindcss/ui": "^0.2.2",
"@tailwindcssinjs/macro": "^0.3.1",
"babel-plugin-macros": "2.8.0",
"tailwindcss": "1.3.5"
} }
} }
import Head from 'next/head'
import '../styles/base.css'
export default function MyApp({ Component, pageProps }) {
return (
<>
<Head>
<title>Tailwindcss Emotion Example</title>
</Head>
<Component {...pageProps} />
</>
)
}
// _document is only rendered on the server side and not on the client side
// Event handlers like onClick can't be added to this file
// ./pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
// Required for @emotion/css
import { extractCritical } from '@emotion/server'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
const page = ctx.renderPage()
const styles = extractCritical(page.html)
return { ...initialProps, ...page, ...styles }
}
render() {
return (
<Html lang="en">
<Head>
<style
data-emotion-css={this.props.ids.join(' ')}
dangerouslySetInnerHTML={{ __html: this.props.css }}
/>
<link rel="stylesheet" href="https://rsms.me/inter/inter.css" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
import React from 'react' import { css } from '@emotion/css'
import { css } from '@emotion/core' import tw from '@tailwindcssinjs/macro'
import styled from '@emotion/styled' import ButtonCss from '../components/ButtonCss'
import tw from 'tailwind.macro' import ButtonReact from '../components/ButtonReact'
import ButtonStyled from '../components/ButtonStyled'
/**
* We can use macros in `styled`. const Index = () => (
*/ <div className={css(tw`grid justify-center items-center h-screen`)}>
const Header = styled.div` <ButtonCss>@emotion/css</ButtonCss>
${tw`font-mono text-sm text-gray-800 hover:text-red-500`} <ButtonReact>@emotion/react</ButtonReact>
transition: .150s ease-in-out; <ButtonStyled>@emotion/styled</ButtonStyled>
` </div>
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 export default Index
This diff is collapsed.
const defaultTheme = require('tailwindcss/defaultTheme')
module.exports = {
theme: {
extend: {
fontFamily: {
sans: ['Inter var', ...defaultTheme.fontFamily.sans],
},
},
},
variants: {},
plugins: [require('@tailwindcss/ui')],
}
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