Skip to content
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

New interface for reactpy.html #1214

Open
Archmonger opened this issue Mar 13, 2024 · 0 comments
Open

New interface for reactpy.html #1214

Archmonger opened this issue Mar 13, 2024 · 0 comments
Labels
priority-2-moderate Should be resolved on a reasonable timeline. type-revision About a change in functionality or behavior

Comments

@Archmonger
Copy link
Contributor

Archmonger commented Mar 13, 2024

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."),
    ),
)
@Archmonger Archmonger added priority-2-moderate Should be resolved on a reasonable timeline. type-revision About a change in functionality or behavior labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-2-moderate Should be resolved on a reasonable timeline. type-revision About a change in functionality or behavior
Projects
None yet
Development

No branches or pull requests

1 participant