11import 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'
45import { codeBlock } from 'common-tags'
56// @ts -ignore
67import 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`
910let counter = 0
10- function hijackedStyled ( ...args : any [ ] ) {
11- // @ts -ignore
12- return styled ( ...args )
13- }
11+ const hijackedStyled = styled . bind ( undefined )
1412const hash = 'runner'
1513const ignoredProps = Object . getOwnPropertyNames ( Function )
1614Object . 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
0 commit comments