stack.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Stack components."""
  2. from __future__ import annotations
  3. from reflex.components.component import Component
  4. from reflex.components.core.breakpoints import Responsive
  5. from reflex.vars.base import Var
  6. from ..base import LiteralAlign, LiteralSpacing
  7. from .flex import Flex, LiteralFlexDirection
  8. class Stack(Flex):
  9. """A stack component."""
  10. # The spacing between each stack item.
  11. spacing: Var[Responsive[LiteralSpacing]] = Var.create("3")
  12. # The alignment of the stack items.
  13. align: Var[Responsive[LiteralAlign]] = Var.create("start")
  14. @classmethod
  15. def create(
  16. cls,
  17. *children,
  18. **props,
  19. ) -> Component:
  20. """Create a new instance of the component.
  21. Args:
  22. *children: The children of the stack.
  23. **props: The properties of the stack.
  24. Returns:
  25. The stack component.
  26. """
  27. # Apply the default classname
  28. given_class_name = props.pop("class_name", [])
  29. if not isinstance(given_class_name, list):
  30. given_class_name = [given_class_name]
  31. props["class_name"] = ["rx-Stack", *given_class_name]
  32. return super().create(
  33. *children,
  34. **props,
  35. )
  36. class VStack(Stack):
  37. """A vertical stack component."""
  38. # The direction of the stack.
  39. direction: Var[Responsive[LiteralFlexDirection]] = Var.create("column")
  40. class HStack(Stack):
  41. """A horizontal stack component."""
  42. # The direction of the stack.
  43. direction: Var[Responsive[LiteralFlexDirection]] = Var.create("row")
  44. stack = Stack.create
  45. hstack = HStack.create
  46. vstack = VStack.create