Skip to content

Commit 8b8acbd

Browse files
committed
chore(website): support emotion imports
1 parent 39bb3dc commit 8b8acbd

File tree

13 files changed

+333
-332
lines changed

13 files changed

+333
-332
lines changed

website/next.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const shouldPrefix = process.env.PREFIX === 'true'
66
module.exports = {
77
assetPrefix: shouldPrefix ? '/react-runner/' : '',
88
basePath: shouldPrefix ? '/react-runner' : '',
9-
compiler: {
10-
styledComponents: true,
11-
},
129
// ensure counter won't be incremented twice
1310
reactStrictMode: false,
1411
webpack: (config, options) => {

website/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010
"deploy": "rm -rf node_modules/.cache build && PREFIX=true next build && next export -o build && touch build/.nojekyll && gh-pages -d build -t true"
1111
},
1212
"dependencies": {
13+
"@emotion/css": "^11.7.1",
14+
"@emotion/react": "^11.7.1",
15+
"@emotion/styled": "^11.6.0",
1316
"common-tags": "^1.8.2",
1417
"next": "12.1.0",
1518
"next-themes": "^0.2.0-beta.0",
1619
"react": "17.0.2",
17-
"react-dom": "17.0.2",
18-
"styled-components": "^5.3.3"
20+
"react-dom": "17.0.2"
1921
},
2022
"devDependencies": {
2123
"@types/common-tags": "^1.8.1",
2224
"@types/lz-string": "^1.3.34",
2325
"@types/node": "17.0.5",
2426
"@types/react": "17.0.38",
25-
"@types/styled-components": "^5.1.15",
2627
"eslint": "8.5.0",
2728
"eslint-config-next": "12.0.7",
2829
"gh-pages": "^3.2.3",

website/src/components/GlobalStyle.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Global, css } from '@emotion/react'
2+
3+
export const GlobalStyle = () => (
4+
<Global
5+
styles={css`
6+
:root {
7+
--header-height: 48px;
8+
}
9+
10+
[data-theme='dark'] [data-hide='dark'],
11+
[data-theme='light'] [data-hide='light'] {
12+
display: none;
13+
}
14+
15+
@media (prefers-color-scheme: dark) {
16+
[data-theme='system'] [data-hide='dark'] {
17+
display: none;
18+
}
19+
}
20+
21+
@media (prefers-color-scheme: light) {
22+
[data-theme='system'] [data-hide='light'] {
23+
display: none;
24+
}
25+
}
26+
27+
body {
28+
margin: 0;
29+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto',
30+
'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans',
31+
'Helvetica Neue', sans-serif;
32+
font-size: 16px;
33+
}
34+
35+
* {
36+
box-sizing: border-box;
37+
}
38+
39+
pre,
40+
code,
41+
kbd {
42+
font-family: source-code-pro, Menlo, Monaco, Consolas, Courier New,
43+
monospace;
44+
}
45+
46+
ul {
47+
list-style: auto inside;
48+
padding: 0;
49+
}
50+
51+
a {
52+
color: steelblue;
53+
text-decoration: none;
54+
}
55+
`}
56+
/>
57+
)

website/src/components/Header.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import NextLink from 'next/link'
22
import { useTheme } from 'next-themes'
3-
import styled from 'styled-components'
3+
import styled from '@emotion/styled'
44

55
const Container = styled.header`
66
background: steelblue;
@@ -93,6 +93,8 @@ const ThemeToggle = () => {
9393
)
9494
}
9595

96+
const GithubButton = IconButton.withComponent(Link)
97+
9698
export const Header = ({ isExample }: { isExample: boolean }) => {
9799
if (isExample) {
98100
return (
@@ -129,7 +131,7 @@ export const Header = ({ isExample }: { isExample: boolean }) => {
129131
<NextLink href="/playground" passHref>
130132
<Link>Playground</Link>
131133
</NextLink>
132-
<IconButton as={Link} href="https://github.com/nihgwu/react-runner">
134+
<GithubButton href="https://github.com/nihgwu/react-runner">
133135
<svg
134136
width="20"
135137
height="20"
@@ -144,7 +146,7 @@ export const Header = ({ isExample }: { isExample: boolean }) => {
144146
clipRule="evenodd"
145147
/>
146148
</svg>
147-
</IconButton>
149+
</GithubButton>
148150
<ThemeToggle />
149151
</Container>
150152
)

website/src/components/LiveRunner.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState } from 'react'
2-
import styled from 'styled-components'
2+
import styled from '@emotion/styled'
33
import { useRunner, Scope } from 'react-runner'
44
import {
55
LiveProvider,
@@ -85,11 +85,13 @@ type Props = {
8585
language?: Language
8686
}
8787

88+
const StyledEditor = Editor.withComponent(LiveEditor)
89+
8890
export const LiveRunner = (props: Props) => (
8991
<LiveProvider {...props}>
9092
<Container>
9193
<EditorContainer>
92-
<Editor as={LiveEditor} />
94+
<StyledEditor />
9395
</EditorContainer>
9496
<PreviewContainer>
9597
<Preview as={LivePreview} />

website/src/components/MultiFilesExample/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState, useMemo } from 'react'
2-
import styled from 'styled-components'
2+
import styled from '@emotion/styled'
33
import { importCode, Runner, Scope } from 'react-runner'
44

55
import {

website/src/constants.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import React from 'react'
2-
import styled, { css, keyframes, createGlobalStyle } from 'styled-components'
3-
import * as Styled from 'styled-components'
2+
import * as EmotionCss from '@emotion/css'
3+
import * as EmotionReact from '@emotion/react'
4+
import styled from '@emotion/styled'
45
import { codeBlock } from 'common-tags'
56
// @ts-ignore
67
import hn from '!!raw-loader!./pages/examples/hacker-news.tsx'
78

8-
// mimic babel plugin's behaviour to support SSR
9+
// mimic the babel plugin's behaviour to support `Components as selectors`
910
let counter = 0
10-
function hijackedStyled(...args: any[]) {
11-
// @ts-ignore
12-
return styled(...args)
13-
}
11+
const hijackedStyled = styled.bind(undefined)
1412
const hash = 'runner'
1513
const ignoredProps = Object.getOwnPropertyNames(Function)
1614
Object.getOwnPropertyNames(styled).forEach((tag) => {
1715
if (ignoredProps.includes(tag)) return
1816
Object.defineProperty(hijackedStyled, tag, {
1917
get() {
20-
return styled[tag].withConfig({
21-
componentId: `sc-${hash}-${counter++}`,
18+
return styled(tag as keyof JSX.IntrinsicElements, {
19+
target: `e${hash}${counter++}`,
2220
})
2321
},
2422
})
@@ -28,19 +26,23 @@ export const resetCounter = () => {
2826
counter = 0
2927
}
3028

31-
export const scope = {
29+
const _React = {
3230
...React,
31+
createElement: EmotionReact.jsx,
32+
}
33+
34+
export const scope = {
35+
React: _React,
36+
..._React,
3337
styled: hijackedStyled,
34-
css,
35-
keyframes,
36-
createGlobalStyle,
38+
css: EmotionReact.css,
39+
keyframes: EmotionReact.keyframes,
40+
createElement: EmotionReact.jsx,
3741
import: {
38-
react: React,
39-
'styled-components': {
40-
...Styled,
41-
default: hijackedStyled,
42-
__esModule: true,
43-
},
42+
react: _React,
43+
'@emotion/css': EmotionCss,
44+
'@emotion/react': EmotionReact,
45+
'@emotion/styled': hijackedStyled,
4446
},
4547
}
4648

website/src/pages/_document.tsx

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,17 @@
1-
import Document, {
2-
DocumentContext,
3-
Html,
4-
Head,
5-
Main,
6-
NextScript,
7-
} from 'next/document'
8-
import { ServerStyleSheet } from 'styled-components'
1+
import { Html, Head, Main, NextScript } from 'next/document'
92

103
import { resetCounter } from '../constants'
114

12-
export default class MyDocument extends Document {
13-
static async getInitialProps(ctx: DocumentContext) {
14-
const sheet = new ServerStyleSheet()
15-
const originalRenderPage = ctx.renderPage
16-
17-
try {
18-
ctx.renderPage = () =>
19-
originalRenderPage({
20-
enhanceApp: (App) => (props) =>
21-
sheet.collectStyles(<App {...props} />),
22-
})
23-
24-
const initialProps = await Document.getInitialProps(ctx)
25-
return {
26-
...initialProps,
27-
styles: (
28-
<>
29-
{initialProps.styles}
30-
{sheet.getStyleElement()}
31-
</>
32-
),
33-
}
34-
} finally {
35-
sheet.seal()
36-
}
37-
}
38-
39-
render() {
40-
// reset counter for SSR
41-
resetCounter()
42-
return (
43-
<Html lang="en" data-theme="system">
44-
<Head />
45-
<body>
46-
<Main />
47-
<NextScript />
48-
</body>
49-
</Html>
50-
)
51-
}
5+
export default function Document() {
6+
// reset counter for SSR
7+
resetCounter()
8+
return (
9+
<Html lang="en" data-theme="system">
10+
<Head />
11+
<body>
12+
<Main />
13+
<NextScript />
14+
</body>
15+
</Html>
16+
)
5217
}

website/src/pages/examples/hacker-news.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect } from 'react'
2-
import styled from 'styled-components'
2+
import styled from '@emotion/styled'
33

44
type Item = {
55
id: number

0 commit comments

Comments
 (0)