menu.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """Menu components."""
  2. from typing import Set
  3. from pynecone.components.component import Component
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.vars import Var
  6. class Menu(ChakraComponent):
  7. """The wrapper component provides context, state, and focus management."""
  8. tag = "Menu"
  9. # The padding required to prevent the arrow from reaching the very edge of the popper.
  10. arrow_padding: Var[int]
  11. # If true, the first enabled menu item will receive focus and be selected when the menu opens.
  12. auto_select: Var[bool]
  13. # The boundary area for the popper. Used within the preventOverflow modifier
  14. boundary: Var[str]
  15. # If true, the menu will close when you click outside the menu list
  16. close_on_blur: Var[bool]
  17. # If true, the menu will close when a menu item is clicked
  18. close_on_select: Var[bool]
  19. # If by default the menu is open.
  20. default_is_open: Var[bool]
  21. # If rtl, popper placement positions will be flipped i.e. 'top-right' will become 'top-left' and vice-verse ("ltr" | "rtl")
  22. direction: Var[str]
  23. # If true, the popper will change its placement and flip when it's about to overflow its boundary area.
  24. flip: Var[bool]
  25. # The distance or margin between the reference and popper. It is used internally to create an offset modifier. NB: If you define offset prop, it'll override the gutter.
  26. gutter: Var[int]
  27. # Performance 🚀: If true, the MenuItem rendering will be deferred until the menu is open.
  28. is_lazy: Var[bool]
  29. # Performance 🚀: The lazy behavior of menu's content when not visible. Only works when `isLazy={true}` - "unmount": The menu's content is always unmounted when not open. - "keepMounted": The menu's content initially unmounted, but stays mounted when menu is open.
  30. lazy_behavior: Var[str]
  31. # Determines if the menu is open or not.
  32. is_open: Var[bool]
  33. # If true, the popper will match the width of the reference at all times. It's useful for autocomplete, `date-picker` and select patterns.
  34. match_width: Var[bool]
  35. # The placement of the popper relative to its reference.
  36. placement: Var[str]
  37. # If true, will prevent the popper from being cut off and ensure it's visible within the boundary area.
  38. prevent_overflow: Var[bool]
  39. # The CSS positioning strategy to use. ("fixed" | "absolute")
  40. strategy: Var[str]
  41. def get_triggers(self) -> Set[str]:
  42. """Get the event triggers for the component.
  43. Returns:
  44. The event triggers.
  45. """
  46. return super().get_triggers() | {"on_close", "on_open"}
  47. @classmethod
  48. def create(cls, *children, button=None, items=None, **props) -> Component:
  49. """Create a menu component.
  50. Args:
  51. children: The children of the component.
  52. button: the button that open the menu.
  53. items (list): The items of the menu.
  54. props: The properties of the component.
  55. Returns:
  56. The menu component.
  57. """
  58. if len(children) == 0:
  59. children = []
  60. if button:
  61. children.append(MenuButton.create(button))
  62. if not items:
  63. items = []
  64. children.append(MenuList.create(*items))
  65. return super().create(*children, **props)
  66. class MenuButton(ChakraComponent):
  67. """The trigger for the menu list. Must be a direct child of Menu."""
  68. tag = "MenuButton"
  69. # The variant of the menu button.
  70. variant: Var[str]
  71. # The tag to use for the menu button.
  72. as_: Var[str]
  73. class MenuList(ChakraComponent):
  74. """The wrapper for the menu items. Must be a direct child of Menu."""
  75. tag = "MenuList"
  76. class MenuItem(ChakraComponent):
  77. """The trigger that handles menu selection. Must be a direct child of a MenuList."""
  78. tag = "MenuItem"
  79. # Overrides the parent menu's closeOnSelect prop.
  80. close_on_select: Var[bool]
  81. # Right-aligned label text content, useful for displaying hotkeys.
  82. command: Var[str]
  83. # The spacing between the command and menu item's label.
  84. command_spacing: Var[int]
  85. # If true, the menuitem will be disabled.
  86. is_disabled: Var[bool]
  87. # If true and the menuitem is disabled, it'll remain keyboard-focusable
  88. is_focusable: Var[bool]
  89. class MenuItemOption(ChakraComponent):
  90. """The checkable menu item, to be used with MenuOptionGroup."""
  91. tag = "MenuItemOption"
  92. # Overrides the parent menu's closeOnSelect prop.
  93. close_on_select: Var[bool]
  94. # Right-aligned label text content, useful for displaying hotkeys.
  95. command: Var[str]
  96. # The spacing between the command and menu item's label.
  97. command_spacing: Var[int]
  98. # Determines if menu item is checked.
  99. is_checked: Var[bool]
  100. # If true, the menuitem will be disabled.
  101. is_disabled: Var[bool]
  102. # If true and the menuitem is disabled, it'll remain keyboard-focusable
  103. is_focusable: Var[bool]
  104. # "checkbox" | "radio"
  105. type_: Var[str]
  106. # Value of the menu item.
  107. value: Var[str]
  108. class MenuGroup(ChakraComponent):
  109. """A wrapper to group related menu items."""
  110. tag = "MenuGroup"
  111. class MenuOptionGroup(ChakraComponent):
  112. """A wrapper for checkable menu items (radio and checkbox)."""
  113. tag = "MenuOptionGroup"
  114. # "checkbox" | "radio"
  115. type_: Var[str]
  116. # Value of the option group.
  117. value: Var[str]
  118. class MenuDivider(ChakraComponent):
  119. """A visual separator for menu items and groups."""
  120. tag = "MenuDivider"