sidebar.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """Sidebar component for the app."""
  2. import reflex as rx
  3. from .state import State
  4. from .styles import *
  5. def sidebar_header() -> rx.Component:
  6. """Sidebar header.
  7. Returns:
  8. rx.Component: The sidebar header component.
  9. """
  10. return rx.hstack(
  11. rx.image(
  12. src="/icon.svg",
  13. height="2em",
  14. ),
  15. rx.spacer(),
  16. rx.link(
  17. rx.center(
  18. rx.image(
  19. src="/github.svg",
  20. height="3em",
  21. padding="0.5em",
  22. ),
  23. box_shadow=box_shadow,
  24. bg="transparent",
  25. border_radius=border_radius,
  26. _hover={
  27. "bg": accent_color,
  28. },
  29. ),
  30. href="https://github.com/reflex-dev/reflex",
  31. ),
  32. width="100%",
  33. border_bottom=border,
  34. padding="1em",
  35. )
  36. def sidebar_footer() -> rx.Component:
  37. """Sidebar footer.
  38. Returns:
  39. rx.Component: The sidebar footer component.
  40. """
  41. return rx.hstack(
  42. rx.link(
  43. rx.center(
  44. rx.image(
  45. src="/paneleft.svg",
  46. height="2em",
  47. padding="0.5em",
  48. ),
  49. bg="transparent",
  50. border_radius=border_radius,
  51. **hover_accent_bg,
  52. ),
  53. on_click=State.toggle_sidebar_displayed,
  54. transform=rx.cond(~State.sidebar_displayed, "rotate(180deg)", ""),
  55. transition="transform 0.5s, left 0.5s",
  56. position="relative",
  57. left=rx.cond(State.sidebar_displayed, "0px", "20.5em"),
  58. **overlapping_button_style,
  59. ),
  60. rx.spacer(),
  61. rx.link(
  62. rx.text(
  63. "Docs",
  64. ),
  65. href="https://reflex.dev/docs/getting-started/introduction/",
  66. ),
  67. rx.link(
  68. rx.text(
  69. "Blog",
  70. ),
  71. href="https://reflex.dev/blog/",
  72. ),
  73. width="100%",
  74. border_top=border,
  75. padding="1em",
  76. )
  77. def sidebar_item(text: str, icon: str, url: str) -> rx.Component:
  78. """Sidebar item.
  79. Args:
  80. text (str): The text of the item.
  81. icon (str): The icon of the item.
  82. url (str): The URL of the item.
  83. Returns:
  84. rx.Component: The sidebar item component.
  85. """
  86. return rx.link(
  87. rx.hstack(
  88. rx.image(
  89. src=icon,
  90. height="2.5em",
  91. padding="0.5em",
  92. ),
  93. rx.text(
  94. text,
  95. ),
  96. bg=rx.cond(
  97. State.origin_url == f"/{text.lower()}/",
  98. accent_color,
  99. "transparent",
  100. ),
  101. color=rx.cond(
  102. State.origin_url == f"/{text.lower()}/",
  103. accent_text_color,
  104. text_color,
  105. ),
  106. border_radius=border_radius,
  107. box_shadow=box_shadow,
  108. width="100%",
  109. padding_x="1em",
  110. ),
  111. href=url,
  112. width="100%",
  113. )
  114. def sidebar() -> rx.Component:
  115. """Sidebar.
  116. Returns:
  117. rx.Component: The sidebar component.
  118. """
  119. return rx.box(
  120. rx.vstack(
  121. sidebar_header(),
  122. rx.vstack(
  123. sidebar_item(
  124. "Dashboard",
  125. "/github.svg",
  126. "/dashboard",
  127. ),
  128. sidebar_item(
  129. "Settings",
  130. "/github.svg",
  131. "/settings",
  132. ),
  133. width="100%",
  134. overflow_y="auto",
  135. align_items="flex-start",
  136. padding="1em",
  137. ),
  138. rx.spacer(),
  139. sidebar_footer(),
  140. height="100dvh",
  141. ),
  142. min_width=sidebar_width,
  143. height="100%",
  144. position="sticky",
  145. top="0px",
  146. border_right=border,
  147. )