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
Fix selection on visually hidden elements (#1996)
Ivan Starkova190deb48b5b
diff --git a/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.ts b/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.tsindex d4570e4024..16810ee3f1 100644--- a/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.ts+++ b/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.ts@@ -19,6 +19,7 @@ import { selectedInstanceOutlineStore } from "~/shared/nano-states";import type { UnitSizes } from "~/builder/features/style-panel/shared/css-value-input/convert-units";import { setDataCollapsed } from "~/canvas/collapsed";import { type InstanceSelector } from "~/shared/tree-utils";+import { getIsVisuallyHidden } from "./visually-hidden";const isHtmlTag = (tag: string): tag is HtmlTags =>htmlTags.includes(tag as HtmlTags);@@ -92,6 +93,11 @@ export const SelectedInstanceConnector = ({if (element === null) {return;}++ if (getIsVisuallyHidden(element)) {+ return;+ }+const showOutline = () => {if (isResizingCanvasStore.get()) {return;diff --git a/apps/builder/app/canvas/features/webstudio-component/visually-hidden.ts b/apps/builder/app/canvas/features/webstudio-component/visually-hidden.tsnew file mode 100644index 0000000000..161469cb68--- /dev/null+++ b/apps/builder/app/canvas/features/webstudio-component/visually-hidden.ts@@ -0,0 +1,42 @@+import { useState, type RefObject, useEffect } from "react";++export const getIsVisuallyHidden = (currentElement: HTMLElement) => {+ for (+ let element: HTMLElement | null = currentElement;+ element !== null;+ element = element.parentElement+ ) {+ if (+ element.style.overflow === "hidden" &&+ element.style.clip === "rect(0px, 0px, 0px, 0px)" &&+ element.style.position === "absolute" &&+ element.style.width === "1px" &&+ element.style.height === "1px"+ ) {+ return true;+ }+ }+ return false;+};++/**+ * Radix's VisuallyHiddenPrimitive.Root https://github.com/radix-ui/primitives/blob/main/packages/react/visually-hidden/src/VisuallyHidden.tsx+ * component makes content from hidden elements accessible to screen readers.+ * react-aria VisuallyHidden https://github.com/adobe/react-spectrum/blob/e4bc3269fa41aa096700445c6bfa9c8620545e6a/packages/%40react-aria/visually-hidden/src/VisuallyHidden.tsx#L32-L43+ * The problem we're addressing is that the Radix and similar frameworks reuse the same Content children+ * for VisuallyHiddenPrimitive.Root within the Tooltip and similar components.+ * Using the same Content children, however, leads to duplicated React elements, breaking our 'isSelected' logic.+ * To prevent this, we check if an instance is a descendant of VisuallyHiddenPrimitive.Root, and if so,+ * we avoid rendering it.+ */+export const useIsVisuallyHidden = (ref: RefObject<HTMLElement>) => {+ const [isVisuallyHidden, setIsVisuallyHidden] = useState(false);++ useEffect(() => {+ if (ref.current !== null && getIsVisuallyHidden(ref.current)) {+ setIsVisuallyHidden(true);+ }+ }, [ref]);++ return isVisuallyHidden;+};diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsxindex 67a44bd587..7e1b891a04 100644--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx@@ -1,11 +1,4 @@-import {- type MouseEvent,- type FormEvent,- useEffect,- forwardRef,- type RefObject,- useState,-} from "react";+import { type MouseEvent, type FormEvent, useEffect, forwardRef } from "react";import { Suspense, lazy, useCallback, useRef } from "react";import { useStore } from "@nanostores/react";import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";@@ -44,6 +37,7 @@ import { handleLinkClick } from "./link";import { mergeRefs } from "@react-aria/utils";import { composeEventHandlers } from "@radix-ui/primitive";import { setDataCollapsed } from "~/canvas/collapsed";+import { useIsVisuallyHidden } from "./visually-hidden";const TextEditor = lazy(() => import("../text-editor"));@@ -119,43 +113,6 @@ type WebstudioComponentDevProps = {components: Components;};-/**- * Radix's VisuallyHiddenPrimitive.Root https://github.com/radix-ui/primitives/blob/main/packages/react/visually-hidden/src/VisuallyHidden.tsx- * component makes content from hidden elements accessible to screen readers.- * react-aria VisuallyHidden https://github.com/adobe/react-spectrum/blob/e4bc3269fa41aa096700445c6bfa9c8620545e6a/packages/%40react-aria/visually-hidden/src/VisuallyHidden.tsx#L32-L43- * The problem we're addressing is that the Radix reuses the same Content children for VisuallyHiddenPrimitive.Root within the Tooltip component.- * Using the same Content children, however, leads to duplicated React elements, breaking our 'isSelected' logic.- * To prevent this, we check if an instance is a descendant of VisuallyHiddenPrimitive.Root, and if so,- * we avoid rendering it.- */-const useIsVisuallyHidden = (ref: RefObject<HTMLElement>) => {- const [isScreenReaderDescendant, setIsScreenReaderDescendant] =- useState(false);-- useEffect(() => {- if (ref.current !== null) {- for (- let element: HTMLElement | null = ref.current;- element !== null;- element = element.parentElement- ) {- if (- element.style.overflow === "hidden" &&- element.style.clip === "rect(0px, 0px, 0px, 0px)" &&- element.style.position === "absolute" &&- element.style.width === "1px" &&- element.style.height === "1px"- ) {- setIsScreenReaderDescendant(true);- return;- }- }- }- }, [ref]);-- return isScreenReaderDescendant;-};-/*** For some components that are wrapped with Radix Slot-like components (Triggers etc where asChild=true),* Slots in react-aria https://react-spectrum.adobe.com/react-spectrum/layout.html#slots