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

Closed
Archmonger opened this issue Mar 13, 2024 · 0 comments · Fixed by #1255
Closed

New interface for reactpy.html #1214

Archmonger opened this issue Mar 13, 2024 · 0 comments · Fixed by #1255
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.

from reactpy.core.types import VdomDictConstructor
from reactpy.core.vdom import custom_vdom_constructor, make_vdom_constructor
from reactpy.html import _fragment, _script

NO_CHILDREN_ALLOWED = {
    "area",
    "base",
    "br",
    "col",
    "command",
    "embed",
    "hr",
    "img",
    "input",
    "iframe",
    "keygen",
    "link",
    "meta",
    "param",
    "portal",
    "source",
    "track",
    "wbr",
}


class HtmlConstructor:
    cache: dict[str, VdomDictConstructor] = {
        "script": custom_vdom_constructor(_script),
        "fragment": custom_vdom_constructor(_fragment),
    }

    def __getattribute__(self, value: str) -> VdomDictConstructor:
        if value.startswith("__") or value in {"cache"}:
            return super().__getattribute__(value)

        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...
tree = html.html(
    {"lang": "en"},
    html.head(
        html.title("Hello, world!"),
    ),
    html.body(
        html.h1("Hello, world!"),
        html.p("This is a paragraph."),
    ),
)

print(tree)
@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
@Archmonger Archmonger linked a pull request Jan 26, 2025 that will close this issue
4 tasks
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

Successfully merging a pull request may close this issue.

1 participant