Unverified Commit 4761c66a authored by Tasso Evangelista's avatar Tasso Evangelista
Browse files

Prepare jsx-runtime

parent 630044c3
{
"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/*"
]
}
}
}
......@@ -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';
/** @jsx jsx */
/** @jsxFrag jsxFrag */
/** @jsxImportSource . */
import { Option } from '../blocks/Option';
import { jsx, jsxFrag } from './jsx';
describe('divider', () => {
it('renders', () => {
......
......@@ -2,6 +2,14 @@
/* 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<
{
......@@ -19,15 +27,17 @@ namespace JSXInternal {
};
}
export { JSXInternal as JSX };
export const jsxFrag = Symbol('jsxFrag');
export function jsx(
type: undefined,
props: {
children: Block;
}
): [Block];
export function jsx<B extends Block>(
type: typeof jsxFrag,
props: never,
...children: B[]
): B;
export function jsx(
type: TextObject['type'],
props: TypeToPropsMap[TextObject['type']]
): TextObject;
export function jsx<B extends Block>(
type: B['type'],
......@@ -36,20 +46,51 @@ export function jsx<B extends Block>(
): B;
export function jsx(
type: unknown,
props: Record<string, unknown>,
...children: Block[]
type: undefined | TextObject['type'] | unknown,
props:
| {
children: Block;
}
| TypeToPropsMap[TextObject['type']]
| Record<string, unknown>
): unknown {
if (type === jsxFrag) {
return children;
switch (type) {
case undefined:
return [props.children];
case TextObjectType.PLAIN_TEXT:
case TextObjectType.MARKDOWN: {
const { children, ...rest } = props;
return { type, text: children, ...rest };
}
return {
type,
...props,
};
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 };
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment