element.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """Base class definition for raw HTML elements."""
  2. from typing import Dict
  3. from pynecone.components.component import Component
  4. class Element(Component):
  5. """The base class for all raw HTML elements.
  6. The key difference between `Element` and `Component` is that elements do not
  7. use Chakra's `sx` prop, instead passing styles directly to the React style
  8. prop.
  9. """
  10. def render(self) -> Dict:
  11. """Render the element.
  12. Returns:
  13. The code to render the element.
  14. """
  15. tag = self._render()
  16. return dict(
  17. tag.add_props(
  18. **self.event_triggers,
  19. key=self.key,
  20. id=self.id,
  21. style=self.style,
  22. class_name=self.class_name,
  23. ).set(
  24. contents=str(tag.contents),
  25. children=[child.render() for child in self.children],
  26. props=tag.format_props(),
  27. )
  28. )
  29. def __eq__(self, other):
  30. """Two elements are equal if they have the same tag.
  31. Args:
  32. other: The other element.
  33. Returns:
  34. True if the elements have the same tag, False otherwise.
  35. """
  36. return isinstance(other, Element) and self.tag == other.tag