|
1 | 1 | 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' |
4 | 7 | import { codeBlock } from 'common-tags' |
5 | 8 | // @ts-ignore |
6 | 9 | import hn from '!!raw-loader!./pages/examples/hacker-news.tsx' |
7 | 10 |
|
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 = { |
9 | 27 | ...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, |
14 | 39 | 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, |
17 | 45 | }, |
18 | 46 | } |
19 | 47 |
|
|
0 commit comments