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.

Fix jumps on editing text inside tooltips, and probably other components (#2054)

Ivan Starkov3df5c96a9c95
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index fcd8529021..129326bf6f 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -235,10 +235,14 @@ export const Canvas = ({ params }: CanvasProps): JSX.Element | null => {
return (
<>
<GlobalStyles params={params} />
+ {elements}
+ {
+ // Call hooks after render to ensure effects are last.
+ // Helps improve outline calculations as all styles are then applied.
+ }
{isPreviewMode === false && handshaken === true && (
<DesignMode params={params} />
)}
- {elements}
</>
);
};
diff --git a/apps/builder/app/canvas/features/text-editor/text-editor.tsx b/apps/builder/app/canvas/features/text-editor/text-editor.tsx
index 0eab76fa21..c9048a4f9a 100644
--- a/apps/builder/app/canvas/features/text-editor/text-editor.tsx
+++ b/apps/builder/app/canvas/features/text-editor/text-editor.tsx
@@ -49,8 +49,15 @@ const AutofocusPlugin = () => {
const [editor] = useLexicalComposerContext();
useEffect(() => {
+ const rootElement = editor.getRootElement();
+
+ if (rootElement === null) {
+ return;
+ }
+
editor.focus();
- }, [editor]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
return null;
};
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 9b09942a0b..9ea4c115e4 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -5,6 +5,7 @@ import {
forwardRef,
type ForwardedRef,
useRef,
+ useLayoutEffect,
} from "react";
import { Suspense, lazy } from "react";
import { useStore } from "@nanostores/react";
@@ -16,7 +17,6 @@ import {
Instances,
} from "@webstudio-is/project-build";
import {
- type AnyComponent,
type Components,
renderWebstudioComponentChildren,
idAttribute,
@@ -43,30 +43,35 @@ import { handleLinkClick } from "./link";
import { mergeRefs } from "@react-aria/utils";
import { composeEventHandlers } from "@radix-ui/primitive";
import { setDataCollapsed } from "~/canvas/collapsed";
+import { getIsVisuallyHidden } from "~/shared/visually-hidden";
const TextEditor = lazy(() => import("../text-editor"));
const ContentEditable = ({
- Component,
- elementRef,
- ...props
+ renderComponentWithRef,
}: {
- Component: AnyComponent;
- elementRef: ForwardedRef<HTMLElement>;
- [idAttribute]: Instance["id"];
- [componentAttribute]: Instance["component"];
+ renderComponentWithRef: (
+ elementRef: ForwardedRef<HTMLElement>
+ ) => JSX.Element;
}) => {
const [editor] = useLexicalComposerContext();
const ref = useRef<HTMLElement>(null);
- useEffect(() => {
+ /**
+ * useLayoutEffect to be sure that editor plugins on useEffect would have access to rootElement
+ */
+ useLayoutEffect(() => {
let rootElement = ref.current;
if (rootElement == null) {
return;
}
+ if (getIsVisuallyHidden(rootElement)) {
+ return;
+ }
+
if (rootElement?.tagName === "BUTTON") {
const span = document.createElement("span");
span.contentEditable = "true";
@@ -80,7 +85,7 @@ const ContentEditable = ({
editor.setRootElement(rootElement);
}, [editor]);
- return <Component ref={mergeRefs(ref, elementRef)} {...props} />;
+ return renderComponentWithRef(ref);
};
// this utility is temporary solution to compute instance selectors
@@ -176,6 +181,15 @@ export const WebstudioComponentDev = forwardRef<
const isPreviewMode = useStore(isPreviewModeStore);
+ /**
+ * Prevents edited element from having a size of 0 on the first render.
+ * Directly using `renderWebstudioComponentChildren(children)` in Text Edit
+ * conflicts with React due to lexical node changes.
+ */
+ const initialContentEditableContent = useRef(
+ renderWebstudioComponentChildren(children)
+ );
+
useCollapsedOnNewElement(instanceId);
// this assumes presence of `useStore(selectedInstanceSelectorStore)` above
@@ -275,6 +289,8 @@ export const WebstudioComponentDev = forwardRef<
areInstanceSelectorsEqual(textEditingInstanceSelector, instanceSelector) ===
false
) {
+ initialContentEditableContent.current =
+ renderWebstudioComponentChildren(children);
return instanceElement;
}
@@ -285,9 +301,11 @@ export const WebstudioComponentDev = forwardRef<
instances={instances}
contentEditable={
<ContentEditable
- {...componentProps}
- elementRef={ref}
- Component={Component}
+ renderComponentWithRef={(elementRef) => (
+ <Component {...componentProps} ref={mergeRefs(ref, elementRef)}>
+ {initialContentEditableContent.current}
+ </Component>
+ )}
/>
}
onChange={(instancesList) => {
diff --git a/apps/builder/app/canvas/shared/styles.ts b/apps/builder/app/canvas/shared/styles.ts
index c3d367e908..6fd4308967 100644
--- a/apps/builder/app/canvas/shared/styles.ts
+++ b/apps/builder/app/canvas/shared/styles.ts
@@ -66,15 +66,21 @@ const helperStyles = [
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.7);
}`,
// Has no width, will collapse
- `[${idAttribute}]:where(:not(body)[${collapsedAttribute}="w"]) {
+ // Ensure the TextEditor element doesn't uncollapse unintentionally:
+ // 1. Some TextEditor elements might appear empty for a few cycles after creation. (depends on existance of initial child)
+ // 2. If detected as empty, the collapsing algorithm could wrongly uncollapse them.
+ // 3. We prevent this by excluding elements with `data-lexical-editor`.
+ // 4. This rule is used here and not in collapsing detection because `data-lexical-editor` might not be set instantly.
+ // Mistakes in collapsing will be corrected in the next cycle.
+ `[${idAttribute}]:where(:not(body):not([data-lexical-editor])[${collapsedAttribute}="w"]) {
padding-right: 50px;
}`,
// Has no height, will collapse
- `[${idAttribute}]:where(:not(body)[${collapsedAttribute}="h"]) {
+ `[${idAttribute}]:where(:not(body):not([data-lexical-editor])[${collapsedAttribute}="h"]) {
padding-top: 50px;
}`,
// Has no width or height, will collapse
- `[${idAttribute}]:where(:not(body)[${collapsedAttribute}="wh"]) {
+ `[${idAttribute}]:where(:not(body):not([data-lexical-editor])[${collapsedAttribute}="wh"]) {
padding-right: 50px;
padding-top: 50px;
}`,