-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-host.html
More file actions
82 lines (76 loc) · 2.77 KB
/
Copy pathreact-host.html
File metadata and controls
82 lines (76 loc) · 2.77 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SendBSV embed — React host using <sendbsv-pay></title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 480px;
margin: 48px auto;
padding: 0 16px;
color: #1a1a1a;
}
.result {
margin-top: 24px;
padding: 12px;
border: 1px solid #ddd;
border-radius: 6px;
font-family: ui-monospace, monospace;
font-size: 12px;
white-space: pre-wrap;
word-break: break-all;
}
</style>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@18/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js" crossorigin></script>
<script src="../../dist-embed/embed.js"></script>
<script>
/**
* React host using the <sendbsv-pay> custom element. The element
* fires `sendbsv-success` / `-declined` / `-error` as native
* CustomEvents — React's synthetic-event layer doesn't know about
* custom events by name, so we bind via a ref + addEventListener
* in useEffect (standard React-with-custom-elements pattern).
*/
const { useState, useEffect, useRef, createElement: h } = React;
function PayButton() {
const ref = useRef(null);
const [result, setResult] = useState("Awaiting interaction…");
useEffect(() => {
const el = ref.current;
if (!el) return;
const onSuccess = (e) => setResult(`✓ paid — txid ${e.detail.txid}\namount ${e.detail.amount} sats`);
const onDeclined = (e) => setResult(`× declined — ${e.detail.reason}`);
const onError = (e) => setResult(`! error — ${e.detail.message}`);
el.addEventListener("sendbsv-success", onSuccess);
el.addEventListener("sendbsv-declined", onDeclined);
el.addEventListener("sendbsv-error", onError);
return () => {
el.removeEventListener("sendbsv-success", onSuccess);
el.removeEventListener("sendbsv-declined", onDeclined);
el.removeEventListener("sendbsv-error", onError);
};
}, []);
return h("div", null,
h("h1", null, "Donate to the project"),
h("p", null, "React app, custom element, no third-party script bundler needed."),
h("sendbsv-pay", {
ref,
to: "alice@sendbsv.com",
amount: "500",
description: "Donation — React demo",
label: "Donate 500 sats"
}),
h("div", { className: "result" }, result)
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(h(PayButton));
</script>
</body>
</html>