progress.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """Progress."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from reflex.components.component import Component, ComponentNamespace
  5. from reflex.components.core.colors import color
  6. from reflex.components.radix.primitives.accordion import DEFAULT_ANIMATION_DURATION
  7. from reflex.components.radix.primitives.base import RadixPrimitiveComponentWithClassName
  8. from reflex.components.radix.themes.base import LiteralAccentColor, LiteralRadius
  9. from reflex.vars.base import Var
  10. class ProgressComponent(RadixPrimitiveComponentWithClassName):
  11. """A Progress component."""
  12. library = "@radix-ui/react-progress@1.1.6"
  13. class ProgressRoot(ProgressComponent):
  14. """The Progress Root component."""
  15. tag = "Root"
  16. alias = "RadixProgressRoot"
  17. # Override theme radius for progress bar: "none" | "small" | "medium" | "large" | "full"
  18. radius: Var[LiteralRadius]
  19. def add_style(self) -> dict[str, Any] | None:
  20. """Add style to the component.
  21. Returns:
  22. The style of the component.
  23. """
  24. if self.radius is not None:
  25. self.custom_attrs["data-radius"] = self.radius
  26. return {
  27. "position": "relative",
  28. "overflow": "hidden",
  29. "background": color("gray", 3, alpha=True),
  30. "border_radius": "max(var(--radius-2), var(--radius-full))",
  31. "width": "100%",
  32. "height": "20px",
  33. "boxShadow": f"inset 0 0 0 1px {color('gray', 5, alpha=True)}",
  34. }
  35. def _exclude_props(self) -> list[str]:
  36. return ["radius"]
  37. class ProgressIndicator(ProgressComponent):
  38. """The Progress bar indicator."""
  39. tag = "Indicator"
  40. alias = "RadixProgressIndicator"
  41. # The current progress value.
  42. value: Var[int | None]
  43. # The maximum progress value.
  44. max: Var[int | None]
  45. # The color scheme of the progress indicator.
  46. color_scheme: Var[LiteralAccentColor]
  47. def add_style(self) -> dict[str, Any] | None:
  48. """Add style to the component.
  49. Returns:
  50. The style of the component.
  51. """
  52. if self.color_scheme is not None:
  53. self.custom_attrs["data-accent-color"] = self.color_scheme
  54. return {
  55. "background_color": color("accent", 9),
  56. "width": "100%",
  57. "height": "100%",
  58. "transition": f"transform {DEFAULT_ANIMATION_DURATION}ms linear",
  59. "&[data_state='loading']": {
  60. "transition": f"transform {DEFAULT_ANIMATION_DURATION}ms linear",
  61. },
  62. "transform": f"translateX(calc(-100% + ({self.value} / {self.max} * 100%)))",
  63. "boxShadow": "inset 0 0 0 1px var(--gray-a5)",
  64. }
  65. def _exclude_props(self) -> list[str]:
  66. return ["color_scheme"]
  67. class Progress(ProgressRoot):
  68. """The high-level Progress component."""
  69. # Override theme color for progress bar indicator
  70. color_scheme: Var[LiteralAccentColor]
  71. # The current progress value.
  72. value: Var[int | None]
  73. # The maximum progress value.
  74. max: Var[int | None]
  75. @classmethod
  76. def create(cls, **props) -> Component:
  77. """High-level API for progress bar.
  78. Args:
  79. **props: The props of the progress bar.
  80. Returns:
  81. The progress bar.
  82. """
  83. progress_indicator_props = {}
  84. if "color_scheme" in props:
  85. progress_indicator_props["color_scheme"] = props.pop("color_scheme")
  86. return ProgressRoot.create(
  87. ProgressIndicator.create(
  88. value=props.pop("value", 0),
  89. max=props.pop("max", 100),
  90. **progress_indicator_props,
  91. ),
  92. **props,
  93. )
  94. class ProgressNamespace(ComponentNamespace):
  95. """High-level API for progress bar."""
  96. root = staticmethod(ProgressRoot.create)
  97. indicator = staticmethod(ProgressIndicator.create)
  98. __call__ = staticmethod(Progress.create)
  99. progress = ProgressNamespace()