Skip to content

Commit 6fa88d9

Browse files
authored
refactor: render html attributes for all components (#5163)
Ref #3632 Here enabled converting class -> className for all components with tags. Now it does not affect anything though later will unlock using html attributes instead of component props. Also got rid from huge map with react properties and replaced it with a set of proper aliases.
1 parent 452c50a commit 6fa88d9

12 files changed

Lines changed: 248 additions & 21 deletions

File tree

apps/builder/app/builder/features/ai/ai-fetch-result.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,15 @@ const $jsx = computed(
285285

286286
const jsx = generateJsxElement({
287287
scope,
288+
metas,
288289
instance,
289290
props,
290291
dataSources,
291292
usedDataSources: new Map(),
292293
indexesWithinAncestors,
293294
children: generateJsxChildren({
294295
scope,
296+
metas,
295297
children: instance.children,
296298
instances,
297299
props,

apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
selectorIdAttribute,
3737
type AnyComponent,
3838
textContentAttribute,
39+
standardAttributesToReactProps,
3940
} from "@webstudio-is/react-sdk";
4041
import { rawTheme } from "@webstudio-is/design-system";
4142
import {
@@ -285,21 +286,36 @@ const useInstanceProps = (instanceSelector: InstanceSelector) => {
285286
$propValuesByInstanceSelectorWithMemoryProps,
286287
$instances,
287288
$indexesWithinAncestors,
289+
$registeredComponentMetas,
288290
],
289-
(propValuesByInstanceSelector, instances, indexesWithinAncestors) => {
291+
(
292+
propValuesByInstanceSelector,
293+
instances,
294+
indexesWithinAncestors,
295+
metas
296+
) => {
290297
const instancePropsObject: Record<Prop["name"], unknown> = {};
291-
const tag = instances.get(instanceId)?.tag;
298+
const instance = instances.get(instanceId);
299+
const tag = instance?.tag;
292300
if (tag !== undefined) {
293301
instancePropsObject[tagProperty] = tag;
294302
}
303+
const hasTags =
304+
Object.keys(metas.get(instance?.component ?? "")?.presetStyle ?? {})
305+
.length > 0;
295306
const index = indexesWithinAncestors.get(instanceId);
296307
if (index !== undefined) {
297308
instancePropsObject[indexProperty] = index.toString();
298309
}
299310
const instanceProps = propValuesByInstanceSelector.get(instanceKey);
300311
if (instanceProps) {
301312
for (const [name, value] of instanceProps) {
302-
instancePropsObject[name] = value;
313+
if (hasTags) {
314+
const reactName = standardAttributesToReactProps[name] ?? name;
315+
instancePropsObject[reactName] = value;
316+
} else {
317+
instancePropsObject[name] = value;
318+
}
303319
}
304320
}
305321
return instancePropsObject;

packages/html-data/bin/aria.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
import { aria } from "aria-query";
2-
import {
3-
findTags,
4-
getAttr,
5-
getTextContent,
6-
loadPage,
7-
parseHtml,
8-
} from "./crawler";
92
import { mkdir, writeFile } from "node:fs/promises";
103
import {
114
createScope,
@@ -16,6 +9,13 @@ import {
169
type Props,
1710
} from "@webstudio-is/sdk";
1811
import { generateWebstudioComponent } from "@webstudio-is/react-sdk";
12+
import {
13+
findTags,
14+
getAttr,
15+
getTextContent,
16+
loadPage,
17+
parseHtml,
18+
} from "./crawler";
1919

2020
type Attribute = {
2121
name: string;
@@ -45,7 +45,14 @@ for (let index = 0; index < terms.length; index += 1) {
4545
descriptions.set(term, detail);
4646
}
4747

48-
const attributes: Attribute[] = [];
48+
const attributes: Attribute[] = [
49+
{
50+
name: "role",
51+
description:
52+
"Defines an explicit role for an element for use by assistive technologies.",
53+
type: "string",
54+
},
55+
];
4956
for (const [name, meta] of aria.entries()) {
5057
const attribute: Attribute = {
5158
name,

packages/html-data/bin/attributes.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
loadHtmlIndices,
1616
parseHtml,
1717
} from "./crawler";
18+
import { possibleStandardNames } from "./possible-standard-names";
19+
20+
const validHtmlAttributes = new Set<string>();
1821

1922
type Attribute = {
2023
name: string;
@@ -110,6 +113,7 @@ for (const row of rows) {
110113
) {
111114
type = "number";
112115
}
116+
validHtmlAttributes.add(attribute);
113117
for (let tag of tags) {
114118
tag = tag.toLowerCase().trim();
115119
if (/custom elements/i.test(tag)) {
@@ -251,3 +255,29 @@ await writeFile(
251255
metas: new Map(),
252256
}) + "export { Page }"
253257
);
258+
259+
// react does not have this one
260+
possibleStandardNames["dirname"] = "dirName";
261+
const standardAttributesToReactProps: Record<string, string> = {};
262+
const reactPropsToStandardAttributes: Record<string, string> = {};
263+
for (const [htmlAttribute, reactProperty] of Object.entries(
264+
possibleStandardNames
265+
)) {
266+
if (
267+
validHtmlAttributes.has(htmlAttribute) &&
268+
htmlAttribute !== reactProperty
269+
) {
270+
standardAttributesToReactProps[htmlAttribute] = reactProperty;
271+
reactPropsToStandardAttributes[reactProperty] = htmlAttribute;
272+
}
273+
}
274+
275+
let standardAttributesContent = "";
276+
standardAttributesContent += `export const standardAttributesToReactProps: Record<string, string> = ${JSON.stringify(standardAttributesToReactProps, null, 2)};\n\n`;
277+
standardAttributesContent += `export const reactPropsToStandardAttributes: Record<string, string> = ${JSON.stringify(reactPropsToStandardAttributes, null, 2)};\n`;
278+
279+
await mkdir("../react-sdk/src/__generated__", { recursive: true });
280+
await writeFile(
281+
"../react-sdk/src/__generated__/standard-attributes.ts",
282+
standardAttributesContent
283+
);

packages/react-sdk/src/standard-attributes.ts renamed to packages/html-data/bin/possible-standard-names.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// https://github.com/facebook/react/blob/6377903074d4b3a2de48c4da91783a5af9fc5237/packages/react-dom-bindings/src/shared/possibleStandardNames.js
9-
108
// When adding attributes to the HTML or SVG allowed attribute list, be sure to
119
// also add them to this module to ensure casing and incorrect name
1210
// warnings.
13-
export const standardAttributesToReactProps: Record<string, string> = {
11+
export const possibleStandardNames: Record<string, string> = {
1412
// HTML
1513
accept: "accept",
1614
acceptcharset: "acceptCharset",
@@ -506,6 +504,4 @@ export const standardAttributesToReactProps: Record<string, string> = {
506504
ychannelselector: "yChannelSelector",
507505
z: "z",
508506
zoomandpan: "zoomAndPan",
509-
// added by us
510-
dirname: "dirName",
511507
};

packages/html-data/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"scripts": {
99
"typecheck": "tsc",
1010
"build:elements": "tsx --conditions=webstudio ./bin/elements.ts && prettier --write ./src/__generated__",
11-
"build:attributes": "tsx --conditions=webstudio ./bin/attributes.ts && prettier --write ./src/__generated__",
11+
"build:attributes": "tsx --conditions=webstudio ./bin/attributes.ts && prettier --write ./src/__generated__ ../react-sdk/src/__generated__",
1212
"build:aria": "tsx --conditions=webstudio ./bin/aria.ts && prettier --write ./src/__generated__"
1313
},
1414
"devDependencies": {

packages/html-data/src/__generated__/aria-jsx-test.tsx

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/html-data/src/__generated__/aria.ts

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-sdk/src/__generated__/standard-attributes.ts

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)