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.

Hide background layers (#1229)

Ivan Starkov01d66bb01519
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
index 38604b136f..f85c49dc30 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
@@ -15,7 +15,6 @@ import {
EyeconClosedIcon,
SubtractIcon,
} from "@webstudio-is/icons";
-import { useState } from "react";
import { PropertyName } from "../../shared/property-name";
import type { StyleInfo } from "../../shared/style-info";
import { ColorControl } from "../../controls/color/color-control";
@@ -39,7 +38,28 @@ const Layer = (props: {
deleteProperty: DeleteBackgroundProperty;
deleteLayer: () => void;
}) => {
- const [hidden, setHidden] = useState(false);
+ const backgrounImageStyle = props.layerStyle.backgroundImage?.value;
+ const isHidden =
+ backgrounImageStyle?.type === "image" ||
+ backgrounImageStyle?.type === "unparsed"
+ ? Boolean(backgrounImageStyle.hidden)
+ : false;
+
+ const handleHiddenChange = (hidden: boolean) => {
+ if (
+ backgrounImageStyle?.type === "image" ||
+ backgrounImageStyle?.type === "unparsed"
+ ) {
+ props.setProperty("backgroundImage")({
+ ...backgrounImageStyle,
+ hidden,
+ });
+ }
+ };
+
+ const canDisable =
+ backgrounImageStyle?.type !== "image" &&
+ backgrounImageStyle?.type !== "unparsed";
return (
<FloatingPanel
@@ -62,15 +82,16 @@ const Layer = (props: {
/>
}
thumbnail={<LayerThumbnail layerStyle={props.layerStyle} />}
- hidden={hidden}
+ hidden={isHidden}
buttons={
<>
<SmallToggleButton
- pressed={hidden}
- onPressedChange={setHidden}
+ disabled={canDisable}
+ pressed={isHidden}
+ onPressedChange={handleHiddenChange}
variant="normal"
tabIndex={0}
- icon={hidden ? <EyeconClosedIcon /> : <EyeconOpenIcon />}
+ icon={isHidden ? <EyeconClosedIcon /> : <EyeconOpenIcon />}
/>
<SmallIconButton
diff --git a/packages/css-data/src/schema.ts b/packages/css-data/src/schema.ts
index ba2733ab08..867b6d3cff 100644
--- a/packages/css-data/src/schema.ts
+++ b/packages/css-data/src/schema.ts
@@ -51,6 +51,8 @@ export type KeywordValue = z.infer<typeof KeywordValue>;
export const UnparsedValue = z.object({
type: z.literal("unparsed"),
value: z.string(),
+ // For the builder we want to be able to hide background-image
+ hidden: z.boolean().optional(),
});
const FontFamilyValue = z.object({
@@ -71,6 +73,8 @@ export type RgbValue = z.infer<typeof RgbValue>;
export const ImageValue = z.object({
type: z.literal("image"),
value: z.object({ type: z.literal("asset"), value: ImageAsset }),
+ // For the builder we want to be able to hide images
+ hidden: z.boolean().optional(),
});
export type ImageValue = z.infer<typeof ImageValue>;
diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.ts
index c4108c036c..ef40fb2a45 100644
--- a/packages/css-engine/src/core/to-value.ts
+++ b/packages/css-engine/src/core/to-value.ts
@@ -16,7 +16,8 @@ const assertUnreachable = (_arg: never, errorMessage: string) => {
export const toValue = (
value?: StyleValue,
- options: ToCssOptions = defaultOptions
+ options: ToCssOptions = defaultOptions,
+ isEditMode?: boolean
): string => {
if (value === undefined) {
return "";
@@ -36,9 +37,11 @@ export const toValue = (
return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");
}
if (value.type === "var") {
+ // We use var() in edit mode only
+ const isEditMode = true;
const fallbacks = [];
for (const fallback of value.fallbacks) {
- fallbacks.push(toValue(fallback, options));
+ fallbacks.push(toValue(fallback, options, isEditMode));
}
const fallbacksString =
fallbacks.length > 0 ? `, ${fallbacks.join(", ")}` : "";
@@ -62,20 +65,38 @@ export const toValue = (
}
if (value.type === "image") {
+ if (isEditMode && value.hidden) {
+ // We assume that property is background-image and use this to hide background layers
+ // In the future we might want to have a more generic way to hide values
+ // i.e. have knowledge about property-name, as none is property specific
+ return "none";
+ }
+
// @todo image-set
return `url(${value.value.value.path}) /* id=${value.value.value.id} */`;
}
if (value.type === "unparsed") {
+ if (isEditMode && value.hidden) {
+ // We assume that property is background-image and use this to hide background layers
+ // In the future we might want to have a more generic way to hide values
+ // i.e. have knowledge about property-name, as none is property specific
+ return "none";
+ }
+
return value.value;
}
if (value.type === "layers") {
- return value.value.map((value) => toValue(value, options)).join(",");
+ return value.value
+ .map((value) => toValue(value, options, isEditMode))
+ .join(",");
}
if (value.type === "tuple") {
- return value.value.map((value) => toValue(value, options)).join(" ");
+ return value.value
+ .map((value) => toValue(value, options, isEditMode))
+ .join(" ");
}
// Will give ts error in case of missing type
diff --git a/packages/project-build/src/schema/styles.ts b/packages/project-build/src/schema/styles.ts
index 27cb7159d8..a5b551752e 100644
--- a/packages/project-build/src/schema/styles.ts
+++ b/packages/project-build/src/schema/styles.ts
@@ -14,6 +14,9 @@ import {
const StoredImageValue = z.object({
type: z.literal("image"),
value: z.object({ type: z.literal("asset"), value: z.string() }),
+
+ // For the builder we want to be able to hide images
+ hidden: z.boolean().optional(),
});
const StoredLayersValue = z.object({