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 focus after style source duplicating (#1805)

Bogdan Chadkin87ea4a40c350
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 5187621a01..22a6e9a5e6 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
@@ -125,19 +125,24 @@ const useEditableText = ({
const getValue = () => elementRef.current?.textContent ?? "";
useEffect(() => {
- if (elementRef.current === null) {
+ const element = elementRef.current;
+ if (element === null) {
return;
}
if (isEditing) {
- elementRef.current.setAttribute("contenteditable", "plaintext-only");
- elementRef.current.focus();
- getSelection()?.selectAllChildren(elementRef.current);
- lastValueRef.current = getValue();
+ 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;
}
- elementRef.current?.removeAttribute("contenteditable");
+ element.removeAttribute("contenteditable");
}, [isEditing]);
const handleFinishEditing = (