|
1 |
| -import React from "preact/compat"; |
2 |
| -import ReactDOM from "preact/compat"; |
3 |
| -import { createLocationObject, pushState, replaceState } from "./utils"; |
4 |
| -import { |
5 |
| - HistoryProps, |
6 |
| - LinkProps, |
7 |
| - NavigateProps, |
8 |
| - FirstLoadProps, |
9 |
| -} from "./types"; |
10 |
| - |
11 |
| -/** |
12 |
| - * Interface used to bind a ReactPy node to React. |
13 |
| - */ |
14 |
| -export function bind(node) { |
15 |
| - return { |
16 |
| - create: (type, props, children) => |
17 |
| - React.createElement(type, props, ...children), |
18 |
| - render: (element) => { |
19 |
| - ReactDOM.render(element, node); |
20 |
| - }, |
21 |
| - unmount: () => ReactDOM.unmountComponentAtNode(node), |
22 |
| - }; |
23 |
| -} |
24 |
| - |
25 |
| -/** |
26 |
| - * History component that captures browser "history go back" actions and notifies the server. |
27 |
| - */ |
28 |
| -export function History({ onHistoryChangeCallback }: HistoryProps): null { |
29 |
| - React.useEffect(() => { |
30 |
| - // Register a listener for the "popstate" event and send data back to the server using the `onHistoryChange` callback. |
31 |
| - const listener = () => { |
32 |
| - onHistoryChangeCallback(createLocationObject()); |
33 |
| - }; |
34 |
| - |
35 |
| - // Register the event listener |
36 |
| - window.addEventListener("popstate", listener); |
37 |
| - |
38 |
| - // Delete the event listener when the component is unmounted |
39 |
| - return () => window.removeEventListener("popstate", listener); |
40 |
| - }); |
41 |
| - |
42 |
| - // Tell the server about the URL during the initial page load |
43 |
| - // FIXME: This code is commented out since it currently runs every time any component |
44 |
| - // is mounted due to a ReactPy core rendering bug. `FirstLoad` component is used instead. |
45 |
| - // https://github.com/reactive-python/reactpy/pull/1224 |
46 |
| - // React.useEffect(() => { |
47 |
| - // onHistoryChange({ |
48 |
| - // pathname: window.location.pathname, |
49 |
| - // search: window.location.search, |
50 |
| - // }); |
51 |
| - // return () => {}; |
52 |
| - // }, []); |
53 |
| - return null; |
54 |
| -} |
55 |
| - |
56 |
| -/** |
57 |
| - * Link component that captures clicks on anchor links and notifies the server. |
58 |
| - * |
59 |
| - * This component is not the actual `<a>` link element. It is just an event |
60 |
| - * listener for ReactPy-Router's server-side link component. |
61 |
| - * |
62 |
| - * @disabled This component is currently unused due to a ReactPy core rendering bug |
63 |
| - * which causes duplicate rendering (and thus duplicate event listeners). |
64 |
| - */ |
65 |
| -export function Link({ onClickCallback, linkClass }: LinkProps): null { |
66 |
| - React.useEffect(() => { |
67 |
| - // Event function that will tell the server about clicks |
68 |
| - const handleClick = (event: MouseEvent) => { |
69 |
| - event.preventDefault(); |
70 |
| - let to = (event.target as HTMLElement).getAttribute("href"); |
71 |
| - pushState(to); |
72 |
| - onClickCallback(createLocationObject()); |
73 |
| - }; |
74 |
| - |
75 |
| - // Register the event listener |
76 |
| - let link = document.querySelector(`.${linkClass}`); |
77 |
| - if (link) { |
78 |
| - link.addEventListener("click", handleClick); |
79 |
| - } else { |
80 |
| - console.warn(`Link component with class name ${linkClass} not found.`); |
81 |
| - } |
82 |
| - |
83 |
| - // Delete the event listener when the component is unmounted |
84 |
| - return () => { |
85 |
| - let link = document.querySelector(`.${linkClass}`); |
86 |
| - if (link) { |
87 |
| - link.removeEventListener("click", handleClick); |
88 |
| - } |
89 |
| - }; |
90 |
| - }); |
91 |
| - return null; |
92 |
| -} |
93 |
| - |
94 |
| -/** |
95 |
| - * Client-side portion of the navigate component, that allows the server to command the client to change URLs. |
96 |
| - */ |
97 |
| -export function Navigate({ |
98 |
| - onNavigateCallback, |
99 |
| - to, |
100 |
| - replace = false, |
101 |
| -}: NavigateProps): null { |
102 |
| - React.useEffect(() => { |
103 |
| - if (replace) { |
104 |
| - replaceState(to); |
105 |
| - } else { |
106 |
| - pushState(to); |
107 |
| - } |
108 |
| - onNavigateCallback(createLocationObject()); |
109 |
| - return () => {}; |
110 |
| - }, []); |
111 |
| - |
112 |
| - return null; |
113 |
| -} |
114 |
| - |
115 |
| -/** |
116 |
| - * FirstLoad component that captures the URL during the initial page load and notifies the server. |
117 |
| - * |
118 |
| - * FIXME: This component only exists because of a ReactPy core rendering bug, and should be removed when the bug |
119 |
| - * is fixed. In the future, all this logic should be handled by the `History` component. |
120 |
| - * https://github.com/reactive-python/reactpy/pull/1224 |
121 |
| - */ |
122 |
| -export function FirstLoad({ onFirstLoadCallback }: FirstLoadProps): null { |
123 |
| - React.useEffect(() => { |
124 |
| - onFirstLoadCallback(createLocationObject()); |
125 |
| - return () => {}; |
126 |
| - }, []); |
127 |
| - |
128 |
| - return null; |
129 |
| -} |
| 1 | +export { bind, History, Link, Navigate } from "./components"; |
0 commit comments