progress.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Progress."""
  2. from __future__ import annotations
  3. from types import SimpleNamespace
  4. from typing import Optional
  5. from reflex.components.component import Component
  6. from reflex.components.radix.primitives.base import RadixPrimitiveComponentWithClassName
  7. from reflex.style import Style
  8. from reflex.vars import Var
  9. class ProgressComponent(RadixPrimitiveComponentWithClassName):
  10. """A Progress component."""
  11. library = "@radix-ui/react-progress@^1.0.3"
  12. class ProgressRoot(ProgressComponent):
  13. """The Progress Root component."""
  14. tag = "Root"
  15. alias = "RadixProgressRoot"
  16. # The current progress value.
  17. value: Var[Optional[int]]
  18. # The maximum progress value.
  19. max: Var[int]
  20. def _apply_theme(self, theme: Component | None):
  21. self.style = Style(
  22. {
  23. "position": "relative",
  24. "overflow": "hidden",
  25. "background": "black",
  26. "border_radius": "99999px",
  27. "width": "300px",
  28. "height": "25px",
  29. **self.style,
  30. }
  31. )
  32. class ProgressIndicator(ProgressComponent):
  33. """The Progress bar indicator."""
  34. tag = "Indicator"
  35. alias = "RadixProgressIndicator"
  36. # The current progress value.
  37. value: Var[Optional[int]]
  38. def _apply_theme(self, theme: Component | None):
  39. self.style = Style(
  40. {
  41. "background-color": "white",
  42. "width": "100%",
  43. "height": "100%",
  44. "transition": f"transform 660ms linear",
  45. "&[data_state='loading']": {
  46. "transition": f"transform 660ms linear",
  47. },
  48. "transform": f"translateX(-{100 - self.value}%)", # type: ignore
  49. }
  50. )
  51. class Progress(SimpleNamespace):
  52. """High level API for progress bar."""
  53. root = staticmethod(ProgressRoot.create)
  54. indicator = staticmethod(ProgressIndicator.create)
  55. @staticmethod
  56. def __call__(**props) -> Component:
  57. """High level API for progress bar.
  58. Args:
  59. **props: The props of the progress bar
  60. Returns:
  61. The progress bar.
  62. """
  63. return ProgressRoot.create(
  64. ProgressIndicator.create(value=props.get("value")),
  65. **props,
  66. )
  67. progress = Progress()