@pylonsync/react 0.22.74 → 0.22.76

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/dist/Link.d.ts CHANGED
@@ -47,6 +47,11 @@ export interface LinkProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorEle
47
47
  * has somewhere to show).
48
48
  */
49
49
  seed?: unknown;
50
+ /**
51
+ * A ref to the anchor element. A component that renders through this link,
52
+ * such as a button with a `render` prop, passes one.
53
+ */
54
+ ref?: React.Ref<HTMLAnchorElement>;
50
55
  children?: React.ReactNode;
51
56
  }
52
- export declare function Link({ href, prefetch, seed, children, ...rest }: LinkProps): React.JSX.Element;
57
+ export declare function Link({ href, prefetch, seed, ref: callerRef, children, ...rest }: LinkProps): React.JSX.Element;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.22.74",
6
+ "version": "0.22.76",
7
7
  "type": "module",
8
8
  "main": "./src/index.ts",
9
9
  "types": "./dist/index.d.ts",
@@ -14,9 +14,9 @@
14
14
  "prepack": "bun run build"
15
15
  },
16
16
  "dependencies": {
17
- "@pylonsync/realtime": "0.22.74",
18
- "@pylonsync/sdk": "0.22.74",
19
- "@pylonsync/sync": "0.22.74"
17
+ "@pylonsync/realtime": "0.22.76",
18
+ "@pylonsync/sdk": "0.22.76",
19
+ "@pylonsync/sync": "0.22.76"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": ">=19.0.0"
@@ -0,0 +1,76 @@
1
+ // A component that renders through <Link> (a button with a `render` prop)
2
+ // passes its own ref. The anchor reaches that ref, and Link still warms the
3
+ // destination and registers the seed for the same anchor.
4
+
5
+ import { afterEach, expect, test } from "bun:test";
6
+ import { cleanup, render } from "@testing-library/react";
7
+ import { createRef } from "react";
8
+
9
+ import { Link } from "./Link";
10
+
11
+ type PylonWindow = {
12
+ __pylon?: { prefetch: (href: string, opts?: { document?: boolean }) => void };
13
+ __pylonLinkSeeds?: WeakMap<Element, unknown>;
14
+ };
15
+
16
+ afterEach(() => {
17
+ cleanup();
18
+ delete (window as PylonWindow).__pylon;
19
+ delete (window as PylonWindow).__pylonLinkSeeds;
20
+ });
21
+
22
+ function recordPrefetches() {
23
+ const hrefs: string[] = [];
24
+ (window as PylonWindow).__pylon = { prefetch: (href) => void hrefs.push(href) };
25
+ return hrefs;
26
+ }
27
+
28
+ const idle = () => new Promise((resolve) => setTimeout(resolve, 300));
29
+
30
+ test("an object ref from the caller receives the anchor, and the link still prefetches", async () => {
31
+ const prefetched = recordPrefetches();
32
+ const ref = createRef<HTMLAnchorElement>();
33
+
34
+ const { container, unmount } = render(
35
+ <Link href="/sell" ref={ref} seed={{ id: 1 }}>
36
+ Sell
37
+ </Link>,
38
+ );
39
+
40
+ const anchor = container.querySelector("a");
41
+ expect(anchor).not.toBeNull();
42
+ expect(ref.current).toBe(anchor);
43
+ expect((window as PylonWindow).__pylonLinkSeeds?.get(anchor!)).toEqual({ id: 1 });
44
+
45
+ await idle();
46
+ expect(prefetched).toContain("/sell");
47
+
48
+ unmount();
49
+ expect(ref.current).toBeNull();
50
+ });
51
+
52
+ test("a callback ref from the caller receives the anchor and runs its cleanup", async () => {
53
+ const prefetched = recordPrefetches();
54
+ const calls: string[] = [];
55
+
56
+ const { unmount } = render(
57
+ <Link
58
+ href="/account"
59
+ ref={(el) => {
60
+ calls.push(el ? "attach" : "null");
61
+ return () => {
62
+ calls.push("cleanup");
63
+ };
64
+ }}
65
+ >
66
+ Account
67
+ </Link>,
68
+ );
69
+
70
+ expect(calls).toEqual(["attach"]);
71
+ await idle();
72
+ expect(prefetched).toContain("/account");
73
+
74
+ unmount();
75
+ expect(calls).toEqual(["attach", "cleanup"]);
76
+ });
package/src/Link.tsx CHANGED
@@ -34,7 +34,7 @@
34
34
  // Disable client-side nav for a specific link with `prefetch={false}`
35
35
  // (HTML still prefetches) or by setting `target="_blank"`.
36
36
 
37
- import React, { useEffect, useRef } from "react";
37
+ import React, { useCallback, useEffect, useRef } from "react";
38
38
  import { watchLinkIntent } from "./linkIntent";
39
39
 
40
40
  declare global {
@@ -89,6 +89,11 @@ export interface LinkProps
89
89
  * has somewhere to show).
90
90
  */
91
91
  seed?: unknown;
92
+ /**
93
+ * A ref to the anchor element. A component that renders through this link,
94
+ * such as a button with a `render` prop, passes one.
95
+ */
96
+ ref?: React.Ref<HTMLAnchorElement>;
92
97
  children?: React.ReactNode;
93
98
  }
94
99
 
@@ -96,11 +101,34 @@ export function Link({
96
101
  href,
97
102
  prefetch = true,
98
103
  seed,
104
+ ref: callerRef,
99
105
  children,
100
106
  ...rest
101
107
  }: LinkProps) {
102
108
  const ref = useRef<HTMLAnchorElement>(null);
103
109
 
110
+ // The anchor goes to both the caller's ref and this component's, which the
111
+ // effects below read.
112
+ const setRef = useCallback(
113
+ (el: HTMLAnchorElement | null) => {
114
+ ref.current = el;
115
+ if (typeof callerRef === "function") {
116
+ const cleanup = callerRef(el);
117
+ return () => {
118
+ ref.current = null;
119
+ if (typeof cleanup === "function") cleanup();
120
+ else callerRef(null);
121
+ };
122
+ }
123
+ if (callerRef) callerRef.current = el;
124
+ return () => {
125
+ ref.current = null;
126
+ if (callerRef) callerRef.current = null;
127
+ };
128
+ },
129
+ [callerRef],
130
+ );
131
+
104
132
  // Register the seed keyed by this anchor element so the runtime's global
105
133
  // click handler can find it and hand it to navigate(). A WeakMap keeps it
106
134
  // GC-safe (no leak when the link unmounts) and off the DOM node itself.
@@ -181,7 +209,7 @@ export function Link({
181
209
 
182
210
  return (
183
211
  <a
184
- ref={ref}
212
+ ref={setRef}
185
213
  href={href}
186
214
  data-pylon-link=""
187
215
  {...rest}