Unverified Commit 453adc68 authored by Arthur Petrie's avatar Arthur Petrie Committed by GitHub

[Examples] update with-tailwindcss-emotion to tailwind 2.0 (#19476)

Hello, this PR updates the with-tailwindcss-emotion example to be compatible with tailwindcss 2.0

Here is a summary of all the changes:
- update `.babelrc` config
- update `README.md`
- delete `@emotion/css component` (makes the example simpler)
- update `@emotion/react` component to use xwind
- update `@emotion/styled` component to use xwind
- Add global styles to `_app.js`
- remove `_document.js` page
- remove `base.css` global style files (global styles are added in _app.js)
- update `tailwind.config.js`
- update `package.json` dependencies + remove unnecessary `build:base-css` script
parent 14cb6556
{ {
"presets": ["next/babel"], "presets": [
"plugins": ["macros", "@emotion/babel-plugin"] [
"next/babel",
{
"preset-react": {
"runtime": "automatic",
"importSource": "@emotion/react"
}
}
]
],
"plugins": ["xwind/babel", "@emotion/babel-plugin"]
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
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/vercel/next.js/blob/canary/examples/with-tailwindcss/README.md). 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/vercel/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. `xwind` 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
...@@ -27,9 +27,7 @@ Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&ut ...@@ -27,9 +27,7 @@ Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&ut
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
const Header = styled.div` const Header = styled.div(xw`font-mono text-sm text-gray-800`)
${tw`font-mono text-sm text-gray-800`}
`
``` ```
Will be transformed into: Will be transformed into:
...@@ -43,7 +41,7 @@ Will be transformed into: ...@@ -43,7 +41,7 @@ Will be transformed into:
} }
``` ```
### tailwind CSS config ### Tailwind CSS config
Use the following command when you add a tailwind plugin that adds to tailwind's base css: Use the following command when you add a tailwind plugin that adds to tailwind's base css:
......
/*
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 */
Example with @emotion/react import xw, { cx } from 'xwind'
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" //"react native style"
const styles = { const styles = {
button: tw` button: xw`
relative relative
w-64 min-w-full w-64 min-w-full
flex justify-center flex justify-center
...@@ -26,24 +12,20 @@ const styles = { ...@@ -26,24 +12,20 @@ const styles = {
text-sm leading-5 font-medium text-sm leading-5 font-medium
rounded-md rounded-md
text-white text-white
bg-teal-600 bg-gray-600
hover:bg-teal-500 hover:bg-gray-500
focus[outline-none border-teal-700 shadow-outline-teal] focus[outline-none ring-4 ring-gray-400]
active:bg-teal-700 active:bg-gray-700
transition duration-150 ease-in-out transition duration-150 ease-in-out
`, `,
} }
const ButtonReact = ({ className, children, ...props }) => ( const ButtonReact = ({ className, children, ...props }) => (
<button <button {...props} css={styles.button} className={cx('group', className)}>
{...props} {/* inline style*/}
css={styles.button} <span css={xw`absolute left-0 inset-y-0 flex items-center pl-3`}>
className={['group', className].join(' ')}
>
{/* inline style */}
<span css={tw`absolute left-0 inset-y-0 flex items-center pl-3`}>
<svg <svg
css={tw`h-5 w-5 text-teal-500 group-hover:text-teal-400 transition ease-in-out duration-150`} css={xw`h-5 w-5 text-gray-500 group-hover:text-gray-400 transition ease-in-out duration-150`}
fill="currentColor" fill="currentColor"
viewBox="0 0 20 20" viewBox="0 0 20 20"
> >
......
/* /* Example with @emotion/styled */
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 styled from '@emotion/styled'
import tw from '@tailwindcssinjs/macro' import xw, { cx } from 'xwind'
const Button = styled.button(tw` const Button = styled.button(xw`
relative relative
w-64 min-w-full w-64 min-w-full
flex justify-center flex justify-center
...@@ -23,28 +11,28 @@ const Button = styled.button(tw` ...@@ -23,28 +11,28 @@ const Button = styled.button(tw`
text-sm leading-5 font-medium text-sm leading-5 font-medium
rounded-md rounded-md
text-white text-white
bg-indigo-600 bg-blue-600
hover:bg-indigo-500 hover:bg-blue-500
focus[outline-none border-indigo-700 shadow-outline-indigo] focus[outline-none ring-4 ring-blue-400]
active:bg-indigo-700 active:bg-blue-700
transition duration-150 ease-in-out transition duration-150 ease-in-out
`) `)
const IconWrapper = styled.span(tw` const IconWrapper = styled.span(xw`
absolute left-0 inset-y-0 absolute left-0 inset-y-0
flex items-center flex items-center
pl-3 pl-3
`) `)
const Icon = styled.svg(tw` const Icon = styled.svg(xw`
h-5 w-5 h-5 w-5
text-indigo-500 text-blue-500
group-hover:text-indigo-400 group-hover:text-blue-400
transition ease-in-out duration-150 transition ease-in-out duration-150
`) `)
const ButtonStyled = ({ className, children, ...props }) => ( const ButtonStyled = ({ className, children, ...props }) => (
<Button {...props} className={['group', className].join(' ')}> <Button {...props} className={cx('group', className)}>
<IconWrapper> <IconWrapper>
<Icon fill="currentColor" viewBox="0 0 20 20"> <Icon fill="currentColor" viewBox="0 0 20 20">
<path <path
......
...@@ -8,21 +8,19 @@ ...@@ -8,21 +8,19 @@
"build:base-css": "tailwindcss build ./styles/tailwind.base.css -o ./styles/base.css" "build:base-css": "tailwindcss build ./styles/tailwind.base.css -o ./styles/base.css"
}, },
"dependencies": { "dependencies": {
"@emotion/css": "^11.0.0-next.12", "@emotion/react": "^11.1.1",
"@emotion/react": "^11.0.0-next.12", "@emotion/styled": "11.0.0",
"@emotion/styled": "^11.0.0-next.12", "react": "17.0.1",
"next": "latest", "react-dom": "17.0.1"
"react": "16.13.1",
"react-dom": "16.13.1"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.9.0", "@babel/core": "^7.12.9",
"@emotion/babel-plugin": "^11.0.0-next.12", "@emotion/babel-plugin": "11.0.0",
"@emotion/server": "^11.0.0-next.12", "autoprefixer": "10.0.2",
"@tailwindcss/ui": "^0.2.2", "next": "latest",
"@tailwindcssinjs/macro": "^0.3.1", "postcss": "8.1.10",
"babel-plugin-macros": "2.8.0", "tailwindcss": "^2.0.1",
"tailwindcss": "^1.6.0" "xwind": "0.7.2"
}, },
"license": "MIT" "license": "MIT"
} }
import Head from 'next/head' import Head from 'next/head'
import { Global } from '@emotion/react'
import xw from 'xwind'
import '../styles/base.css' import '../styles/base.css'
export default function MyApp({ Component, pageProps }) { function App({ Component, pageProps }) {
return ( return (
<> <>
<Head> <Head>
<title>Tailwindcss Emotion Example</title> <title>Tailwindcss Emotion Example</title>
</Head> </Head>
<Global
//keyframes + ring and shadow classes variables ... to global styles
styles={xw`XWIND_GLOBAL`}
/>
<Component {...pageProps} /> <Component {...pageProps} />
</> </>
) )
} }
export default App
// _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 = await 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 { css } from '@emotion/css' import xw from 'xwind'
import tw from '@tailwindcssinjs/macro'
import ButtonCss from '../components/ButtonCss'
import ButtonReact from '../components/ButtonReact' import ButtonReact from '../components/ButtonReact'
import ButtonStyled from '../components/ButtonStyled' import ButtonStyled from '../components/ButtonStyled'
const Index = () => ( const Index = () => (
<div className={css(tw`grid justify-center items-center h-screen`)}> <div css={xw`grid justify-center items-center h-screen space-y-20`}>
<ButtonCss>@emotion/css</ButtonCss> <div css={xw`space-y-20`}>
<ButtonReact>@emotion/react</ButtonReact> <ButtonReact>@emotion/react</ButtonReact>
<ButtonStyled>@emotion/styled</ButtonStyled> <ButtonStyled>@emotion/styled</ButtonStyled>
</div> </div>
</div>
) )
export default Index export default Index
const defaultTheme = require('tailwindcss/defaultTheme') const colors = require('tailwindcss/colors')
module.exports = { module.exports = {
darkMode: 'class',
theme: { theme: {
extend: { extend: {
fontFamily: { colors,
sans: ['Inter var', ...defaultTheme.fontFamily.sans],
},
}, },
}, },
variants: {}, variants: {},
plugins: [require('@tailwindcss/ui')], plugins: [],
// xwind options
xwind: {
mode: 'objectstyles',
},
} }
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