Skip to content

Commit 9aeb7a6

Browse files
authored
refactor: add sdk-components-react-router package (#4596)
This is 1:1 from sdk remix but with react-router imports to unlock step-by-step migration to react-router v7
1 parent bc12e5d commit 9aeb7a6

19 files changed

+1045
-39
lines changed

packages/sdk-components-react-remix/package.json

-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
},
3232
"scripts": {
3333
"build": "vite build --config ../../vite.sdk-components.config.ts",
34-
"build:args": "NODE_OPTIONS=--conditions=webstudio generate-arg-types './src/*.tsx !./src/**/*.stories.tsx !./src/**/*.ws.ts' && prettier --write \"**/*.props.ts\"",
3534
"dts": "tsc --project tsconfig.dts.json",
3635
"typecheck": "tsc"
3736
},
@@ -41,7 +40,6 @@
4140
"react-dom": "18.3.0-canary-14898b6a9-20240318"
4241
},
4342
"dependencies": {
44-
"@webstudio-is/icons": "workspace:*",
4543
"@webstudio-is/react-sdk": "workspace:*",
4644
"@webstudio-is/sdk": "workspace:*",
4745
"@webstudio-is/sdk-components-react": "workspace:*"
@@ -50,7 +48,6 @@
5048
"@remix-run/react": "^2.15.2",
5149
"@types/react": "^18.2.70",
5250
"@types/react-dom": "^18.2.25",
53-
"@webstudio-is/generate-arg-types": "workspace:*",
5451
"@webstudio-is/tsconfig": "workspace:*",
5552
"react": "18.3.0-canary-14898b6a9-20240318",
5653
"react-dom": "18.3.0-canary-14898b6a9-20240318"

packages/sdk-components-react-remix/src/remix-form.tsx

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import {
2-
type ElementRef,
3-
type ComponentProps,
4-
forwardRef,
5-
useContext,
6-
} from "react";
1+
import { type ElementRef, type ComponentProps, forwardRef } from "react";
72
import { Form, type FormProps } from "@remix-run/react";
8-
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
93

104
export const defaultTag = "form";
115

@@ -19,27 +13,21 @@ export const RemixForm = forwardRef<
1913
action?: string;
2014
}
2115
>(({ action, ...props }, ref) => {
22-
const { renderer } = useContext(ReactSdkContext);
2316
// remix casts action to relative url
2417
if (
2518
action === undefined ||
2619
action === "" ||
2720
(typeof action === "string" && action?.startsWith("/"))
2821
) {
29-
// remix forms specifies own action instead of provided one
30-
// which makes it hard to handle intercepted submit events
31-
// render remix form only in published sites
32-
if (renderer !== "canvas" && renderer !== "preview") {
33-
return (
34-
<Form
35-
action={action}
36-
{...props}
37-
ref={ref}
38-
// Preserve scroll position for navigation on the same path, as it's used for filtering and sorting
39-
preventScrollReset={action === undefined || action === ""}
40-
/>
41-
);
42-
}
22+
return (
23+
<Form
24+
action={action}
25+
{...props}
26+
ref={ref}
27+
// Preserve scroll position for navigation on the same path, as it's used for filtering and sorting
28+
preventScrollReset={action === undefined || action === ""}
29+
/>
30+
);
4331
}
4432
return <form {...props} ref={ref} />;
4533
});

packages/sdk-components-react-remix/src/shared/remix-link.tsx

+4-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Props = Omit<ComponentPropsWithoutRef<typeof Link>, "target"> & {
1616

1717
export const wrapLinkComponent = (BaseLink: typeof Link) => {
1818
const Component = forwardRef<HTMLAnchorElement, Props>((props, ref) => {
19-
const { assetBaseUrl, renderer } = useContext(ReactSdkContext);
19+
const { assetBaseUrl } = useContext(ReactSdkContext);
2020
// cast to string when invalid value type is provided with binding
2121
const href = String(props.href ?? "");
2222

@@ -29,14 +29,9 @@ export const wrapLinkComponent = (BaseLink: typeof Link) => {
2929
href.startsWith("#") ||
3030
(href.startsWith("/") && href.startsWith(assetBaseUrl) === false)
3131
) {
32-
// remix links behave in unexpected way when delete in content editable
33-
// always render simple <a> in canvas and preview
34-
// since remix links do not affect it
35-
if (renderer !== "canvas" && renderer !== "preview") {
36-
// In the future, we will switch to the :local-link pseudo-class (https://developer.mozilla.org/en-US/docs/Web/CSS/:local-link). (aria-current="page" is used now)
37-
// Therefore, we decided to use end={true} (exact route matching) for all links to facilitate easier migration.
38-
return <RemixLink {...props} to={href} ref={ref} end />;
39-
}
32+
// In the future, we will switch to the :local-link pseudo-class (https://developer.mozilla.org/en-US/docs/Web/CSS/:local-link). (aria-current="page" is used now)
33+
// Therefore, we decided to use end={true} (exact route matching) for all links to facilitate easier migration.
34+
return <RemixLink {...props} to={href} ref={ref} end />;
4035
}
4136

4237
const { prefetch, reloadDocument, replace, preventScrollReset, ...rest } =

0 commit comments

Comments
 (0)