deno.land / x / netzo@0.5.16 / components / blocks / plot.tsx

نووسراو ببینە
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { IS_BROWSER } from "$fresh/runtime.ts";import { createElement as h } from "preact";import { useEffect, useRef } from "preact/compat";import * as Plot from "../../deps/@observablehq/plot.ts";
export * from "../../deps/@observablehq/plot.ts";
export function usePlot<T = Plot.Data>(options: Plot.PlotOptions) { if (!IS_BROWSER) return;
const containerRef = useRef();
useEffect(() => { const plot = Plot.plot(options); // .replaceWith() to replace the server-side rendered plot with // the client-side (hydrated) plot entirely (avoids flickering) containerRef.current.replaceWith(plot); }, []);
return containerRef;}
/** * Isomorphic Plot component for rendering charts on the server and * hydrating them on the client for client-side interactivity. * * @example import * as Plot from "netzo/components/blocks/plot/plot.tsx"; * export default () => <Plot.Figure options={options} />; * * @param {Plot.PlotOptions} options - the plot options * @returns {JSX.Element} the plot figure */export function Figure({ options }: { options: Plot.PlotOptions }) { // // server-side render: uses a virtual Document implementation to generate HTML // if (!IS_BROWSER) return <PlotSSR options={options} />;
// // client-side render: uses a real DOM container and the usePlot() hook for mounting // const containerRef = usePlot(options); // return <figure ref={containerRef} />;
// client-side render: uses a real DOM container and the usePlot() hook for mounting const containerRef = usePlot(options); return ( <figure ref={containerRef}> <PlotSSR options={options} /> </figure> );}
function PlotSSR({ options }: { options: Plot.PlotOptions }) { return Plot.plot({ ...options, document: new Document() }).toHyperScript();}
// (virtual) Document implementation for SSR:
class Document { constructor() { this.documentElement = new Element(this, "html"); } createElementNS(namespace, tagName) { return new Element(this, tagName); } createElement(tagName) { return new Element(this, tagName); } createTextNode(value) { return new TextNode(this, value); } querySelector() { return null; } querySelectorAll() { return []; }}
class Style { static empty = new Style(); setProperty() {} removeProperty() {}}
class Element { constructor(ownerDocument, tagName) { this.ownerDocument = ownerDocument; this.tagName = tagName; this.attributes = {}; this.children = []; this.parentNode = null; } setAttribute(name, value) { this.attributes[name] = String(value); } setAttributeNS(namespace, name, value) { this.setAttribute(name, value); } getAttribute(name) { return this.attributes[name]; } getAttributeNS(name) { return this.getAttribute(name); } hasAttribute(name) { return name in this.attributes; } hasAttributeNS(name) { return this.hasAttribute(name); } removeAttribute(name) { delete this.attributes[name]; } removeAttributeNS(namespace, name) { this.removeAttribute(name); } addEventListener() { // ignored; interaction needs real DOM } removeEventListener() { // ignored; interaction needs real DOM } dispatchEvent() { // ignored; interaction needs real DOM } append(...children) { for (const child of children) { this.appendChild( child?.ownerDocument ? child : this.ownerDocument.createTextNode(child), ); } } appendChild(child) { this.children.push(child); child.parentNode = this; return child; } insertBefore(child, after) { if (after == null) { this.children.push(child); } else { const i = this.children.indexOf(after); if (i < 0) throw new Error("insertBefore reference node not found"); this.children.splice(i, 0, child); } child.parentNode = this; return child; } querySelector() { return null; } querySelectorAll() { return []; } set textContent(value) { this.children = [this.ownerDocument.createTextNode(value)]; } set style(value) { this.attributes.style = value; } get style() { return Style.empty; } toHyperScript() { return h( this.tagName, this.attributes, this.children.map((c) => c.toHyperScript()), ); }}
class TextNode { constructor(ownerDocument, nodeValue) { this.ownerDocument = ownerDocument; this.nodeValue = String(nodeValue); } toHyperScript() { return this.nodeValue; }}
netzo

Version Info

Tagged at
a year ago