html.py 1.3 KB

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