Skip to content

Commit c5ce5f6

Browse files
authored
refactor: move index property to sdk (#5097)
Slowly moving stuff out of react-sdk
1 parent a80c062 commit c5ce5f6

File tree

11 files changed

+37
-43
lines changed

11 files changed

+37
-43
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ import {
2828
blockTemplateComponent,
2929
getIndexesWithinAncestors,
3030
} from "@webstudio-is/sdk";
31+
import { indexProperty, tagProperty } from "@webstudio-is/sdk/runtime";
3132
import {
3233
idAttribute,
3334
componentAttribute,
3435
showAttribute,
3536
selectorIdAttribute,
36-
indexAttribute,
3737
type AnyComponent,
3838
textContentAttribute,
3939
} from "@webstudio-is/react-sdk";
@@ -70,7 +70,6 @@ import {
7070
editablePlaceholderAttribute,
7171
editingPlaceholderVariable,
7272
} from "~/canvas/shared/styles";
73-
import { tagProperty } from "@webstudio-is/sdk/runtime";
7473

7574
const ContentEditable = ({
7675
placeholder,
@@ -294,7 +293,7 @@ const useInstanceProps = (instanceSelector: InstanceSelector) => {
294293
}
295294
const index = indexesWithinAncestors.get(instanceId);
296295
if (index !== undefined) {
297-
instancePropsObject[indexAttribute] = index.toString();
296+
instancePropsObject[indexProperty] = index.toString();
298297
}
299298
const instanceProps = propValuesByInstanceSelector.get(instanceKey);
300299
if (instanceProps) {

packages/react-sdk/src/component-generator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
getIndexesWithinAncestors,
2222
elementComponent,
2323
} from "@webstudio-is/sdk";
24-
import { tagProperty } from "@webstudio-is/sdk/runtime";
25-
import { indexAttribute, isAttributeNameSafe, showAttribute } from "./props";
24+
import { indexProperty, tagProperty } from "@webstudio-is/sdk/runtime";
25+
import { isAttributeNameSafe, showAttribute } from "./props";
2626
import { standardAttributesToReactProps } from "./standard-attributes";
2727

2828
/**
@@ -172,7 +172,7 @@ export const generateJsxElement = ({
172172

173173
const index = indexesWithinAncestors.get(instance.id);
174174
if (index !== undefined) {
175-
generatedProps += `\n${indexAttribute}="${index}"`;
175+
generatedProps += `\n${indexProperty}="${index}"`;
176176
}
177177
if (instance.tag !== undefined && instance.component !== elementComponent) {
178178
generatedProps += `\n${tagProperty}=${JSON.stringify(instance.tag)}`;

packages/react-sdk/src/props.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ export const idAttribute = "data-ws-id" as const;
126126
export const selectorIdAttribute = "data-ws-selector" as const;
127127
export const componentAttribute = "data-ws-component" as const;
128128
export const showAttribute = "data-ws-show" as const;
129-
export const indexAttribute = "data-ws-index" as const;
130129
export const collapsedAttribute = "data-ws-collapsed" as const;
131130
export const textContentAttribute = "data-ws-text-content" as const;
132131
export const animationCanPlayOnCanvasAttribute =

packages/react-sdk/src/runtime.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,3 @@ export { PageSettingsCanonicalLink } from "./page-settings-canonical-link";
1111
*/
1212
export const xmlNodeTagSuffix =
1313
"-ws-xml-node-fb77f896-8e96-40b9-b8f8-90a4e70d724a";
14-
15-
export const getIndexWithinAncestorFromComponentProps = (
16-
props: Record<string, unknown>
17-
) => {
18-
return props["data-ws-index"] as string | undefined;
19-
};

packages/sdk-components-react-radix/src/accordion.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ import {
1212
Trigger,
1313
Content,
1414
} from "@radix-ui/react-accordion";
15-
import {
16-
getClosestInstance,
17-
getIndexWithinAncestorFromComponentProps,
18-
type Hook,
19-
} from "@webstudio-is/react-sdk/runtime";
15+
import { getIndexWithinAncestorFromProps } from "@webstudio-is/sdk/runtime";
16+
import { getClosestInstance, type Hook } from "@webstudio-is/react-sdk/runtime";
2017

2118
export const Accordion = forwardRef<
2219
HTMLDivElement,
@@ -32,7 +29,7 @@ export const AccordionItem = forwardRef<
3229
HTMLDivElement,
3330
Omit<ComponentPropsWithoutRef<typeof Item>, "value"> & { value?: string }
3431
>(({ value, ...props }, ref) => {
35-
const index = getIndexWithinAncestorFromComponentProps(props);
32+
const index = getIndexWithinAncestorFromProps(props);
3633
return <Item ref={ref} value={value ?? index ?? ""} {...props} />;
3734
});
3835

packages/sdk-components-react-radix/src/navigation-menu.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu";
2-
import {
3-
getClosestInstance,
4-
getIndexWithinAncestorFromComponentProps,
5-
ReactSdkContext,
6-
type Hook,
7-
} from "@webstudio-is/react-sdk/runtime";
81
import {
92
Children,
103
forwardRef,
114
type ComponentPropsWithoutRef,
125
useContext,
136
} from "react";
7+
import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu";
8+
import { getIndexWithinAncestorFromProps } from "@webstudio-is/sdk/runtime";
9+
import {
10+
getClosestInstance,
11+
ReactSdkContext,
12+
type Hook,
13+
} from "@webstudio-is/react-sdk/runtime";
1414

1515
export const NavigationMenu = forwardRef<
1616
HTMLLIElement,
@@ -43,7 +43,7 @@ export const NavigationMenuItem = forwardRef<
4343
HTMLLIElement,
4444
Omit<ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.Item>, "asChild">
4545
>(({ value, ...props }, ref) => {
46-
const index = getIndexWithinAncestorFromComponentProps(props);
46+
const index = getIndexWithinAncestorFromProps(props);
4747
return (
4848
<NavigationMenuPrimitive.Item ref={ref} value={value ?? index} {...props} />
4949
);

packages/sdk-components-react-radix/src/tabs.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1+
import interactionResponse from "await-interaction-response";
12
import { type ComponentPropsWithoutRef, forwardRef, useCallback } from "react";
2-
import { Root, List, Trigger, Content } from "@radix-ui/react-tabs";
3-
import {
4-
getClosestInstance,
5-
getIndexWithinAncestorFromComponentProps,
6-
type Hook,
7-
} from "@webstudio-is/react-sdk/runtime";
83
import { useControllableState } from "@radix-ui/react-use-controllable-state";
9-
import interactionResponse from "await-interaction-response";
4+
import { Root, List, Trigger, Content } from "@radix-ui/react-tabs";
5+
import { getIndexWithinAncestorFromProps } from "@webstudio-is/sdk/runtime";
6+
import { getClosestInstance, type Hook } from "@webstudio-is/react-sdk/runtime";
107

118
export const Tabs = forwardRef<
129
HTMLDivElement,
@@ -45,15 +42,15 @@ export const TabsTrigger = forwardRef<
4542
HTMLButtonElement,
4643
Omit<ComponentPropsWithoutRef<typeof Trigger>, "value"> & { value?: string }
4744
>(({ value, ...props }, ref) => {
48-
const index = getIndexWithinAncestorFromComponentProps(props);
45+
const index = getIndexWithinAncestorFromProps(props);
4946
return <Trigger ref={ref} value={value ?? index ?? ""} {...props} />;
5047
});
5148

5249
export const TabsContent = forwardRef<
5350
HTMLDivElement,
5451
Omit<ComponentPropsWithoutRef<typeof Content>, "value"> & { value?: string }
5552
>(({ value, ...props }, ref) => {
56-
const index = getIndexWithinAncestorFromComponentProps(props);
53+
const index = getIndexWithinAncestorFromProps(props);
5754
return <Content ref={ref} value={value ?? index ?? ""} {...props} />;
5855
});
5956

packages/sdk-components-react/src/box.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type ElementRef,
55
type ComponentProps,
66
} from "react";
7-
import { getTagFromComponentProps } from "@webstudio-is/sdk/runtime";
7+
import { getTagFromProps } from "@webstudio-is/sdk/runtime";
88

99
const defaultTag = "div";
1010

@@ -14,7 +14,7 @@ type Props = ComponentProps<typeof defaultTag> & {
1414

1515
export const Box = forwardRef<ElementRef<typeof defaultTag>, Props>(
1616
({ tag: legacyTag, ...props }, ref) => {
17-
const tag = getTagFromComponentProps(props) ?? legacyTag ?? defaultTag;
17+
const tag = getTagFromProps(props) ?? legacyTag ?? defaultTag;
1818
return createElement(tag, { ...props, ref });
1919
}
2020
);

packages/sdk-components-react/src/heading.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type ComponentProps,
55
createElement,
66
} from "react";
7-
import { getTagFromComponentProps } from "@webstudio-is/sdk/runtime";
7+
import { getTagFromProps } from "@webstudio-is/sdk/runtime";
88

99
const defaultTag = "h1";
1010

@@ -14,7 +14,7 @@ type Props = ComponentProps<typeof defaultTag> & {
1414

1515
export const Heading = forwardRef<ElementRef<typeof defaultTag>, Props>(
1616
({ tag: legacyTag, ...props }, ref) => {
17-
const tag = getTagFromComponentProps(props) ?? legacyTag ?? defaultTag;
17+
const tag = getTagFromProps(props) ?? legacyTag ?? defaultTag;
1818
return createElement(tag, { ...props, ref });
1919
}
2020
);

packages/sdk-components-react/src/text.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type ElementRef,
55
type ComponentProps,
66
} from "react";
7-
import { getTagFromComponentProps } from "@webstudio-is/sdk/runtime";
7+
import { getTagFromProps } from "@webstudio-is/sdk/runtime";
88

99
const defaultTag = "div";
1010

@@ -14,7 +14,7 @@ type Props = ComponentProps<typeof defaultTag> & {
1414

1515
export const Text = forwardRef<ElementRef<typeof defaultTag>, Props>(
1616
({ tag: legacyTag, ...props }, ref) => {
17-
const tag = getTagFromComponentProps(props) ?? legacyTag ?? defaultTag;
17+
const tag = getTagFromProps(props) ?? legacyTag ?? defaultTag;
1818
return createElement(tag, { ...props, ref });
1919
}
2020
);

0 commit comments

Comments
 (0)