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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
ensure focus from wrapper component #8
Oleg Isonen8e8372067ffb
diff --git a/app/canvas/features/wrapper-component/use-ensure-focus.ts b/app/canvas/features/wrapper-component/use-ensure-focus.tsnew file mode 100644index 0000000000..a6260b7c75--- /dev/null+++ b/app/canvas/features/wrapper-component/use-ensure-focus.ts@@ -0,0 +1,21 @@+import { useCallback } from "react";+import { useSelectedInstance } from "~/canvas/shared/nano-values";++/**+ * When instance is created or selected via setInstance, we need to focus it.+ */+export const useEnsureFocus = () => {+ const [selectedInstance] = useSelectedInstance();+ return useCallback(+ (element: HTMLElement | null) => {+ if (+ element !== null &&+ element.id === selectedInstance?.id &&+ document.activeElement !== element+ ) {+ element.focus();+ }+ },+ [selectedInstance]+ );+};diff --git a/app/canvas/features/wrapper-component/wrapper-component.tsx b/app/canvas/features/wrapper-component/wrapper-component.tsxindex 492d65eddb..38c3d9bebf 100644--- a/app/canvas/features/wrapper-component/wrapper-component.tsx+++ b/app/canvas/features/wrapper-component/wrapper-component.tsx@@ -10,6 +10,7 @@ import { primitives } from "~/shared/component";import { useContentEditable } from "./editable";import { useCss } from "./use-css";import { useDraggable } from "./use-draggable";+import { useEnsureFocus } from "./use-ensure-focus";type WrapperComponentDevProps = {instance: Instance;@@ -43,13 +44,16 @@ export const WrapperComponentDev = ({isDisabled: isContentEditableDisabled === false,});+ const focusRefCallback = useEnsureFocus();+const refCallback = useCallback((element) => {contentEditableRef.current = element;// We can't drag if we are editing text.dragRefCallback(element);+ focusRefCallback(element);},- [contentEditableRef, dragRefCallback]+ [contentEditableRef, dragRefCallback, focusRefCallback]);const userProps = useUserProps(instance.id);diff --git a/app/canvas/shared/active-element.ts b/app/canvas/shared/active-element.tsindex f87d1c34a0..87d98416a9 100644--- a/app/canvas/shared/active-element.ts+++ b/app/canvas/shared/active-element.ts@@ -23,18 +23,6 @@ export const useActiveElementTracking = () => {select(element);});- useEffect(() => {- // Alredy focused over DOM event.- if (- selectedInstance === undefined ||- selectedElement?.id === selectedInstance.id- ) {- return;- }- // Instance was added and we want to auto focus it now.- document.getElementById(selectedInstance.id)?.focus();- }, [selectedInstance, selectedElement]);-const select = useCallback((element: Element | null) => {if (element === null || rootInstance === undefined) return;