link.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(value="{NextLink}", _var_is_local=False) # type: ignore
  19. # If true, the link will open in new tab.
  20. is_external: Var[bool]
  21. def _get_imports_list(self) -> list[imports.ImportVar]:
  22. return [*super()._get_imports_list(), *next_link._get_imports_list()]
  23. @classmethod
  24. def create(cls, *children, **props) -> Component:
  25. """Create a Link component.
  26. Args:
  27. *children: The children of the component.
  28. **props: The props of the component.
  29. Raises:
  30. ValueError: in case of missing children
  31. Returns:
  32. Component: The link component
  33. """
  34. if props.get("href") is not None:
  35. if not len(children):
  36. raise ValueError("Link without a child will not display")
  37. else:
  38. # Don't use a NextLink if there is no href.
  39. props["as_"] = ""
  40. return super().create(*children, **props)