Skip to content

Commit 98b6145

Browse files
committed
refactor: add sdk-components-react-router package
This is 1:1 from sdk remix but with react-router imports to unlock step-by-step migration to react-router v7
1 parent b52ae76 commit 98b6145

28 files changed

+4307
-31
lines changed

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

Lines changed: 10 additions & 22 deletions
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

Lines changed: 4 additions & 9 deletions
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 } =

packages/sdk-components-react-router/LICENSE

Lines changed: 661 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Webstudio SDK Components
2+
3+
Webstudio SDK is a TypeScript API that lets you use your Webstudio project or some components in your custom codebase or just render a complete Remix Document.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "@webstudio-is/sdk-components-react-router",
3+
"version": "0.0.0-webstudio-version",
4+
"description": "Webstudio components for React Router",
5+
"author": "Webstudio <[email protected]>",
6+
"homepage": "https://webstudio.is",
7+
"license": "AGPL-3.0-or-later",
8+
"private": false,
9+
"type": "module",
10+
"files": [
11+
"lib/*",
12+
"!*.{test,stories}.*"
13+
],
14+
"sideEffects": false,
15+
"exports": {
16+
".": {
17+
"webstudio": "./src/components.ts",
18+
"types": "./lib/types/components.d.ts",
19+
"import": "./lib/components.js"
20+
},
21+
"./metas": {
22+
"webstudio": "./src/metas.ts",
23+
"types": "./lib/types/metas.d.ts",
24+
"import": "./lib/metas.js"
25+
},
26+
"./props": {
27+
"webstudio": "./src/props.ts",
28+
"types": "./lib/types/props.d.ts",
29+
"import": "./lib/props.js"
30+
}
31+
},
32+
"scripts": {
33+
"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\"",
35+
"dts": "tsc --project tsconfig.dts.json",
36+
"typecheck": "tsc"
37+
},
38+
"peerDependencies": {
39+
"react": "18.3.0-canary-14898b6a9-20240318",
40+
"react-dom": "18.3.0-canary-14898b6a9-20240318"
41+
},
42+
"dependencies": {
43+
"@webstudio-is/icons": "workspace:*",
44+
"@webstudio-is/react-sdk": "workspace:*",
45+
"@webstudio-is/sdk": "workspace:*",
46+
"@webstudio-is/sdk-components-react": "workspace:*",
47+
"react-router": "^7.0.2"
48+
},
49+
"devDependencies": {
50+
"@types/node": "^22.9.3",
51+
"@types/react": "^18.2.70",
52+
"@types/react-dom": "^18.2.25",
53+
"@webstudio-is/generate-arg-types": "workspace:*",
54+
"@webstudio-is/tsconfig": "workspace:*",
55+
"react": "18.3.0-canary-14898b6a9-20240318",
56+
"react-dom": "18.3.0-canary-14898b6a9-20240318"
57+
}
58+
}

0 commit comments

Comments
 (0)