mathbox-react 0.0.4 → 0.0.5-dev

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.
Files changed (54) hide show
  1. package/.eslintrc.js +3 -2
  2. package/.github/workflows/lint.yaml +3 -1
  3. package/.prettierrc +1 -0
  4. package/babel.config.js +3 -0
  5. package/build/cjs/index.js +1 -1
  6. package/build/cjs/index.js.map +1 -1
  7. package/build/cjs/types/components/Cartesian.spec.d.ts +1 -0
  8. package/build/cjs/types/components/Mathbox.d.ts +7 -5
  9. package/build/cjs/types/components/MathboxNodeContext.d.ts +4 -0
  10. package/build/cjs/types/components/components.d.ts +261 -0
  11. package/build/cjs/types/components/hooks.d.ts +4 -0
  12. package/build/cjs/types/components/index.d.ts +2 -0
  13. package/build/cjs/types/components/types.d.ts +7 -0
  14. package/build/cjs/types/index.d.ts +1 -2
  15. package/build/cjs/types/stories/utils.d.ts +5 -0
  16. package/build/cjs/types/testSetup.d.ts +1 -0
  17. package/build/cjs/types/testUtils.d.ts +1 -0
  18. package/build/esm/index.js +15 -1
  19. package/build/esm/index.js.map +1 -1
  20. package/build/esm/types/components/Cartesian.spec.d.ts +1 -0
  21. package/build/esm/types/components/Mathbox.d.ts +7 -5
  22. package/build/esm/types/components/MathboxNodeContext.d.ts +4 -0
  23. package/build/esm/types/components/components.d.ts +261 -0
  24. package/build/esm/types/components/hooks.d.ts +4 -0
  25. package/build/esm/types/components/index.d.ts +2 -0
  26. package/build/esm/types/components/types.d.ts +7 -0
  27. package/build/esm/types/index.d.ts +1 -2
  28. package/build/esm/types/stories/utils.d.ts +5 -0
  29. package/build/esm/types/testSetup.d.ts +1 -0
  30. package/build/esm/types/testUtils.d.ts +1 -0
  31. package/build/index.d.ts +271 -5
  32. package/jest.config.js +12 -0
  33. package/mathbox-react-0.0.4.tgz +0 -0
  34. package/package.json +15 -6
  35. package/scratch.js +149 -0
  36. package/src/components/Cartesian.spec.tsx +101 -0
  37. package/src/components/Mathbox.tsx +50 -33
  38. package/src/components/MathboxNodeContext.ts +6 -0
  39. package/src/components/components.tsx +361 -0
  40. package/src/components/hooks.ts +51 -0
  41. package/src/components/index.ts +2 -0
  42. package/src/components/types.ts +9 -0
  43. package/src/index.ts +1 -3
  44. package/src/stories/Cartesian.stories.tsx +36 -0
  45. package/src/stories/Grid.stories.tsx +38 -0
  46. package/src/stories/Mathbox.stories.tsx +16 -0
  47. package/src/stories/Point.stories.tsx +68 -0
  48. package/src/stories/utils.tsx +31 -0
  49. package/src/testSetup.ts +3 -0
  50. package/src/testUtils.ts +46 -0
  51. package/tsconfig.base.json +0 -1
  52. package/tsconfig.json +1 -0
  53. package/src/components/Mathbox.stories.tsx +0 -28
  54. package/src/types.d.ts +0 -4
@@ -1,41 +1,58 @@
1
- import { useEffect } from 'react'
2
- import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
3
- import { Color } from "three/src/math/Color";
4
- import * as MB from 'mathbox'
1
+ import React, {
2
+ useEffect,
3
+ useState,
4
+ forwardRef,
5
+ useImperativeHandle,
6
+ useMemo,
7
+ } from "react"
8
+ import { Color } from "three"
9
+ import { mathBox, MathboxSelection, MathBoxOptions } from "mathbox"
10
+ import MathboxAPIContext from "./MathboxNodeContext"
5
11
 
6
12
  type Props = {
7
- element: HTMLElement
8
- }
13
+ options?: MathBoxOptions
14
+ initialCameraPosition?: number[]
15
+ } & React.HTMLProps<HTMLDivElement>
9
16
 
10
- const Mathbox = (props: Props) => {
17
+ const Mathbox = (
18
+ props: Props,
19
+ ref: React.Ref<MathboxSelection<"root"> | null>
20
+ ) => {
21
+ const { children, initialCameraPosition, options, ...divProps } = props
22
+ const mathboxOptions = useMemo(() => options ?? {}, [options])
23
+ const [selection, setSelection] = useState<MathboxSelection<"root"> | null>(
24
+ null
25
+ )
26
+ const [container, setContainer] = useState<HTMLDivElement | null>(null)
11
27
  useEffect(() => {
12
- const mathbox = MB.mathBox({
13
- plugins: ["core", "controls", "cursor"],
14
- controls: {
15
- klass: OrbitControls,
16
- },
17
- element: props.element,
18
- });
19
-
20
- mathbox.three.camera.position.set(1, 1, 2);
21
- mathbox.three.renderer.setClearColor(new Color(0xffffff), 1.0);
28
+ if (!container) return () => {}
22
29
 
23
- const view = mathbox.cartesian({
24
- range: [
25
- [-2, 2],
26
- [-1, 1],
27
- [-1, 1],
28
- ],
29
- scale: [2, 1, 1],
30
- });
31
- view.grid({
32
- divideX: 20,
33
- width: 5,
34
- opacity: 0.3,
35
- });
30
+ const mathbox = mathBox({
31
+ ...mathboxOptions,
32
+ element: container,
33
+ })
34
+ setSelection(mathbox)
36
35
 
37
- }, [props.element])
38
- return null
36
+ /**
37
+ * TODO: Should Mathbox component allow setting these more easily?
38
+ */
39
+ mathbox.three.renderer.setClearColor(new Color(0xffffff), 1.0)
40
+ if (initialCameraPosition) {
41
+ mathbox.three.camera.position.set(...initialCameraPosition)
42
+ }
43
+ return () => {
44
+ mathbox.select("*").remove()
45
+ mathbox.three.destroy()
46
+ }
47
+ }, [container, mathboxOptions, initialCameraPosition])
48
+ useImperativeHandle(ref, () => selection)
49
+ return (
50
+ <div ref={setContainer} {...divProps}>
51
+ <MathboxAPIContext.Provider value={selection}>
52
+ {children}
53
+ </MathboxAPIContext.Provider>
54
+ </div>
55
+ )
39
56
  }
40
57
 
41
- export default Mathbox
58
+ export default forwardRef(Mathbox)
@@ -0,0 +1,6 @@
1
+ import { createContext } from "react"
2
+ import { MathboxSelection, NodeType } from "mathbox"
3
+
4
+ const MathboxAPIContext = createContext<MathboxSelection<NodeType> | null>(null)
5
+
6
+ export default MathboxAPIContext
@@ -0,0 +1,361 @@
1
+ import React, { forwardRef } from "react"
2
+ import { Props, NodeType, MathboxSelection } from "mathbox"
3
+ import MathboxAPIContext from "./MathboxNodeContext"
4
+ import { useMathboxAPI } from "./hooks"
5
+ import { WithChildren } from "./types"
6
+
7
+ type MathboxComponent<T extends NodeType> = React.ForwardRefExoticComponent<
8
+ WithChildren<Props[T]> & React.RefAttributes<MathboxSelection<T>>
9
+ >
10
+
11
+ const mathboxComponentFactory = <T extends NodeType>(
12
+ type: T
13
+ ): MathboxComponent<T> => {
14
+ const Comp = (
15
+ props: WithChildren<Props[T]>,
16
+ ref: React.Ref<MathboxSelection<T> | null>
17
+ ) => {
18
+ const nodeAPI = useMathboxAPI(type, props, ref)
19
+ return (
20
+ <MathboxAPIContext.Provider value={nodeAPI}>
21
+ {props.children}
22
+ </MathboxAPIContext.Provider>
23
+ )
24
+ }
25
+ /**
26
+ * The line below gives a TS error without explicit any.
27
+ * But if you replace each generic T above with a specific instance, e.g.,
28
+ * 'cartesian', there is no error.
29
+ *
30
+ * So an alternative without "any" would be be to copy the definition of Comp
31
+ * with all ~64 different node types.
32
+ *
33
+ * I'm not 100% sure why there's a ts error here, but the above indicates to
34
+ * me that this is actually type-safe. And I do not want that much copy pasta.
35
+ * So instead, use the any and put explicit return type on the factory.
36
+ */
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
+ return forwardRef(Comp) as any
39
+ }
40
+
41
+ /**
42
+ * Component wrapper for mathbox [`area`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#dataarea).
43
+ */
44
+ export const Area = mathboxComponentFactory("area")
45
+
46
+ /**
47
+ * Component wrapper for mathbox [`array`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#dataarray).
48
+ */
49
+ const MBArray = mathboxComponentFactory("array")
50
+
51
+ export { MBArray as Array }
52
+
53
+ /**
54
+ * Component wrapper for mathbox [`axis`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawaxis).
55
+ */
56
+ export const Axis = mathboxComponentFactory("axis")
57
+
58
+ /**
59
+ * Component wrapper for mathbox [`camera`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#cameracamera).
60
+ */
61
+ export const Camera = mathboxComponentFactory("camera")
62
+
63
+ /**
64
+ * Component wrapper for mathbox [`cartesian`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#viewcartesian).
65
+ */
66
+ export const Cartesian = mathboxComponentFactory("cartesian")
67
+
68
+ /**
69
+ * Component wrapper for mathbox [`cartesian4`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#viewcartesian4).
70
+ */
71
+ export const Cartesian4 = mathboxComponentFactory("cartesian4")
72
+
73
+ /**
74
+ * Component wrapper for mathbox [`clamp`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorclamp).
75
+ */
76
+ export const Clamp = mathboxComponentFactory("clamp")
77
+
78
+ /**
79
+ * Component wrapper for mathbox [`clock`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#timeclock).
80
+ */
81
+ export const Clock = mathboxComponentFactory("clock")
82
+
83
+ /**
84
+ * Component wrapper for mathbox [`compose`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#rttcompose).
85
+ */
86
+ export const Compose = mathboxComponentFactory("compose")
87
+
88
+ /**
89
+ * Component wrapper for mathbox [`dom`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#overlaydom).
90
+ */
91
+ export const Dom = mathboxComponentFactory("dom")
92
+
93
+ /**
94
+ * Component wrapper for mathbox [`face`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawface).
95
+ */
96
+ export const Face = mathboxComponentFactory("face")
97
+
98
+ /**
99
+ * Component wrapper for mathbox [`format`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#textformat).
100
+ */
101
+ export const Format = mathboxComponentFactory("format")
102
+
103
+ /**
104
+ * Component wrapper for mathbox [`fragment`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#transformfragment).
105
+ */
106
+ export const Fragment = mathboxComponentFactory("fragment")
107
+
108
+ /**
109
+ * Component wrapper for mathbox [`grid`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawgrid).
110
+ */
111
+ export const Grid = mathboxComponentFactory("grid")
112
+
113
+ /**
114
+ * Component wrapper for mathbox [`group`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#basegroup).
115
+ */
116
+ export const Group = mathboxComponentFactory("group")
117
+
118
+ /**
119
+ * Component wrapper for mathbox [`grow`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorgrow).
120
+ */
121
+ export const Grow = mathboxComponentFactory("grow")
122
+
123
+ /**
124
+ * Component wrapper for mathbox [`html`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#overlayhtml).
125
+ */
126
+ export const Html = mathboxComponentFactory("html")
127
+
128
+ /**
129
+ * Component wrapper for mathbox [`inherit`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#baseinherit).
130
+ */
131
+ export const Inherit = mathboxComponentFactory("inherit")
132
+
133
+ /**
134
+ * Component wrapper for mathbox [`interval`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#datainterval).
135
+ */
136
+ export const Interval = mathboxComponentFactory("interval")
137
+
138
+ /**
139
+ * Component wrapper for mathbox [`join`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorjoin).
140
+ */
141
+ export const Join = mathboxComponentFactory("join")
142
+
143
+ /**
144
+ * Component wrapper for mathbox [`label`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#textlabel).
145
+ */
146
+ export const Label = mathboxComponentFactory("label")
147
+
148
+ /**
149
+ * Component wrapper for mathbox [`layer`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#transformlayer).
150
+ */
151
+ export const Layer = mathboxComponentFactory("layer")
152
+
153
+ /**
154
+ * Component wrapper for mathbox [`lerp`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorlerp).
155
+ */
156
+ export const Lerp = mathboxComponentFactory("lerp")
157
+
158
+ /**
159
+ * Component wrapper for mathbox [`line`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawline).
160
+ */
161
+ export const Line = mathboxComponentFactory("line")
162
+
163
+ /**
164
+ * Component wrapper for mathbox [`mask`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#transformmask).
165
+ */
166
+ export const Mask = mathboxComponentFactory("mask")
167
+
168
+ /**
169
+ * Component wrapper for mathbox [`matrix`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#datamatrix).
170
+ */
171
+ export const Matrix = mathboxComponentFactory("matrix")
172
+
173
+ /**
174
+ * Component wrapper for mathbox [`memo`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatormemo).
175
+ */
176
+ export const Memo = mathboxComponentFactory("memo")
177
+
178
+ /**
179
+ * Component wrapper for mathbox [`move`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#presentmove).
180
+ */
181
+ export const Move = mathboxComponentFactory("move")
182
+
183
+ /**
184
+ * Component wrapper for mathbox [`now`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#timenow).
185
+ */
186
+ export const Now = mathboxComponentFactory("now")
187
+
188
+ /**
189
+ * Component wrapper for mathbox [`play`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#presentplay).
190
+ */
191
+ export const Play = mathboxComponentFactory("play")
192
+
193
+ /**
194
+ * Component wrapper for mathbox [`point`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawpoint).
195
+ */
196
+ export const Point = mathboxComponentFactory("point")
197
+
198
+ /**
199
+ * Component wrapper for mathbox [`polar`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#viewpolar).
200
+ */
201
+ export const Polar = mathboxComponentFactory("polar")
202
+
203
+ /**
204
+ * Component wrapper for mathbox [`present`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#presentpresent).
205
+ */
206
+ export const Present = mathboxComponentFactory("present")
207
+
208
+ /**
209
+ * Component wrapper for mathbox [`readback`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorreadback).
210
+ */
211
+ export const Readback = mathboxComponentFactory("readback")
212
+
213
+ /**
214
+ * Component wrapper for mathbox [`repeat`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorrepeat).
215
+ */
216
+ export const Repeat = mathboxComponentFactory("repeat")
217
+
218
+ /**
219
+ * Component wrapper for mathbox [`resample`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorresample).
220
+ */
221
+ export const Resample = mathboxComponentFactory("resample")
222
+
223
+ /**
224
+ * Component wrapper for mathbox [`retext`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#textretext).
225
+ */
226
+ export const Retext = mathboxComponentFactory("retext")
227
+
228
+ /**
229
+ * Component wrapper for mathbox [`reveal`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#presentreveal).
230
+ */
231
+ export const Reveal = mathboxComponentFactory("reveal")
232
+
233
+ /**
234
+ * Component wrapper for mathbox [`rtt`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#rttrtt).
235
+ */
236
+ export const Rtt = mathboxComponentFactory("rtt")
237
+
238
+ /**
239
+ * Component wrapper for mathbox [`scale`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#datascale).
240
+ */
241
+ export const Scale = mathboxComponentFactory("scale")
242
+
243
+ /**
244
+ * Component wrapper for mathbox [`shader`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#shadershader).
245
+ */
246
+ export const Shader = mathboxComponentFactory("shader")
247
+
248
+ /**
249
+ * Component wrapper for mathbox [`slice`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorslice).
250
+ */
251
+ export const Slice = mathboxComponentFactory("slice")
252
+
253
+ /**
254
+ * Component wrapper for mathbox [`slide`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#presentslide).
255
+ */
256
+ export const Slide = mathboxComponentFactory("slide")
257
+
258
+ /**
259
+ * Component wrapper for mathbox [`spherical`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#viewspherical).
260
+ */
261
+ export const Spherical = mathboxComponentFactory("spherical")
262
+
263
+ /**
264
+ * Component wrapper for mathbox [`split`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorsplit).
265
+ */
266
+ export const Split = mathboxComponentFactory("split")
267
+
268
+ /**
269
+ * Component wrapper for mathbox [`spread`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorspread).
270
+ */
271
+ export const Spread = mathboxComponentFactory("spread")
272
+
273
+ /**
274
+ * Component wrapper for mathbox [`step`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#presentstep).
275
+ */
276
+ export const Step = mathboxComponentFactory("step")
277
+
278
+ /**
279
+ * Component wrapper for mathbox [`stereographic`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#viewstereographic).
280
+ */
281
+ export const Stereographic = mathboxComponentFactory("stereographic")
282
+
283
+ /**
284
+ * Component wrapper for mathbox [`stereographic4`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#viewstereographic4).
285
+ */
286
+ export const Stereographic4 = mathboxComponentFactory("stereographic4")
287
+
288
+ /**
289
+ * Component wrapper for mathbox [`strip`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawstrip).
290
+ */
291
+ export const Strip = mathboxComponentFactory("strip")
292
+
293
+ /**
294
+ * Component wrapper for mathbox [`subdivide`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorsubdivide).
295
+ */
296
+ export const Subdivide = mathboxComponentFactory("subdivide")
297
+
298
+ /**
299
+ * Component wrapper for mathbox [`surface`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawsurface).
300
+ */
301
+ export const Surface = mathboxComponentFactory("surface")
302
+
303
+ /**
304
+ * Component wrapper for mathbox [`swizzle`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatorswizzle).
305
+ */
306
+ export const Swizzle = mathboxComponentFactory("swizzle")
307
+
308
+ /**
309
+ * Component wrapper for mathbox [`text`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#texttext).
310
+ */
311
+ export const Text = mathboxComponentFactory("text")
312
+
313
+ /**
314
+ * Component wrapper for mathbox [`ticks`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawticks).
315
+ */
316
+ export const Ticks = mathboxComponentFactory("ticks")
317
+
318
+ /**
319
+ * Component wrapper for mathbox [`transform`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#transformtransform).
320
+ */
321
+ export const Transform = mathboxComponentFactory("transform")
322
+
323
+ /**
324
+ * Component wrapper for mathbox [`transform4`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#transformtransform4).
325
+ */
326
+ export const Transform4 = mathboxComponentFactory("transform4")
327
+
328
+ /**
329
+ * Component wrapper for mathbox [`transpose`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#operatortranspose).
330
+ */
331
+ export const Transpose = mathboxComponentFactory("transpose")
332
+
333
+ /**
334
+ * Component wrapper for mathbox [`unit`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#baseunit).
335
+ */
336
+ export const Unit = mathboxComponentFactory("unit")
337
+
338
+ /**
339
+ * Component wrapper for mathbox [`vector`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#drawvector).
340
+ */
341
+ export const Vector = mathboxComponentFactory("vector")
342
+
343
+ /**
344
+ * Component wrapper for mathbox [`vertex`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#transformvertex).
345
+ */
346
+ export const Vertex = mathboxComponentFactory("vertex")
347
+
348
+ /**
349
+ * Component wrapper for mathbox [`view`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#viewview).
350
+ */
351
+ export const View = mathboxComponentFactory("view")
352
+
353
+ /**
354
+ * Component wrapper for mathbox [`volume`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#datavolume).
355
+ */
356
+ export const Volume = mathboxComponentFactory("volume")
357
+
358
+ /**
359
+ * Component wrapper for mathbox [`voxel`](https://gitgud.io/unconed/mathbox/-/blob/master/docs/primitives.md#datavoxel).
360
+ */
361
+ export const Voxel = mathboxComponentFactory("voxel")
@@ -0,0 +1,51 @@
1
+ import React, {
2
+ useContext,
3
+ useEffect,
4
+ useState,
5
+ useImperativeHandle,
6
+ } from "react"
7
+ import type { MathboxSelection, NodeType, Props } from "mathbox"
8
+ import MathboxAPIContext from "./MathboxNodeContext"
9
+ import type { WithChildren } from "./types"
10
+
11
+ export const useMathboxAPI = <T extends NodeType>(
12
+ name: T,
13
+ props: WithChildren<Props[T]>,
14
+ ref: React.Ref<MathboxSelection<T> | null>
15
+ ) => {
16
+ const parent = useContext(MathboxAPIContext)
17
+ const [selection, setSelection] = useState<MathboxSelection<T> | null>(null)
18
+
19
+ useEffect(
20
+ () => () => {
21
+ if (selection) {
22
+ selection.remove()
23
+ }
24
+ },
25
+ [selection]
26
+ )
27
+
28
+ useEffect(() => {
29
+ const { children, ...mbProps } = props
30
+ if (!parent) return
31
+ if (!selection) {
32
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
+ // @ts-ignore
34
+ const thisNode: MathboxSelection<T> = parent[name](mbProps)
35
+ setSelection(thisNode)
36
+ } else {
37
+ /**
38
+ * Set all the props anew. It's MathBox's responsibility to diff out the
39
+ * unchanged props if it wants to do that optimization.
40
+ *
41
+ * (In fact, Mathbox delegates that optimization to Threestrap through
42
+ * some rather byzantine inheritance.)
43
+ */
44
+ selection.set(mbProps)
45
+ }
46
+ }, [parent, selection, setSelection, props, name])
47
+
48
+ useImperativeHandle(ref, () => selection)
49
+
50
+ return selection
51
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./components"
2
+ export { default as Mathbox } from "./Mathbox"
@@ -0,0 +1,9 @@
1
+ import type { ReactNode } from "react"
2
+ import type { MathboxSelection, NodeType } from "mathbox"
3
+ import React from "react"
4
+
5
+ export type WithChildren<T> = {
6
+ children?: ReactNode | ReactNode[]
7
+ } & T
8
+
9
+ export type MathboxRef<T extends NodeType> = React.Ref<MathboxSelection<T>>
package/src/index.ts CHANGED
@@ -1,3 +1 @@
1
- import Mathbox from "./components/Mathbox";
2
-
3
- export { Mathbox };
1
+ export * from "./components"
@@ -0,0 +1,36 @@
1
+ import React from "react"
2
+ import { Story, Meta } from "@storybook/react"
3
+
4
+ import { CustomMathbox as Mathbox } from "./utils"
5
+ import { Cartesian, Grid } from "../components/components"
6
+
7
+ export default {
8
+ title: "Cartesian",
9
+ component: Cartesian,
10
+ argTypes: {
11
+ range: {
12
+ type: "array",
13
+ default: [
14
+ [-1, 1],
15
+ [-1, 1],
16
+ [-1, 1],
17
+ [-1, 1],
18
+ ],
19
+ },
20
+ scale: {
21
+ type: "array",
22
+ default: [1, 1, 1],
23
+ },
24
+ },
25
+ } as Meta<typeof Cartesian>
26
+
27
+ const Template: Story<React.ComponentProps<typeof Cartesian>> = (args) => (
28
+ <Mathbox style={{ height: 450 }}>
29
+ <Cartesian {...args}>
30
+ <Grid />
31
+ </Cartesian>
32
+ </Mathbox>
33
+ )
34
+
35
+ export const DefaultCartesian = Template.bind({})
36
+ DefaultCartesian.args = {}
@@ -0,0 +1,38 @@
1
+ import React from "react"
2
+ import { Story, Meta } from "@storybook/react"
3
+
4
+ import { CustomMathbox as Mathbox } from "./utils"
5
+ import { Grid } from "../components"
6
+
7
+ export default {
8
+ title: "Grid",
9
+ component: Grid,
10
+ argTypes: {
11
+ color: {
12
+ type: "string",
13
+ default: "rgb(128, 128, 128)",
14
+ },
15
+ axes: {
16
+ type: "string",
17
+ default: "xy",
18
+ },
19
+ },
20
+ } as Meta<typeof Grid>
21
+
22
+ const Template: Story<React.ComponentProps<typeof Grid>> = (args) => (
23
+ <Mathbox style={{ height: 450 }}>
24
+ <Grid {...args} />
25
+ </Mathbox>
26
+ )
27
+
28
+ export const DefaultGrid = Template.bind({})
29
+ DefaultGrid.args = {}
30
+
31
+ export const GridXY = Template.bind({})
32
+ GridXY.args = { axes: "xy" }
33
+
34
+ export const GridYZ = Template.bind({})
35
+ GridYZ.args = { axes: "yz" }
36
+
37
+ export const GridZX = Template.bind({})
38
+ GridZX.args = { axes: "zx" }
@@ -0,0 +1,16 @@
1
+ import React from "react"
2
+ import { Story, Meta } from "@storybook/react"
3
+
4
+ import Mathbox from "../components/Mathbox"
5
+
6
+ export default {
7
+ title: "Mathbox",
8
+ component: Mathbox,
9
+ argTypes: {},
10
+ } as Meta<typeof Mathbox>
11
+
12
+ const Template: Story<React.ComponentProps<typeof Mathbox>> = () => (
13
+ <Mathbox style={{ height: 450 }} />
14
+ )
15
+
16
+ export const HelloWorld = Template.bind({})