Skip to content

Commit 3769306

Browse files
committed
chore(website): support emotion imports
1 parent 9ab5e9a commit 3769306

14 files changed

Lines changed: 308 additions & 277 deletions

File tree

website/next.config.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ const shouldPrefix = process.env.PREFIX === 'true'
66
module.exports = {
77
assetPrefix: shouldPrefix ? '/react-runner/' : '',
88
basePath: shouldPrefix ? '/react-runner' : '',
9-
experimental: {
10-
// ssr and displayName are configured by default
11-
styledComponents: true,
12-
},
139
reactStrictMode: true,
1410
webpack: (config, options) => {
1511
config.resolve.alias = {

website/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
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/hash": "^0.8.0",
15+
"@emotion/is-prop-valid": "^1.1.1",
16+
"@emotion/react": "^11.7.1",
17+
"@emotion/styled": "^11.6.0",
1318
"common-tags": "^1.8.2",
1419
"next": "12.0.9",
1520
"react": "17.0.2",
16-
"react-dom": "17.0.2",
17-
"styled-components": "^5.3.3"
21+
"react-dom": "17.0.2"
1822
},
1923
"devDependencies": {
2024
"@types/common-tags": "^1.8.1",
2125
"@types/lz-string": "^1.3.34",
2226
"@types/node": "17.0.5",
2327
"@types/react": "17.0.38",
24-
"@types/styled-components": "^5.1.15",
2528
"eslint": "8.5.0",
2629
"eslint-config-next": "12.0.7",
2730
"gh-pages": "^3.2.3",

website/src/components/BackButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from 'styled-components'
1+
import styled from '@emotion/styled'
22
import NextLink from 'next/link'
33

44
const RoundLink = styled.a`

website/src/components/GlobalStyle.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Global, css } from '@emotion/react'
2+
3+
export const GlobalStyle = () => (
4+
<Global
5+
styles={css`
6+
body {
7+
margin: 0;
8+
padding: 0;
9+
font-family: sans-serif;
10+
font-size: 16px;
11+
}
12+
13+
* {
14+
box-sizing: border-box;
15+
}
16+
17+
pre,
18+
code,
19+
kbd {
20+
font-family: source-code-pro, Menlo, Monaco, Consolas, Courier New,
21+
monospace;
22+
}
23+
24+
ul {
25+
list-style: auto inside;
26+
padding: 0;
27+
}
28+
`}
29+
/>
30+
)

website/src/components/Header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from 'styled-components'
1+
import styled from '@emotion/styled'
22
import NextLink from 'next/link'
33

44
const Container = styled.header`

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,
@@ -86,11 +86,13 @@ type Props = {
8686
language?: Language
8787
}
8888

89+
const StyledEditor = Editor.withComponent(LiveEditor)
90+
8991
export const LiveRunner = (props: Props) => (
9092
<LiveProvider {...props}>
9193
<Container>
9294
<EditorContainer>
93-
<Editor as={LiveEditor} />
95+
<StyledEditor />
9496
</EditorContainer>
9597
<PreviewContainer>
9698
<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: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
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'
5+
import isPropValid from '@emotion/is-prop-valid'
6+
import hashString from '@emotion/hash'
47
import { codeBlock } from 'common-tags'
58
// @ts-ignore
69
import hn from '!!raw-loader!./pages/examples/hacker-news.tsx'
710

8-
export const scope = {
11+
// mimic the @emotion/bable-plugin's behaviour to support `Components as selectors`
12+
let counter = 0
13+
const hijackedStyled = styled.bind(undefined)
14+
const hash = hashString('react-runner')
15+
Object.getOwnPropertyNames(styled).forEach((tag) => {
16+
if (tag === 'length') return
17+
Object.defineProperty(hijackedStyled, tag, {
18+
get() {
19+
return styled(tag as keyof JSX.IntrinsicElements, {
20+
target: `e${hash}${counter++}`,
21+
})
22+
},
23+
})
24+
})
25+
26+
const _React = {
927
...React,
10-
styled,
11-
css,
12-
keyframes,
13-
createGlobalStyle,
28+
createElement: EmotionReact.jsx,
29+
}
30+
31+
export const scope = {
32+
React: _React,
33+
..._React,
34+
styled: hijackedStyled,
35+
css: EmotionReact.css,
36+
keyframes: EmotionReact.keyframes,
37+
Global: EmotionReact.Global,
38+
createElement: EmotionReact.jsx,
1439
import: {
15-
react: React,
16-
'styled-components': Styled,
40+
react: _React,
41+
'@emotion/css': EmotionCss,
42+
'@emotion/react': EmotionReact,
43+
'@emotion/styled': hijackedStyled,
44+
'@emotion/is-prop-valid': isPropValid,
1745
},
1846
}
1947

website/src/pages/_document.tsx

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)