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.

moving body into the instances tree

Oleg Isonen9335b05c4318
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index e1a4da37d1..1c866c5787 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -8,7 +8,6 @@ import {
type Data,
type Tree,
useAllUserProps,
- WrapperComponent,
globalStyles,
useSubscribe,
createElementsTree,
@@ -70,7 +69,7 @@ const useElementsTree = () => {
}, []);
return useMemo(() => {
- if (rootInstance === undefined) return;
+ if (rootInstance === undefined) return null;
return createElementsTree({
instance: rootInstance,
@@ -87,20 +86,10 @@ const useSubscribePreviewMode = () => {
return isPreviewMode;
};
-const PreviewMode = () => {
- const [rootInstance] = useRootInstance();
- const [breakpoints] = useBreakpoints();
- if (rootInstance === undefined) return null;
- return createElementsTree({
- breakpoints,
- instance: rootInstance,
- Component: WrapperComponent,
- });
-};
-
type DesignModeProps = {
treeId: Tree["id"];
project: db.project.Project;
+ children: JSX.Element | null;
};
const dndOptions = {
@@ -109,7 +98,7 @@ const dndOptions = {
delay: 10,
};
-const DesignMode = ({ treeId, project }: DesignModeProps) => {
+const DesignMode = ({ treeId, project, children }: DesignModeProps) => {
useDragDropHandlers();
useUpdateStyle();
useManageProps();
@@ -131,12 +120,11 @@ const DesignMode = ({ treeId, project }: DesignModeProps) => {
usePublishScrollState();
useSubscribeScrollState();
usePublishTextEditingInstanceId();
- const elements = useElementsTree();
return (
// Using touch backend becuase html5 drag&drop doesn't fire drag events in our case
<DndProvider backend={TouchBackend} options={dndOptions}>
- {elements && (
- <LexicalComposer initialConfig={config}>{elements}</LexicalComposer>
+ {children && (
+ <LexicalComposer initialConfig={config}>{children}</LexicalComposer>
)}
</DndProvider>
);
@@ -157,10 +145,14 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
// e.g. toggling preview is still needed in both modes
useShortcuts();
const isPreviewMode = useSubscribePreviewMode();
-
+ const elements = useElementsTree();
if (isPreviewMode) {
- return <PreviewMode />;
+ return elements;
}
- return <DesignMode treeId={data.tree.id} project={data.project} />;
+ return (
+ <DesignMode treeId={data.tree.id} project={data.project}>
+ {elements}
+ </DesignMode>
+ );
};
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index 50f4e9ecd8..3ac1ef6323 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -38,10 +38,10 @@ import {
import { useMeasure } from "~/shared/dom-hooks";
export const usePopulateRootInstance = (tree: Tree) => {
- const [, setRootInstance] = useRootInstance();
- useEffect(() => {
- setRootInstance(tree.root);
- }, [tree, setRootInstance]);
+ // It is only set once when the canvas is first loaded.
+ if (rootInstanceContainer.value === undefined) {
+ rootInstanceContainer.value = tree.root;
+ }
};
export const useInsertInstance = () => {
diff --git a/apps/designer/app/routes/canvas.tsx b/apps/designer/app/routes/canvas.tsx
deleted file mode 100644
index e506e9d440..0000000000
--- a/apps/designer/app/routes/canvas.tsx
+++ /dev/null
@@ -1,13 +0,0 @@
-import { LoaderFunction, type MetaFunction } from "@remix-run/node";
-import { Canvas } from "~/shared/documents/canvas";
-import env from "~/env.server";
-
-export const loader: LoaderFunction = () => ({
- env,
-});
-
-export const meta: MetaFunction = () => {
- return { title: "Webstudio canvas" };
-};
-
-export default Canvas;
diff --git a/apps/designer/app/routes/canvas/$projectId.tsx b/apps/designer/app/routes/canvas/$projectId.tsx
index 341525e63a..72cd7c38e9 100644
--- a/apps/designer/app/routes/canvas/$projectId.tsx
+++ b/apps/designer/app/routes/canvas/$projectId.tsx
@@ -1,13 +1,18 @@
import { useLoaderData } from "@remix-run/react";
-import type { LoaderFunction } from "@remix-run/node";
+import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import { Canvas } from "~/canvas";
import { loadCanvasData, type ErrorData, type CanvasData } from "~/shared/db";
import env, { Env } from "~/env.server";
import { ErrorMessage } from "~/shared/error";
import { sentryException } from "~/shared/sentry";
+import { Canvas as CanvasDocument } from "~/shared/documents/canvas";
type Data = (CanvasData | ErrorData) & { env: Env };
+export const meta: MetaFunction = () => {
+ return { title: "Webstudio canvas" };
+};
+
export const loader: LoaderFunction = async ({ params }): Promise<Data> => {
if (params.projectId === undefined) {
return { errors: "Missing projectId", env };
@@ -37,7 +42,9 @@ const CanvasRoute = () => {
if ("errors" in data) {
return <ErrorMessage message={data.errors} />;
}
- return <Canvas data={data} />;
+ const Outlet = () => <Canvas data={data} />;
+
+ return <CanvasDocument Outlet={Outlet} />;
};
export default CanvasRoute;
diff --git a/apps/designer/app/routes/index.tsx b/apps/designer/app/routes/index.tsx
index 7158259251..0f9169dbb9 100644
--- a/apps/designer/app/routes/index.tsx
+++ b/apps/designer/app/routes/index.tsx
@@ -3,7 +3,7 @@ import { useLoaderData } from "@remix-run/react";
import { Root, type Data } from "@webstudio-is/react-sdk";
import config from "~/config";
import * as db from "~/shared/db";
-import Document from "./canvas";
+import { Canvas as CanvasDocument } from "~/shared/documents/canvas";
import env, { Env } from "~/env.server";
// @todo all this subdomain logic is very hacky
@@ -71,7 +71,7 @@ const Index = () => {
// We need to render essentially the preview route but from the index,
// so we have to know the layout and the outlet from here.
// Maybe there is a way to tell remix to use the right outlet somehow and avoid passing it?
- return <Document Outlet={Outlet} />;
+ return <CanvasDocument Outlet={Outlet} />;
}
return null;
diff --git a/apps/designer/app/routes/preview.tsx b/apps/designer/app/routes/preview.tsx
deleted file mode 100644
index 9c73c174e5..0000000000
--- a/apps/designer/app/routes/preview.tsx
+++ /dev/null
@@ -1,13 +0,0 @@
-import { LoaderFunction, type MetaFunction } from "@remix-run/node";
-import { Canvas } from "~/shared/documents/canvas";
-import env from "~/env.server";
-
-export const loader: LoaderFunction = () => ({
- env,
-});
-
-export const meta: MetaFunction = () => {
- return { title: "Webstudio site preview" };
-};
-
-export default Canvas;
diff --git a/apps/designer/app/routes/preview/$projectId.tsx b/apps/designer/app/routes/preview/$projectId.tsx
index 321694dc05..22fb84c525 100644
--- a/apps/designer/app/routes/preview/$projectId.tsx
+++ b/apps/designer/app/routes/preview/$projectId.tsx
@@ -1,9 +1,14 @@
import { useLoaderData } from "@remix-run/react";
-import type { LoaderFunction } from "@remix-run/node";
+import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import { Root } from "@webstudio-is/react-sdk";
import { loadPreviewData, type PreviewData, type ErrorData } from "~/shared/db";
+import { Canvas as CanvasDocument } from "~/shared/documents/canvas";
import env, { Env } from "~/env.server";
+export const meta: MetaFunction = () => {
+ return { title: "Webstudio site preview" };
+};
+
type LoaderReturnType = Promise<
(PreviewData & { env: Env }) | (ErrorData & { env: Env })
>;
@@ -34,7 +39,9 @@ const PreviewRoute = () => {
if ("errors" in data) {
return <p>{data.errors}</p>;
}
- return <Root data={data} />;
+ const Outlet = () => <Root data={data} />;
+
+ return <CanvasDocument Outlet={Outlet} />;
};
export default PreviewRoute;
diff --git a/apps/designer/app/shared/documents/canvas.tsx b/apps/designer/app/shared/documents/canvas.tsx
index 83fda3bf2a..0231013bd3 100644
--- a/apps/designer/app/shared/documents/canvas.tsx
+++ b/apps/designer/app/shared/documents/canvas.tsx
@@ -1,13 +1,5 @@
-import {
- Links,
- Meta,
- Outlet as DefaultOutlet,
- Scripts,
- ScrollRestoration,
-} from "@remix-run/react";
-import { CriticalCss, components } from "@webstudio-is/react-sdk";
-
-const Body = components.Body.Component;
+import { Links, Meta, Outlet as DefaultOutlet } from "@remix-run/react";
+import { CriticalCss } from "@webstudio-is/react-sdk";
/**
* We are using Outlet prop from index layout when user renders site from a subdomain.
@@ -28,11 +20,7 @@ export const Canvas = ({
<Links />
<CriticalCss />
</head>
- <body>
- <Outlet />
- <ScrollRestoration />
- <Scripts />
- </body>
+ <Outlet />
</html>
);
};
diff --git a/packages/react-sdk/src/components/body.tsx b/packages/react-sdk/src/components/body.tsx
index a842fdac68..719dbd96d5 100644
--- a/packages/react-sdk/src/components/body.tsx
+++ b/packages/react-sdk/src/components/body.tsx
@@ -11,11 +11,6 @@ const defaultTag = "body";
export const Body = forwardRef<
ElementRef<typeof defaultTag>,
ComponentProps<typeof defaultTag>
->(({ children, className }, ref) => {
- useEffect(() => {
- document.body.className = className;
- }, []);
- return children;
-});
+>((props, ref) => <body {...props} ref={ref} />);
Body.displayName = "Body";
diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsx
index 270213a0c7..4633b1ab42 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -1,8 +1,9 @@
-import React from "react";
+import { Fragment } from "react";
import { toCss } from "../stitches";
import type { Instance } from "../db";
import type { Breakpoint } from "../css";
import { type WrapperComponentProps } from "./wrapper-component";
+import { Scripts, ScrollRestoration } from "@remix-run/react";
export type ChildrenUpdates = Array<
| string
@@ -32,26 +33,67 @@ export const createElementsTree = ({
breakpoints: Array<Breakpoint>;
Component: (props: WrapperComponentProps) => JSX.Element;
onChangeChildren?: OnChangeChildren;
-}): JSX.Element => {
- const children: Array<string | JSX.Element> = [];
+}) => {
+ const children = createInstanceChildrenElements({
+ Component,
+ children: instance.children,
+ breakpoints,
+ onChangeChildren,
+ });
+ const body = createInstanceElement({
+ Component,
+ instance,
+ children: [
+ <Fragment key="children">{children}</Fragment>,
+ <ScrollRestoration key="scrollRestoration" />,
+ <Scripts key="scripts" />,
+ ],
+ breakpoints,
+ });
+ return body;
+};
- for (const child of instance.children) {
- const element =
- typeof child === "string"
- ? child
- : createElementsTree({
- instance: child,
- breakpoints,
- Component,
- onChangeChildren,
- });
- children.push(element);
+const createInstanceChildrenElements = ({
+ children,
+ breakpoints,
+ Component,
+ onChangeChildren,
+}: {
+ children: Instance["children"];
+ breakpoints: Array<Breakpoint>;
+ Component: (props: WrapperComponentProps) => JSX.Element;
+ onChangeChildren?: OnChangeChildren;
+}) => {
+ const elements = [];
+ for (const child of children) {
+ if (typeof child === "string") {
+ elements.push(child);
+ continue;
+ }
+ const element = createInstanceElement({
+ instance: child,
+ breakpoints,
+ Component,
+ onChangeChildren,
+ });
+ elements.push(element);
}
+ return elements;
+};
- //if (instance.component === "Body") {
- // return children;
- //}
-
+const createInstanceElement = ({
+ Component,
+ instance,
+ children = [],
+ breakpoints,
+ onChangeChildren,
+}: {
+ instance: Instance;
+ breakpoints: Array<Breakpoint>;
+ Component: (props: WrapperComponentProps) => JSX.Element;
+ onChangeChildren?: OnChangeChildren;
+ children?: Array<JSX.Element>;
+}) => {
const props = {
instance,
children,
@@ -59,5 +101,6 @@ export const createElementsTree = ({
key: instance.id,
onChangeChildren,
};
+
return <Component {...props} />;
};
diff --git a/packages/react-sdk/src/user-props/all-user-props.ts b/packages/react-sdk/src/user-props/all-user-props.ts
index 5bffff85af..36b9d38596 100644
--- a/packages/react-sdk/src/user-props/all-user-props.ts
+++ b/packages/react-sdk/src/user-props/all-user-props.ts
@@ -7,15 +7,16 @@ export type AllUserProps = { [id: Instance["id"]]: InstanceProps };
export const allUserPropsContainer = createValueContainer<AllUserProps>({});
export const useAllUserProps = (initialUserProps?: Array<InstanceProps>) => {
- useMemo(() => {
- if (initialUserProps === undefined) return;
+ if (
+ allUserPropsContainer.value === undefined &&
+ initialUserProps !== undefined
+ ) {
const propsMap: AllUserProps = {};
for (const props of initialUserProps) {
propsMap[props.instanceId] = props;
}
//We don't need to trigger rerender when setting the initial value
allUserPropsContainer.value = propsMap;
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []);
+ }
return useValue(allUserPropsContainer);
};