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.

Add new RGB Style, then fix typescript errors (#578)

Ivan Starkov2be851178035
diff --git a/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx b/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx
index be949031dc..e501528551 100644
--- a/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx
@@ -1,7 +1,9 @@
import { Flex } from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
import { getFinalValue } from "../../shared/get-final-value";
import type { ControlProps } from "../../style-sections";
import { ColorPicker } from "../../shared/color-picker";
+import { colord } from "colord";
export const ColorControl = ({
currentStyle,
@@ -9,21 +11,49 @@ export const ColorControl = ({
setProperty,
styleConfig,
}: ControlProps) => {
- const value = getFinalValue({
+ let value = getFinalValue({
currentStyle,
inheritedStyle,
property: styleConfig.property,
});
+
if (value === undefined) {
return null;
}
+
const setValue = setProperty(styleConfig.property);
+ if (value.type !== "rgb") {
+ // Support previously set colors
+ const colordValue = colord(toValue(value));
+
+ if (colordValue.isValid()) {
+ const rgb = colordValue.toRgb();
+ value = {
+ type: "rgb",
+ r: rgb.r,
+ g: rgb.g,
+ b: rgb.b,
+ alpha: rgb.a ?? 1,
+ };
+ } else {
+ // @todo what to show as default?
+ // Default to black
+ value = {
+ type: "rgb",
+ r: 0,
+ g: 0,
+ b: 0,
+ alpha: 1,
+ };
+ }
+ }
+
return (
<Flex align="center" css={{ gridColumn: "2/4" }} gap="1">
<ColorPicker
id={styleConfig.property}
- value={String(value.value)}
+ value={value}
onChange={(value) => {
setValue(value, { isEphemeral: true });
}}
diff --git a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
index 691223728a..e5d54ef0e5 100644
--- a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
@@ -4,6 +4,7 @@ import type { ControlProps } from "../../style-sections";
import { getFinalValue } from "../../shared/get-final-value";
import { ValuePickerPopover } from "../../shared/value-picker-popover";
import { useState } from "react";
+import { toValue } from "@webstudio-is/css-engine";
export const FontFamilyControl = ({
currentStyle,
@@ -28,11 +29,11 @@ export const FontFamilyControl = ({
return (
<ValuePickerPopover
title="Fonts"
- content={<FontsManager value={String(value.value)} onChange={setValue} />}
+ content={<FontsManager value={toValue(value)} onChange={setValue} />}
onOpenChange={setIsOpen}
>
<TextField
- defaultValue={value.value}
+ defaultValue={toValue(value)}
state={isOpen ? "active" : undefined}
/>
</ValuePickerPopover>
diff --git a/apps/designer/app/designer/features/style-panel/controls/image/image-control.tsx b/apps/designer/app/designer/features/style-panel/controls/image/image-control.tsx
index b60f4f70e5..899987140d 100644
--- a/apps/designer/app/designer/features/style-panel/controls/image/image-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/image/image-control.tsx
@@ -4,6 +4,7 @@ import { getFinalValue } from "../../shared/get-final-value";
import { ValuePickerPopover } from "../../shared/value-picker-popover";
import { ImageManager } from "~/designer/shared/image-manager";
import { useAssets } from "~/designer/shared/assets";
+import { toValue } from "@webstudio-is/css-engine";
export const ImageControl = ({
currentStyle,
@@ -25,7 +26,7 @@ export const ImageControl = ({
const setValue = setProperty(styleConfig.property);
- const selectedAsset = assets.find((asset) => asset.id === value.value);
+ const selectedAsset = assets.find((asset) => asset.id === toValue(value));
return (
<ValuePickerPopover
diff --git a/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsx b/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsx
index 34aa7c8f3e..66d50e09b5 100644
--- a/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsx
@@ -3,6 +3,7 @@ import { getFinalValue } from "../../shared/get-final-value";
import { useIsFromCurrentBreakpoint } from "../../shared/use-is-from-current-breakpoint";
import type { ControlProps } from "../../style-sections";
import { iconConfigs } from "../../shared/configs";
+import { toValue } from "@webstudio-is/css-engine";
export const MenuControl = ({
currentStyle,
@@ -24,7 +25,7 @@ export const MenuControl = ({
}
const setValue = setProperty(styleConfig.property);
- const currentValue = value.value as string;
+ const currentValue = toValue(value);
const iconProps = iconConfigs[styleConfig.property];
const iconStyle =
@@ -32,7 +33,7 @@ export const MenuControl = ({
? {}
: {
transform: `rotate(${
- currentStyle.flexDirection?.value === "column"
+ toValue(currentStyle.flexDirection) === "column"
? 90 * (styleConfig.property === "alignItems" ? -1 : 1)
: 0
}deg)`,
diff --git a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx
index c0e3fdbc68..a08b36c77d 100644
--- a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx
@@ -1,3 +1,4 @@
+import { toValue } from "@webstudio-is/css-engine";
import { Select } from "@webstudio-is/design-system";
import { getFinalValue } from "../../shared/get-final-value";
import type { ControlProps } from "../../style-sections";
@@ -24,7 +25,7 @@ export const SelectControl = ({
return (
<Select
options={styleConfig.items.map(({ label }) => label)}
- value={String(value.value)}
+ value={toValue(value)}
onChange={setValue}
/>
);
diff --git a/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx b/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx
index 5f64c483a3..a22b77acbb 100644
--- a/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx
+++ b/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx
@@ -1,4 +1,5 @@
import { Flex, Grid } from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
import type { RenderCategoryProps } from "../../style-sections";
import { ToggleGroupControl } from "../../controls/toggle/toggle-control";
import { PropertyName } from "../../shared/property-name";
@@ -41,7 +42,7 @@ const FlexChildSectionAlign = (props: RenderCategoryProps) => {
<ToggleGroupControl
property={sectionStyle.alignSelf?.styleConfig.property}
onValueChange={(value) => setAlignSelf(value)}
- value={String(currentStyle.alignSelf?.value)}
+ value={toValue(currentStyle.alignSelf)}
items={[
{
child: <CrossSmallIcon />,
@@ -120,8 +121,8 @@ const FlexChildSectionSizing = (props: RenderCategoryProps) => {
}
}}
value={getSizingValue(
- String(currentStyle.flexGrow?.value),
- String(currentStyle.flexShrink?.value)
+ toValue(currentStyle.flexGrow),
+ toValue(currentStyle.flexShrink)
)}
items={[
{
@@ -217,7 +218,7 @@ const FlexChildSectionOrder = (props: RenderCategoryProps) => {
}
}
}}
- value={String(currentStyle.order?.value)}
+ value={toValue(currentStyle.order)}
items={[
{
child: <CrossSmallIcon />,
diff --git a/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsx b/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsx
index e30c77fcb5..17aba83303 100644
--- a/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsx
+++ b/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsx
@@ -1,4 +1,5 @@
import { Box, Flex, Grid } from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
import type { RenderCategoryProps } from "../../style-sections";
import { FlexGrid } from "./shared/flex-grid";
import { Lock } from "./shared/lock";
@@ -153,7 +154,7 @@ export const LayoutSection = ({
styleConfigsByCategory,
moreStyleConfigsByCategory,
}: RenderCategoryProps) => {
- const ActiveLayout = layouts.get(String(currentStyle.display?.value));
+ const ActiveLayout = layouts.get(toValue(currentStyle.display));
if (ActiveLayout) {
return (
diff --git a/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx b/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx
index cf8fdb73b3..7527c565ed 100644
--- a/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx
+++ b/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx
@@ -1,4 +1,5 @@
import { Flex, Grid, IconButtonDeprecated } from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
import { DotFilledIcon } from "@webstudio-is/icons";
import { useIsFromCurrentBreakpoint } from "../../../shared/use-is-from-current-breakpoint";
import type { Style } from "@webstudio-is/css-data";
@@ -18,9 +19,9 @@ export const FlexGrid = ({
"alignContent",
"alignItems",
]);
- const flexDirection = currentStyle.flexDirection?.value as string;
- const justifyContent = currentStyle.justifyContent?.value as string;
- const alignItems = currentStyle.alignItems?.value as string;
+ const flexDirection = toValue(currentStyle.flexDirection);
+ const justifyContent = toValue(currentStyle.justifyContent);
+ const alignItems = toValue(currentStyle.alignItems);
const direction = Number(flexDirection.includes("column"));
const cells = [
"_0_0",
diff --git a/apps/designer/app/designer/features/style-panel/sections/layout/shared/lock.tsx b/apps/designer/app/designer/features/style-panel/sections/layout/shared/lock.tsx
index 4f0a595eb4..2d6b1df892 100644
--- a/apps/designer/app/designer/features/style-panel/sections/layout/shared/lock.tsx
+++ b/apps/designer/app/designer/features/style-panel/sections/layout/shared/lock.tsx
@@ -4,6 +4,7 @@ import {
IconButtonDeprecated,
Tooltip,
} from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
import { LockOpenIcon, LockCloseIcon } from "@webstudio-is/icons";
import type { Style } from "@webstudio-is/css-data";
import type { CreateBatchUpdate } from "../../../shared/use-style-data";
@@ -22,9 +23,9 @@ export const Lock = ({
const a = currentStyle[aKey];
const b = currentStyle[bKey];
const aVal =
- a?.value + (a?.type === "unit" && a.unit !== "number" ? a.unit : "");
+ toValue(a) + (a?.type === "unit" && a.unit !== "number" ? a.unit : "");
const bVal =
- b?.value + (b?.type === "unit" && b.unit !== "number" ? b.unit : "");
+ toValue(b) + (b?.type === "unit" && b.unit !== "number" ? b.unit : "");
const [isPaired, setIsPaired] = useState(aVal === bVal);
useEffect(() => {
diff --git a/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx b/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx
index 73e3c9632b..67c941d281 100644
--- a/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx
+++ b/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx
@@ -5,6 +5,7 @@ import {
IconButtonDeprecated,
Tooltip,
} from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
import { PropertyName } from "../../shared/property-name";
import {
ColorControl,
@@ -117,7 +118,7 @@ export const TypographySectionAdvanced = (props: RenderCategoryProps) => {
<ToggleGroupControl
property="textAlign"
onValueChange={(value) => setTextAlign(value)}
- value={String(getTextAlign(String(currentStyle.textAlign?.value)))}
+ value={String(getTextAlign(toValue(currentStyle.textAlign)))}
items={[
{
child: <TextAlignLeftIcon />,
@@ -144,7 +145,7 @@ export const TypographySectionAdvanced = (props: RenderCategoryProps) => {
<ToggleGroupControl
property="textDecorationLine"
onValueChange={(value) => setTextDecorationLine(value)}
- value={String(currentStyle.textDecorationLine?.value)}
+ value={toValue(currentStyle.textDecorationLine)}
items={[
{
child: <CrossSmallIcon />,
@@ -174,7 +175,7 @@ export const TypographySectionAdvanced = (props: RenderCategoryProps) => {
<ToggleGroupControl
property="textTransform"
onValueChange={(value) => setTextTransform(value)}
- value={String(currentStyle.textTransform?.value)}
+ value={toValue(currentStyle.textTransform)}
items={[
{
child: <CrossSmallIcon />,
@@ -201,7 +202,7 @@ export const TypographySectionAdvanced = (props: RenderCategoryProps) => {
<ToggleGroupControl
property="fontStyle"
onValueChange={(value) => setFontStyle(value)}
- value={String(currentStyle.fontStyle?.value)}
+ value={toValue(currentStyle.fontStyle)}
items={[
{
child: <CrossSmallIcon />,
@@ -242,7 +243,7 @@ export const TypographySectionAdvancedPopover = (
<ToggleGroupControl
property="direction"
onValueChange={(value) => setDirection(value)}
- value={String(currentStyle.direction?.value)}
+ value={toValue(currentStyle.direction)}
items={[
{
child: <TextDirectionLTRIcon />,
@@ -262,7 +263,7 @@ export const TypographySectionAdvancedPopover = (
<ToggleGroupControl
property="hyphens"
onValueChange={(value) => setHyphens(value)}
- value={String(currentStyle.hyphens?.value)}
+ value={toValue(currentStyle.hyphens)}
items={[
{
child: <CrossSmallIcon />,
@@ -282,7 +283,7 @@ export const TypographySectionAdvancedPopover = (
<ToggleGroupControl
property="textOverflow"
onValueChange={(value) => setTextOverflow(value)}
- value={String(currentStyle.textOverflow?.value)}
+ value={toValue(currentStyle.textOverflow)}
items={[
{
child: <CrossSmallIcon />,
diff --git a/apps/designer/app/designer/features/style-panel/shared/color-picker.tsx b/apps/designer/app/designer/features/style-panel/shared/color-picker.tsx
index b27074b909..41afb2c69d 100644
--- a/apps/designer/app/designer/features/style-panel/shared/color-picker.tsx
+++ b/apps/designer/app/designer/features/style-panel/shared/color-picker.tsx
@@ -1,5 +1,8 @@
import { useState } from "react";
+import { colord, type RgbaColor } from "colord";
import { ColorResult, RGBColor, SketchPicker } from "react-color";
+import type { RgbValue } from "@webstudio-is/css-data";
+
import {
Box,
Popover,
@@ -9,12 +12,7 @@ import {
TextField,
css,
} from "@webstudio-is/design-system";
-
-const stringifyRGBA = (color: RGBColor) => {
- const { r, g, b, a = 1 } = color;
-
- return `rgba(${r},${g},${b},${a})`;
-};
+import { toValue } from "@webstudio-is/css-engine";
const pickerStyle = css({
padding: "$spacing$5",
@@ -35,12 +33,22 @@ const defaultPickerStyles = {
};
type ColorPickerProps = {
- onChange: (value: string) => void;
- onChangeComplete: (value: string) => void;
- value: string;
+ onChange: (value: RgbValue) => void;
+ onChangeComplete: (value: RgbValue) => void;
+ value: RgbValue;
id: string;
};
+const colorResultToRgbValue = (rgb: RgbaColor | RGBColor): RgbValue => {
+ return {
+ type: "rgb",
+ r: rgb.r,
+ g: rgb.g,
+ b: rgb.b,
+ alpha: rgb.a ?? 100,
+ };
+};
+
export const ColorPicker = ({
value,
onChange,
@@ -48,10 +56,59 @@ export const ColorPicker = ({
id,
}: ColorPickerProps) => {
const [displayColorPicker, setDisplayColorPicker] = useState(false);
- // Color picker will use 0 as alpha value, which will force user to set alpha every time they have to change from transparent
- if (value === "transparent") {
- value = "";
- }
+ const [intermediateValue, setIntermediateValue] = useState<
+ | {
+ /**
+ * TextField value
+ */
+ stringValue: string;
+ /**
+ * Color picker value
+ */
+ rgbValue: RGBColor;
+ state: "invalid" | undefined;
+ }
+ | undefined
+ >(undefined);
+
+ const onInputChangeComplete = (source: "enter" | "blur") => {
+ if (intermediateValue === undefined) {
+ return;
+ }
+
+ const colordValue = colord(intermediateValue.stringValue);
+ if (colordValue.isValid()) {
+ const rgb = colordValue.toRgb();
+ onChangeComplete({
+ type: "rgb",
+ r: rgb.r,
+ g: rgb.g,
+ b: rgb.b,
+ alpha: rgb.a,
+ });
+ setIntermediateValue(undefined);
+ return;
+ }
+
+ // We don't store invalid values in the CSS data, below is the only way we can show errors.
+ // See for details: https://github.com/webstudio-is/webstudio-designer/issues/564
+ // Anyway same behavior has Webflow - shows an error state on "Enter", and resets on Blur
+
+ // In case of "Enter" click show that the value is invalid
+ if (source === "enter") {
+ setIntermediateValue({
+ ...intermediateValue,
+ state: "invalid",
+ });
+ return;
+ }
+
+ // In case of blur, just reset to external value
+ setIntermediateValue(undefined);
+ };
+
+ const stringValue = intermediateValue?.stringValue ?? toValue(value);
+ const rgbValue = intermediateValue?.rgbValue ?? value;
const prefix = (
<Popover
@@ -70,19 +127,25 @@ export const ColorPicker = ({
width: "$spacing$10",
height: "$spacing$10",
borderRadius: 2,
- background: value,
+ background: toValue(value),
}}
/>
</PopoverTrigger>
<PopoverPortal>
<PopoverContent>
<SketchPicker
- color={value}
- onChange={(color: ColorResult) =>
- onChange(stringifyRGBA(color.rgb))
- }
+ color={rgbValue}
+ onChange={(color: ColorResult) => {
+ setIntermediateValue({
+ stringValue: toValue(colorResultToRgbValue(color.rgb)),
+ rgbValue: color.rgb,
+ state: undefined,
+ });
+ onChange(colorResultToRgbValue(color.rgb));
+ }}
onChangeComplete={(color: ColorResult) => {
- onChangeComplete(stringifyRGBA(color.rgb));
+ setIntermediateValue(undefined);
+ onChangeComplete(colorResultToRgbValue(color.rgb));
}}
// @todo to remove both when we have preset colors
presetColors={[]}
@@ -96,10 +159,28 @@ export const ColorPicker = ({
return (
<TextField
- onChange={(event) => onChange(event.target.value)}
- value={value}
+ onChange={(event) => {
+ setIntermediateValue({
+ stringValue: event.target.value,
+ rgbValue: rgbValue,
+ state: undefined,
+ });
+ }}
+ onBlur={() => {
+ if (displayColorPicker) {
+ return;
+ }
+ onInputChangeComplete("blur");
+ }}
+ value={stringValue}
+ state={intermediateValue?.state}
id={id}
prefix={prefix}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ onInputChangeComplete("enter");
+ }
+ }}
/>
);
};
diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.stories.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.stories.tsx
index 7d31fb0676..e2f7ceccf2 100644
--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.stories.tsx
+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.stories.tsx
@@ -3,6 +3,7 @@ import { Flex, TextField } from "@webstudio-is/design-system";
import type { StyleValue } from "@webstudio-is/css-data";
import { CssValueInput, type IntermediateStyleValue } from "./css-value-input";
import { action } from "@storybook/addon-actions";
+import { toValue } from "@webstudio-is/css-engine";
export default {
component: CssValueInput,
@@ -113,9 +114,9 @@ export const WithUnits = () => {
readOnly
value={
value
- ? value?.type === "unit"
+ ? value?.type === "intermediate"
? value.value + value.unit
- : value.value
+ : toValue(value)
: ""
}
/>
diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
index 01fa4c9622..248b3d3574 100644
--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
@@ -32,6 +32,7 @@ import { useIsFromCurrentBreakpoint } from "../use-is-from-current-breakpoint";
import { useUnitSelect } from "./unit-select";
import { unstable_batchedUpdates as unstableBatchedUpdates } from "react-dom";
import { parseIntermediateOrInvalidValue } from "./parse-intermediate-or-invalid-value";
+import { toValue } from "@webstudio-is/css-engine";
const unsetValue: UnsetValue = { type: "unset", value: "" };
@@ -263,7 +264,12 @@ export const CssValueInput = ({
} = useCombobox<CssValueInputValue>({
items: keywords,
value,
- itemToString: (item) => (item === null ? "" : String(item.value)),
+ itemToString: (item) =>
+ item === null
+ ? ""
+ : item.type === "intermediate" || item.type === "unit"
+ ? String(item.value)
+ : toValue(item),
onInputChange: (inputValue) => {
onChange(inputValue ?? unsetValue.value);
},
@@ -408,7 +414,7 @@ export const CssValueInput = ({
{...getItemProps({ item, index })}
key={index}
>
- {item.value}
+ {item.type === "intermediate" ? item.value : toValue(item)}
</ComboboxListboxItem>
))}
</ComboboxListbox>
diff --git a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts b/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts
index c8d4b5ebce..9c88303f23 100644
--- a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts
@@ -19,14 +19,15 @@ export const getFinalValue = ({
const currentValue = currentStyle[property];
const inheritedValue =
property in inheritedStyle ? inheritedStyle[property].value : undefined;
- if (currentValue?.value === "inherit" && inheritedValue !== undefined) {
+ if (toValue(currentValue) === "inherit" && inheritedValue !== undefined) {
return inheritedValue;
}
- if (
- currentValue?.type !== "unit" &&
- isNumericString(String(currentValue?.value))
- ) {
- return { value: Number(currentValue?.value), type: "unit", unit: "number" };
+ if (currentValue?.type !== "unit" && isNumericString(toValue(currentValue))) {
+ return {
+ value: Number(toValue(currentValue)),
+ type: "unit",
+ unit: "number",
+ };
}
if (
currentValue &&
diff --git a/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts b/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts
index f2713690de..f6aa47e36e 100644
--- a/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts
@@ -1,5 +1,6 @@
import { properties } from "@webstudio-is/css-data";
import type { StyleValue, StyleProperty } from "@webstudio-is/css-data";
+import { toValue } from "@webstudio-is/css-engine";
import type { Instance } from "@webstudio-is/react-sdk";
export type InheritedStyle = {
@@ -60,7 +61,7 @@ export const getInheritedStyle = (
for (const property in cssRule.style) {
const isInheritable = property in inheritableProperties;
const value = cssRule.style[property as StyleProperty];
- const hasValue = value !== undefined && value.value !== "inherit";
+ const hasValue = value !== undefined && toValue(value) !== "inherit";
const isFirst = property in inheritedStyle === false;
if (isInheritable && hasValue && isFirst) {
inheritedStyle[property as StyleProperty] = {
diff --git a/apps/designer/app/designer/features/style-panel/style-sections.tsx b/apps/designer/app/designer/features/style-panel/style-sections.tsx
index de9cef54c3..d97d770f76 100644
--- a/apps/designer/app/designer/features/style-panel/style-sections.tsx
+++ b/apps/designer/app/designer/features/style-panel/style-sections.tsx
@@ -1,4 +1,5 @@
import { Grid, type CSS } from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
import type { StyleConfig } from "./shared/configs";
import type { Category } from "@webstudio-is/react-sdk";
import type { Style, StyleProperty } from "@webstudio-is/css-data";
@@ -113,9 +114,9 @@ export const shouldRenderCategory = ({
}: RenderCategoryProps) => {
switch (category) {
case "flexChild":
- return (currentStyle.display?.value as string)?.includes("flex");
+ return toValue(currentStyle.display).includes("flex");
case "gridChild":
- return (currentStyle.display?.value as string)?.includes("grid");
+ return toValue(currentStyle.display).includes("grid");
}
return true;
diff --git a/apps/designer/app/designer/features/style-panel/style-settings.tsx b/apps/designer/app/designer/features/style-panel/style-settings.tsx
index 0a6e750ea7..50a7976f6b 100644
--- a/apps/designer/app/designer/features/style-panel/style-settings.tsx
+++ b/apps/designer/app/designer/features/style-panel/style-settings.tsx
@@ -1,6 +1,7 @@
import hyphenate from "hyphenate-style-name";
import { categories, type Category } from "@webstudio-is/react-sdk";
import type { CssRule, Style } from "@webstudio-is/css-data";
+import { toValue } from "@webstudio-is/css-engine";
import { type StyleConfig, styleConfigs } from "./shared/configs";
import { CollapsibleSection } from "~/designer/shared/inspector";
@@ -61,7 +62,7 @@ const appliesTo = (styleConfig: StyleConfig, currentStyle: Style): boolean => {
if (dependency === undefined) {
return false;
}
- const currentValue = currentStyle[dependency.property]?.value;
+ const currentValue = toValue(currentStyle[dependency.property]);
if (currentValue === undefined) {
return false;
}
diff --git a/apps/designer/package.json b/apps/designer/package.json
index 41dc24f064..5a6fe48447 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -35,6 +35,7 @@
"@webstudio-is/react-sdk": "*",
"bson-objectid": "^2.0.2",
"camelcase": "^6.3.0",
+ "colord": "^2.9.3",
"css-tree": "^2.3.0",
"debug": "^4.3.4",
"detect-font": "^0.1.5",
diff --git a/packages/css-data/src/schema.ts b/packages/css-data/src/schema.ts
index ab96274004..4573cc0794 100644
--- a/packages/css-data/src/schema.ts
+++ b/packages/css-data/src/schema.ts
@@ -16,11 +16,11 @@ type CustomProperty = `--${string}`;
export type AppliesTo = Properties[StyleProperty]["appliesTo"];
-export const Unit = z.union([z.enum(units), z.literal("number")]);
+const Unit = z.union([z.enum(units), z.literal("number")]);
export type Unit = z.infer<typeof Unit>;
-export const UnitValue = z.object({
+const UnitValue = z.object({
type: z.literal("unit"),
unit: Unit,
value: z.number(),
@@ -28,58 +28,74 @@ export const UnitValue = z.object({
export type UnitValue = z.infer<typeof UnitValue>;
-export const KeywordValue = z.object({
+const KeywordValue = z.object({
type: z.literal("keyword"),
// @todo use exact type
value: z.string(),
});
export type KeywordValue = z.infer<typeof KeywordValue>;
-export const FontFamilyValue = z.object({
+const FontFamilyValue = z.object({
type: z.literal("fontFamily"),
value: z.array(z.string()),
});
export type FontFamilyValue = z.infer<typeof FontFamilyValue>;
+const RgbValue = z.object({
+ type: z.literal("rgb"),
+ r: z.number(),
+ g: z.number(),
+ b: z.number(),
+ alpha: z.number(),
+});
+export type RgbValue = z.infer<typeof RgbValue>;
+
// We want to be able to render the invalid value
// and show it is invalid visually, without saving it to the db
-export const InvalidValue = z.object({
+const InvalidValue = z.object({
type: z.literal("invalid"),
value: z.string(),
});
export type InvalidValue = z.infer<typeof InvalidValue>;
-export const UnsetValue = z.object({
+const UnsetValue = z.object({
type: z.literal("unset"),
value: z.literal(""),
});
export type UnsetValue = z.infer<typeof UnsetValue>;
-export const validStaticValueTypes = ["unit", "keyword", "fontFamily"] as const;
+export const validStaticValueTypes = [
+ "unit",
+ "keyword",
+ "fontFamily",
+ "rgb",
+] as const;
-export const ValidStaticStyleValue = z.union([
+const ValidStaticStyleValue = z.union([
UnitValue,
KeywordValue,
FontFamilyValue,
+ RgbValue,
]);
export type ValidStaticStyleValue = z.infer<typeof ValidStaticStyleValue>;
-export const VarValue = z.object({
+const VarValue = z.object({
type: z.literal("var"),
value: z.string(),
fallbacks: z.array(ValidStaticStyleValue),
});
export type VarValue = z.infer<typeof VarValue>;
-export const StyleValue = z.union([
+const StyleValue = z.union([
ValidStaticStyleValue,
InvalidValue,
UnsetValue,
VarValue,
+ RgbValue,
]);
export type StyleValue = z.infer<typeof StyleValue>;
-export const Style = z.record(z.string(), StyleValue);
+const Style = z.record(z.string(), StyleValue);
export type Style = {
[property in StyleProperty]?: StyleValue;
diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.ts
index d68cdc5d02..8300efee39 100644
--- a/packages/css-engine/src/core/to-value.ts
+++ b/packages/css-engine/src/core/to-value.ts
@@ -9,6 +9,11 @@ const defaultOptions = {
withFallback: true,
};
+// exhaustive check, should never happen in runtime as ts would give error
+const assertUnreachable = (_arg: never, errorMessage: string) => {
+ throw new Error(errorMessage);
+};
+
export const toValue = (
value?: StyleValue,
options: ToCssOptions = defaultOptions
@@ -39,5 +44,26 @@ export const toValue = (
fallbacks.length > 0 ? `, ${fallbacks.join(", ")}` : "";
return `var(--${value.value}${fallbacksString})`;
}
- return value.value;
+
+ if (value.type === "keyword") {
+ return value.value;
+ }
+
+ if (value.type === "invalid") {
+ return value.value;
+ }
+
+ if (value.type === "unset") {
+ return value.value;
+ }
+
+ if (value.type === "rgb") {
+ return `rgba(${value.r}, ${value.g}, ${value.b}, ${value.alpha})`;
+ }
+
+ // Will give ts error in case of missing type
+ assertUnreachable(value, `Unknown value type`);
+
+ // Will never happen
+ throw new Error("Unknown value type");
};
diff --git a/yarn.lock b/yarn.lock
index 905c9b6cc6..03edc83ea5 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -7816,6 +7816,11 @@ color@^4.2.3:
color-convert "^2.0.1"
color-string "^1.9.0"
+colord@^2.9.3:
+ version "2.9.3"
+ resolved "https://registry.yarnpkg.com/colord/-/colord-2.9.3.tgz#4f8ce919de456f1d5c1c368c307fe20f3e59fb43"
+ integrity sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==
+
colorette@^1.2.2:
version "1.4.0"
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.4.0.tgz#5190fbb87276259a86ad700bff2c6d6faa3fca40"