reactpy
reactpy copied to clipboard
New interface for `reactpy.html`
Current Situation
Currently, we need to manually write every single HTML element that could exist. This is not efficient and is fairly annoying to maintain.
Proposed Actions
Create a generic that can automate this.
Here's an a draft implementation I made in a few minutes.
NO_CHILDREN_ALLOWED = {
"area",
"base",
"br",
"col",
"command",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
}
class HtmlConstructor:
cache: dict[str, VdomConstructor] = {}
def __getattribute__(self, value: str) -> VdomConstructor:
if value.startswith("__") or value == "script":
return super().__getattribute__(value)
if value == "script":
return _script
if value in self.cache:
return self.cache[value]
self.cache[value] = make_vdom_constructor(
value,
allow_children=value not in NO_CHILDREN_ALLOWED,
)
return self.cache[value]
html = HtmlConstructor()
# Here's an example usage...
html.html(
{"lang": "en"},
html.head(
html.title("Hello, world!"),
),
html.body(
html.h1("Hello, world!"),
html.p("This is a paragraph."),
),
)