diff --git a/docs/source/_exts/autogen_api_docs.py b/docs/source/_exts/autogen_api_docs.py index 2522ad388..cdd1f184e 100644 --- a/docs/source/_exts/autogen_api_docs.py +++ b/docs/source/_exts/autogen_api_docs.py @@ -43,7 +43,7 @@ def generate_api_docs(): if file.name == "__init__.py": if file.parent != PYTHON_PACKAGE: content.append(make_package_section(file)) - else: + elif not file.name.startswith("_"): content.append(make_module_section(file)) API_FILE.write_text("\n".join(content)) diff --git a/docs/source/about/changelog.rst b/docs/source/about/changelog.rst index ccbd3d728..e7a0b6ccf 100644 --- a/docs/source/about/changelog.rst +++ b/docs/source/about/changelog.rst @@ -19,6 +19,14 @@ Unreleased - :pull:`1251` - Substitute client-side usage of ``react`` with ``preact``. - :pull:`1239` - Script elements no longer support behaving like effects. They now strictly behave like plain HTML script elements. +- :pull:`1255` - The ``reactpy.html`` module has been modified to allow for auto-creation of any HTML nodes. For example, you can create a ```` element by calling ``html.data_table()``. + +**Removed** + +- :pull:`1255` - Removed the ability to import ``reactpy.html.*`` elements directly. You must now call ``html.*`` to access the elements. +- :pull:`1255` - Removed ``reactpy.sample`` module. +- :pull:`1255` - Removed ``reactpy.svg`` module. Contents previously within ``reactpy.svg.*`` can now be accessed via ``html.svg.*``. +- :pull:`1255` - Removed ``reactpy.html._`` function. Use ``html.fragment`` instead. **Fixed** diff --git a/docs/source/guides/getting-started/_examples/run_starlette.py b/docs/source/guides/getting-started/_examples/run_starlette.py index 966b9ef77..47a08fb47 100644 --- a/docs/source/guides/getting-started/_examples/run_starlette.py +++ b/docs/source/guides/getting-started/_examples/run_starlette.py @@ -1,4 +1,4 @@ -# :lines: 11- +# :lines: 10- from reactpy import run from reactpy.backend import starlette as starlette_server diff --git a/src/reactpy/__init__.py b/src/reactpy/__init__.py index d54e82174..f22aa5832 100644 --- a/src/reactpy/__init__.py +++ b/src/reactpy/__init__.py @@ -1,4 +1,5 @@ -from reactpy import backend, config, html, logging, sample, svg, types, web, widgets +from reactpy import backend, config, logging, types, web, widgets +from reactpy._html import html from reactpy.backend.utils import run from reactpy.core import hooks from reactpy.core.component import component @@ -37,8 +38,6 @@ "html_to_vdom", "logging", "run", - "sample", - "svg", "types", "use_callback", "use_connection", diff --git a/src/reactpy/_html.py b/src/reactpy/_html.py new file mode 100644 index 000000000..e2d4f096a --- /dev/null +++ b/src/reactpy/_html.py @@ -0,0 +1,416 @@ +from __future__ import annotations + +from collections.abc import Sequence +from typing import TYPE_CHECKING, ClassVar + +from reactpy.core.vdom import custom_vdom_constructor, make_vdom_constructor + +if TYPE_CHECKING: + from reactpy.core.types import ( + EventHandlerDict, + Key, + VdomAttributes, + VdomChild, + VdomChildren, + VdomDict, + VdomDictConstructor, + ) + +__all__ = ["html"] + +NO_CHILDREN_ALLOWED_HTML_BODY = { + "area", + "base", + "br", + "col", + "command", + "embed", + "hr", + "img", + "input", + "iframe", + "keygen", + "link", + "meta", + "param", + "portal", + "source", + "track", + "wbr", +} + +NO_CHILDREN_ALLOWED_SVG = { + "animate", + "animateMotion", + "animateTransform", + "circle", + "desc", + "discard", + "ellipse", + "feBlend", + "feColorMatrix", + "feComponentTransfer", + "feComposite", + "feConvolveMatrix", + "feDiffuseLighting", + "feDisplacementMap", + "feDistantLight", + "feDropShadow", + "feFlood", + "feFuncA", + "feFuncB", + "feFuncG", + "feFuncR", + "feGaussianBlur", + "feImage", + "feMerge", + "feMergeNode", + "feMorphology", + "feOffset", + "fePointLight", + "feSpecularLighting", + "feSpotLight", + "feTile", + "feTurbulence", + "filter", + "foreignObject", + "hatch", + "hatchpath", + "image", + "line", + "linearGradient", + "metadata", + "mpath", + "path", + "polygon", + "polyline", + "radialGradient", + "rect", + "script", + "set", + "stop", + "style", + "text", + "textPath", + "title", + "tspan", + "use", + "view", +} + + +def _fragment( + attributes: VdomAttributes, + children: Sequence[VdomChild], + key: Key | None, + event_handlers: EventHandlerDict, +) -> VdomDict: + """An HTML fragment - this element will not appear in the DOM""" + if attributes or event_handlers: + msg = "Fragments cannot have attributes besides 'key'" + raise TypeError(msg) + model: VdomDict = {"tagName": ""} + + if children: + model["children"] = children + + if key is not None: + model["key"] = key + + return model + + +def _script( + attributes: VdomAttributes, + children: Sequence[VdomChild], + key: Key | None, + event_handlers: EventHandlerDict, +) -> VdomDict: + """Create a new `