-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tags.script() doesn't execute when it's a child of a JSXTag #26
Comments
This is a good explanation of why the script tag doesn't execute: https://stackoverflow.com/a/64815699/412655
|
I've been thinking about another way of dealing with the JSX stuff. First issue: the JS code generation is a bit risky instead of writing JS code, it'll be more robust to create a JSON data structure from htmltools import *
Foo = jsx_tag_create("Foo")
x = Foo(
"hello",
span(
"world",
Foo("more text", z=100)
),
tags.script("alert('This is the script!');"),
x=[1,2,3],
y="abc"
)
print(str(x))
#> <script type="text/javascript">
#> (function() {
#> var container = new DocumentFragment();
#> ReactDOM.render(
#> React.createElement(
#> Foo, {"x": [1, 2, 3], "y": "abc"},
#> "hello",
#> React.createElement(
#> 'span', {},
#> "world",
#> React.createElement(
#> Foo, {"z": 100},
#> "more text"
#> ),
#> ),
#> React.createElement(
#> 'script', {},
#> "alert('This is the script!');"
#> )
#> )
#> , container);
#> document.currentScript.after(container);
#> })();
#> </script> Instead of generating the JS code, we can encode the data structure like this. (For clarity, this is written as TypeScript.) type HtmlTag = {
type: "html";
name: string;
attrs?: Record<string, string>;
children?: Array<string | Tag>;
};
type JsxTag = {
type: "jsx";
name: string;
attrs?: Record<string, any>;
children?: Array<string | Tag>;
};
type Tag = HtmlTag | JsxTag;
const x: Tag = {
type: "jsx",
name: "Foo",
attrs: { x: [1, 2, 3], y: "abc" },
children: [
"hello",
{
type: "html",
name: "span",
children: [
{
type: "jsx",
name: "Foo",
attrs: { z: 100 },
children: ["more text"],
},
],
},
{
type: "html",
name: "script",
children: ["alert('This is the script!');"],
},
],
};
insertJSX(x, container); One advantage of this is that we can use Python's Another advantage is that it leaves us with a data structure which we can manipulate from the JavaScript side. For example, we can recurse over it looking for specific types tags and have customized behavior with them. The next issue is, how do we handle the specific case of Some possibilities:
See this SO question for a bit more. If we go this way, we'll also need to think about whether there are any weird corner cases that can trip us up. For example:
|
Have you considered how we would support |
Two comments about First, I think we should use another name, like Second, in terms of the data structures, we could package it up with something like this: {
type: "raw_js",
children: ["() => console.log('here')"]
} |
Some notes on the JSX stuff: I did some experiments using WebComponents. https://stackblitz.com/edit/web-platform-lkzkdq Another approach that I tested is is to send custom tags (but not WebComponents) and manipulate/transform them into HTML tags before inserting them into the DOM. With this method, there’s no FOUC, but the code needed to manipulate the content is a bit verbose and clunky. For the methods above, A third is to use the htm library, which can transform code that's very similar to JSX, but does not require a heavy-weight transpilation tool like Babel ( Note: If All that said, it might not be necessary to use |
For example, when you
open example.html
, you should get a 'hello' alert box (but you currently get nothing).This effectively means you can use things like
shiny.ui.panel_absolute(draggable=True)
inside aui.page_navbar
(because it's all one big JSX componentThe text was updated successfully, but these errors were encountered: