Skip to content

Commit

Permalink
Prepare jsx-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Feb 25, 2021
1 parent 630044c commit 4761c66
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 60 deletions.
13 changes: 13 additions & 0 deletions packages/ui-kit/jsx-runtime/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"main": "../dist/cjs/jsx/jsx-runtime.js",
"module": "../dist/esm/jsx/jsx-runtime.js",
"types": "../dist/esm/jsx/jsx-runtime.d.ts",
"typesVersions": {
"<4.1": {
"*": [
"../dist/ts3.4/jsx/*"
]
}
}
}
2 changes: 0 additions & 2 deletions packages/ui-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@ export { uiKitModal } from './rendering/surfaces/uiKitModal';
export { UiKitParserBanner } from './rendering/surfaces/UiKitParserBanner';
export { UiKitParserMessage } from './rendering/surfaces/UiKitParserMessage';
export { UiKitParserModal } from './rendering/surfaces/UiKitParserModal';

export { jsx } from './rendering/jsx';
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/** @jsx jsx */
/** @jsxFrag jsxFrag */
/** @jsxImportSource . */

import { Option } from '../blocks/Option';
import { jsx, jsxFrag } from './jsx';

describe('divider', () => {
it('renders', () => {
Expand Down
96 changes: 96 additions & 0 deletions packages/ui-kit/src/jsx/jsx-runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-disable import/export */
/* eslint-disable @typescript-eslint/no-namespace */

import { Block } from '../blocks/Block';
import { TextObject } from '../blocks/TextObject';
import { TextObjectType } from '../blocks/TextObjectType';

type TypeToPropsMap = {
[T in TextObject as T['type']]: Omit<T, 'type' | 'text'> & {
children?: string;
};
};

type PropsForBlock<B extends Block> = Omit<
{
[K in keyof B]: Exclude<B[K], undefined> extends Block | Block[]
? JSXInternal.Element
: B[K];
},
'type'
>;

namespace JSXInternal {
export type Element = Block | Block[];
export type IntrinsicElements = {
[B in Block as B['type']]: PropsForBlock<B>;
};
}

export function jsx(
type: undefined,
props: {
children: Block;
}
): [Block];

export function jsx(
type: TextObject['type'],
props: TypeToPropsMap[TextObject['type']]
): TextObject;

export function jsx<B extends Block>(
type: B['type'],
props: PropsForBlock<B>,
...children: never
): B;

export function jsx(
type: undefined | TextObject['type'] | unknown,
props:
| {
children: Block;
}
| TypeToPropsMap[TextObject['type']]
| Record<string, unknown>
): unknown {
switch (type) {
case undefined:
return [props.children];

case TextObjectType.PLAIN_TEXT:
case TextObjectType.MARKDOWN: {
const { children, ...rest } = props;
return { type, text: children, ...rest };
}

default:
return { type, ...props };
}
}

export function jsxs<B extends Block[]>(
type: undefined,
props: {
children: B;
}
): B;

export function jsxs(
type: Block['type'] | undefined,
props: Record<string, unknown>
) {
switch (type) {
case undefined:
return props.children;

default:
return jsx(type, props);
}
}

export namespace jsx {
export import JSX = JSXInternal;
}

export { JSXInternal as JSX };
55 changes: 0 additions & 55 deletions packages/ui-kit/src/rendering/jsx.ts

This file was deleted.

0 comments on commit 4761c66

Please sign in to comment.