menu.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """Menu components."""
  2. from typing import Set
  3. from pynecone.components.libs.chakra import ChakraComponent
  4. from pynecone.var import Var
  5. class Menu(ChakraComponent):
  6. """The wrapper component provides context, state, and focus management."""
  7. tag = "Menu"
  8. # The padding required to prevent the arrow from reaching the very edge of the popper.
  9. arrow_padding: Var[int]
  10. # If true, the first enabled menu item will receive focus and be selected when the menu opens.
  11. auto_select: Var[bool]
  12. # The boundary area for the popper. Used within the preventOverflow modifier
  13. boundary: Var[str]
  14. # If true, the menu will close when you click outside the menu list
  15. close_on_blur: Var[bool]
  16. # If true, the menu will close when a menu item is clicked
  17. close_on_select: Var[bool]
  18. # If by default the menu is open.
  19. default_is_open: Var[bool]
  20. # If rtl, popper placement positions will be flipped i.e. 'top-right' will become 'top-left' and vice-verse ("ltr" | "rtl")
  21. direction: Var[str]
  22. # If true, the popper will change its placement and flip when it's about to overflow its boundary area.
  23. flip: Var[bool]
  24. # 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.
  25. gutter: Var[int]
  26. # Performance 🚀: If true, the MenuItem rendering will be deferred until the menu is open.
  27. is_lazy: Var[bool]
  28. # 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.
  29. lazy_behavior: Var[str]
  30. # Determines if the menu is open or not.
  31. is_open: Var[bool]
  32. # If true, the popper will match the width of the reference at all times. It's useful for autocomplete, `date-picker` and select patterns.
  33. match_width: Var[bool]
  34. # The placement of the popper relative to its reference.
  35. placement: Var[str]
  36. # If true, will prevent the popper from being cut off and ensure it's visible within the boundary area.
  37. prevent_overflow: Var[bool]
  38. # The CSS positioning strategy to use. ("fixed" | "absolute")
  39. strategy: Var[str]
  40. @classmethod
  41. def get_triggers(cls) -> 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. class MenuButton(ChakraComponent):
  48. """The trigger for the menu list. Must be a direct child of Menu."""
  49. tag = "MenuButton"
  50. # The variant of the menu button.
  51. variant: Var[str]
  52. # The tag to use for the menu button.
  53. as_: Var[str]
  54. class MenuList(ChakraComponent):
  55. """The wrapper for the menu items. Must be a direct child of Menu."""
  56. tag = "MenuList"
  57. class MenuItem(Menu):
  58. """The trigger that handles menu selection. Must be a direct child of a MenuList."""
  59. tag = "MenuItem"
  60. # Overrides the parent menu's closeOnSelect prop.
  61. close_on_select: Var[bool]
  62. # Right-aligned label text content, useful for displaying hotkeys.
  63. command: Var[str]
  64. # The spacing between the command and menu item's label.
  65. command_spacing: Var[int]
  66. # If true, the menuitem will be disabled.
  67. is_disabled: Var[bool]
  68. # If true and the menuitem is disabled, it'll remain keyboard-focusable
  69. is_focusable: Var[bool]
  70. class MenuItemOption(Menu):
  71. """The checkable menu item, to be used with MenuOptionGroup."""
  72. tag = "MenuItemOption"
  73. # Overrides the parent menu's closeOnSelect prop.
  74. close_on_select: Var[bool]
  75. # Right-aligned label text content, useful for displaying hotkeys.
  76. command: Var[str]
  77. # The spacing between the command and menu item's label.
  78. command_spacing: Var[int]
  79. # Determines if menu item is checked.
  80. is_checked: Var[bool]
  81. # If true, the menuitem will be disabled.
  82. is_disabled: Var[bool]
  83. # If true and the menuitem is disabled, it'll remain keyboard-focusable
  84. is_focusable: Var[bool]
  85. # "checkbox" | "radio"
  86. type_: Var[str]
  87. # Value of the menu item.
  88. value: Var[str]
  89. class MenuGroup(Menu):
  90. """A wrapper to group related menu items."""
  91. tag = "MenuGroup"
  92. class MenuOptionGroup(Menu):
  93. """A wrapper for checkable menu items (radio and checkbox)."""
  94. tag = "MenuOptionGroup"
  95. # "checkbox" | "radio"
  96. type_: Var[str]
  97. # Value of the option group.
  98. value: Var[str]
  99. class MenuDivider(Menu):
  100. """A visual separator for menu items and groups."""
  101. tag = "MenuDivider"