Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): use property mapping from component meta #484

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/react-output-target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prepublishOnly": "pnpm run build",
"build": "vite build && pnpm run build:dts",
"build:dts": "tsc -p tsconfig.json",
"build:tsup": "tsup",
"dev": "vite build --watch",
"version": "pnpm run build",
"prettier": "prettier \"./src/**/*.{html,ts,tsx,js,jsx}\" --write",
"release": "np",
Expand Down Expand Up @@ -51,7 +51,6 @@
"gitHead": "a3588e905186a0e86e7f88418fd5b2f9531b55e0",
"dependencies": {
"@lit/react": "^1.0.4",
"decamelize": "^6.0.0",
"html-react-parser": "^5.1.10",
"react-dom": "^18.3.1",
"ts-morph": "^22.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('createComponentWrappers', () => {
{
tagName: 'my-component',
componentClassName: 'MyComponent',
properties: [],
events: [
{
originalName: 'my-event',
Expand Down Expand Up @@ -62,6 +63,7 @@ export const MyComponent: StencilReactComponent<MyComponentElement, MyComponentE
{
tagName: 'my-component',
componentClassName: 'MyComponent',
properties: [],
events: [
{
originalName: 'my-event',
Expand Down Expand Up @@ -115,6 +117,7 @@ export default MyComponent;
components: [
{
tagName: 'my-component',
properties: [],
internal: true,
} as any,
],
Expand Down Expand Up @@ -162,6 +165,7 @@ export default MyComponent;
{
tagName: 'my-component',
componentClassName: 'MyComponent',
properties: [],
events: [
{
originalName: 'my-event',
Expand Down Expand Up @@ -226,6 +230,12 @@ export const MyComponent: StencilReactComponent<MyComponentElement, MyComponentE
{
tagName: 'my-component',
componentClassName: 'MyComponent',
properties: [{
name: 'hasMaxLength',
attribute: 'max-length'
}, {
name: 'links'
}],
events: [
{
originalName: 'my-event',
Expand Down Expand Up @@ -273,6 +283,7 @@ export const MyComponent: StencilReactComponent<MyComponentElement, MyComponentE
})
: /*@__PURE__*/ createSSRComponent<MyComponentElement, MyComponentEvents>({
tagName: 'my-component',
properties: { hasMaxLength: 'max-length' },
hydrateModule: import('my-package/hydrate')
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ import type { EventName, StencilReactComponent } from '@stencil/react-output-tar
})`;
const serverComponentCall = `/*@__PURE__*/ createSSRComponent<${componentElement}, ${componentEventNamesType}>({
tagName: '${tagName}',
properties: {${component.properties
/**
* Filter out properties that don't have an attribute.
* These are properties with complex types and can't be serialized.
*/
.filter((prop) => Boolean(prop.attribute))
.map((e) => `${e.name}: '${e.attribute}'`)
.join(',\n')
}},
hydrateModule: import('${hydrateModule}')
})`;

Expand Down
3 changes: 3 additions & 0 deletions packages/react-output-target/src/react/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export const createComponent = <I extends HTMLElement, E extends EventNames = {}
*/
export const createSSRComponent = <I extends HTMLElement, E extends EventNames = {}>({
hydrateModule,
properties,
tagName,
}: {
hydrateModule: Promise<{ renderToString: RenderToString }>;
properties: Record<string, string>;
tagName: string;
}): ReactWebComponent<I, E> => {
/**
Expand All @@ -43,6 +45,7 @@ export const createSSRComponent = <I extends HTMLElement, E extends EventNames =
const { createComponentForServerSideRendering } = await import('./ssr');
return createComponentForServerSideRendering<I, E>({
tagName,
properties,
renderToString: (await hydrateModule).renderToString,
})(props as any);
}) as unknown as ReactWebComponent<I, E>;
Expand Down
19 changes: 14 additions & 5 deletions packages/react-output-target/src/react/ssr.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import decamelize from 'decamelize';
import type { EventName, ReactWebComponent, WebComponentProps } from '@lit/react';

import { possibleStandardNames } from './constants';

const LOG_PREFIX = '[react-output-target]';

// A key value map matching React prop names to event names.
type EventNames = Record<string, EventName | string>;

Expand All @@ -19,6 +20,7 @@ interface RenderToStringOptions {
export type RenderToString = (html: string, options: RenderToStringOptions) => Promise<{ html: string | null }>;
interface CreateComponentForServerSideRenderingOptions {
tagName: string;
properties: Record<string, string>;
renderToString: RenderToString;
}

Expand Down Expand Up @@ -59,8 +61,15 @@ export const createComponentForServerSideRendering = <I extends HTMLElement, E e
continue;
}

const propName =
possibleStandardNames[key as keyof typeof possibleStandardNames] || decamelize(key, { separator: '-' });
let propName = possibleStandardNames[key as keyof typeof possibleStandardNames] || options.properties[key];
if (!propName) {
console.warn(
`${LOG_PREFIX} ignore component property "${key}" for ${options.tagName} ` +
'- property type is unknown or not a primitive and can\'t be serialized'
);
continue;
}

stringProps += ` ${propName}=${propValue}`;
}

Expand All @@ -71,8 +80,8 @@ export const createComponentForServerSideRendering = <I extends HTMLElement, E e
serializedChildren = ReactDOMServer.renderToString(awaitedChildren);
} catch (err: unknown) {
const error = err instanceof Error ? err : new Error('Unknown error');
console.log(
`Failed to serialize light DOM for ${toSerialize.slice(0, -1)} />: ${
console.warn(
`${LOG_PREFIX} Failed to serialize light DOM for ${toSerialize.slice(0, -1)} />: ${
error.message
} - this may impact the hydration of the component`
);
Expand Down
17 changes: 5 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading