menu.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.var 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. @classmethod
  42. def get_triggers(cls) -> Set[str]:
  43. """Get the event triggers for the component.
  44. Returns:
  45. The event triggers.
  46. """
  47. return super().get_triggers() | {"on_close", "on_open"}
  48. @classmethod
  49. def create(cls, *children, items=None, **props) -> Component:
  50. """Create a menu component.
  51. Args:
  52. children: The children of the component.
  53. items (list): The item of the menu.
  54. props: The properties of the component.
  55. Returns:
  56. The menu component.
  57. """
  58. if len(children) == 0:
  59. button = MenuButton.create(*props.pop("button"))
  60. if not items:
  61. items = []
  62. children = [button, MenuList.create(*items)]
  63. return super().create(*children, **props)
  64. class MenuButton(ChakraComponent):
  65. """The trigger for the menu list. Must be a direct child of Menu."""
  66. tag = "MenuButton"
  67. # The variant of the menu button.
  68. variant: Var[str]
  69. # The tag to use for the menu button.
  70. as_: Var[str]
  71. class MenuList(ChakraComponent):
  72. """The wrapper for the menu items. Must be a direct child of Menu."""
  73. tag = "MenuList"
  74. class MenuItem(ChakraComponent):
  75. """The trigger that handles menu selection. Must be a direct child of a MenuList."""
  76. tag = "MenuItem"
  77. # Overrides the parent menu's closeOnSelect prop.
  78. close_on_select: Var[bool]
  79. # Right-aligned label text content, useful for displaying hotkeys.
  80. command: Var[str]
  81. # The spacing between the command and menu item's label.
  82. command_spacing: Var[int]
  83. # If true, the menuitem will be disabled.
  84. is_disabled: Var[bool]
  85. # If true and the menuitem is disabled, it'll remain keyboard-focusable
  86. is_focusable: Var[bool]
  87. class MenuItemOption(ChakraComponent):
  88. """The checkable menu item, to be used with MenuOptionGroup."""
  89. tag = "MenuItemOption"
  90. # Overrides the parent menu's closeOnSelect prop.
  91. close_on_select: Var[bool]
  92. # Right-aligned label text content, useful for displaying hotkeys.
  93. command: Var[str]
  94. # The spacing between the command and menu item's label.
  95. command_spacing: Var[int]
  96. # Determines if menu item is checked.
  97. is_checked: Var[bool]
  98. # If true, the menuitem will be disabled.
  99. is_disabled: Var[bool]
  100. # If true and the menuitem is disabled, it'll remain keyboard-focusable
  101. is_focusable: Var[bool]
  102. # "checkbox" | "radio"
  103. type_: Var[str]
  104. # Value of the menu item.
  105. value: Var[str]
  106. class MenuGroup(ChakraComponent):
  107. """A wrapper to group related menu items."""
  108. tag = "MenuGroup"
  109. class MenuOptionGroup(ChakraComponent):
  110. """A wrapper for checkable menu items (radio and checkbox)."""
  111. tag = "MenuOptionGroup"
  112. # "checkbox" | "radio"
  113. type_: Var[str]
  114. # Value of the option group.
  115. value: Var[str]
  116. class MenuDivider(ChakraComponent):
  117. """A visual separator for menu items and groups."""
  118. tag = "MenuDivider"