1
0

link.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """A link component."""
  2. from typing import Optional
  3. from pynecone.components.component import Component
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.components.navigation.nextlink import NextLink
  6. from pynecone.utils import imports
  7. from pynecone.vars import BaseVar, Var
  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("{NextLink}", is_local=False) # type: ignore
  19. # If true, the link will open in new tab.
  20. is_external: Var[bool]
  21. def _get_imports(self) -> imports.ImportDict:
  22. return {**super()._get_imports(), **NextLink.create()._get_imports()}
  23. @classmethod
  24. def create(cls, *children, href: Optional[Var] = None, **props) -> Component:
  25. """Create a Link component.
  26. Args:
  27. *children: The children of the component.
  28. href: The href attribute of the link.
  29. **props: The props of the component.
  30. Raises:
  31. ValueError: in case of missing children
  32. Returns:
  33. Component: The link component
  34. """
  35. if href and not len(children):
  36. raise ValueError("Link without a child will not display")
  37. elif href is None and len(children):
  38. # Don't use a NextLink if there is no href.
  39. props["as_"] = "Link"
  40. if href:
  41. props["href"] = href
  42. return super().create(*children, **props)