stack.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Stack components."""
  2. from __future__ import annotations
  3. from typing import Literal, Optional
  4. from reflex.components.component import Component
  5. from .flex import Flex
  6. LiteralJustify = Literal["start", "center", "end"]
  7. LiteralAlign = Literal["start", "center", "end", "stretch"]
  8. class Stack(Flex):
  9. """A stack component."""
  10. @classmethod
  11. def create(
  12. cls,
  13. *children,
  14. justify: Optional[LiteralJustify] = "start",
  15. align: Optional[LiteralAlign] = "center",
  16. spacing: Optional[str] = "0.5rem",
  17. **props,
  18. ) -> Component:
  19. """Create a new instance of the component.
  20. Args:
  21. *children: The children of the stack.
  22. justify: The justify of the stack elements.
  23. align: The alignment of the stack elements.
  24. spacing: The spacing between each stack item.
  25. **props: The properties of the stack.
  26. Returns:
  27. The stack component.
  28. """
  29. style = props.setdefault("style", {})
  30. style.update(
  31. {
  32. "alignItems": align,
  33. "justifyContent": justify,
  34. "gap": spacing,
  35. }
  36. )
  37. return super().create(*children, **props)
  38. class VStack(Stack):
  39. """A vertical stack component."""
  40. def _apply_theme(self, theme: Component):
  41. self.style.update({"flex_direction": "column"})
  42. class HStack(Stack):
  43. """A horizontal stack component."""
  44. def _apply_theme(self, theme: Component):
  45. self.style.update({"flex_direction": "row"})