Skip to content

Commit b72b8e2

Browse files
azizhkTimer
andcommitted
[with-emotion-10,11] Update emotion examples to v10 & v11 (vercel#9646)
* Update emotion to v10, use @emotion/core, @emotion/styled * Add with-emotion-11 and make review changes * Fix package names and run prettier * fix readmes Co-authored-by: Joe Haddad <timer150@gmail.com>
1 parent 7a728d6 commit b72b8e2

File tree

14 files changed

+256
-63
lines changed

14 files changed

+256
-63
lines changed

examples/with-emotion-10/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Pass Server Data Directly to a Next.js Page during SSR
2+
3+
Extract and inline critical css with
4+
[emotion](https://github.com/emotion-js/emotion/tree/master/packages/emotion),
5+
[emotion-server](https://github.com/emotion-js/emotion/tree/master/packages/emotion-server),
6+
[@emotion/core](https://github.com/emotion-js/emotion/tree/master/packages/core),
7+
and [@emotion/styled](https://github.com/emotion-js/emotion/tree/master/packages/styled).
8+
9+
## Deploy your own
10+
11+
Deploy the example using [ZEIT Now](https://zeit.co/now):
12+
13+
[![Deploy with ZEIT Now](https://zeit.co/button)](https://zeit.co/new/project?template=https://github.com/zeit/next.js/tree/canary/examples/with-emotion-10)
14+
15+
## How to use
16+
17+
### Using `create-next-app`
18+
19+
Execute [`create-next-app`](https://github.com/zeit/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:
20+
21+
```bash
22+
npm init next-app --example with-emotion-10 with-emotion-10-app
23+
# or
24+
yarn create next-app --example with-emotion-10 with-emotion-10-app
25+
```
26+
27+
### Download manually
28+
29+
Download the example:
30+
31+
```bash
32+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-emotion-10
33+
cd with-emotion-10
34+
```
35+
36+
Install it and run:
37+
38+
```bash
39+
npm install
40+
npm run dev
41+
# or
42+
yarn
43+
yarn dev
44+
```
45+
46+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)):
47+
48+
```bash
49+
now
50+
```
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "with-emotion-fiber",
2+
"name": "with-emotion-10",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
@@ -12,11 +12,12 @@
1212
"author": "Thomas Greco",
1313
"license": "ISC",
1414
"dependencies": {
15-
"emotion": "^8.0.11",
16-
"emotion-server": "^8.0.11",
15+
"@emotion/core": "10.0.27",
16+
"@emotion/styled": "10.0.27",
17+
"emotion": "10.0.27",
18+
"emotion-server": "10.0.27",
1719
"next": "latest",
1820
"react": "^16.7.0",
19-
"react-dom": "^16.7.0",
20-
"react-emotion": "^8.0.11"
21+
"react-dom": "^16.7.0"
2122
}
2223
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as React from 'react'
2+
import NextApp from 'next/app'
3+
import { CacheProvider } from '@emotion/core'
4+
5+
// Use only { cache } from 'emotion'. Don't use { css }.
6+
import { cache } from 'emotion'
7+
8+
import { globalStyles } from '../shared/styles'
9+
10+
export default class App extends NextApp {
11+
render() {
12+
const { Component, pageProps } = this.props
13+
return (
14+
<CacheProvider value={cache}>
15+
{globalStyles}
16+
<Component {...pageProps} />
17+
</CacheProvider>
18+
)
19+
}
20+
}

examples/with-emotion-fiber/pages/_document.js renamed to examples/with-emotion-10/pages/_document.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ export default class MyDocument extends Document {
88
return { ...page, ...styles }
99
}
1010

11-
constructor(props) {
12-
super(props)
13-
const { __NEXT_DATA__, ids } = props
14-
if (ids) {
15-
__NEXT_DATA__.ids = ids
16-
}
17-
}
18-
1911
render() {
2012
return (
2113
<html>
2214
<Head>
23-
<style dangerouslySetInnerHTML={{ __html: this.props.css }} />
15+
<style
16+
data-emotion-css={this.props.ids.join(' ')}
17+
dangerouslySetInnerHTML={{ __html: this.props.css }}
18+
/>
2419
</Head>
2520
<body>
2621
<Main />

examples/with-emotion-fiber/shared/styles.js renamed to examples/with-emotion-10/shared/styles.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
import styled, { keyframes, css, injectGlobal } from 'react-emotion'
1+
import { keyframes, css, Global } from '@emotion/core'
2+
import styled from '@emotion/styled'
23

3-
export const injectGlobalStyles = () => injectGlobal`
4-
html, body {
5-
padding: 3rem 1rem;
6-
margin: 0;
7-
background: papayawhip;
8-
min-height: 100%;
9-
font-family: Helvetica, Arial, sans-serif;
10-
font-size: 24px;
11-
}
12-
`
4+
export const globalStyles = (
5+
<Global
6+
styles={css`
7+
html,
8+
body {
9+
padding: 3rem 1rem;
10+
margin: 0;
11+
background: papayawhip;
12+
min-height: 100%;
13+
font-family: Helvetica, Arial, sans-serif;
14+
font-size: 24px;
15+
}
16+
`}
17+
/>
18+
)
1319

1420
export const basicStyles = css`
1521
background-color: white;
@@ -32,12 +38,12 @@ export const hoverStyles = css`
3238
}
3339
`
3440
export const bounce = keyframes`
35-
from {
36-
transform: scale(1.01);
37-
}
38-
to {
39-
transform: scale(0.99);
40-
}
41+
from {
42+
transform: scale(1.01);
43+
}
44+
to {
45+
transform: scale(0.99);
46+
}
4147
`
4248

4349
export const Basic = styled('div')`

examples/with-emotion-fiber/README.md renamed to examples/with-emotion-11/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Pass Server Data Directly to a Next.js Page during SSR
22

3-
Extract and inline critical css with [emotion](https://github.com/emotion-js/emotion) using [emotion-server](https://github.com/emotion-js/emotion/tree/master/packages/emotion-server) and [react-emotion](https://github.com/emotion-js/emotion/tree/master/packages/react-emotion).
3+
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)
44

55
## Deploy your own
66

77
Deploy the example using [ZEIT Now](https://zeit.co/now):
88

9-
[![Deploy with ZEIT Now](https://zeit.co/button)](https://zeit.co/new/project?template=https://github.com/zeit/next.js/tree/canary/examples/with-emotion-fiber)
9+
[![Deploy with ZEIT Now](https://zeit.co/button)](https://zeit.co/new/project?template=https://github.com/zeit/next.js/tree/canary/examples/with-emotion-11)
1010

1111
## How to use
1212

@@ -15,18 +15,18 @@ Deploy the example using [ZEIT Now](https://zeit.co/now):
1515
Execute [`create-next-app`](https://github.com/zeit/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:
1616

1717
```bash
18-
npm init next-app --example with-emotion-fiber with-emotion-fiber-app
18+
npm init next-app --example with-emotion-11 with-emotion-11-app
1919
# or
20-
yarn create next-app --example with-emotion-fiber with-emotion-fiber-app
20+
yarn create next-app --example with-emotion-11 with-emotion-11-app
2121
```
2222

2323
### Download manually
2424

2525
Download the example:
2626

2727
```bash
28-
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-emotion-fiber
29-
cd with-emotion-fiber
28+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-emotion-11
29+
cd with-emotion-11
3030
```
3131

3232
Install it and run:

examples/with-emotion-11/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "with-emotion-11",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"dev": "next",
8+
"build": "next build",
9+
"start": "next start"
10+
},
11+
"keywords": [],
12+
"author": "Thomas Greco",
13+
"license": "ISC",
14+
"dependencies": {
15+
"@emotion/cache": "11.0.0-next.10",
16+
"@emotion/css": "11.0.0-next.10",
17+
"@emotion/react": "11.0.0-next.10",
18+
"@emotion/server": "11.0.0-next.10",
19+
"@emotion/styled": "11.0.0-next.10",
20+
"next": "latest",
21+
"react": "^16.7.0",
22+
"react-dom": "^16.7.0"
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as React from 'react'
2+
import NextApp from 'next/app'
3+
import { CacheProvider } from '@emotion/react'
4+
import { cache } from '@emotion/css'
5+
import { globalStyles } from '../shared/styles'
6+
7+
export default class App extends NextApp {
8+
render() {
9+
const { Component, pageProps } = this.props
10+
return (
11+
<CacheProvider value={cache}>
12+
{globalStyles}
13+
<Component {...pageProps} />
14+
</CacheProvider>
15+
)
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Document, { Head, Main, NextScript } from 'next/document'
2+
import { extractCritical } from '@emotion/server'
3+
4+
export default class MyDocument extends Document {
5+
static getInitialProps({ renderPage }) {
6+
const page = renderPage()
7+
const styles = extractCritical(page.html)
8+
return { ...page, ...styles }
9+
}
10+
11+
render() {
12+
return (
13+
<html>
14+
<Head>
15+
<style
16+
data-emotion-css={this.props.ids.join(' ')}
17+
dangerouslySetInnerHTML={{ __html: this.props.css }}
18+
/>
19+
</Head>
20+
<body>
21+
<Main />
22+
<NextScript />
23+
</body>
24+
</html>
25+
)
26+
}
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy