Skip to content

Commit 71752b2

Browse files
committed
implement script element
1 parent 8c02d19 commit 71752b2

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/client/packages/idom-client-react/src/components.js

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export function Element({ model }) {
3434
} else {
3535
return null;
3636
}
37+
} else if (model.tagName == "script") {
38+
return html`<${ScriptElement} script=${model.children[0]} />`;
3739
} else if (model.importSource) {
3840
return html`<${ImportedElement} model=${model} />`;
3941
} else {
@@ -56,6 +58,12 @@ function StandardElement({ model }) {
5658
);
5759
}
5860

61+
function ScriptElement({ script }) {
62+
const el = React.useRef();
63+
React.useEffect(eval(script), [script]);
64+
return null;
65+
}
66+
5967
function ImportedElement({ model }) {
6068
const layoutContext = React.useContext(LayoutContext);
6169

src/idom/html.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
- :func:`template`
151151
"""
152152

153-
from .core.vdom import make_vdom_constructor
153+
from .core.vdom import VdomDict, make_vdom_constructor
154154

155155

156156
# Dcument metadata
@@ -253,6 +253,22 @@
253253
del_ = make_vdom_constructor("del")
254254
ins = make_vdom_constructor("ins")
255255

256+
# Scripting
257+
258+
259+
def script(content: str) -> VdomDict:
260+
"""Create a new `<{script}> <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script>`__ element.
261+
262+
Parameters:
263+
content: The text of the script should evaluate to a function. This function
264+
will be called when the script is initially created or when the content of the
265+
script changes. The function may optionally return a teardown function that is
266+
called when the script element is removed from the tree, or when the script
267+
content changes.
268+
"""
269+
return {"tagName": "script", "children": [content]}
270+
271+
256272
# Table content
257273
caption = make_vdom_constructor("caption")
258274
col = make_vdom_constructor("col")

0 commit comments

Comments
 (0)