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: expand backgound-position shorthand (#3646)

Bogdan Chadkin87210fa06946
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
index f98e6fbae3..9d2cd8dc39 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
@@ -13,7 +13,7 @@ import {
Separator,
styled,
} from "@webstudio-is/design-system";
-import { ImageControl, SelectControl, PositionControl } from "../../controls";
+import { ImageControl, SelectControl } from "../../controls";
import type { StyleInfo } from "../../shared/style-info";
import type {
DeleteProperty,
@@ -42,6 +42,7 @@ import { toValue } from "@webstudio-is/css-engine";
import { BackgroundGradient } from "./background-gradient";
import { NonResetablePropertyName } from "../../shared/property-name";
import { BackgroundImage } from "./background-image";
+import { BackgroundPosition } from "./background-position";
type BackgroundContentProps = {
currentStyle: StyleInfo;
@@ -214,11 +215,10 @@ export const BackgroundContent = (props: BackgroundContentProps) => {
<Spacer />
- <PositionControl
+ <BackgroundPosition
+ currentStyle={currentStyle}
setProperty={setProperty}
deleteProperty={deleteProperty}
- currentStyle={currentStyle}
- property="backgroundPosition"
/>
<Grid
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.test.ts b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.test.ts
index a775147ae5..036af51b27 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.test.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.test.ts
@@ -45,17 +45,17 @@ describe("setLayerProperty", () => {
changedProps.push([propertyName, newValue]);
},
- deleteProperty: (propertyName: string) => {
+ deleteProperty: (_propertyName: string) => {
// not used
},
- publish: (options?: unknown) => {
+ publish: (_options?: unknown) => {
published = true;
},
});
const setProperty = setLayerProperty(0, styleInfo, createBatchUpdate);
- setProperty("backgroundPosition")({ type: "unit", value: 10, unit: "px" });
+ setProperty("backgroundPositionX")({ type: "unit", value: 10, unit: "px" });
expect(published).toBe(true);
@@ -168,17 +168,17 @@ describe("setLayerProperty", () => {
changedProps.push([propertyName, newValue]);
},
- deleteProperty: (propertyName: string) => {
+ deleteProperty: (_propertyName: string) => {
// not used
},
- publish: (options?: unknown) => {
+ publish: (_options?: unknown) => {
published = true;
},
});
const setProperty = setLayerProperty(0, styleInfo, createBatchUpdate);
- setProperty("backgroundPosition")({ type: "unit", value: 10, unit: "px" });
+ setProperty("backgroundPositionX")({ type: "unit", value: 10, unit: "px" });
expect(published).toBe(true);
@@ -224,8 +224,8 @@ describe("setLayerProperty", () => {
},
};
- const cascadedPosition: NonNullable<
- StyleInfo["backgroundPosition"]
+ const cascadedPositionX: NonNullable<
+ StyleInfo["backgroundPositionX"]
>["cascaded"] = {
breakpointId: "mobile",
value: {
@@ -250,9 +250,9 @@ describe("setLayerProperty", () => {
cascaded,
value: cascaded.value,
},
- backgroundPosition: {
- cascaded: cascadedPosition,
- value: cascadedPosition.value,
+ backgroundPositionX: {
+ cascaded: cascadedPositionX,
+ value: cascadedPositionX.value,
},
};
@@ -271,10 +271,10 @@ describe("setLayerProperty", () => {
styleInfo[propertyName] = { value: newValue, local: newValue };
},
- deleteProperty: (propertyName: string) => {
+ deleteProperty: (_propertyName: string) => {
// not used
},
- publish: (options?: unknown) => {
+ publish: (_options?: unknown) => {
published = true;
},
});
@@ -311,7 +311,7 @@ describe("setLayerProperty", () => {
`);
// We should not reset existing properties to default values
- expect(styleInfo.backgroundPosition?.value).toMatchInlineSnapshot(`
+ expect(styleInfo.backgroundPositionX?.value).toMatchInlineSnapshot(`
{
"type": "layers",
"value": [
@@ -363,7 +363,7 @@ describe("setLayerProperty", () => {
}
`);
- expect(styleInfo.backgroundPosition?.value).toMatchInlineSnapshot(`
+ expect(styleInfo.backgroundPositionX?.value).toMatchInlineSnapshot(`
{
"type": "layers",
"value": [
@@ -432,7 +432,7 @@ describe("deleteLayer", () => {
// not used
deletedProperties.add(propertyName);
},
- publish: (options?: unknown) => {
+ publish: (_options?: unknown) => {
published = true;
},
});
@@ -493,10 +493,10 @@ describe("swapLayers", () => {
styleInfo[propertyName] = { value: newValue, local: newValue };
},
- deleteProperty: (propertyName: string) => {
+ deleteProperty: (_propertyName: string) => {
// not used
},
- publish: (options?: unknown) => {
+ publish: (_options?: unknown) => {
published = true;
},
});
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts
index 9742239500..72a4342153 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts
@@ -16,7 +16,8 @@ export const layeredBackgroundPropsDefaults = {
backgroundBlendMode: { type: "keyword", value: "normal" },
backgroundImage: { type: "keyword", value: "none" },
backgroundOrigin: { type: "keyword", value: "padding-box" },
- backgroundPosition: { type: "unit", value: 0, unit: "%" },
+ backgroundPositionX: { type: "unit", value: 0, unit: "%" },
+ backgroundPositionY: { type: "unit", value: 0, unit: "%" },
backgroundRepeat: { type: "keyword", value: "repeat" },
backgroundSize: { type: "keyword", value: "auto" },
} as const satisfies Record<string, BackgroundStyleValue>;
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx
new file mode 100644
index 0000000000..e61f914e81
--- /dev/null
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx
@@ -0,0 +1,88 @@
+import { keywordValues } from "@webstudio-is/css-data";
+import { Flex, Grid, PositionGrid } from "@webstudio-is/design-system";
+import { getStyleSource, type StyleInfo } from "../../shared/style-info";
+import { CssValueInputContainer } from "../../shared/css-value-input";
+import { NonResetablePropertyName } from "../../shared/property-name";
+import type { DeleteProperty, SetProperty } from "../../shared/use-style-data";
+
+export const BackgroundPosition = ({
+ currentStyle,
+ setProperty,
+ deleteProperty,
+}: {
+ currentStyle: StyleInfo;
+ setProperty: SetProperty;
+ deleteProperty: DeleteProperty;
+}) => {
+ const xInfo = currentStyle.backgroundPositionX;
+ const x = xInfo?.value.type === "unit" ? xInfo.value.value : 0;
+ const yInfo = currentStyle.backgroundPositionY;
+ const y = yInfo?.value.type === "unit" ? yInfo.value.value : 0;
+
+ return (
+ <Flex direction="column" gap="1">
+ <NonResetablePropertyName
+ style={currentStyle}
+ properties={["backgroundPositionX", "backgroundPositionY"]}
+ label="Position"
+ />
+ <Flex gap="6">
+ <PositionGrid
+ selectedPosition={{ x, y }}
+ onSelect={({ x, y }) => {
+ setProperty("backgroundPositionX")({
+ type: "unit",
+ value: x,
+ unit: "%",
+ });
+ setProperty("backgroundPositionY")({
+ type: "unit",
+ value: y,
+ unit: "%",
+ });
+ }}
+ />
+ <Grid
+ css={{ gridTemplateColumns: "max-content 1fr" }}
+ align="center"
+ gapX="2"
+ >
+ <NonResetablePropertyName
+ style={currentStyle}
+ properties={["backgroundPositionX"]}
+ description="Left position offset"
+ label="Left"
+ />
+ <CssValueInputContainer
+ property="backgroundPositionX"
+ styleSource={getStyleSource(xInfo)}
+ keywords={keywordValues.backgroundPositionX.map((value) => ({
+ type: "keyword",
+ value,
+ }))}
+ value={xInfo?.value}
+ setValue={setProperty("backgroundPositionX")}
+ deleteProperty={deleteProperty}
+ />
+ <NonResetablePropertyName
+ style={currentStyle}
+ properties={["backgroundPositionY"]}
+ description="Top position offset"
+ label="Top"
+ />
+ <CssValueInputContainer
+ property="backgroundPositionY"
+ styleSource={getStyleSource(yInfo)}
+ keywords={keywordValues.backgroundPositionY.map((value) => ({
+ type: "keyword",
+ value,
+ }))}
+ value={yInfo?.value}
+ setValue={setProperty("backgroundPositionY")}
+ deleteProperty={deleteProperty}
+ />
+ </Grid>
+ </Flex>
+ </Flex>
+ );
+};
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 13e99ad3c7..19083c15ce 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
@@ -145,7 +145,8 @@ export const properties = [
"backgroundColor",
"backgroundImage",
"backgroundOrigin",
- "backgroundPosition",
+ "backgroundPositionX",
+ "backgroundPositionY",
"backgroundRepeat",
"backgroundSize",
"backgroundBlendMode",
diff --git a/apps/builder/app/shared/webstudio-data-migrator.ts b/apps/builder/app/shared/webstudio-data-migrator.ts
index 3035f4f82c..239d6d78cf 100644
--- a/apps/builder/app/shared/webstudio-data-migrator.ts
+++ b/apps/builder/app/shared/webstudio-data-migrator.ts
@@ -33,7 +33,8 @@ export const migrateWebstudioDataMutable = (data: WebstudioData) => {
if (
property === "overflow" ||
property === "transition" ||
- property === "white-space"
+ property === "white-space" ||
+ property === "background-position"
) {
data.styles.delete(styleDeclKey);
const longhands = expandShorthands([
diff --git a/packages/css-data/bin/mdn-data.ts b/packages/css-data/bin/mdn-data.ts
index 2c937808d1..356dd65f3a 100755
--- a/packages/css-data/bin/mdn-data.ts
+++ b/packages/css-data/bin/mdn-data.ts
@@ -242,6 +242,8 @@ const experimentalProperties = [
"aspect-ratio",
"text-size-adjust",
"-webkit-line-clamp",
+ "background-position-x",
+ "background-position-y",
];
const unsupportedProperties = [
@@ -253,6 +255,7 @@ const unsupportedProperties = [
"overflow",
"white-space",
"text-wrap",
+ "background-position",
];
const animatableProperties: string[] = [];
diff --git a/packages/css-data/src/__generated__/animatable-properties.ts b/packages/css-data/src/__generated__/animatable-properties.ts
index 6af90b197a..62705988f2 100644
--- a/packages/css-data/src/__generated__/animatable-properties.ts
+++ b/packages/css-data/src/__generated__/animatable-properties.ts
@@ -6,7 +6,8 @@ export const animatableProperties = [
"background-clip",
"background-color",
"background-origin",
- "background-position",
+ "background-position-x",
+ "background-position-y",
"background-size",
"block-size",
"border-block-color",
diff --git a/packages/css-data/src/__generated__/keyword-values.ts b/packages/css-data/src/__generated__/keyword-values.ts
index 06f8d6dbec..4ea7cca62c 100644
--- a/packages/css-data/src/__generated__/keyword-values.ts
+++ b/packages/css-data/src/__generated__/keyword-values.ts
@@ -493,12 +493,22 @@ export const keywordValues = {
"inherit",
"unset",
],
- backgroundPosition: [
- "left",
+ backgroundPositionX: [
"center",
+ "left",
"right",
+ "x-start",
+ "x-end",
+ "initial",
+ "inherit",
+ "unset",
+ ],
+ backgroundPositionY: [
+ "center",
"top",
"bottom",
+ "y-start",
+ "y-end",
"initial",
"inherit",
"unset",
diff --git a/packages/css-data/src/__generated__/properties.ts b/packages/css-data/src/__generated__/properties.ts
index 92e643c86e..896a40c1c1 100644
--- a/packages/css-data/src/__generated__/properties.ts
+++ b/packages/css-data/src/__generated__/properties.ts
@@ -237,23 +237,23 @@ export const properties = {
},
types: [],
},
- backgroundPosition: {
+ backgroundPositionX: {
unitGroups: ["length", "percentage"],
inherited: false,
initial: {
- type: "tuple",
- value: [
- {
- type: "unit",
- unit: "%",
- value: 0,
- },
- {
- type: "unit",
- unit: "%",
- value: 0,
- },
- ],
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ types: ["length", "percentage"],
+ },
+ backgroundPositionY: {
+ unitGroups: ["length", "percentage"],
+ inherited: false,
+ initial: {
+ type: "unit",
+ unit: "%",
+ value: 0,
},
types: ["length", "percentage"],
},
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index 468d0092eb..1d41d0a3bf 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -170,40 +170,23 @@ test("parse background-image property as layers", () => {
});
});
-test("parse background-position property as layers", () => {
- expect(
- parseCssValue("backgroundPosition", `0px 0px, 550px 0px, 0px 0px, 0px 0px`)
- ).toEqual({
+test("parse background-position-* properties as layers", () => {
+ expect(parseCssValue("backgroundPositionX", `0px, 550px, 0px, 0px`)).toEqual({
type: "layers",
value: [
- {
- type: "tuple",
- value: [
- { type: "unit", unit: "px", value: 0 },
- { type: "unit", unit: "px", value: 0 },
- ],
- },
- {
- type: "tuple",
- value: [
- { type: "unit", unit: "px", value: 550 },
- { type: "unit", unit: "px", value: 0 },
- ],
- },
- {
- type: "tuple",
- value: [
- { type: "unit", unit: "px", value: 0 },
- { type: "unit", unit: "px", value: 0 },
- ],
- },
- {
- type: "tuple",
- value: [
- { type: "unit", unit: "px", value: 0 },
- { type: "unit", unit: "px", value: 0 },
- ],
- },
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 550 },
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 0 },
+ ],
+ });
+ expect(parseCssValue("backgroundPositionY", `0px, 0px, 0px, 0px`)).toEqual({
+ type: "layers",
+ value: [
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 0 },
],
});
});
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index b8718c719d..f5dedc9d7f 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -83,7 +83,8 @@ const repeatedProps = new Set<StyleProperty>([
"backgroundClip",
"backgroundBlendMode",
"backgroundOrigin",
- "backgroundPosition",
+ "backgroundPositionX",
+ "backgroundPositionY",
"backgroundRepeat",
"backgroundSize",
]);
diff --git a/packages/css-data/src/parse-css.test.ts b/packages/css-data/src/parse-css.test.ts
index d2a8c37f31..4b5762bb7c 100644
--- a/packages/css-data/src/parse-css.test.ts
+++ b/packages/css-data/src/parse-css.test.ts
@@ -95,24 +95,27 @@ describe("Parse CSS", () => {
},
},
{
- "property": "backgroundPosition",
+ "property": "backgroundPositionX",
"value": {
"type": "layers",
"value": [
{
- "type": "tuple",
- "value": [
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- ],
+ "type": "unit",
+ "unit": "px",
+ "value": 0,
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundPositionY",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 0,
},
],
},
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.ts
index a0e47987af..6ab5e02ecb 100644
--- a/packages/css-data/src/shorthands.test.ts
+++ b/packages/css-data/src/shorthands.test.ts
@@ -1053,10 +1053,74 @@ test("expand text-wrap", () => {
]);
});
+test("expand background-position", () => {
+ expect(expandShorthands([["background-position", "initial"]])).toEqual([
+ ["background-position-x", "initial"],
+ ["background-position-y", "initial"],
+ ]);
+ expect(expandShorthands([["background-position", "0"]])).toEqual([
+ ["background-position-x", "0"],
+ ["background-position-y", "center"],
+ ]);
+ expect(expandShorthands([["background-position", "top left"]])).toEqual([
+ ["background-position-x", "left"],
+ ["background-position-y", "top"],
+ ]);
+ expect(expandShorthands([["background-position", "right bottom"]])).toEqual([
+ ["background-position-x", "right"],
+ ["background-position-y", "bottom"],
+ ]);
+ expect(expandShorthands([["background-position", "25% 75%"]])).toEqual([
+ ["background-position-x", "25%"],
+ ["background-position-y", "75%"],
+ ]);
+ expect(
+ expandShorthands([["background-position", "bottom 10px right"]])
+ ).toEqual([
+ ["background-position-x", "right"],
+ ["background-position-y", "bottom 10px"],
+ ]);
+ expect(
+ expandShorthands([["background-position", "center right 10px"]])
+ ).toEqual([
+ ["background-position-x", "right 10px"],
+ ["background-position-y", "center"],
+ ]);
+ expect(
+ expandShorthands([["background-position", "center bottom 10px"]])
+ ).toEqual([
+ ["background-position-x", "center"],
+ ["background-position-y", "bottom 10px"],
+ ]);
+ expect(expandShorthands([["background-position", "top right 10px"]])).toEqual(
+ [
+ ["background-position-x", "right 10px"],
+ ["background-position-y", "top"],
+ ]
+ );
+ expect(
+ expandShorthands([["background-position", "bottom 10px right 20px"]])
+ ).toEqual([
+ ["background-position-x", "right 20px"],
+ ["background-position-y", "bottom 10px"],
+ ]);
+ expect(expandShorthands([["background-position", "0 10px, center"]])).toEqual(
+ [
+ ["background-position-x", "0,center"],
+ ["background-position-y", "10px,center"],
+ ]
+ );
+});
+
test.todo("all - can negatively affect build size");
test.todo("background - not used in webflow");
-test.todo("background-position-x - we use shorthand");
-test.todo("background-position-y - we use shorthand");
-test.todo("translate - are these directly mappable to transform");
-test.todo("rotate");
-test.todo("scale");
+// https://developer.mozilla.org/en-US/docs/Web/CSS/animation-range
+test.todo("animation-range");
+// https://developer.mozilla.org/en-US/docs/Web/CSS/view-timeline
+test.todo("view-timeline");
+// https://www.w3.org/TR/css-anchor-position-1/#position-try-prop
+test.todo("position-try - not used in webflow");
+// https://www.w3.org/TR/css-ui-4/
+test.todo("caret - caret-color & caret-shape");
+// https://developer.mozilla.org/en-US/docs/Web/CSS/overscroll-behavior
+test.todo("overscroll-behavior");
diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.ts
index a8a78a8ab6..3488b3c579 100644
--- a/packages/css-data/src/shorthands.ts
+++ b/packages/css-data/src/shorthands.ts
@@ -75,7 +75,7 @@ const joinByOperator = (list: List<CssNode> | CssNode[], operator: string) => {
const parseRepeated = (
value: CssNode,
- parseSingle: (single: CssNode) => Value[]
+ parseSingle: (single: Value) => Value[]
): Value[] => {
const values: CssNode[][] = [];
for (const single of splitByOperator(value, ",")) {
@@ -830,6 +830,56 @@ const expandWhiteSpace = function* (value: CssNode) {
yield ["text-wrap-mode", wrapMode] as const;
};
+/**
+ *
+ * background-position = <bg-position>#
+ * <bg-position> =
+ * [ left | center | right | top | bottom | <length-percentage> ] |
+ * [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] |
+ * [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ]
+ *
+ */
+const expandBackgroundPosition = function* (value: CssNode) {
+ const center = createIdentifier("center");
+ const [x, y] = parseRepeated(value, (single) => {
+ const list = getValueList(single);
+ if (list.length === 1) {
+ const [first] = list;
+ if (lexer.match(`center | <length-percentage>`, first).matched) {
+ return [createValueNode([first]), center];
+ }
+ if (lexer.match(`left | right`, first).matched) {
+ return [createValueNode([first]), center];
+ }
+ if (lexer.match(`top | bottom`, first).matched) {
+ return [center, createValueNode([first])];
+ }
+ return [single, single];
+ }
+ if (list.length === 2) {
+ const [first, second] = list;
+ if (
+ lexer.match(`top | bottom`, first).matched ||
+ lexer.match(`left | right`, second).matched
+ ) {
+ return [createValueNode([second]), createValueNode([first])];
+ }
+ return [createValueNode([first]), createValueNode([second])];
+ }
+ const [_center, x, y] = parseUnordered(
+ [
+ `center`,
+ `[ left | right ] <length-percentage>?`,
+ `[ top | bottom ] <length-percentage>?`,
+ ],
+ single
+ );
+ return [x ?? center, y ?? center];
+ });
+ yield ["background-position-x", x] as const;
+ yield ["background-position-y", y] as const;
+};
+
const expandShorthand = function* (property: string, value: CssNode) {
switch (property) {
case "font":
@@ -1129,6 +1179,10 @@ const expandShorthand = function* (property: string, value: CssNode) {
break;
}
+ case "background-position":
+ yield* expandBackgroundPosition(value);
+ break;
+
default:
yield [property, value] as const;
}
diff --git a/packages/css-data/src/tailwind-parser/parse.test.ts b/packages/css-data/src/tailwind-parser/parse.test.ts
index 1b310bcc7f..58679c6092 100644
--- a/packages/css-data/src/tailwind-parser/parse.test.ts
+++ b/packages/css-data/src/tailwind-parser/parse.test.ts
@@ -113,22 +113,25 @@ describe("parseTailwindToWebstudio", () => {
},
},
{
- "property": "backgroundPosition",
+ "property": "backgroundPositionX",
"value": {
"type": "layers",
"value": [
{
- "type": "tuple",
- "value": [
- {
- "type": "keyword",
- "value": "left",
- },
- {
- "type": "keyword",
- "value": "top",
- },
- ],
+ "type": "keyword",
+ "value": "left",
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundPositionY",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "top",
},
],
},
diff --git a/packages/css-engine/src/__generated__/types.ts b/packages/css-engine/src/__generated__/types.ts
index fc4e3c5053..abd90d11e7 100644
--- a/packages/css-engine/src/__generated__/types.ts
+++ b/packages/css-engine/src/__generated__/types.ts
@@ -25,7 +25,8 @@ export type Property =
| "backgroundColor"
| "backgroundImage"
| "backgroundOrigin"
- | "backgroundPosition"
+ | "backgroundPositionX"
+ | "backgroundPositionY"
| "backgroundRepeat"
| "backgroundSize"
| "blockSize"
diff --git a/packages/css-engine/src/core/merger.test.ts b/packages/css-engine/src/core/merger.test.ts
index a4a8ec817a..5bc2c3e9de 100644
--- a/packages/css-engine/src/core/merger.test.ts
+++ b/packages/css-engine/src/core/merger.test.ts
@@ -1,4 +1,5 @@
import { expect, test } from "@jest/globals";
+import type { StyleValue } from "../schema";
import { mergeStyles } from "./merger";
import type { StyleMap } from "./rules";
import { toValue } from "./to-value";
@@ -299,3 +300,54 @@ test("merge text-wrap with vars", () => {
)
).toEqual([["text-wrap", "var(--style)"]]);
});
+
+const layers = (...keywords: string[]): StyleValue => ({
+ type: "layers",
+ value: keywords.map((value) => ({ type: "keyword", value })),
+});
+
+test("merge background-position-{x,y}", () => {
+ expect(
+ toStringMap(
+ mergeStyles(
+ new Map([
+ ["background-position-x", layers("right")],
+ ["background-position-y", layers("bottom")],
+ ])
+ )
+ )
+ ).toEqual([["background-position", "right bottom"]]);
+ expect(
+ toStringMap(
+ mergeStyles(new Map([["background-position-x", layers("right")]]))
+ )
+ ).toEqual([["background-position-x", "right"]]);
+ expect(
+ toStringMap(
+ mergeStyles(new Map([["background-position-y", layers("bottom")]]))
+ )
+ ).toEqual([["background-position-y", "bottom"]]);
+ expect(
+ toStringMap(
+ mergeStyles(
+ new Map([
+ ["background-position-x", layers("right", "left")],
+ ["background-position-y", layers("bottom", "top")],
+ ])
+ )
+ )
+ ).toEqual([["background-position", "right bottom, left top"]]);
+ expect(
+ toStringMap(
+ mergeStyles(
+ new Map([
+ ["background-position-x", layers("right", "left")],
+ ["background-position-y", layers("bottom")],
+ ])
+ )
+ )
+ ).toEqual([
+ ["background-position-x", "right, left"],
+ ["background-position-y", "bottom"],
+ ]);
+});
diff --git a/packages/css-engine/src/core/merger.ts b/packages/css-engine/src/core/merger.ts
index 904086b996..e26e30c045 100644
--- a/packages/css-engine/src/core/merger.ts
+++ b/packages/css-engine/src/core/merger.ts
@@ -1,4 +1,4 @@
-import { StyleValue, TupleValueItem } from "../schema";
+import { StyleValue, TupleValue, TupleValueItem } from "../schema";
import type { StyleMap } from "./rules";
import { toValue } from "./to-value";
@@ -112,6 +112,30 @@ const mergeWhiteSpaceAndTextWrap = (styleMap: StyleMap) => {
}
};
+const mergeBackgroundPosition = (styleMap: StyleMap) => {
+ const x = styleMap.get("background-position-x");
+ const y = styleMap.get("background-position-y");
+ if (
+ x?.type === "layers" &&
+ y?.type === "layers" &&
+ x.value.length === y.value.length
+ ) {
+ const position = x.value.map((xValue, index): TupleValue => {
+ const yValue = y.value[index];
+ return {
+ type: "tuple",
+ value: [xValue as TupleValueItem, yValue as TupleValueItem],
+ };
+ });
+ styleMap.delete("background-position-x");
+ styleMap.delete("background-position-y");
+ styleMap.set("background-position", {
+ type: "layers",
+ value: position,
+ });
+ }
+};
+
export const mergeStyles = (styleMap: StyleMap) => {
const newStyle = new Map(styleMap);
mergeBorder(newStyle, "border-top");
@@ -124,5 +148,6 @@ export const mergeStyles = (styleMap: StyleMap) => {
mergeBox(newStyle, "margin");
mergeBox(newStyle, "padding");
mergeWhiteSpaceAndTextWrap(newStyle);
+ mergeBackgroundPosition(newStyle);
return newStyle;
};