piral-react
Version:
Plugin for integrating React 16+ components in Piral.
31 lines (28 loc) • 898 B
text/typescript
import type { ForeignComponent, BaseComponentProps } from 'piral-core';
import type { ComponentType } from 'react';
import { createExtension } from './extension';
import { mountReact, unmountReact } from './mount';
export interface ReactConverterOptions {
/**
* Defines the name of the root element.
* @default piral-slot
*/
rootName?: string;
}
export function createConverter(config: ReactConverterOptions = {}) {
const { rootName = 'piral-slot' } = config;
const Extension = createExtension(rootName);
const convert = <TProps extends BaseComponentProps>(root: ComponentType<TProps>): ForeignComponent<TProps> => ({
mount(el, props, ctx) {
mountReact(el, root, props, ctx);
},
update(el, props, ctx) {
mountReact(el, root, props, ctx);
},
unmount(el) {
unmountReact(el);
},
});
convert.Extension = Extension;
return convert;
}