@@ -30,28 +30,62 @@ Custom Javascript Components
30
30
For projects that will be shared with others we recommend bundling your Javascript with
31
31
`rollup <https://rollupjs.org/guide/en/ >`__ or `webpack <https://webpack.js.org/ >`__
32
32
into a
33
- `web module <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules >`__
34
- using IDOM's
33
+ `web module <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules >`__.
34
+ IDOM also provides a
35
35
`template repository <https://github.com/idom-team/idom-react-component-cookiecutter >`__
36
- as a blueprint to build a React component. Once you've done this, you can distribute
37
- bundled javascript in your Python package and integrate it into IDOM by defining
38
- :class: `~idom.client.module.Module ` objects that load them from source:
36
+ that can be used as a blueprint to build a library of React components.
37
+
38
+ The core benefit of loading Javascript in this way is that users of your code won't need
39
+ to have NPM _ installed. Rather, they can use ``pip `` to install your Python package
40
+ without any other build steps because the bundled Javascript you distributed with it
41
+ will be symlinked into the IDOM client at runtime.
42
+
43
+ To work as intended, the Javascript bundle must export the following named functions:
44
+
45
+ .. code-block :: typescript
46
+
47
+ type createElement = (component : any , props : Object ) => any ;
48
+ type renderElement = (element : any , container : HTMLElement ) => void ;
49
+ type unmountElement = (element : HTMLElement ) => void ;
50
+
51
+ These functions can be thought of as being analogous to those from React.
52
+
53
+ - ``createElement `` ➜ |react.createElement |_
54
+ - ``renderElement `` ➜ |reactDOM.render |_
55
+ - ``unmountElement `` ➜ |reactDOM.unmountComponentAtNode |_
56
+
57
+ .. |react.createElement | replace :: ``react.createElement ``
58
+ .. _react.createElement : https://reactjs.org/docs/react-api.html#createelement
59
+
60
+ .. |reactDOM.render | replace :: ``reactDOM.render ``
61
+ .. _reactDOM.render : https://reactjs.org/docs/react-dom.html#render
62
+
63
+ .. |reactDOM.unmountComponentAtNode | replace :: ``reactDOM.unmountComponentAtNode ``
64
+ .. _reactDOM.unmountComponentAtNode : https://reactjs.org/docs/react-api.html#createelement
65
+
66
+ And will be called in the following manner:
67
+
68
+ .. code-block ::
69
+
70
+ // on every render
71
+ renderElement(createElement(type, props), container);
72
+ // on unmount
73
+ unmountElement(container);
74
+
75
+ Once you've done this, you can distribute the bundled javascript in your Python package
76
+ and integrate it into IDOM by defining :class: `~idom.client.module.Module ` objects that
77
+ load them from source:
39
78
40
79
.. code-block ::
41
80
42
81
import idom
43
82
my_js_package = idom.Module("my-js-package", source_file="/path/to/my/bundle.js")
44
83
45
- The core benefit of loading Javascript in this way is that users of your code won't need
46
- NPM _. Rather, they can use ``pip `` to install your Python package without any other build
47
- steps because the bundled Javascript you distributed with it will be symlinked into the
48
- IDOM client at runtime.
49
-
50
- With that said, if you just want to see how this all works it might be easiest to hook
51
- in simple a hand-crafted Javascript component. In the example to follow we'll create a
52
- very basic SVG line chart. The catch though is that we are limited to using Javascript
53
- that can run directly in the browser. This means we can't use fancy syntax like
54
- `JSX <https://reactjs.org/docs/introducing-jsx.html >`__ and instead will use
84
+ The simplest way to try this out yourself though, is to hook in simple a hand-crafted
85
+ Javascript module that has the requisite interface. In the example to follow we'll
86
+ create a very basic SVG line chart. The catch though is that we are limited to using
87
+ Javascript that can run directly in the browser. This means we can't use fancy syntax
88
+ like `JSX <https://reactjs.org/docs/introducing-jsx.html >`__ and instead will use
55
89
`htm <https://github.com/developit/htm >`__ to simulate JSX in plain Javascript.
56
90
57
91
.. example :: super_simple_chart
0 commit comments