html.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """A html component."""
  2. from typing import Dict
  3. from reflex.components.el.elements.typography import Div
  4. from reflex.vars.base import Var
  5. class Html(Div):
  6. """Render the html.
  7. Returns:
  8. The code to render the html component.
  9. """
  10. # The HTML to render.
  11. dangerouslySetInnerHTML: Var[Dict[str, str]] # noqa: N815
  12. @classmethod
  13. def create(cls, *children, **props):
  14. """Create a html component.
  15. Args:
  16. *children: The children of the component.
  17. **props: The props to pass to the component.
  18. Returns:
  19. The html component.
  20. Raises:
  21. ValueError: If children are not provided or more than one child is provided.
  22. """
  23. # If children are not provided, throw an error.
  24. if len(children) != 1:
  25. raise ValueError("Must provide children to the html component.")
  26. else:
  27. props["dangerouslySetInnerHTML"] = {"__html": children[0]}
  28. # Apply the default classname
  29. given_class_name = props.pop("class_name", [])
  30. if isinstance(given_class_name, str):
  31. given_class_name = [given_class_name]
  32. props["class_name"] = ["rx-Html", *given_class_name]
  33. # Create the component.
  34. return super().create(**props)
  35. html = Html.create