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 ending contenteditable mode layout recalc (#1931)

Oleg Isonenfde3c2c3126f
diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx
index 22a6e9a5e6..f511fac711 100644
--- a/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx
@@ -11,15 +11,8 @@ import {
Flex,
} from "@webstudio-is/design-system";
import { ChevronDownIcon } from "@webstudio-is/icons";
-import {
- useEffect,
- useLayoutEffect,
- useRef,
- type KeyboardEvent,
- type KeyboardEventHandler,
- type FocusEvent,
- type ReactNode,
-} from "react";
+import { useLayoutEffect, useRef, type ReactNode } from "react";
+import { useContentEditable } from "~/shared/dom-hooks";
const menuTriggerVisibilityVar = cssVars.define("menu-trigger-visibility");
const menuTriggerVisibilityOverrideVar = cssVars.define(
@@ -109,79 +102,6 @@ const Menu = (props: MenuProps) => {
export type ItemSource = "token" | "tag" | "local";
-const useEditableText = ({
- isEditable,
- isEditing,
- onChangeEditing,
- onChangeValue,
-}: {
- isEditable: boolean;
- isEditing: boolean;
- onChangeEditing: (isEditing: boolean) => void;
- onChangeValue: (value: string) => void;
-}) => {
- const elementRef = useRef<HTMLDivElement>(null);
- const lastValueRef = useRef<string>("");
- const getValue = () => elementRef.current?.textContent ?? "";
-
- useEffect(() => {
- const element = elementRef.current;
- if (element === null) {
- return;
- }
-
- if (isEditing) {
- element.setAttribute("contenteditable", "plaintext-only");
- // the next frame is necessary when newly created element
- // need to get focus, for example after duplicate operation
- requestAnimationFrame(() => {
- element.focus();
- getSelection()?.selectAllChildren(element);
- lastValueRef.current = getValue();
- });
- return;
- }
-
- element.removeAttribute("contenteditable");
- }, [isEditing]);
-
- const handleFinishEditing = (
- event: KeyboardEvent<Element> | FocusEvent<Element>
- ) => {
- event.preventDefault();
- if (isEditing) {
- onChangeEditing(false);
- }
- onChangeValue(getValue());
- lastValueRef.current = "";
- };
-
- const handleKeyDown: KeyboardEventHandler = (event) => {
- if (event.key === "Enter") {
- handleFinishEditing(event);
- return;
- }
- if (event.key === "Escape" && elementRef.current !== null) {
- elementRef.current.textContent = lastValueRef.current;
- handleFinishEditing(event);
- }
- };
-
- const handleDoubleClick = () => {
- if (isEditable) {
- onChangeEditing(true);
- }
- };
-
- const handlers = {
- onKeyDown: handleKeyDown,
- onBlur: handleFinishEditing,
- onDoubleClick: handleDoubleClick,
- };
-
- return { ref: elementRef, handlers };
-};
-
type EditableTextProps = {
label: string;
isEditable: boolean;
@@ -197,7 +117,7 @@ const EditableText = ({
onChangeEditing,
onChangeValue,
}: EditableTextProps) => {
- const { ref, handlers } = useEditableText({
+ const { ref, handlers } = useContentEditable({
isEditable,
isEditing,
onChangeEditing,
diff --git a/apps/builder/app/shared/dom-hooks/index.ts b/apps/builder/app/shared/dom-hooks/index.ts
index ac94eeeefa..2b9bf73f5c 100644
--- a/apps/builder/app/shared/dom-hooks/index.ts
+++ b/apps/builder/app/shared/dom-hooks/index.ts
@@ -1 +1,2 @@
export * from "./use-window-resize";
+export * from "./use-content-editable";
diff --git a/apps/builder/app/shared/dom-hooks/use-content-editable.ts b/apps/builder/app/shared/dom-hooks/use-content-editable.ts
new file mode 100644
index 0000000000..9de0878ab7
--- /dev/null
+++ b/apps/builder/app/shared/dom-hooks/use-content-editable.ts
@@ -0,0 +1,92 @@
+import {
+ useRef,
+ type FocusEvent,
+ type KeyboardEvent,
+ type KeyboardEventHandler,
+ useEffect,
+} from "react";
+
+export const useContentEditable = ({
+ isEditable,
+ isEditing,
+ onChangeEditing,
+ onChangeValue,
+}: {
+ isEditable: boolean;
+ isEditing: boolean;
+ onChangeEditing: (isEditing: boolean) => void;
+ onChangeValue: (value: string) => void;
+}) => {
+ const elementRef = useRef<HTMLDivElement>(null);
+ const lastValueRef = useRef<string>("");
+ const getValue = () => elementRef.current?.textContent ?? "";
+
+ useEffect(() => {
+ const element = elementRef.current;
+ if (element === null) {
+ return;
+ }
+
+ // Nothing changed, do nothing
+ if (element.hasAttribute("contenteditable") === isEditing) {
+ return;
+ }
+
+ if (isEditing) {
+ element.setAttribute("contenteditable", "plaintext-only");
+ // the next frame is necessary when newly created element
+ // need to get focus, for example after duplicate operation
+ requestAnimationFrame(() => {
+ element.focus();
+ getSelection()?.selectAllChildren(element);
+ lastValueRef.current = getValue();
+ });
+ return;
+ }
+
+ element.removeAttribute("contenteditable");
+ // This is needed to force layout to recalc max-width when editing is done,
+ // because otherwise, layout will keep the value from before engaging contenteditable.
+ const { parentElement } = element;
+ if (parentElement) {
+ parentElement.removeChild(element);
+ parentElement.appendChild(element);
+ }
+ }, [isEditing]);
+
+ const handleFinishEditing = (
+ event: KeyboardEvent<Element> | FocusEvent<Element>
+ ) => {
+ event.preventDefault();
+ if (isEditing) {
+ onChangeEditing(false);
+ }
+ onChangeValue(getValue());
+ lastValueRef.current = "";
+ };
+
+ const handleKeyDown: KeyboardEventHandler = (event) => {
+ if (event.key === "Enter") {
+ handleFinishEditing(event);
+ return;
+ }
+ if (event.key === "Escape" && elementRef.current !== null) {
+ elementRef.current.textContent = lastValueRef.current;
+ handleFinishEditing(event);
+ }
+ };
+
+ const handleDoubleClick = () => {
+ if (isEditable) {
+ onChangeEditing(true);
+ }
+ };
+
+ const handlers = {
+ onKeyDown: handleKeyDown,
+ onBlur: handleFinishEditing,
+ onDoubleClick: handleDoubleClick,
+ };
+
+ return { ref: elementRef, handlers };
+};