@webstudio-is/sdk-components-react-remix 0.95.0 → 0.96.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/components.js CHANGED
@@ -1,4 +1,82 @@
1
- "use strict";
2
- export { Link } from "./link";
3
- export { RichTextLink } from "./rich-text-link";
4
- export { Form } from "./form";
1
+ // src/link.tsx
2
+ import { Link as BaseLink } from "@webstudio-is/sdk-components-react";
3
+
4
+ // src/shared/remix-link.tsx
5
+ import { forwardRef } from "react";
6
+ import { NavLink as RemixLink } from "@remix-run/react";
7
+ import {
8
+ usePropUrl,
9
+ getInstanceIdFromComponentProps
10
+ } from "@webstudio-is/react-sdk";
11
+ import { jsx } from "react/jsx-runtime";
12
+ var wrapLinkComponent = (BaseLink3) => {
13
+ const Component = forwardRef((props, ref) => {
14
+ const href = usePropUrl(getInstanceIdFromComponentProps(props), "href");
15
+ if (href?.type === "page") {
16
+ let to = href.page.path === "" ? "/" : href.page.path;
17
+ const urlTo = new URL(to, "https://any-valid.url");
18
+ to = urlTo.pathname;
19
+ if (href.hash !== void 0) {
20
+ urlTo.hash = encodeURIComponent(href.hash);
21
+ to = `${urlTo.pathname}${urlTo.hash}`;
22
+ }
23
+ return /* @__PURE__ */ jsx(RemixLink, { ...props, to, ref });
24
+ }
25
+ const { prefetch, reloadDocument, replace, preventScrollReset, ...rest } = props;
26
+ return /* @__PURE__ */ jsx(BaseLink3, { ...rest, ref });
27
+ });
28
+ Component.displayName = BaseLink3.displayName;
29
+ return Component;
30
+ };
31
+
32
+ // src/link.tsx
33
+ var Link = wrapLinkComponent(BaseLink);
34
+
35
+ // src/rich-text-link.tsx
36
+ import { RichTextLink as BaseLink2 } from "@webstudio-is/sdk-components-react";
37
+ var RichTextLink = wrapLinkComponent(BaseLink2);
38
+
39
+ // src/form.tsx
40
+ import {
41
+ forwardRef as forwardRef2,
42
+ useRef,
43
+ useEffect,
44
+ useContext
45
+ } from "react";
46
+ import { useFetcher } from "@remix-run/react";
47
+ import { formIdFieldName } from "@webstudio-is/form-handlers";
48
+ import {
49
+ ReactSdkContext,
50
+ getInstanceIdFromComponentProps as getInstanceIdFromComponentProps2
51
+ } from "@webstudio-is/react-sdk";
52
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
53
+ var useOnFetchEnd = (fetcher, handler) => {
54
+ const latestHandler = useRef(handler);
55
+ latestHandler.current = handler;
56
+ const prevFetcher = useRef(fetcher);
57
+ useEffect(() => {
58
+ if (prevFetcher.current.state !== fetcher.state && fetcher.state === "idle" && fetcher.data !== void 0) {
59
+ latestHandler.current(fetcher.data);
60
+ }
61
+ prevFetcher.current = fetcher;
62
+ }, [fetcher]);
63
+ };
64
+ var Form = forwardRef2(({ children, action, method, state = "initial", ...rest }, ref) => {
65
+ const { setBoundDataSourceValue } = useContext(ReactSdkContext);
66
+ const fetcher = useFetcher();
67
+ const instanceId = getInstanceIdFromComponentProps2(rest);
68
+ useOnFetchEnd(fetcher, (data) => {
69
+ const state2 = data?.success === true ? "success" : "error";
70
+ setBoundDataSourceValue(instanceId, "state", state2);
71
+ });
72
+ return /* @__PURE__ */ jsxs(fetcher.Form, { ...rest, method: "post", "data-state": state, ref, children: [
73
+ /* @__PURE__ */ jsx2("input", { type: "hidden", name: formIdFieldName, value: instanceId }),
74
+ children
75
+ ] });
76
+ });
77
+ Form.displayName = "Form";
78
+ export {
79
+ Form,
80
+ Link,
81
+ RichTextLink
82
+ };