Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

refactor: simplify text child rendering (#2611)

Bogdan Chadkinf3a75d150a35
diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
index c0f3a8e3e5..9b3209023e 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -16,14 +16,13 @@ import { mergeRefs } from "@react-aria/utils";
import type { Instance, Instances, Prop } from "@webstudio-is/sdk";
import { findTreeInstanceIds } from "@webstudio-is/sdk";
import {
- type Components,
- renderWebstudioComponentChildren,
idAttribute,
componentAttribute,
showAttribute,
selectorIdAttribute,
indexAttribute,
getIndexesWithinAncestors,
+ type WebstudioComponentProps,
} from "@webstudio-is/react-sdk";
import {
computeExpression,
@@ -188,13 +187,6 @@ const useInstanceProps = (instanceId: Instance["id"]) => {
return instancePropsObject;
};
-type WebstudioComponentProps = {
- instance: Instance;
- instanceSelector: Instance["id"][];
- children: Array<JSX.Element | string>;
- components: Components;
-};
-
const existingElements = new Set<string>();
/**
@@ -275,12 +267,10 @@ export const WebstudioComponentCanvas = forwardRef<
/**
* Prevents edited element from having a size of 0 on the first render.
- * Directly using `renderWebstudioComponentChildren(children)` in Text Edit
+ * Directly using `children` in Text Edit
* conflicts with React due to lexical node changes.
*/
- const initialContentEditableContent = useRef(
- renderWebstudioComponentChildren(children)
- );
+ const initialContentEditableContent = useRef(children);
useCollapsedOnNewElement(instanceId);
@@ -324,7 +314,7 @@ export const WebstudioComponentCanvas = forwardRef<
const instanceElement = (
<>
<Component {...props} ref={mergeRefs(ref, rootRef)}>
- {renderWebstudioComponentChildren(children)}
+ {children}
</Component>
</>
);
@@ -333,8 +323,7 @@ export const WebstudioComponentCanvas = forwardRef<
areInstanceSelectorsEqual(textEditingInstanceSelector, instanceSelector) ===
false
) {
- initialContentEditableContent.current =
- renderWebstudioComponentChildren(children);
+ initialContentEditableContent.current = children;
return instanceElement;
}
@@ -406,7 +395,7 @@ export const WebstudioComponentPreview = forwardRef<
}
return (
<Component {...props} ref={ref}>
- {renderWebstudioComponentChildren(children)}
+ {children}
</Component>
);
});
diff --git a/packages/react-sdk/src/component-generator.test.ts b/packages/react-sdk/src/component-generator.test.ts
index 4afc2bb283..34f39deccb 100644
--- a/packages/react-sdk/src/component-generator.test.ts
+++ b/packages/react-sdk/src/component-generator.test.ts
@@ -6,13 +6,13 @@ import {
type Instance,
type Prop,
} from "@webstudio-is/sdk";
-import { showAttribute } from "./tree/webstudio-component";
+import { showAttribute } from "./props";
+import { collectionComponent } from "./core-components";
import {
generateJsxChildren,
generateJsxElement,
generatePageComponent,
} from "./component-generator";
-import { collectionComponent } from "./core-components";
const clear = (input: string) =>
stripIndent(input).trimStart().replace(/ +$/, "");
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index 723d85deb5..7e2bc37df4 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -12,14 +12,14 @@ import {
idAttribute,
indexAttribute,
showAttribute,
-} from "./tree/webstudio-component";
+} from "./props";
+import { collectionComponent } from "./core-components";
import {
decodeDataSourceVariable,
generateDataSources,
validateExpression,
} from "./expression";
import type { IndexesWithinAncestors } from "./instance-utils";
-import { collectionComponent } from "./core-components";
const generatePropValue = ({
scope,
diff --git a/packages/react-sdk/src/components/components-utils.ts b/packages/react-sdk/src/components/components-utils.ts
index 4fefcc3fdb..72b23b61c2 100644
--- a/packages/react-sdk/src/components/components-utils.ts
+++ b/packages/react-sdk/src/components/components-utils.ts
@@ -1,4 +1,4 @@
-import { componentAttribute, idAttribute } from "../tree";
+import { componentAttribute, idAttribute } from "../props";
export type AnyComponent = React.ForwardRefExoticComponent<
Omit<
diff --git a/packages/react-sdk/src/css/css.ts b/packages/react-sdk/src/css/css.ts
index d5ad757034..9810f8db4d 100644
--- a/packages/react-sdk/src/css/css.ts
+++ b/packages/react-sdk/src/css/css.ts
@@ -9,7 +9,7 @@ import type {
StyleSourceSelection,
} from "@webstudio-is/sdk";
import type { WsComponentMeta } from "../components/component-meta";
-import { idAttribute } from "../tree/index";
+import { idAttribute } from "../props";
import { addGlobalRules } from "./global-rules";
import { getPresetStyleRules, getStyleRules } from "./style-rules";
diff --git a/packages/react-sdk/src/css/style-rules.ts b/packages/react-sdk/src/css/style-rules.ts
index c4883c13b5..2d0d337ad9 100644
--- a/packages/react-sdk/src/css/style-rules.ts
+++ b/packages/react-sdk/src/css/style-rules.ts
@@ -7,7 +7,7 @@ import type {
StyleSourceSelections,
} from "@webstudio-is/sdk";
import type { PresetStyle } from "../components/component-meta";
-import { componentAttribute } from "../tree/index";
+import { componentAttribute } from "../props";
type StyleRule = {
instanceId: string;
diff --git a/packages/react-sdk/src/embed-template.test.ts b/packages/react-sdk/src/embed-template.test.ts
index 6519430e50..b03fd46b43 100644
--- a/packages/react-sdk/src/embed-template.test.ts
+++ b/packages/react-sdk/src/embed-template.test.ts
@@ -1,6 +1,6 @@
import { expect, test } from "@jest/globals";
import { generateDataFromEmbedTemplate, namespaceMeta } from "./embed-template";
-import { showAttribute } from "./tree";
+import { showAttribute } from "./props";
import type { WsComponentMeta } from "./components/component-meta";
const expectString = expect.any(String);
diff --git a/packages/react-sdk/src/index.ts b/packages/react-sdk/src/index.ts
index 107eeaf7a1..63678ae0ec 100644
--- a/packages/react-sdk/src/index.ts
+++ b/packages/react-sdk/src/index.ts
@@ -14,12 +14,7 @@ export {
defaultStates,
} from "./components/component-meta";
export * from "./embed-template";
-export {
- normalizeProps,
- getPropsByInstanceId,
- getInstanceIdFromComponentProps,
- getIndexWithinAncestorFromComponentProps,
-} from "./props";
+export * from "./props";
export { type Params, ReactSdkContext } from "./context";
export {
validateExpression,
diff --git a/packages/react-sdk/src/props.ts b/packages/react-sdk/src/props.ts
index e125f114d3..ad666f0e2e 100644
--- a/packages/react-sdk/src/props.ts
+++ b/packages/react-sdk/src/props.ts
@@ -1,5 +1,4 @@
import type { Instance, Page, Prop, Props, Assets } from "@webstudio-is/sdk";
-import { idAttribute, indexAttribute } from "./tree/webstudio-component";
export type PropsByInstanceId = Map<Instance["id"], Prop[]>;
@@ -87,6 +86,13 @@ export const getPropsByInstanceId = (props: Props) => {
return propsByInstanceId;
};
+export const idAttribute = "data-ws-id" as const;
+export const selectorIdAttribute = "data-ws-selector" as const;
+export const componentAttribute = "data-ws-component" as const;
+export const showAttribute = "data-ws-show" as const;
+export const indexAttribute = "data-ws-index" as const;
+export const collapsedAttribute = "data-ws-collapsed" as const;
+
export const getInstanceIdFromComponentProps = (
props: Record<string, unknown>
) => {
diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsx
index c70d58abbe..2d7165520b 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -2,11 +2,17 @@ import type { ForwardRefExoticComponent, RefAttributes } from "react";
import type { Instance, Instances } from "@webstudio-is/sdk";
import type { Components } from "../components/components-utils";
import { type Params, ReactSdkContext } from "../context";
-import type { WebstudioComponentProps } from "./webstudio-component";
import type { ImageLoader } from "@webstudio-is/image";
type InstanceSelector = Instance["id"][];
+export type WebstudioComponentProps = {
+ instance: Instance;
+ instanceSelector: Instance["id"][];
+ children: ReactNode;
+ components: Components;
+};
+
export const createElementsTree = ({
renderer,
assetBaseUrl,
@@ -61,6 +67,16 @@ export const createElementsTree = ({
);
};
+const renderText = (text: string): Array<JSX.Element> => {
+ const lines = text.split("\n");
+ return lines.map((line, index) => (
+ <Fragment key={index}>
+ {line}
+ {index < lines.length - 1 && <br />}
+ </Fragment>
+ ));
+};
+
const createInstanceChildrenElements = ({
instances,
instanceSelector,
@@ -76,10 +92,10 @@ const createInstanceChildrenElements = ({
>;
components: Components;
}) => {
- const elements = [];
+ const elements: ReactNode[] = [];
for (const child of children) {
if (child.type === "text") {
- elements.push(child.value);
+ elements.push(renderText(child.value));
continue;
}
const childInstance = instances.get(child.value);
@@ -103,6 +119,10 @@ const createInstanceChildrenElements = ({
});
elements.push(element);
}
+ // let empty children be coalesced with fallback
+ if (elements.length === 0) {
+ return;
+ }
return elements;
};
@@ -110,7 +130,7 @@ const createInstanceElement = ({
Component,
instance,
instanceSelector,
- children = [],
+ children,
components,
}: {
instance: Instance;
@@ -118,7 +138,7 @@ const createInstanceElement = ({
Component: ForwardRefExoticComponent<
WebstudioComponentProps & RefAttributes<HTMLElement>
>;
- children?: Array<JSX.Element | string>;
+ children?: ReactNode;
components: Components;
}) => {
return (
diff --git a/packages/react-sdk/src/tree/index.ts b/packages/react-sdk/src/tree/index.ts
index 55a31ab71b..271ce3a910 100644
--- a/packages/react-sdk/src/tree/index.ts
+++ b/packages/react-sdk/src/tree/index.ts
@@ -1,2 +1 @@
export * from "./create-elements-tree";
-export * from "./webstudio-component";
diff --git a/packages/react-sdk/src/tree/webstudio-component.tsx b/packages/react-sdk/src/tree/webstudio-component.tsx
deleted file mode 100644
index 370122ca43..0000000000
--- a/packages/react-sdk/src/tree/webstudio-component.tsx
+++ /dev/null
@@ -1,40 +0,0 @@
-import { Fragment } from "react";
-import type { Instance } from "@webstudio-is/sdk";
-import type { Components } from "../components/components-utils";
-
-const renderText = (text: string): Array<JSX.Element> => {
- const lines = text.split("\n");
- return lines.map((line, index) => (
- <Fragment key={index}>
- {line}
- {index < lines.length - 1 ? <br /> : null}
- </Fragment>
- ));
-};
-
-export const renderWebstudioComponentChildren = (
- children: Array<JSX.Element | string> | undefined
-): Array<JSX.Element | string | Array<JSX.Element | string>> | undefined => {
- // Some elements like input can't have children
- // @todo needs to be made impossible to drag element into input
- if (children === undefined || children.length === 0) {
- return;
- }
- return children.map((child) => {
- return typeof child === "string" ? renderText(child) : child;
- });
-};
-
-export type WebstudioComponentProps = {
- instance: Instance;
- instanceSelector: Instance["id"][];
- children: Array<JSX.Element | string>;
- components: Components;
-};
-
-export const idAttribute = "data-ws-id" as const;
-export const selectorIdAttribute = "data-ws-selector" as const;
-export const componentAttribute = "data-ws-component" as const;
-export const showAttribute = "data-ws-show" as const;
-export const indexAttribute = "data-ws-index" as const;
-export const collapsedAttribute = "data-ws-collapsed" as const;