Commit fc335ac3 authored by Lucas Rosa's avatar Lucas Rosa Committed by Tim Neutkens

Add example with tailwind css (#3317)

* Add Tailwind css example

* Fix read me

* Add create-next-app setup to read me
parent f0eacf66
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-tailwindcss)
# Tailwind CSS example
This is an example of how you can include a global stylesheet in a next.js webapp.
## How to use
If you like [create-next-app](https://github.com/segmentio/create-next-app) and/or [yarn](https://yarnpkg.com/en/docs/cli/create) simply run:
```bash
yarn create next-app --example with-tailwindcss my-app
cd my-app
```
*Otherwise:*
Download the example [or clone the repo](https://github.com/zeit/next.js):
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-tailwindcss
cd with-tailwindcss
```
**Running**
To get this example running you just need to
yarn install .
yarn dev
Visit [http://localhost:3000](http://localhost:3000) and try to modify `styles/index.css`.
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
### Extras
In the `package.json` you'll see some extra commands.
* `yarn dev:css`
* used by `yarn dev` generate css bundle and watch css files for changes
* includes css imported into `index.css`
* will **not** autoreload browser when css changes
* `yarn build:css`
* used by `yarn build` to generate css bundle
These can be used manually but using the usual commands will run them anyways.
## The idea behind the example
This setup is a basic starting point for using tailwind css and next. Along with tailwind, this example
also uses some other postcss plugins for imports, autoprefixing, and stripping whitespace/comments. The imports simply
allow for an easy way to split up css files but still get one bundled css file in `static/css/bundle.css`.
Changing stylesheets does not trigger auto-reloads. Setting up auto-reloads was avoided
because the next.js read me does not recommend doing so. Although, that can easily be done with
some webpack loaders. If you are curious about using loaders with next look at this
[example](https://github.com/zeit/next.js/tree/canary/examples/with-global-stylesheet).
This project shows how you can set it up. Have a look at:
* pages/_document.js
* styles/config/postcss.config.js
* styles/config/tailwind.config.js
* styles/index.css
* styles/button.css
import NextHead from 'next/head'
import { string } from 'prop-types'
const defaultDescription = ''
const defaultOGURL = ''
const defaultOGImage = ''
const Head = (props) => (
<NextHead>
<meta charset="UTF-8" />
<title>{props.title || ''}</title>
<meta name="description" content={props.description || defaultDescription} />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" sizes="192x192" href="/static/touch-icon.png" />
<link rel="apple-touch-icon" href="/static/touch-icon.png" />
<link rel="mask-icon" href="/static/favicon-mask.svg" color="#49B882" />
<link rel="icon" href="/static/favicon.ico" />
<meta property="og:url" content={props.url || defaultOGURL} />
<meta property="og:title" content={props.title || ''} />
<meta property="og:description" content={props.description || defaultDescription} />
<meta name="twitter:site" content={props.url || defaultOGURL} />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content={props.ogImage || defaultOGImage} />
<meta property="og:image" content={props.ogImage || defaultOGImage} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
</NextHead>
)
Head.propTypes = {
title: string,
description: string,
url: string,
ogImage: string
}
export default Head
import Head from './head'
import Link from 'next/link'
const links = [
{ href: 'https://github.com/segmentio/create-next-app', label: 'Github' }
].map(link => {
link.key = `nav-link-${link.href}-${link.label}`
return link
})
const Nav = () => (
<nav>
<ul>
<li>
<Link prefetch href="/">
<a>Home</a>
</Link>
</li>
<ul>
{links.map(
({ key, href, label }) => (
<li key={key}>
<Link href={href}>
<a className="btn-blue">{label}</a>
</Link>
</li>
)
)}
</ul>
</ul>
<style jsx>{`
:global(body) {
margin: 0;
font-family: -apple-system,BlinkMacSystemFont,Avenir Next,Avenir,Helvetica,sans-serif;
}
nav {
text-align: center;
}
ul {
display: flex;
justify-content: space-between;
}
nav > ul {
padding: 4px 16px;
}
li {
display: flex;
padding: 6px 8px;
}
a {
color: #067df7;
text-decoration: none;
font-size: 13px;
}
`}</style>
</nav>
)
export default Nav
{
"name": "with-tailwindcss",
"version": "1.0.0",
"scripts": {
"dev": "next & yarn dev:css",
"dev:css": "postcss styles/index.css --watch -c styles/config/postcss.config.js -o static/css/bundle.css",
"build": "next build & yarn build:css",
"build:css": "postcss styles/index.css -c styles/config/postcss.config.js -o static/css/bundle.css",
"start": "next start"
},
"dependencies": {
"next": "^4.1.3",
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"devDependencies": {
"autoprefixer": "^7.1.6",
"cssnano": "^3.10.0",
"postcss-cli": "^4.1.1",
"postcss-easy-import": "^3.0.0",
"tailwindcss": "^0.2.2"
}
}
import Document, { Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static getInitialProps({ renderPage }) {
const { html, head, errorHtml, chunks } = renderPage()
return { html, head, errorHtml, chunks }
}
render() {
return (
<html>
<Head>
<link rel="stylesheet" href="/static/css/bundle.css" />
</Head>
<body>
{this.props.customValue}
<Main />
<NextScript />
</body>
</html>
)
}
}
export default MyDocument;
import Link from 'next/link'
import Head from '../components/head'
import Nav from '../components/nav'
export default () => (
<div>
<Head title="Home" />
<Nav />
<div className="hero">
<h1 className="title">Next.js + Tailwind css</h1>
</div>
</div>
)
.btn-blue {
@apply .bg-blue .text-white .font-bold .py-2 .px-4 .rounded;
}
var tailwindcss = require('tailwindcss');
module.exports = {
plugins: [
require('postcss-easy-import'),
tailwindcss('./styles/config/tailwind.config.js'),
require('autoprefixer'),
require('cssnano'),
]
}
This diff is collapsed.
@import "./button.css";
@tailwind preflight;
@tailwind utilities;
.hero {
width: 100%;
color: #333;
}
.title {
margin: 0;
width: 100%;
padding-top: 80px;
line-height: 1.15;
font-size: 48px;
}
.title, .description {
text-align: center;
}
.row {
max-width: 880px;
margin: 80px auto 40px;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.card {
padding: 18px 18px 24px;
width: 220px;
text-align: left;
text-decoration: none;
color: #434343;
border: 1px solid #9B9B9B;
}
.card:hover {
border-color: #067df7;
}
.card h3 {
margin: 0;
color: #067df7;
font-size: 18px;
}
.card p {
margin: 0;
padding: 12px 0 0;
font-size: 13px;
color: #333;
}
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