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.

prevent rerendering from Wrapper component during editing

Oleg Isonenc68a02b167a6
diff --git a/app/canvas/features/wrapper-component/editor/editor.tsx b/app/canvas/features/wrapper-component/editor/editor.tsx
index b35f4220c3..b41a708fd4 100644
--- a/app/canvas/features/wrapper-component/editor/editor.tsx
+++ b/app/canvas/features/wrapper-component/editor/editor.tsx
@@ -3,7 +3,7 @@ import {
RichTextPlugin,
HistoryPlugin,
InstancePlugin,
- ToolbarPlugin,
+ ToolbarConnectorPlugin,
OnChangePlugin,
} from "~/shared/text-editor";
@@ -20,7 +20,7 @@ export const Editor = ({ children, editable, onChange }: EditorProps) => {
<OnChangePlugin onChange={onChange} />
<HistoryPlugin />
<InstancePlugin>{children}</InstancePlugin>
- <ToolbarPlugin />
+ <ToolbarConnectorPlugin />
</>
);
};
diff --git a/app/canvas/features/wrapper-component/wrapper-component.tsx b/app/canvas/features/wrapper-component/wrapper-component.tsx
index d4f49a1954..f16204c7bc 100644
--- a/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -1,4 +1,4 @@
-import { useCallback, MouseEvent, FormEvent } from "react";
+import { useCallback, MouseEvent, FormEvent, useRef, useMemo } from "react";
import type { OnChangeChildren } from "~/shared/tree-utils";
import {
type Instance,
@@ -54,42 +54,41 @@ export const WrapperComponentDev = ({
instance.component === "Input" ? { readOnly: true } : undefined;
const { Component } = primitives[instance.component];
- const element = (
- <Component
- {...userProps}
- {...rest}
- {...draggableProps}
- {...readonlyProps}
- {...isEditingProps}
- {...editableProps}
- className={className}
- id={instance.id}
- tabIndex={0}
- data-component={instance.component}
- data-id={instance.id}
- ref={refCallback}
- onClick={(event: MouseEvent) => {
- // Prevent click handler for element selection
- if (isEditing) {
- event.stopPropagation();
- }
- if (instance.component === "Link") {
- event.preventDefault();
- }
- }}
- onSubmit={(event: FormEvent) => {
- // Prevent submitting the form when clicking a button type submit
+
+ const props = {
+ ...userProps,
+ ...rest,
+ ...draggableProps,
+ ...readonlyProps,
+ ...isEditingProps,
+ ...editableProps,
+ className: className,
+ id: instance.id,
+ tabIndex: 0,
+ "data-component": instance.component,
+ "data-id": instance.id,
+ ref: refCallback,
+ onClick: (event: MouseEvent) => {
+ // Prevent click handler for element selection
+ if (isEditing) {
+ event.stopPropagation();
+ }
+ if (instance.component === "Link") {
event.preventDefault();
- }}
- >
- {isEditing ? null : renderWrapperComponentChildren(children)}
- </Component>
- );
+ }
+ },
+ onSubmit: (event: FormEvent) => {
+ // Prevent submitting the form when clicking a button type submit
+ event.preventDefault();
+ },
+ };
- if (isEditing) {
+ // Prevent rerender because in editing mode Editor controls the node.
+ const editor = useMemo(() => {
+ if (isEditing === false) return;
return (
<Editor
- editable={element}
+ editable={<Component {...props} />}
onChange={(updates) => {
onChangeChildren({ instanceId: instance.id, updates });
}}
@@ -97,7 +96,13 @@ export const WrapperComponentDev = ({
{instance.children}
</Editor>
);
+ }, [isEditing]);
+
+ if (editor) {
+ return editor;
}
- return element;
+ return (
+ <Component {...props}>{renderWrapperComponentChildren(children)}</Component>
+ );
};
diff --git a/app/shared/text-editor/index.ts b/app/shared/text-editor/index.ts
index e47acb3978..f057b52da6 100644
--- a/app/shared/text-editor/index.ts
+++ b/app/shared/text-editor/index.ts
@@ -7,6 +7,6 @@ export {
} from "./lexical";
export { config } from "./config";
export { InstancePlugin } from "./plugins/plugin-instance";
-export { ToolbarPlugin } from "./plugins/plugin-toolbar";
+export { ToolbarConnectorPlugin } from "./plugins/plugin-toolbar-connector";
export { OnChangePlugin } from "./plugins/plugin-on-change";
export { useContentEditable } from "./content-editable";
diff --git a/app/shared/text-editor/plugins/plugin-toolbar.tsx b/app/shared/text-editor/plugins/plugin-toolbar-connector.tsx
similarity index 96%
rename from app/shared/text-editor/plugins/plugin-toolbar.tsx
rename to app/shared/text-editor/plugins/plugin-toolbar-connector.tsx
index ae826f7db9..e5af86110e 100644
--- a/app/shared/text-editor/plugins/plugin-toolbar.tsx
+++ b/app/shared/text-editor/plugins/plugin-toolbar-connector.tsx
@@ -9,7 +9,7 @@ import {
useLexicalComposerContext,
} from "../lexical";
-export const ToolbarPlugin = () => {
+export const ToolbarConnectorPlugin = () => {
const [editor] = useLexicalComposerContext();
const lastSelectionRef = useRef<unknown>();