Skip to content

Commit 7a0f0cd

Browse files
committed
chore(website): support emotion imports
1 parent de96dac commit 7a0f0cd

File tree

4 files changed

+296
-13
lines changed

4 files changed

+296
-13
lines changed

website/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
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.7",
1520
"react": "17.0.2",

website/src/constants.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
import React from 'react'
22
import { createRequire } from 'react-live-runner'
3-
import styled, { css, keyframes, createGlobalStyle } from 'styled-components'
4-
import * as Styled from 'styled-components'
3+
import * as EmotionCss from '@emotion/css'
4+
import * as EmotionReact from '@emotion/react'
5+
import * as EmotionSytled from '@emotion/styled'
6+
import * as EmotionIsPropValid from '@emotion/is-prop-valid'
7+
import hashString from '@emotion/hash'
58
import { codeBlock } from 'common-tags'
69
// @ts-ignore
710
import hn from '!!raw-loader!./pages/examples/hacker-news.tsx'
811

9-
const imports = { react: React, 'styled-components': Styled }
12+
let counter = 0
13+
const emotionStyled = EmotionSytled.default
14+
Object.getOwnPropertyNames(emotionStyled).forEach((tag) => {
15+
if (tag === 'length') return
16+
Object.defineProperty(emotionStyled, tag, {
17+
get() {
18+
return emotionStyled(tag as keyof JSX.IntrinsicElements, {
19+
target: `e${hashString('react-runner')}${counter++}`,
20+
})
21+
},
22+
})
23+
})
24+
25+
const imports = {
26+
react: React,
27+
'@emotion/css': EmotionCss,
28+
'@emotion/react': EmotionReact,
29+
'@emotion/styled': EmotionSytled,
30+
'@emotion/is-prop-valid': EmotionIsPropValid,
31+
}
1032
export const scope = {
1133
...React,
12-
styled,
13-
css,
14-
keyframes,
15-
createGlobalStyle,
34+
styled: EmotionSytled.default,
35+
css: EmotionReact.css,
36+
keyframes: EmotionReact.keyframes,
37+
Global: EmotionReact.Global,
1638
require: createRequire(imports),
39+
jsxPragma: EmotionReact.jsx,
1740
}
1841

1942
export const examples = [

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)