Skip to content

Commit

Permalink
Export JSX namespace from automatic runtime entries (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Nov 9, 2020
1 parent 9e3671c commit 74ec012
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/react/jsx-dev-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"./dist/react.cjs.js": "./dist/react.browser.cjs.js",
"./dist/react.esm.js": "./dist/react.browser.esm.js"
},
"types": "../types/jsx-dev-runtime",
"preconstruct": {
"source": "../src/jsx-dev-runtime",
"umdName": "emotionReactJSXDevRuntime"
Expand Down
1 change: 1 addition & 0 deletions packages/react/jsx-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"./dist/react.cjs.js": "./dist/react.browser.cjs.js",
"./dist/react.esm.js": "./dist/react.browser.esm.js"
},
"types": "../types/jsx-runtime",
"preconstruct": {
"source": "../src/jsx-runtime",
"umdName": "emotionReactJSXRuntime"
Expand Down
43 changes: 12 additions & 31 deletions packages/react/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '@emotion/serialize'
import {
ClassAttributes,
ComponentClass,
Context,
Provider,
FC,
Expand All @@ -23,6 +22,7 @@ import {
Ref,
createElement
} from 'react'
import { EmotionJSX } from './jsx-namespace'

export {
ArrayInterpolation,
Expand Down Expand Up @@ -92,41 +92,22 @@ export interface ClassNamesProps {
*/
export function ClassNames(props: ClassNamesProps): ReactElement

type WithConditionalCSSProp<P> = 'className' extends keyof P
? (P extends { className?: string } ? P & { css?: Interpolation<Theme> } : P)
: P

// unpack all here to avoid infinite self-referencing when defining our own JSX namespace
type ReactJSXElement = JSX.Element
type ReactJSXElementClass = JSX.ElementClass
type ReactJSXElementAttributesProperty = JSX.ElementAttributesProperty
type ReactJSXElementChildrenAttribute = JSX.ElementChildrenAttribute
type ReactJSXLibraryManagedAttributes<C, P> = JSX.LibraryManagedAttributes<C, P>
type ReactJSXIntrinsicAttributes = JSX.IntrinsicAttributes
type ReactJSXIntrinsicClassAttributes<T> = JSX.IntrinsicClassAttributes<T>
type ReactJSXIntrinsicElements = JSX.IntrinsicElements

export const jsx: typeof createElement
export namespace jsx {
namespace JSX {
interface Element extends ReactJSXElement {}
interface ElementClass extends ReactJSXElementClass {}
interface Element extends EmotionJSX.Element {}
interface ElementClass extends EmotionJSX.ElementClass {}
interface ElementAttributesProperty
extends ReactJSXElementAttributesProperty {}
extends EmotionJSX.ElementAttributesProperty {}
interface ElementChildrenAttribute
extends ReactJSXElementChildrenAttribute {}

type LibraryManagedAttributes<C, P> = WithConditionalCSSProp<P> &
ReactJSXLibraryManagedAttributes<C, P>

interface IntrinsicAttributes extends ReactJSXIntrinsicAttributes {}
extends EmotionJSX.ElementChildrenAttribute {}
type LibraryManagedAttributes<C, P> = EmotionJSX.LibraryManagedAttributes<
C,
P
>
interface IntrinsicAttributes extends EmotionJSX.IntrinsicAttributes {}
interface IntrinsicClassAttributes<T>
extends ReactJSXIntrinsicClassAttributes<T> {}

type IntrinsicElements = {
[K in keyof ReactJSXIntrinsicElements]: ReactJSXIntrinsicElements[K] & {
css?: Interpolation<Theme>
}
}
extends EmotionJSX.IntrinsicClassAttributes<T> {}
type IntrinsicElements = EmotionJSX.IntrinsicElements
}
}
1 change: 1 addition & 0 deletions packages/react/types/jsx-dev-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { EmotionJSX as JSX } from './jsx-namespace'
38 changes: 38 additions & 0 deletions packages/react/types/jsx-namespace.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'react'
import { Interpolation } from '@emotion/serialize'
import { Theme } from './index'

type WithConditionalCSSProp<P> = 'className' extends keyof P
? (P extends { className?: string } ? P & { css?: Interpolation<Theme> } : P)
: P

// unpack all here to avoid infinite self-referencing when defining our own JSX namespace
type ReactJSXElement = JSX.Element
type ReactJSXElementClass = JSX.ElementClass
type ReactJSXElementAttributesProperty = JSX.ElementAttributesProperty
type ReactJSXElementChildrenAttribute = JSX.ElementChildrenAttribute
type ReactJSXLibraryManagedAttributes<C, P> = JSX.LibraryManagedAttributes<C, P>
type ReactJSXIntrinsicAttributes = JSX.IntrinsicAttributes
type ReactJSXIntrinsicClassAttributes<T> = JSX.IntrinsicClassAttributes<T>
type ReactJSXIntrinsicElements = JSX.IntrinsicElements

export namespace EmotionJSX {
interface Element extends ReactJSXElement {}
interface ElementClass extends ReactJSXElementClass {}
interface ElementAttributesProperty
extends ReactJSXElementAttributesProperty {}
interface ElementChildrenAttribute extends ReactJSXElementChildrenAttribute {}

type LibraryManagedAttributes<C, P> = WithConditionalCSSProp<P> &
ReactJSXLibraryManagedAttributes<C, P>

interface IntrinsicAttributes extends ReactJSXIntrinsicAttributes {}
interface IntrinsicClassAttributes<T>
extends ReactJSXIntrinsicClassAttributes<T> {}

type IntrinsicElements = {
[K in keyof ReactJSXIntrinsicElements]: ReactJSXIntrinsicElements[K] & {
css?: Interpolation<Theme>
}
}
}
1 change: 1 addition & 0 deletions packages/react/types/jsx-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { EmotionJSX as JSX } from './jsx-namespace'

0 comments on commit 74ec012

Please sign in to comment.