Skip to content

Commit 5637e71

Browse files
authored
refactor: migrate css-data to hyphenated properties (#5031)
All parsers/generators accept and produce hyphenated css property. Camel case is only used for StyleProperty type.
1 parent 5c3a6e5 commit 5637e71

File tree

13 files changed

+94
-101
lines changed

13 files changed

+94
-101
lines changed

apps/builder/app/builder/features/settings-panel/props-section/animation/new-view-animations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ const newWipeInViewAnimation: ViewAnimation = {
114114
{
115115
offset: 0,
116116
styles: {
117-
"clip-path": parseCssValue("clipPath", "inset(0 100% 0 0)"),
117+
"clip-path": parseCssValue("clip-path", "inset(0 100% 0 0)"),
118118
},
119119
},
120120
{
121121
offset: 1,
122122
styles: {
123-
"clip-path": parseCssValue("clipPath", "inset(0 0 0 0)"),
123+
"clip-path": parseCssValue("clip-path", "inset(0 0 0 0)"),
124124
},
125125
},
126126
],
@@ -140,14 +140,14 @@ const newWipeOutViewAnimation: ViewAnimation = {
140140
{
141141
offset: 0,
142142
styles: {
143-
"clip-path": parseCssValue("clipPath", "inset(0 0 0 0)"),
143+
"clip-path": parseCssValue("clip-path", "inset(0 0 0 0)"),
144144
},
145145
},
146146

147147
{
148148
offset: 1,
149149
styles: {
150-
"clip-path": parseCssValue("clipPath", "inset(0 0 0 100%)"),
150+
"clip-path": parseCssValue("clip-path", "inset(0 0 0 100%)"),
151151
},
152152
},
153153
],

apps/builder/app/builder/features/style-panel/controls/font-family/font-family-control.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const FontFamilyControl = () => {
8686
itemToString={(item) => item?.label ?? item?.value ?? ""}
8787
onItemHighlight={(item) => {
8888
if (item === null) {
89-
setValue(parseCssValue("fontFamily", itemValue), {
89+
setValue(parseCssValue("font-family", itemValue), {
9090
isEphemeral: true,
9191
});
9292
return;
@@ -97,7 +97,7 @@ export const FontFamilyControl = () => {
9797
);
9898
}}
9999
onItemSelect={(item) => {
100-
setValue(parseCssValue("fontFamily", item.value));
100+
setValue(parseCssValue("font-family", item.value));
101101
setIntermediateValue(undefined);
102102
}}
103103
value={{ value: intermediateValue ?? itemValue }}
@@ -108,7 +108,7 @@ export const FontFamilyControl = () => {
108108
if (isFontManagerOpen) {
109109
return;
110110
}
111-
setValue(parseCssValue("fontFamily", itemValue));
111+
setValue(parseCssValue("font-family", itemValue));
112112
}}
113113
match={matchOrSuggestToCreate}
114114
/>

apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const BackgroundGradient = ({ index }: { index: number }) => {
4949
// This doesn't have the same behavior as CssValueInput.
5050
// However, it's great to see the immediate results when making gradient changes,
5151
// especially until we have a better gradient tool.
52-
const newValue = parseCssValue("backgroundImage", value);
52+
const newValue = parseCssValue("background-image", value);
5353

5454
if (newValue.type === "unparsed" || newValue.type === "var") {
5555
setRepeatedStyleItem(styleDecl, index, newValue, { isEphemeral: true });

apps/builder/app/builder/features/style-panel/sections/shared/value-text.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default {
4646
control: "select",
4747
options: valueOptions,
4848
labels: Object.fromEntries(
49-
valueOptions.map((value) => [value, parseCssValue("marginTop", value)])
49+
valueOptions.map((value) => [value, parseCssValue("margin-top", value)])
5050
),
5151
},
5252
},

apps/builder/app/builder/features/style-panel/sections/transforms/transform-extractors.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
112112
test("parses transform-origin and returns the individual properties from the value", () => {
113113
expect(
114114
extractTransformOrPerspectiveOriginValues(
115-
parseCssValue("transformOrigin", "center") as TupleValue
115+
parseCssValue("transform-origin", "center") as TupleValue
116116
)
117117
).toEqual({
118118
x: { type: "keyword", value: "center" },
@@ -122,7 +122,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
122122

123123
expect(
124124
extractTransformOrPerspectiveOriginValues(
125-
parseCssValue("transformOrigin", "top") as TupleValue
125+
parseCssValue("transform-origin", "top") as TupleValue
126126
)
127127
).toEqual({
128128
x: { type: "keyword", value: "center" },
@@ -132,7 +132,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
132132

133133
expect(
134134
extractTransformOrPerspectiveOriginValues(
135-
parseCssValue("transformOrigin", "right") as TupleValue
135+
parseCssValue("transform-origin", "right") as TupleValue
136136
)
137137
).toEqual({
138138
x: { type: "keyword", value: "right" },
@@ -142,7 +142,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
142142

143143
expect(
144144
extractTransformOrPerspectiveOriginValues(
145-
parseCssValue("transformOrigin", "45px") as TupleValue
145+
parseCssValue("transform-origin", "45px") as TupleValue
146146
)
147147
).toEqual({
148148
x: { type: "unit", unit: "px", value: 45 },
@@ -152,7 +152,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
152152

153153
expect(
154154
extractTransformOrPerspectiveOriginValues(
155-
parseCssValue("transformOrigin", "20px 40px") as TupleValue
155+
parseCssValue("transform-origin", "20px 40px") as TupleValue
156156
)
157157
).toEqual({
158158
x: { type: "unit", unit: "px", value: 20 },
@@ -162,7 +162,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
162162

163163
expect(
164164
extractTransformOrPerspectiveOriginValues(
165-
parseCssValue("transformOrigin", "10px 20px 30px") as TupleValue
165+
parseCssValue("transform-origin", "10px 20px 30px") as TupleValue
166166
)
167167
).toEqual({
168168
x: { type: "unit", unit: "px", value: 10 },
@@ -172,7 +172,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
172172

173173
expect(
174174
extractTransformOrPerspectiveOriginValues(
175-
parseCssValue("transformOrigin", "left top 30px") as TupleValue
175+
parseCssValue("transform-origin", "left top 30px") as TupleValue
176176
)
177177
).toEqual({
178178
x: { type: "keyword", value: "left" },
@@ -182,7 +182,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
182182

183183
expect(
184184
extractTransformOrPerspectiveOriginValues(
185-
parseCssValue("transformOrigin", "bottom right 60px") as TupleValue
185+
parseCssValue("transform-origin", "bottom right 60px") as TupleValue
186186
)
187187
).toEqual({
188188
x: { type: "keyword", value: "right" },
@@ -192,7 +192,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
192192

193193
expect(
194194
extractTransformOrPerspectiveOriginValues(
195-
parseCssValue("transformOrigin", "left 50% 60px") as TupleValue
195+
parseCssValue("transform-origin", "left 50% 60px") as TupleValue
196196
)
197197
).toEqual({
198198
x: { type: "keyword", value: "left" },
@@ -202,7 +202,7 @@ describe("extractTransformOrPerspectiveOriginValues", () => {
202202

203203
expect(
204204
extractTransformOrPerspectiveOriginValues(
205-
parseCssValue("transformOrigin", "50% bottom 60px") as TupleValue
205+
parseCssValue("transform-origin", "50% bottom 60px") as TupleValue
206206
)
207207
).toEqual({
208208
x: { type: "unit", value: 50, unit: "%" },

apps/builder/app/builder/features/style-panel/shared/css-fragment.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ setEnv("*");
77

88
test("parse var()", () => {
99
const result = new Map([
10-
["background-image", parseCssValue("backgroundImage", "var(--bg)")],
10+
["background-image", parseCssValue("background-image", "var(--bg)")],
1111
]);
1212
expect(
1313
parseCssFragment("var(--bg)", ["background-image", "background"])
@@ -22,17 +22,17 @@ test("parse var()", () => {
2222

2323
test("fallback further to valid values", () => {
2424
const result = new Map([
25-
["background-image", parseCssValue("backgroundImage", "none")],
26-
["background-position-x", parseCssValue("backgroundPositionX", "0%")],
27-
["background-position-y", parseCssValue("backgroundPositionY", "0%")],
28-
["background-size", parseCssValue("backgroundSize", "auto auto")],
29-
["background-repeat", parseCssValue("backgroundRepeat", "repeat")],
30-
["background-attachment", parseCssValue("backgroundAttachment", "scroll")],
31-
["background-origin", parseCssValue("backgroundOrigin", "padding-box")],
32-
["background-clip", parseCssValue("backgroundClip", "border-box")],
25+
["background-image", parseCssValue("background-image", "none")],
26+
["background-position-x", parseCssValue("background-position-x", "0%")],
27+
["background-position-y", parseCssValue("background-position-y", "0%")],
28+
["background-size", parseCssValue("background-size", "auto auto")],
29+
["background-repeat", parseCssValue("background-repeat", "repeat")],
30+
["background-attachment", parseCssValue("background-attachment", "scroll")],
31+
["background-origin", parseCssValue("background-origin", "padding-box")],
32+
["background-clip", parseCssValue("background-clip", "border-box")],
3333
[
3434
"background-color",
35-
parseCssValue("backgroundColor", "rgba(255, 255, 255, 1)"),
35+
parseCssValue("background-color", "rgba(255, 255, 255, 1)"),
3636
],
3737
]);
3838
expect(parseCssFragment("#fff", ["background-image", "background"])).toEqual(
@@ -42,14 +42,14 @@ test("fallback further to valid values", () => {
4242

4343
test("parse shorthand property", () => {
4444
const result = new Map([
45-
["transition-property", parseCssValue("transitionProperty", "opacity")],
46-
["transition-duration", parseCssValue("transitionDuration", "1s")],
45+
["transition-property", parseCssValue("transition-property", "opacity")],
46+
["transition-duration", parseCssValue("transition-duration", "1s")],
4747
[
4848
"transition-timing-function",
49-
parseCssValue("transitionTimingFunction", "ease"),
49+
parseCssValue("transition-timing-function", "ease"),
5050
],
51-
["transition-delay", parseCssValue("transitionDelay", "0s")],
52-
["transition-behavior", parseCssValue("transitionBehavior", "normal")],
51+
["transition-delay", parseCssValue("transition-delay", "0s")],
52+
["transition-behavior", parseCssValue("transition-behavior", "normal")],
5353
]);
5454
expect(parseCssFragment("opacity 1s", ["transition"])).toEqual(result);
5555
expect(parseCssFragment("transition: opacity 1s", ["transition"])).toEqual(
@@ -68,8 +68,8 @@ test("parse longhand properties", () => {
6868
)
6969
).toEqual(
7070
new Map([
71-
["transition-property", parseCssValue("transitionProperty", "opacity")],
72-
["transition-duration", parseCssValue("transitionDuration", "1s")],
71+
["transition-property", parseCssValue("transition-property", "opacity")],
72+
["transition-duration", parseCssValue("transition-duration", "1s")],
7373
])
7474
);
7575
});

apps/builder/app/builder/features/style-panel/shared/filter-content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const filterFunctions = {
5050
// and pass the args as value and property
5151
"drop-shadow": {
5252
default: "0px 2px 5px rgba(0, 0, 0, 0.2)",
53-
fakeProperty: "textShadow",
53+
fakeProperty: "text-shadow",
5454
},
5555
// https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/grayscale#syntax
5656
// number | percentage

packages/css-data/bin/mdn-data.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import properties from "mdn-data/css/properties.json";
77
import syntaxes from "mdn-data/css/syntaxes.json";
88
import selectors from "mdn-data/css/selectors.json";
99
import data from "css-tree/dist/data";
10-
import {
11-
type KeywordValue,
12-
type StyleValue,
13-
type Unit,
14-
type UnitValue,
15-
type UnparsedValue,
16-
type FontFamilyValue,
17-
type CssProperty,
18-
hyphenateProperty,
10+
import type {
11+
KeywordValue,
12+
StyleValue,
13+
Unit,
14+
UnitValue,
15+
UnparsedValue,
16+
FontFamilyValue,
17+
CssProperty,
1918
} from "@webstudio-is/css-engine";
2019
import * as customData from "../src/custom-data";
2120
import { camelCaseProperty } from "../src/parse-css";
@@ -356,7 +355,7 @@ const getPropertiesData = (
356355
);
357356
}
358357

359-
propertiesData[camelCaseProperty(property as CssProperty)] = {
358+
propertiesData[property] = {
360359
unitGroups: Array.from(unitGroups),
361360
inherited: config.inherited,
362361
initial: parseInitialValue(property, config.initial, unitGroups),
@@ -383,20 +382,16 @@ const getKeywordValues = (filteredProperties: FilteredProperties) => {
383382
const commonKeywords = ["initial", "inherit", "unset"];
384383

385384
for (const property in filteredProperties) {
386-
const key = camelCaseProperty(property as CssProperty);
387385
// prevent merging with custom keywords
388-
if (result[key]) {
386+
if (result[property]) {
389387
continue;
390388
}
391389
const keywords = new Set<string>();
392-
walkSyntax(
393-
filteredProperties[property as keyof typeof filteredProperties].syntax,
394-
(node) => {
395-
if (node.type === "Keyword") {
396-
keywords.add(beautifyKeyword(property, node.name));
397-
}
390+
walkSyntax(filteredProperties[property].syntax, (node) => {
391+
if (node.type === "Keyword") {
392+
keywords.add(beautifyKeyword(property, node.name));
398393
}
399-
);
394+
});
400395

401396
if (property in nonStandardValues) {
402397
for (const nonStandartKeyword of nonStandardValues[
@@ -411,7 +406,7 @@ const getKeywordValues = (filteredProperties: FilteredProperties) => {
411406
keywords.add(commonKeyword);
412407
}
413408

414-
result[key] = [...(result[key] ?? []), ...keywords];
409+
result[property] = [...(result[property] ?? []), ...keywords];
415410
}
416411

417412
return result;
@@ -421,11 +416,11 @@ const getTypes = (propertiesData: typeof customData.propertiesData) => {
421416
let types = "";
422417

423418
const camelCasedProperties = Object.keys(propertiesData).map((property) =>
424-
JSON.stringify(property)
419+
JSON.stringify(camelCaseProperty(property as CssProperty))
425420
);
426421
types += `export type CamelCasedProperty = ${camelCasedProperties.join(" | ")};\n\n`;
427422
const hyphenatedProperties = Object.keys(propertiesData).map((property) =>
428-
JSON.stringify(hyphenateProperty(property))
423+
JSON.stringify(property)
429424
);
430425
types += `export type HyphenatedProperty = ${hyphenatedProperties.join(" | ")};\n\n`;
431426

@@ -450,7 +445,7 @@ writeToFile("units.ts", "units", units);
450445

451446
const propertiesData = Object.fromEntries(
452447
Object.entries(longhandPropertiesData).map(([property, data]) => [
453-
hyphenateProperty(property),
448+
property,
454449
data,
455450
])
456451
);
@@ -474,7 +469,7 @@ writeFileSync(join(targetDir, "properties.ts"), propertiesContent);
474469

475470
const keywordValues = Object.fromEntries(
476471
Object.entries(getKeywordValues(filteredData.allLonghands)).map(
477-
([property, data]) => [hyphenateProperty(property), data]
472+
([property, data]) => [property, data]
478473
)
479474
);
480475
const keywordValuesContent = `${autogeneratedHint}

0 commit comments

Comments
 (0)