Skip to content

Commit

Permalink
Export JSX namespace from automatic runtime entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Nov 9, 2020
1 parent 0d34200 commit 04fd6d4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 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
37 changes: 2 additions & 35 deletions packages/react/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
Ref,
createElement
} from 'react'
import { EmotionJSX } from './jsx-namespace'

export {
ArrayInterpolation,
Expand Down Expand Up @@ -92,41 +93,7 @@ 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 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>
}
}
}
export import JSX = EmotionJSX
}
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 04fd6d4

Please sign in to comment.