Skip to content

Commit ca32832

Browse files
committed
implement script element
1 parent 8c02d19 commit ca32832

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 18 additions & 1 deletion
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,23 @@
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:
264+
The text of the script should evaluate to a function. This function will be
265+
called when the script is initially created or when the content of the
266+
script changes. The function may optionally return a teardown function that
267+
is called when the script element is removed from the tree, or when the
268+
script content changes.
269+
"""
270+
return {"tagName": "script", "children": [content]}
271+
272+
256273
# Table content
257274
caption = make_vdom_constructor("caption")
258275
col = make_vdom_constructor("col")

0 commit comments

Comments
 (0)