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.

feat: Optimize image loading to prevent layout shifts (#2414)

Ivan Starkov86361756df2a
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx
index fcb66a8e01..322b045413 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx
@@ -33,6 +33,7 @@ import {
} from "./use-props-logic";
import { Row, getLabel } from "../shared";
import { serverSyncStore } from "~/shared/sync";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
const itemToString = (item: NameAndLabel | null) =>
item ? getLabel(item, item.name) : "";
@@ -119,6 +120,24 @@ const renderProperty = (
"width" in asset.meta &&
"height" in asset.meta
) {
+ if (isFeatureEnabled("image")) {
+ logic.handleChangeByPropName("width", {
+ value: asset.meta.width,
+ type: "number",
+ });
+ logic.handleChangeByPropName("height", {
+ value: asset.meta.height,
+ type: "number",
+ });
+
+ setCssProperty("height")({
+ type: "keyword",
+ value: "fit-content",
+ });
+
+ return;
+ }
+
setCssProperty("aspectRatio")({
type: "unit",
unit: "number",
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
index 4d8d9fae5e..d0b162c13a 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
+++ b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
@@ -1,11 +1,7 @@
import { useState } from "react";
import { nanoid } from "nanoid";
import type { Instance, Prop } from "@webstudio-is/sdk";
-import {
- type PropMeta,
- type WsComponentPropsMeta,
- showAttribute,
-} from "@webstudio-is/react-sdk";
+import { type PropMeta, showAttribute } from "@webstudio-is/react-sdk";
import type { PropValue } from "../shared";
import { useStore } from "@nanostores/react";
import {
@@ -109,24 +105,6 @@ type UsePropsLogicInput = {
deleteProp: (id: Prop["id"]) => void;
};
-type UsePropsLogicOutput = {
- meta: WsComponentPropsMeta;
- /** Similar to Initial, but displayed as a separate group in UI etc.
- * Currentrly used only for the ID prop. */
- systemProps: PropAndMeta[];
- /** Initial (not deletable) props */
- initialProps: PropAndMeta[];
- /** Optional props that were added by user */
- addedProps: PropAndMeta[];
- /** List of remaining props still available to add */
- availableProps: NameAndLabel[];
- handleAdd: (propName: string) => void;
- handleChange: (prop: PropOrName, value: PropValue) => void;
- handleDelete: (prop: PropOrName) => void;
- /** Delete the prop, but keep it in the list of added props */
- handleSoftDelete: (prop: Prop) => void;
-};
-
const getAndDelete = <Value>(map: Map<string, Value>, key: string) => {
const value = map.get(key);
map.delete(key);
@@ -168,7 +146,7 @@ export const usePropsLogic = ({
props,
updateProp,
deleteProp,
-}: UsePropsLogicInput): UsePropsLogicOutput => {
+}: UsePropsLogicInput) => {
const meta = useStore(registeredComponentPropsMetasStore).get(
instance.component
);
@@ -298,6 +276,16 @@ export const usePropsLogic = ({
);
};
+ const handleChangeByPropName = (propName: string, value: PropValue) => {
+ const prop = props.find((prop) => prop.name === propName);
+
+ updateProp(
+ prop === undefined
+ ? { id: nanoid(), instanceId: instance.id, name: propName, ...value }
+ : { ...prop, ...value }
+ );
+ };
+
const handleDelete = ({ prop, propName }: PropOrName) => {
if (prop) {
deleteProp(prop.id);
@@ -310,17 +298,24 @@ export const usePropsLogic = ({
};
return {
+ handleAdd,
+ handleChange,
+ handleDelete,
+ /** Delete the prop, but keep it in the list of added props */
+ handleSoftDelete,
+ handleChangeByPropName,
meta,
+ /** Similar to Initial, but displayed as a separate group in UI etc.
+ * Currentrly used only for the ID prop. */
systemProps,
+ /** Initial (not deletable) props */
initialProps,
+ /** Optional props that were added by user */
addedProps,
+ /** List of remaining props still available to add */
availableProps: Array.from(
unprocessedKnown.entries(),
([name, { label }]) => ({ name, label })
),
- handleAdd,
- handleChange,
- handleDelete,
- handleSoftDelete,
};
};
diff --git a/apps/builder/app/builder/features/settings-panel/shared.tsx b/apps/builder/app/builder/features/settings-panel/shared.tsx
index acaca5f6d2..92023edac3 100644
--- a/apps/builder/app/builder/features/settings-panel/shared.tsx
+++ b/apps/builder/app/builder/features/settings-panel/shared.tsx
@@ -119,6 +119,7 @@ export const useLocalValue = <Type,>(
savedValue: Type,
onSave: (value: Type) => void
) => {
+ const isEditingRef = useRef(false);
const localValueRef = useRef(savedValue);
const [_, setRefresh] = useState(0);
@@ -127,6 +128,7 @@ export const useLocalValue = <Type,>(
onSaveRef.current = onSave;
const save = () => {
+ isEditingRef.current = false;
if (equal(localValueRef.current, savedValue) === false) {
// To synchronize with setState immediately followed by save
onSaveRef.current(localValueRef.current);
@@ -134,6 +136,7 @@ export const useLocalValue = <Type,>(
};
const setLocalValue = (value: Type) => {
+ isEditingRef.current = true;
localValueRef.current = value;
setRefresh((refresh) => refresh + 1);
};
@@ -143,6 +146,17 @@ export const useLocalValue = <Type,>(
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => save, []);
+ useEffect(() => {
+ // Update local value if saved value changes and control is not in edit mode.
+ if (
+ isEditingRef.current === false &&
+ localValueRef.current !== savedValue
+ ) {
+ localValueRef.current = savedValue;
+ setRefresh((refresh) => refresh + 1);
+ }
+ }, [savedValue]);
+
return {
/**
* Contains:
diff --git a/packages/feature-flags/src/flags.ts b/packages/feature-flags/src/flags.ts
index 593b62ed14..097f54e3e0 100644
--- a/packages/feature-flags/src/flags.ts
+++ b/packages/feature-flags/src/flags.ts
@@ -7,3 +7,5 @@ export const adminRole = false;
// A general flag to enable/disable all the AI features.
export const ai = process.env.NODE_ENV !== "production";
export const aiCopy = ai && process.env.NODE_ENV !== "production";
+// New image optimisations
+export const image = false;