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
chore: Advanced properties performance (chrome only) (#4238)
Ivan Starkovdfb1d458028e
diff --git a/@types/content.d.ts b/@types/content.d.tsnew file mode 100644index 0000000000..48cb9267b6--- /dev/null+++ b/@types/content.d.ts@@ -0,0 +1,9 @@+// CSS Containment+// Specification: https://drafts.csswg.org/css-contain-2/+// Repository: https://github.com/w3c/csswg-drafts/tree/main/css-contain-2++declare var oncontentvisibilityautostatechange: ContentVisibilityAutoStateChangeEvent | null;++interface GlobalEventHandlersEventMap {+ contentvisibilityautostatechange: ContentVisibilityAutoStateChangeEvent;+}diff --git a/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx b/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsxindex a12ea5dc8e..70331256f3 100644--- a/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx@@ -1,5 +1,12 @@import { colord } from "colord";-import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";+import {+ memo,+ useEffect,+ useMemo,+ useRef,+ useState,+ type ReactNode,+} from "react";import { useStore } from "@nanostores/react";import { computed } from "nanostores";import { matchSorter } from "match-sorter";@@ -50,6 +57,7 @@ import {$styles,$styleSourceSelections,} from "~/shared/nano-states";+import { useClientSupports } from "~/shared/client-supports";// Only here to keep the same section module interfaceexport const properties = [];@@ -328,6 +336,81 @@ const $advancedProperties = computed(});+/**+ * The Advanced section in the Style Panel on </> Global Root has performance issues.+ * To fix this, we skip rendering properties not visible in the viewport using the contentvisibilityautostatechange event,+ * and the contentVisibility and containIntrinsicSize CSS properties.+ */+const AdvancedProperty = memo(+ ({+ property,+ autoFocus,+ }: {+ property: StyleProperty;+ autoFocus: boolean;+ }) => {+ const visibilityChangeEventSupported = useClientSupports(+ () => "oncontentvisibilityautostatechange" in document.body+ );+ const ref = useRef<HTMLDivElement>(null);+ const [isVisible, setIsVisible] = useState(!visibilityChangeEventSupported);++ useEffect(() => {+ if (!visibilityChangeEventSupported) {+ return;+ }++ if (ref.current == null) {+ return;+ }++ const controller = new AbortController();++ ref.current.addEventListener(+ "contentvisibilityautostatechange",+ (event) => {+ setIsVisible(!event.skipped);+ },+ {+ signal: controller.signal,+ }+ );++ return () => {+ controller.abort();+ };+ }, [visibilityChangeEventSupported]);++ return (+ <Flex+ ref={ref}+ css={{+ contentVisibility: "auto",+ // https://developer.mozilla.org/en-US/docs/Web/CSS/contain-intrinsic-size+ // containIntrinsicSize is used to set the default size of an element before any content is loaded.+ // This helps in preventing layout shifts and provides a better user experience by maintaining a consistent layout.+ // It also affects the contentvisibilityautostatechange event to be called properly,+ // with "auto" it will call it with skipped false for all initial elements.+ // 44px is the height of the property row with 2 lines of text. This value can be adjusted slightly.+ containIntrinsicSize: "auto 44px",+ }}+ key={property}+ wrap="wrap"+ align="center"+ justify="start"+ >+ {isVisible && (+ <>+ <AdvancedPropertyLabel property={property} />+ <Text>:</Text>+ <AdvancedPropertyValue autoFocus={autoFocus} property={property} />+ </>+ )}+ </Flex>+ );+ }+);+export const Section = () => {const [isAdding, setIsAdding] = useState(false);const advancedProperties = useStore($advancedProperties);@@ -354,14 +437,11 @@ export const Section = () => {)}<Box>{advancedProperties.map((property) => (- <Flex key={property} wrap="wrap" align="center" justify="start">- <AdvancedPropertyLabel property={property} />- <Text>:</Text>- <AdvancedPropertyValue- autoFocus={newlyAddedProperty.current === property}- property={property}- />- </Flex>+ <AdvancedProperty+ key={property}+ property={property}+ autoFocus={newlyAddedProperty.current === property}+ />))}</Box></AdvancedStyleSection>diff --git a/apps/builder/app/shared/client-supports.ts b/apps/builder/app/shared/client-supports.tsnew file mode 100644index 0000000000..a8232e54b0--- /dev/null+++ b/apps/builder/app/shared/client-supports.ts@@ -0,0 +1,12 @@+import { useSyncExternalStore } from "react";++const subscribe = () => () => {};++// https://react.dev/reference/react/useSyncExternalStore#subscribing-to-a-browser-api+// Get a value from the browser API on the first (non hydration) render (without using useEffect or similar methods)+// Ensures the value is consistent between server and client (hydration)+export const useClientSupports = (isSupported: () => boolean) => {+ const result = useSyncExternalStore(subscribe, isSupported, () => false);++ return result;+};