forked from reactive-python/reactpy-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (39 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import React from "react";
import ReactDOM from "react-dom";
import htm from "htm";
const html = htm.bind(React.createElement);
export function bind(node) {
return {
create: (type, props, children) =>
React.createElement(type, props, ...children),
render: (element) => {
ReactDOM.render(element, node);
},
unmount: () => ReactDOM.unmountComponentAtNode(node),
};
}
export function History({ onChange }) {
// capture changes to the browser's history
React.useEffect(() => {
const listener = () => {
onChange({
pathname: window.location.pathname,
search: window.location.search,
});
};
window.addEventListener("popstate", listener);
return () => window.removeEventListener("popstate", listener);
});
return null;
}
export function Link({ to, onClick, children, ...props }) {
const handleClick = (event) => {
event.preventDefault();
window.history.pushState({}, to, new URL(to, window.location));
onClick({
pathname: window.location.pathname,
search: window.location.search,
});
};
return html`<a href=${to} onClick=${handleClick} ...${props}>${children}</a>`;
}