link.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """A link component."""
  2. from reflex.components.chakra import ChakraComponent
  3. from reflex.components.component import Component
  4. from reflex.components.next.link import NextLink
  5. from reflex.utils import imports
  6. from reflex.vars import BaseVar, Var
  7. next_link = NextLink.create()
  8. class Link(ChakraComponent):
  9. """Link to another page."""
  10. tag = "Link"
  11. # The rel.
  12. rel: Var[str]
  13. # The page to link to.
  14. href: Var[str]
  15. # The text to display.
  16. text: Var[str]
  17. # What the link renders to.
  18. as_: Var[str] = BaseVar.create(
  19. value="{NextLink}", _var_is_local=False, _var_is_string=False
  20. ) # type: ignore
  21. # If true, the link will open in new tab.
  22. is_external: Var[bool]
  23. def _get_imports(self) -> imports.ImportDict:
  24. return {**super()._get_imports(), **next_link._get_imports()}
  25. @classmethod
  26. def create(cls, *children, **props) -> Component:
  27. """Create a Link component.
  28. Args:
  29. *children: The children of the component.
  30. **props: The props of the component.
  31. Raises:
  32. ValueError: in case of missing children
  33. Returns:
  34. Component: The link component
  35. """
  36. if props.get("href") is not None:
  37. if not len(children):
  38. raise ValueError("Link without a child will not display")
  39. else:
  40. # Don't use a NextLink if there is no href.
  41. props["as_"] = ""
  42. return super().create(*children, **props)