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.

experimental: support css variables in shadows (#4283)

Bogdan Chadkin3e056f913018
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
index 4e5e43046d..3ab4eb7552 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
@@ -139,6 +139,7 @@ export const BackgroundGradient = ({ index }: { index: number }) => {
</Label>
<CssFragmentEditor
invalid={intermediateValue?.type === "invalid"}
+ autoFocus={styleValue.type === "var"}
value={textAreaValue ?? ""}
onChange={handleChange}
onBlur={handleOnComplete}
diff --git a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
index 06ed6a53b6..c357ec0fc9 100644
--- a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
@@ -10,6 +10,7 @@ import { useComputedStyleDecl } from "../../shared/model";
import {
addRepeatedStyleItem,
editRepeatedStyleItem,
+ getComputedRepeatedItem,
RepeatedStyle,
} from "../../shared/repeated-style";
import { parseCssFragment } from "../../shared/css-fragment";
@@ -23,12 +24,21 @@ const property: StyleProperty = properties[0];
const label = "Box Shadows";
const initialBoxShadow = "0px 2px 5px 0px rgba(0, 0, 0, 0.2)";
-const getItemProps = (_index: number, layer: StyleValue) => {
- const values = layer.type === "tuple" ? layer.value : [];
+const getItemProps = (layer: StyleValue, computedLayer?: StyleValue) => {
+ let values: StyleValue[] = [];
+ if (layer.type === "tuple") {
+ values = layer.value;
+ }
+ if (layer.type === "var" && computedLayer?.type === "tuple") {
+ values = computedLayer.value;
+ }
const labels = [];
let color: RgbaColor | undefined;
let isInset = false;
+ if (layer.type === "var") {
+ labels.push(`--${layer.value}`);
+ }
for (const item of values) {
if (item.type === "rgb") {
color = colord(toValue(item)).toRgb();
@@ -44,7 +54,9 @@ const getItemProps = (_index: number, layer: StyleValue) => {
continue;
}
}
- labels.push(toValue(item));
+ if (layer.type !== "var") {
+ labels.push(toValue(item));
+ }
}
if (isInset) {
@@ -74,11 +86,14 @@ export const Section = () => {
<RepeatedStyle
label={label}
styles={[styleDecl]}
- getItemProps={getItemProps}
+ getItemProps={(index, layer) =>
+ getItemProps(layer, getComputedRepeatedItem(styleDecl, index))
+ }
renderItemContent={(index, value) => (
<ShadowContent
index={index}
layer={value}
+ computedLayer={getComputedRepeatedItem(styleDecl, index)}
property={property}
propertyValue={toValue(value)}
onEditLayer={(index, value, options) => {
diff --git a/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx b/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx
index 7323aa1851..75c4db8a52 100644
--- a/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx
@@ -9,6 +9,7 @@ import { ShadowContent } from "../../shared/shadow-content";
import {
addRepeatedStyleItem,
editRepeatedStyleItem,
+ getComputedRepeatedItem,
RepeatedStyle,
} from "../../shared/repeated-style";
import { parseCssFragment } from "../../shared/css-fragment";
@@ -23,11 +24,20 @@ const property: StyleProperty = properties[0];
const label = "Text Shadows";
const initialTextShadow = "0px 2px 5px rgba(0, 0, 0, 0.2)";
-const getItemProps = (_index: number, layer: StyleValue) => {
- const values = layer.type === "tuple" ? layer.value : [];
+const getItemProps = (layer: StyleValue, computedLayer?: StyleValue) => {
+ let values: StyleValue[] = [];
+ if (layer.type === "tuple") {
+ values = layer.value;
+ }
+ if (layer.type === "var" && computedLayer?.type === "tuple") {
+ values = computedLayer.value;
+ }
const labels = ["Text Shadow:"];
let color: RgbaColor | undefined;
+ if (layer.type === "var") {
+ labels.push(`--${layer.value}`);
+ }
for (const item of values) {
if (item.type === "rgb") {
color = colord(toValue(item)).toRgb();
@@ -37,7 +47,9 @@ const getItemProps = (_index: number, layer: StyleValue) => {
color = colord(item.value).toRgb();
continue;
}
- labels.push(toValue(item));
+ if (layer.type !== "var") {
+ labels.push(toValue(item));
+ }
}
return { label: labels.join(" "), color };
@@ -61,11 +73,14 @@ export const Section = () => {
<RepeatedStyle
label={label}
styles={[styleDecl]}
- getItemProps={getItemProps}
+ getItemProps={(index, layer) =>
+ getItemProps(layer, getComputedRepeatedItem(styleDecl, index))
+ }
renderItemContent={(index, value) => (
<ShadowContent
index={index}
layer={value}
+ computedLayer={getComputedRepeatedItem(styleDecl, index)}
property={property}
propertyValue={toValue(value)}
onEditLayer={(index, value, options) => {
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsx b/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsx
index 03c6c825db..887b2096ef 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsx
@@ -44,7 +44,7 @@ const compareVariables = (left: RankingInfo, right: RankingInfo) => {
};
const scopeCompletionSource: CompletionSource = (context) => {
- const word = context.matchBefore(/[-\w]+/);
+ const word = context.matchBefore(/[-\w()]+/);
if (word === null || (word.from === word.to && false === context.explicit)) {
return null;
}
@@ -60,6 +60,7 @@ const scopeCompletionSource: CompletionSource = (context) => {
});
return {
from: word.from,
+ to: word.to,
filter: false,
options: matches,
};
@@ -75,6 +76,7 @@ export const CssFragmentEditor = ({
...props
}: {
invalid?: boolean;
+ autoFocus?: boolean;
value: string;
onChange: (newValue: string) => void;
onBlur?: (event: FocusEvent) => void;
diff --git a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx
index 95b8206fbb..18387cef41 100644
--- a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx
@@ -8,6 +8,7 @@ import {
type StyleValue,
type TupleValue,
type UnitValue,
+ type VarValue,
} from "@webstudio-is/css-engine";
import {
extractShadowProperties,
@@ -20,8 +21,6 @@ import {
Label,
Separator,
Text,
- TextArea,
- textVariants,
theme,
ToggleGroup,
ToggleGroupButton,
@@ -36,7 +35,7 @@ import type { IntermediateStyleValue } from "../shared/css-value-input";
import { CssValueInputContainer } from "../shared/css-value-input";
import { toPascalCase } from "../shared/keyword-utils";
import type { StyleUpdateOptions } from "../shared/use-style-data";
-import { parseCssFragment } from "./css-fragment";
+import { CssFragmentEditor, parseCssFragment } from "./css-fragment";
import { PropertyInlineLabel } from "../property-label";
import { styleConfigByName } from "./configs";
import { ColorPicker } from "./color-picker";
@@ -69,10 +68,11 @@ type ShadowContentProps = {
index: number;
property: "boxShadow" | "textShadow" | "dropShadow";
layer: StyleValue;
+ computedLayer?: StyleValue;
propertyValue: string;
onEditLayer: (
index: number,
- layers: LayersValue,
+ layers: LayersValue | VarValue,
options: StyleUpdateOptions
) => void;
hideCodeEditor?: boolean;
@@ -120,6 +120,7 @@ const shadowPropertySyntaxes = {
export const ShadowContent = ({
layer,
+ computedLayer,
index,
property,
propertyValue,
@@ -133,10 +134,15 @@ export const ShadowContent = ({
setIntermediateValue({ type: "intermediate", value: propertyValue });
}, [propertyValue]);
const layerValues = useMemo<ExtractedShadowProperties>(() => {
- return extractShadowProperties(
- layer.type === "tuple" ? layer : { type: "tuple", value: [] }
- );
- }, [layer]);
+ let value: TupleValue = { type: "tuple", value: [] };
+ if (layer.type === "tuple") {
+ value = layer;
+ }
+ if (layer.type === "var" && computedLayer?.type === "tuple") {
+ value = computedLayer;
+ }
+ return extractShadowProperties(value);
+ }, [layer, computedLayer]);
const { offsetX, offsetY, blur, spread, color, inset } = layerValues;
const colorControlProp = color ?? {
@@ -169,7 +175,7 @@ export const ShadowContent = ({
const parsedValue = parsed.get(
property === "dropShadow" ? "textShadow" : property
);
- if (parsedValue?.type === "layers") {
+ if (parsedValue?.type === "layers" || parsedValue?.type === "var") {
onEditLayer(index, parsedValue, { isEphemeral: false });
return;
}
@@ -213,6 +219,7 @@ export const ShadowContent = ({
// outline-offset is a fake property for validating box-shadow's offsetX.
property="outlineOffset"
styleSource="local"
+ disabled={layer.type === "var"}
value={offsetX ?? { type: "unit", value: 0, unit: "px" }}
setValue={(value, options) =>
handlePropertyChange({ offsetX: value }, options)
@@ -236,6 +243,7 @@ export const ShadowContent = ({
// outline-offset is a fake property for validating box-shadow's offsetY.
property="outlineOffset"
styleSource="local"
+ disabled={layer.type === "var"}
value={offsetY ?? { type: "unit", value: 0, unit: "px" }}
setValue={(value, options) =>
handlePropertyChange({ offsetY: value }, options)
@@ -259,6 +267,7 @@ export const ShadowContent = ({
// border-top-width is a fake property for validating box-shadow's blur.
property="borderTopWidth"
styleSource="local"
+ disabled={layer.type === "var"}
value={blur ?? { type: "unit", value: 0, unit: "px" }}
setValue={(value, options) =>
handlePropertyChange({ blur: value }, options)
@@ -283,6 +292,7 @@ export const ShadowContent = ({
// outline-offset is a fake property for validating box-shadow's spread.
property="outlineOffset"
styleSource="local"
+ disabled={layer.type === "var"}
value={spread ?? { type: "unit", value: 0, unit: "px" }}
setValue={(value, options) =>
handlePropertyChange({ spread: value }, options)
@@ -313,6 +323,7 @@ export const ShadowContent = ({
/>
<ColorPicker
property="color"
+ disabled={layer.type === "var"}
value={colorControlProp}
currentColor={colorControlProp}
getOptions={() =>
@@ -339,6 +350,7 @@ export const ShadowContent = ({
/>
<ToggleGroup
type="single"
+ disabled={layer.type === "var"}
value={inset?.value ?? "normal"}
defaultValue="inset"
onValueChange={(value) => {
@@ -396,14 +408,10 @@ export const ShadowContent = ({
</Tooltip>
</Flex>
</Label>
- <TextArea
- rows={3}
- name="description"
+ <CssFragmentEditor
+ invalid={intermediateValue?.type === "invalid"}
+ autoFocus={layer.type === "var"}
value={intermediateValue?.value ?? propertyValue ?? ""}
- css={{ minHeight: theme.spacing[14], ...textVariants.mono }}
- color={
- intermediateValue?.type === "invalid" ? "error" : undefined
- }
onChange={handleChange}
onBlur={handleComplete}
onKeyDown={(event) => {