Unverified Commit 65db069d authored by Karl Horky's avatar Karl Horky Committed by GitHub
parent 13f8732f
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
# vercel
.vercel
# Pass Server Data Directly to a Next.js Page during SSR
Extract and inline critical css with [@emotion/react](https://www.npmjs.com/package/@emotion/react) and [@emotion/styled](https://www.npmjs.com/package/@emotion/styled)
## Deploy your own
Deploy the example using [Vercel](https://vercel.com):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-emotion-11)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npx create-next-app --example with-emotion-11 with-emotion-11-app
# or
yarn create next-app --example with-emotion-11 with-emotion-11-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
{
"name": "with-emotion-11",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"keywords": [],
"author": "Thomas Greco",
"license": "MIT",
"dependencies": {
"@emotion/cache": "11.0.0-next.12",
"@emotion/css": "11.0.0-next.12",
"@emotion/react": "11.0.0-next.12",
"@emotion/server": "11.0.0-next.12",
"@emotion/styled": "11.0.0-next.12",
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
}
}
// import App from 'next/app'
import { CacheProvider } from '@emotion/react'
import { cache } from '@emotion/css'
import { globalStyles } from '../shared/styles'
// see: https://nextjs.org/docs/advanced-features/custom-app
function MyApp({ Component, pageProps }) {
return (
<CacheProvider value={cache}>
{globalStyles}
<Component {...pageProps} />
</CacheProvider>
)
}
// Only uncomment this method if you have blocking data requirements for
// every single page in your application. This disables the ability to
// perform automatic static optimization, causing every page in your app to
// be server-side rendered.
//
// MyApp.getInitialProps = async appContext => {
// // calls page's `getInitialProps` and fills `appProps.pageProps`
// const appProps = await App.getInitialProps(appContext)
// return { ...appProps }
// }
export default MyApp
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { extractCritical } from '@emotion/server'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
const styles = extractCritical(initialProps.html)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
<style
data-emotion-css={styles.ids.join(' ')}
dangerouslySetInnerHTML={{ __html: styles.css }}
/>
</>
),
}
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
import { Basic, Combined, Animated, bounce } from '../shared/styles'
const Home = () => (
<div>
<Basic>Cool Styles</Basic>
<Combined>
With <code>:hover</code>.
</Combined>
<Animated animation={bounce}>Let's bounce.</Animated>
</div>
)
export default Home
import { keyframes, css, Global } from '@emotion/react'
import styled from '@emotion/styled'
export const globalStyles = (
<Global
styles={css`
html,
body {
padding: 3rem 1rem;
margin: 0;
background: papayawhip;
min-height: 100%;
font-family: Helvetica, Arial, sans-serif;
font-size: 24px;
}
`}
/>
)
export const basicStyles = css`
background-color: white;
color: cornflowerblue;
border: 1px solid lightgreen;
border-right: none;
border-bottom: none;
box-shadow: 5px 5px 0 0 lightgreen, 10px 10px 0 0 lightyellow;
transition: all 0.1s linear;
margin: 3rem 0;
padding: 1rem 0.5rem;
`
export const hoverStyles = css`
&:hover {
color: white;
background-color: lightgray;
border-color: aqua;
box-shadow: -15px -15px 0 0 aqua, -30px -30px 0 0 cornflowerblue;
}
`
export const bounce = keyframes`
from {
transform: scale(1.01);
}
to {
transform: scale(0.99);
}
`
export const Basic = styled('div')`
${basicStyles};
`
export const Combined = styled('div')`
${basicStyles};
${hoverStyles};
& code {
background-color: linen;
}
`
export const Animated = styled('div')`
${basicStyles};
${hoverStyles};
& code {
background-color: linen;
}
animation: ${(props) => props.animation} 0.2s infinite ease-in-out alternate;
`
{ {
"presets": ["next/babel", "@emotion/babel-preset-css-prop"] "presets": [
[
"next/babel",
{
"preset-react": {
"runtime": "automatic",
"importSource": "@emotion/react"
}
}
]
],
"plugins": ["@emotion/babel-plugin"]
} }
# Pass Server Data Directly to a Next.js Page during SSR # Emotion Example
Extract and inline critical css with Extract and inline critical css with
[emotion](https://github.com/emotion-js/emotion/tree/master/packages/emotion), [emotion](https://github.com/emotion-js/emotion/tree/master/packages/emotion),
......
{ {
"name": "with-emotion", "name": "with-emotion",
"version": "1.0.0", "version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": { "scripts": {
"dev": "next", "dev": "next",
"build": "next build", "build": "next build",
"start": "next start" "start": "next start"
}, },
"keywords": [],
"author": "Thomas Greco",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@emotion/babel-preset-css-prop": "10.0.27" "@emotion/babel-plugin": "11.0.0"
}, },
"dependencies": { "dependencies": {
"@emotion/core": "10.0.35", "@emotion/react": "11.1.1",
"@emotion/styled": "10.0.27", "@emotion/styled": "11.0.0",
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "^17.0.1",
"react-dom": "^16.7.0" "react-dom": "^17.0.1"
} }
} }
import { css, Global, keyframes } from '@emotion/core' import { css, Global, keyframes } from '@emotion/react'
import styled from '@emotion/styled' import styled from '@emotion/styled'
export const globalStyles = ( export const globalStyles = (
......
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